-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCFRNode.java
More file actions
104 lines (98 loc) · 3.53 KB
/
Copy pathCFRNode.java
File metadata and controls
104 lines (98 loc) · 3.53 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.util.Arrays;
public class CFRNode {
private double[][] regretSum;
private double[] strategy;
private double[][] strategySum;
private boolean[] is_valid;
private int total_game_actions; //number of actions the information set with most actions. used to set arrays length
private int num_valid_actions; //number of actions in this information set.
private final int iteration_mod = 3; //has to be at least 2 to allow updating only for the next iteration
private int current_iteration_mod_pointer = 0;
public void Print() {
System.out.println(Arrays.toString(getAverageStrategy()));
}
CFRNode(DecisionNode h){
total_game_actions = h.total_game_actions();
num_valid_actions = h.num_valid_actions();
regretSum = new double[iteration_mod][total_game_actions];
strategy = new double[total_game_actions];
strategySum = new double[iteration_mod][total_game_actions];
is_valid = new boolean[total_game_actions];
for (int a=0; a < total_game_actions; a++)
{
is_valid[a] = h.action_valid(a);
}
}
public void updateTables(int player, int index, double regret, double pi0, double pi1, int current_iteration) {
current_iteration_mod_pointer = current_iteration%iteration_mod;
int next_iteration_mod = (current_iteration+1)%iteration_mod;
int next_next_iteration_mod = (current_iteration+2)%iteration_mod;
if (player == 0) {
regretSum[next_iteration_mod][index] += pi1*regret;
strategySum[next_iteration_mod][index] += pi0*strategy[index];
}
else if (player == 1) {
regretSum[next_iteration_mod][index] += pi0*regret;
strategySum[next_iteration_mod][index] += pi1*strategy[index];
}
regretSum[next_next_iteration_mod][index] = regretSum[next_iteration_mod][index];
strategySum[next_next_iteration_mod][index] = strategySum[next_iteration_mod][index];
}
public double[] getStrategy(int current_iteration)
{
double normalizingSum = 0.0;
int current_iteration_mod = current_iteration%iteration_mod;
for (int a=0; a < total_game_actions; a++)
{
if (is_valid[a] == false) continue;
strategy[a] = regretSum[current_iteration_mod][a] > 0 ? regretSum[current_iteration_mod][a] : 0;
normalizingSum += strategy[a];
}
for (int a=0; a < total_game_actions; a++)
{
if (is_valid[a] == false) continue;
if (normalizingSum > 0)
{
strategy[a] /= normalizingSum;
}
else
{
strategy[a] = 1.0 /num_valid_actions;
}
}
/*
//modified version for transforming regrets to strategy
for (int a=0; a < total_game_actions; a++)
{
if (is_valid[a] == false) continue;
strategy[a] = Math.exp(regretSum[current_iteration_mod_pointer][a]);
normalizingSum += strategy[a];
}
for (int a=0; a < total_game_actions; a++)
{
if (is_valid[a] == false) continue;
strategy[a] /= normalizingSum;
}
*/
return strategy;
}
public double[] getAverageStrategy () {
double[] avgStrategy = new double[total_game_actions];
double normalizingSum = 0.0;
int next_iteration_mod_pointer = (current_iteration_mod_pointer+1)%iteration_mod;
for (int a=0; a < total_game_actions; a++){
if (is_valid[a] == false) continue;
normalizingSum += strategySum[next_iteration_mod_pointer][a];
}
for (int a=0; a < total_game_actions; a++){
if (is_valid[a] == false) continue;
if (normalizingSum > 0) {
avgStrategy[a] = strategySum[next_iteration_mod_pointer][a] / normalizingSum;
}
else {
avgStrategy[a] = 1.0 /num_valid_actions;
}
}
return avgStrategy;
}
}