-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrategyFixed.java
More file actions
43 lines (38 loc) · 1.4 KB
/
StrategyFixed.java
File metadata and controls
43 lines (38 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package game_theory_bimatrix;
import java.util.Arrays;
import java.util.Random;
public class StrategyFixed implements Strategy {
private double[] strategy;
private Random random = new Random();
public StrategyFixed(double[] initStrategy) {
double strategySum = 0;
strategy = Arrays.copyOf(initStrategy, initStrategy.length);
for (double w : strategy) {
if (w < 0) throw new IllegalArgumentException(
"The weights in the strategy array must be non-negative");
strategySum += w;
}
if (strategySum == 0.0) throw new IllegalArgumentException(
"The weights in the strategy array must sum to a positive value");
// Normalize weights to get probabilities
for (int i = 0; i < strategy.length; i++) {
strategy[i] /= strategySum;
}
}
@Override
public int chooseAction() {
double selector = random.nextDouble();
int action = 0;
while (action < strategy.length) {
selector -= strategy[action];
if (selector <= 0)
return action;
action++;
}
return strategy.length - 1; }
@Override
public void update(Game game, int myAction, int otherAction, int player) { // No update needed for a fixed strategy
}
@Override
public double[] getStrategy() { return strategy;
} }