-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTrainCFR_CS.java
More file actions
98 lines (88 loc) · 3.71 KB
/
Copy pathTrainCFR_CS.java
File metadata and controls
98 lines (88 loc) · 3.71 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
/* Implementation of the CFR algorithm - Chance Sampling version */
import java.util.*;
public class TrainCFR_CS {
private TreeMap<String,CFRNode> nodemap = new TreeMap<String,CFRNode>(); //<key, information set data>
static CsvFileWriter CsvWriter = new CsvFileWriter();
public double cfr(History h, int player, int iteration, double pi0, double pi1) {
//Return payoff for terminal states
if (h.is_terminal()) {
return ((TerminalNode)h).get_utility(player);// this actually means "get_payoff", since it doesn't include probabilities (algorithm part)
}
//Sample chance outcome for chance states
else if (h.is_chance()) {
Outcome a = ((ChanceNode)h).sample_outcome();
return cfr(h.append(a), player, iteration, pi0, pi1);
}
//statistic to help compare different algorithms
VisitedNodesCounter.inc();
//Get information set node or create if nonexistant
DecisionNode h_decision = (DecisionNode)h;
int total_game_actions = h_decision.total_game_actions();
String infoset_key = h_decision.get_information_set();
CFRNode infoset_node = nodemap.get(infoset_key);
if (infoset_node == null) {
infoset_node = new CFRNode(h_decision);
nodemap.put(infoset_key, infoset_node);
}
//For each action, recursively call cfr with additional history and probability
double [] node_utility = new double[total_game_actions];
double total_node_utility = 0.0;
double [] strategy = infoset_node.getStrategy(iteration);
for (int a=0; a < total_game_actions; a++){
if (h_decision.action_valid(a) == false) continue;
if (h_decision.get_player() == 0) {
node_utility[a] = cfr(h_decision.append(h_decision.get_decision_outcome(a)), player, iteration, strategy[a]*pi0, pi1);
}
else if (h_decision.get_player() == 1) {
node_utility[a] = cfr(h_decision.append(h_decision.get_decision_outcome(a)), player, iteration, pi0, strategy[a]*pi1);
}
total_node_utility += strategy[a]*node_utility[a];
}
//For each action, compute and accumulate counterfactual regrets
if (h_decision.get_player() == player) {
for (int a=0; a < total_game_actions; a++){
if (h_decision.action_valid(a) == false) continue;
double regret = node_utility[a]-total_node_utility;
infoset_node.updateTables(player,a,regret,pi0,pi1,iteration);
}
}
return total_node_utility;
}
//print strategy profile
public void print() {
System.out.println("Final strategy profile:");
Set set = nodemap.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
CFRNode tmpNode=(CFRNode)me.getValue();
tmpNode.Print();
}
}
//write strategy profiles to csv
public void update_strategy_csv(String log_dir_path) {
Set set = nodemap.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
CFRNode tmpNode=(CFRNode)me.getValue();
String filename = log_dir_path + me.getKey() + "_strategy.csv";
double strategy[] = tmpNode.getAverageStrategy();
CsvWriter.write(filename, strategy);
}
}
//create a csv containing the infosets name
public void create_infoset_csv(String log_dir_path) {
Set set = nodemap.entrySet();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
CFRNode tmpNode = (CFRNode)me.getValue();
double strategy[] = tmpNode.getAverageStrategy();
String filename = log_dir_path + "infosets.csv";
CsvWriter.write(filename, me.getKey().toString(), strategy);
}
CsvWriter.flush_close();
}
}