public class Node {
Node fatherNode;
Node betNode;
Node checkNode;
int poker;
int poker2;
String state;
double probability;
double betIValue;
double checkIValue;
double currentNode;
Node(Node fatherNode,String state, double probability,int poker,int poker2) {
this.fatherNode = fatherNode;
this.poker = poker;
this.state = state;
this.probability = probability;
this.poker2 = poker2;
}
void setChildNode(Node betNode, Node checkNode) {
this.betNode = betNode;
this.checkNode = checkNode;
}
double setBetIValue() {
betIValue = CfrUse.CountV(this.betNode);
return betIValue;
}
double setCheckIValue() {
checkIValue = CfrUse.CountV(this.checkNode);
return checkIValue;
}
double setCurrentNode() {
currentNode = CfrUse.getNowProbability(this);
return currentNode;
}
}
package cfr2;
public class IS{
Node node1;
Node node2;
private double betProbability;
private double checkProbability;
private double betValue = 0;
private double checkValue = 0;
private double averageBetProbability = 0;
private double averageCheckProbability = 0;
private double betRegret = 0;
private double checkRegret = 0;
String state;
IS(Node node1, Node node2 ,String state) {
this.node1 = node1;
this.node2 = node2;
this.state = state;
}
void RegretToStrategy() {
double sum = judge(betRegret) + judge(checkRegret);
if(sum > 0) {
betProbability = judge(betRegret) / sum;
checkProbability = judge(checkRegret) / sum;
}
else {
betProbability = 0.5;
checkProbability = 0.5;
}
setChildProbability();
}
void setChildProbability() {
node1.betNode.probability = betProbability;
node2.betNode.probability = betProbability;
node1.checkNode.probability = checkProbability;
node2.checkNode.probability = checkProbability;
}
void UpdateAverage() {
averageBetProbability += node1.setCurrentNode()*betProbability ;
averageCheckProbability += node1.setCurrentNode()*checkProbability;
}
void UpdateRegrets() {
double node = node1.setBetIValue();
double Node = node2.setBetIValue();
betValue = node+Node;
double v = node1.setCheckIValue();
double v1 = node2.setCheckIValue();
checkValue = v + v1;
betRegret += betValue - betValue * betProbability- checkValue * checkProbability;
checkRegret += checkValue - betValue * betProbability- checkValue * checkProbability;
}
void print() {
double sum = averageBetProbability + averageCheckProbability;
System.out.println("信息集 :" + state + " 平均bet概率 :" + averageBetProbability / sum + " 平均check概率 :" + averageCheckProbability / sum);
}
double judge(double value) {
return value > 0 ? value : 0;
}
}
package cfr2;
public class InformationSet {
Node JQ = new Node(null,"",1,1,2);
Node JQB = new Node(JQ,"b",0.5,1,2);
Node JQC = new Node(JQ,"c",0.5,1,2);
Node JQBC = new Node(JQB,"bc",0.5,1,2);
Node JQBB = new Node(JQB,"bb",0.5,1,2);
Node JQCC = new Node(JQC,"cc",0.5,1,2);
Node JQCB = new Node(JQC,"cb",0.5,1,2);
Node JQCBC = new Node(JQCB,"cbc",0.5,1,2);
Node JQCBB = new Node(JQCB,"cbb",0.5,1,2);
Node JK = new Node(null,"",1,1,3);
Node JKB = new Node(JK,"b",0.5,1,3);
Node JKC = new Node(JK,"c",0.5,1,3);
Node JKBC = new Node(JKB,"bc",0.5,1,3);
Node JKBB = new Node(JKB,"bb",0.5,1,3);
Node JKCC = new Node(JKC,"cc",0.5,1,3);
Node JKCB = new Node(JKC,"cb",0.5,1,3);
Node JKCBC = new Node(JKCB,"cbc",0.5,1,3);
Node JKCBB = new Node(JKCB,"cbb",0.5,1,3);
Node QJ = new Node(null,"",1,2,1);
Node QJB = new Node(QJ,"b",0.5,2,1);
Node QJC = new Node(QJ,"c",0.5,2,1);
Node QJBC = new Node(QJB,"bc",0.5,2,1);
Node QJBB = new Node(QJB,"bb",0.5,2,1);
Node QJCC = new Node(QJC,"cc",0.5,2,1);
Node QJCB = new Node(QJC,"cb",0.5,2,1);
Node QJCBC = new Node(QJCB,"cbc",0.5,2,1);
Node QJCBB = new Node(QJCB,"cbb",0.5,2,1);
Node QK = new Node(null,"",1,2,3);
Node QKB = new Node(QK,"b",0.5,2,3);
Node QKC = new Node(QK,"c",0.5,2,3);
Node QKBC = new Node(QKB,"bc",0.5,2,3);
Node QKBB = new Node(QKB,"bb",0.5,2,3);
Node QKCC = new Node(QKC,"cc",0.5,2,3);
Node QKCB = new Node(QKC,"cb",0.5,2,3);
Node QKCBC = new Node(QKCB,"cbc",0.5,2,3);
Node QKCBB = new Node(QKCB,"cbb",0.5,2,3);
Node KJ = new Node(null,"",1,3,1);
Node KJB = new Node(KJ,"b",0.5,3,1);
Node KJC = new Node(KJ,"c",0.5,3,1);
Node KJBC = new Node(KJB,"bc",0.5,3,1);
Node KJBB = new Node(KJB,"bb",0.5,3,1);
Node KJCC = new Node(KJC,"cc",0.5,3,1);
Node KJCB = new Node(KJC,"cb",0.5,3,1);
Node KJCBC = new Node(KJCB,"cbc",0.5,3,1);
Node KJCBB = new Node(KJCB,"cbb",0.5,3,1);
Node KQ = new Node(null,"",1,3,2);
Node KQB = new Node(KQ,"b",0.5,3,2);
Node KQC = new Node(KQ,"c",0.5,3,2);
Node KQBC = new Node(KQB,"bc",0.5,3,2);
Node KQBB = new Node(KQB,"bb",0.5,3,2);
Node KQCC = new Node(KQC,"cc",0.5,3,2);
Node KQCB = new Node(KQC,"cb",0.5,3,2);
Node KQCBC = new Node(KQCB,"cbc",0.5,3,2);
Node KQCBB = new Node(KQCB,"cbb",0.5,3,2);
IS J = new IS(JQ,JK,"J");
IS Q = new IS(QK,QJ,"Q");
IS K = new IS(KQ,KJ,"K");
IS[] first = new IS[3];
IS checkJ = new IS(QJC,KJC,"checkJ");
IS betJ = new IS(QJB,KJB,"betJ");
IS checkQ = new IS(JQC,KQC,"checkQ");
IS betQ = new IS(JQB,KQB,"betQ");
IS checkK = new IS(JKC,QKC,"checkK");
IS betK = new IS(JKB,QKB,"betK");
IS[] second = new IS[6];
IS jCheckBet = new IS(JQCB,JKCB,"jCheckBet");
IS qCheckBet = new IS(QJCB,QKCB,"qCheckBet");
IS kCheckBet = new IS(KJCB,KQCB,"kCheckBet");
IS[] three = new IS[3];
IS[] sumIS = new IS[12];
InformationSet() {
JQ.setChildNode(JQB,JQC);
JQB.setChildNode(JQBB,JQBC);
JQC.setChildNode(JQCB,JQCC);
JQCB.setChildNode(JQCBB,JQCBC);
JK.setChildNode(JKB,JKC);
JKB.setChildNode(JKBB,JKBC);
JKC.setChildNode(JKCB,JKCC);
JKCB.setChildNode(JKCBB,JKCBC);
QJ.setChildNode(QJB,QJC);
QJB.setChildNode(QJBB,QJBC);
QJC.setChildNode(QJCB,QJCC);
QJCB.setChildNode(QJCBB,QJCBC);
QK.setChildNode(QKB,QKC);
QKB.setChildNode(QKBB,QKBC);
QKC.setChildNode(QKCB,QKCC);
QKCB.setChildNode(QKCBB,QKCBC);
KQ.setChildNode(KQB,KQC);
KQB.setChildNode(KQBB,KQBC);
KQC.setChildNode(KQCB,KQCC);
KQCB.setChildNode(KQCBB,KQCBC);
KJ.setChildNode(KJB,KJC);
KJB.setChildNode(KJBB,KJBC);
KJC.setChildNode(KJCB,KJCC);
KJCB.setChildNode(KJCBB,KJCBC);
sumIS[0] = J;
sumIS[1] = Q;
sumIS[2] = K;
sumIS[3] = checkJ;
sumIS[4] = betJ;
sumIS[5] = checkQ;
sumIS[6] = betQ;
sumIS[7] = checkK;
sumIS[8] = betK;
sumIS[9] =jCheckBet;
sumIS[10] =qCheckBet;
sumIS[11] =kCheckBet;
}
public void run() {
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < sumIS.length; j++) {
sumIS[j].RegretToStrategy();
}
for (int j = 0; j < sumIS.length; j++) {
sumIS[j].UpdateAverage();
}
for (int j = 0; j < sumIS.length; j++) {
sumIS[j].UpdateRegrets();
}
for (int j = 0; j < sumIS.length; j++) {
sumIS[j].print();
}
System.out.println();
System.out.println();
System.out.println();
}
}
}
package cfr2;
import java.util.ArrayList;
public class CfrUse {
static double CountV(Node now) {
double IRealValue = 0;
ArrayList<Node> leaf = new ArrayList<>();
getLeafNode(leaf,now);
for (int i = 0; i < leaf.size(); i++) {
if(getNowRightPoker(now.fatherNode)) {
IRealValue -= getNotNowNodeProbability(now.fatherNode) * getNowLeafProbability(now.fatherNode, leaf.get(i)) * getResult(leaf.get(i));
}
else
IRealValue += getNotNowNodeProbability(now.fatherNode) * getNowLeafProbability(now.fatherNode,leaf.get(i)) * getResult(leaf.get(i));
}
return IRealValue/6;
}
private static double getNotNowNodeProbability(Node now) {
Node node = now;
double Probability = 1;
int i = 0;
while (node != null) {
if(i%2 == 0)
Probability = Probability * node.probability;
node = node.fatherNode;
i++;
}
return Probability;
}
private static double getNowLeafProbability(Node now, Node leaf) {
Node node = leaf;
double Probability = 1;
while(node.fatherNode != now) {
Probability = node.probability * Probability;
node = node.fatherNode;
}
return Probability;
}
static double getNowProbability(Node now) {
Node node = now;
double Probability = 1;
int i = 0;
while (node != null) {
i++;
node = node.fatherNode;
}
node = now;
if(i == 3) {
Probability = node.fatherNode.probability;
}
return Probability;
}
private static void getLeafNode(ArrayList<Node> Leaf, Node now) {
if(now.betNode == null)
Leaf.add(now);
else {
getLeafNode(Leaf,now.betNode);
getLeafNode(Leaf,now.checkNode);
}
}
static boolean getNowRightPoker(Node now) {
boolean b = false;
Node node = now;
int i = 0;
while (node != null) {
node = node.fatherNode;
i++;
}
if(i%2 == 0) {
b = true;
}
return b;
}
static int getResult(Node leaf) {
int result = 0;
if (leaf.state.equals("bb")) {
if(leaf.poker > leaf.poker2) {
result += 2;
}else {
result -= 2;
}
} else if(leaf.state.equals("bc")) {
result += 1;
} else if(leaf.state.equals("cc")) {
if(leaf.poker > leaf.poker2) {
result += 1;
}else {
result -= 1;
}
} else if(leaf.state.equals("cbc")) {
result -= 1;
} else if(leaf.state.equals("cbb")) {
if(leaf.poker > leaf.poker2) {
result += 2;
}else {
result -= 2;
}
}
return result;
}
}