虚拟遗憾最小化-java

public class Node {
    Node fatherNode;
    Node betNode;
    Node checkNode;
    int poker;
    int poker2;
    String state;
    double probability;
    double betIValue;
    double checkIValue;
    double currentNode;

    /**
     *
     * @param fatherNode :父节点
     * @param state :状态
     * @param probability :执行当前动作概率
     * @param poker :手牌1
     * @param poker2 :手牌2
     */
    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;
    }

    /**
     *
     * @param betNode :bet节点
     * @param checkNode :check节点
     */
    void setChildNode(Node betNode, Node checkNode) {
        this.betNode = betNode;
        this.checkNode = checkNode;
    }

    /**
     * 得到betIValue 公式:  v(h) = ∑z∈Z,h⊏z π-P(h)π(z|h)u(z)
     */
    double setBetIValue() {
        betIValue = CfrUse.CountV(this.betNode);
        return betIValue;
    }

    /**
     * 得到checkIValue 公式:  v(h) = ∑z∈Z,h⊏z π-P(h)π(z|h)u(z)
     */
    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);
        //System.out.println("信息集 :" + state + " " + sum);
        if(sum > 0) {
            betProbability = judge(betRegret) / sum;
            checkProbability = judge(checkRegret) / sum;
            //System.out.println("信息集 :" + state + " betProbability:" + betProbability);
            //System.out.println("信息集 :" + state + " checkProbability:" + checkProbability);
        }
        else {
            betProbability = 0.5;
            checkProbability = 0.5;
        }
//        System.out.println("judge(betRegret): " + judge(betRegret) +
//                " judge(betRegret) + judge(checkRegret): " +
//                (judge(betRegret) + judge(checkRegret)));
//        System.out.println("judge(checkRegret): " + judge(checkRegret) + " judge(betRegret) + judge(checkRegret) : " +
//                (judge(betRegret) + judge(checkRegret)));
//        System.out.println("信息集 :" + state + " BET孩子概率"+betProbability);
//        System.out.println("信息集 :" + state + " check孩子概率"+checkProbability);
        setChildProbability();
    }

    void setChildProbability() {
        node1.betNode.probability = betProbability;
        node2.betNode.probability = betProbability;
        node1.checkNode.probability = checkProbability;
        node2.checkNode.probability = checkProbability;
    }
    void UpdateAverage() {
        //System.out.println("信息集 :" + state + " node1.setCurrentNode(): " + node1.setCurrentNode() );
        averageBetProbability += node1.setCurrentNode()*betProbability ;
        averageCheckProbability += node1.setCurrentNode()*checkProbability;
    }

    void UpdateRegrets() {
        //System.out.println("-------------------------------------------------");\
        //System.out.println("信息集 :" + state);
        double node = node1.setBetIValue();
        double Node = node2.setBetIValue();
        betValue = node+Node;

        double v = node1.setCheckIValue();
        double v1 = node2.setCheckIValue();
        checkValue = v + v1;
        //System.out.println("信息集 :" + state + " check遗憾值:"  + checkValue+ " " + v +" " + v1);
        //System.out.println("信息集 :" + state + " bet遗憾值:"  + betValue+ " " + node1.setBetIValue() +" " + node2.setBetIValue() );
        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);
       // System.out.println("信息集 :" + state + " bet概率 :"  + betProbability  + " check概率 :" + checkProbability );
//        System.out.println("信息集 :" + state + " bet遗憾值:"  + betRegret+ " check遗憾值:" + checkRegret);
//        System.out.println("信息集 :" + state + " bet反事实值:"  + betValue+ " check反事实值:" + checkValue);checkValue
    }

    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));
//               System.out.println( "向上概率: " + getNotNowNodeProbability(now.fatherNode) +
//                  " 向下概率: "+getNowLeafProbability(now.fatherNode,leaf.get(i)) +
//                 " 结果: "+getResult(leaf.get(i))+
//                   " 信息集: " +leaf.get(i).state);
        }
        //System.out.println(  " 信息集: " + now.state +" 反事实值:" +IRealValue);
        return IRealValue/6;
    }

    /**
     * @param now 当前节点
     * @return 返回当前节点非我方节点概率
     */
    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;
    }

    /**
     * @param now 当前节点
     * @param leaf 叶子节点
     * @return 返回叶子到节点的概率
     */
    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;
    }

    /**
     * @param now 当前节点
     * @return 返回当前节点的概率
     */
    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;
    }

    /**
     * @param Leaf : 存储叶子节点集合
     * @param now :计算反事实值的几点
     */
    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);
        }
    }

    /**
     * 根据当前玩家的位置交换poker和poker2的位置
     * @param now 当前节点
     */
    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;
    }

    /**
     *
     * @param leaf 叶子节点的
     * @return 返回叶子节点的结果
     */
    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;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值