田忌賽馬博弈矩陣分析

TXR_TIME發表於2020-04-06

之前幫別人寫的田忌賽馬博弈矩陣分析

import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;

/**
 * 田忌賽馬 博弈矩陣 
 * @author txr
 *
 */
public class MyMatrix {

    /**
     * @param args
     */


    //賽馬出場 順序次數
    private int cnt;

    //賽馬數量
    //private int horscnt;

    //申明 博弈矩陣
    private int[][] matrix = null;

    String[] rs = null;

    int count = 0;

    int[] fpGameArray = null; 
    int[] spGameArray = null;

    public MyMatrix(int horse){
        /*String[] s = new String[cnt];
        s[0]="123";
        s[1]="132";
        s[2]="213";
        s[3]="231";
        s[4]="321";
        s[5]="312";
        String[] s1 = new String[cnt];*/

        if(horse<2){
            System.out.println("賽馬的數量少於兩匹,不能進行賽馬出場順序排序");
            return;
        }

        cnt = horse*(horse-1);
        matrix = new int[cnt][cnt];
        rs = new String[cnt];
        String[] s1 = new String[cnt];
        String ss = "";
        for(int n=1;n<=horse;n++){
            ss = ss + n;
        }
        System.out.println("排列組合");
        array(ss.toCharArray(),0,ss.length()-1);
        System.arraycopy(rs, 0, s1, 0, rs.length);
        for(int i=0;i<cnt;i++){
            for(int j=0;j<cnt;j++){
                int a = winPer(rs[i],s1[j]);
                matrix[i][j] = a;
            }
        }
    }

    //排列組合
    public void array(char[] c,int start,int end){
        String s1 = "";
        if(start == end){
            for(int i=0;i<=end;i++){
                s1 = s1 + c[i];
                System.out.print(c[i]);
            }
            rs[count] = s1;
            count++;
            System.out.println();
        }else{
            for(int i=start;i<=end;i++){
                char temp = c[start];
                c[start] = c[i];
                c[i] = temp;
                array(c,start+1,end);
                temp=c[start];
                c[start] = c[i];
                c[i]=temp;
            }
        }
        //return rs;
    }

    //齊王 和 田忌  一等馬對一等馬 1,二等馬對二等馬 1,三等馬對三等馬1,二等馬對一等馬 -1,三等馬對二等馬 -1,一局比賽之後的比賽結果
    public int winPer(String s1,String s2){
        int n = 0;
        char[] c1 = s1.toCharArray();
        char[] c2 = s2.toCharArray();
        for(int i=0;i<s1.length();i++){
            if(c1[i] <= c2[i])
                n = n + 1;
            else
                n = n - 1;
        }
        return n;
    }

    public void print(){
        System.out.println("輸出");
        System.out.print("    ");
        for(int i=0;i<cnt;i++){
            System.out.print("β["+(i+1)+ "] ");
        }
        System.out.println();
        for(int i=0;i<cnt;i++){
            System.out.print("α["+(i+1)+"]  ");
            for(int j=0;j<cnt;j++){
                System.out.print(matrix[i][j] + "    ");
            }
                System.out.println();
        }
    }

    public void pt(){
        System.out.println("請輸入比賽的局數:");
        Scanner sc = new Scanner(System.in);
        int count = 0;
        int index = 0;
        count = sc.nextInt();
        fpGameArray = new int[count];
        spGameArray = new int[count];

        Boolean bl = false;
        do{
            System.out.println("請輸入第一局α的下標");
            Scanner sc1 = new Scanner(System.in);
            //輸入引數 0-5
            index = sc1.nextInt()-1;
            if(index>5){
                System.out.println("請輸入1-6之間的數字");
            }else
            bl = true; 
        }while(!bl);

        System.out.print("局數 "+"   " );
        System.out.print("I   ");
        for(int i=1;i<=cnt;i++){
            System.out.print("β["+i+"] ");
        }
        System.out.print("  II   ");
        for(int i=1;i<=6;i++){
            System.out.print("α["+i+"] ");
        }
        System.out.println();
        int bmaxIndex = 0;
        int[] b = new int[cnt];
        int[] a = new int[cnt];

        for(int i=1;i<=count;i++){
            if(i != 1){
                index = bmaxIndex;
            }
            System.out.print(i+"   α["+ (index +1)+"] ");
            fpGameArray[i-1] = index;
            for(int j=0;j<cnt;j++){
                a[j] = a[j] + matrix[index][j];
                System.out.print(a[j]+"    ");
            }
            int amin = min(a);
            int aminIndex = 0;
            //int[] tempArray = null;
            int[] aminIndexArray =  findIndex(a,amin);
            if(aminIndexArray.length == 1){
                aminIndex  = aminIndexArray[0];
            }else if(aminIndexArray.length >1){
                int secondPerson = conditionThirdα(aminIndexArray,b);
                aminIndex = secondPerson;
            }

            System.out.print("β["+(aminIndex+1)+"] " );
            spGameArray[i-1] = aminIndex ;
            for(int k=0;k<6;k++){
                b[k] = b[k] + matrix[k][aminIndex];
                System.out.print(b[k]+"    ");

            }
             int[] bmaxIndexArray = findIndex(b,max(b));

             if(bmaxIndexArray.length == 1){
                 bmaxIndex = bmaxIndexArray[0];
             }else{
                 int firstPerson = conditionThirdβ(bmaxIndexArray,b);
                 bmaxIndex = firstPerson;
             }
            System.out.println();
        }
    }

    //求出 該數字在矩陣中的座標  縱座標位置
    public int[] findIndex(int[] a,int j){
        int k = 0;
        for(int m=0;m<a.length;m++){
            if(a[m] == j){
                k++;
            }
        }

        int k1 = 0;
        int[] indexArray = new int[k];
        for(int m=0;m<a.length;m++){
            if(a[m] == j){
                indexArray[k1]=m;
                k1++;
            }
        }

        return indexArray;
    }

    //查詢  局數n 羅馬數字Ⅰ 下面 α右邊最小的β值
    public int  min(int[] a){
        int[] at = new int[a.length];
        System.arraycopy(a, 0, at, 0, a.length);
        int temp = 0;
        for(int i=0;i<at.length;i++){
            for(int j=i+1;j<at.length;j++){
                if(at[i]>at[j]){
                    temp = at [i];
                    at[i] = at[j];
                    at[j] = temp;
                }
            }
        }
        int min = at[0];
        return min;
    }

    //查詢  局數n 羅馬數字Ⅱ 下面 β右邊最大的α值
    public int max(int[] b){
        int temp = 0;
        int[] bt = new int[b.length];
        System.arraycopy(b, 0, bt, 0, b.length);
        for(int i=0;i<bt.length;i++){
            for(int j=i+1;j<bt.length;j++){
                if(bt[i]>bt[j]){
                    temp = bt [i];
                    bt[i] = bt[j];
                    bt[j] = temp;
                }
            }
        }
        int max = bt[bt.length-1];
        return max; 
    }

    //局中人Ⅰ 選擇後,局中人Ⅱ 有多個選擇選擇的時候的規則   
    public int conditionThirdα(int[] a,int[] b){
        int len = b.length;
        int alen = a.length;
        int[] bTemp = new int[len];
        int[] max = new int[alen];
        for(int i=0;i<alen;i++){
            System.arraycopy(b, 0, bTemp, 0, len);
            for(int k=0;k<6;k++){
                bTemp[k] = bTemp[k] + matrix[k][a[i]];
            }
            max[i] = max(bTemp);
        }
        int[] index = findIndex(max,min(max));
        int r = 0;
        if(index.length == 1){
            r = index[0];
        }else{
            Random rd=new Random();
            r = index[rd.nextInt(index.length)];
        }
        return r;
    }

    //局中人Ⅱ 選擇後,局中人Ⅰ 有多個選擇選擇的時候的規則
    public int conditionThirdβ(int[] a,int[] b){
        int len = b.length;
        int alen = a.length;
        int[] bTemp = new int[len];
        int[] min = new int[alen];
        for(int i=0;i<alen;i++){
            System.arraycopy(b, 0, bTemp, 0, len);
            for(int k=0;k<6;k++){
                bTemp[k] = bTemp[k] + matrix[k][a[i]];
            }
            min[i] = min(bTemp);
        }
        int[] index = findIndex(min,max(min));
        int r = 0;
        if(index.length == 1){
            r = index[0];
        }else{
            Random rd = new Random();
            r = index[rd.nextInt(index.length)];
        }
        return r;
    }


    public void  proportion2(){
        HashMap<Integer,Double> map = new HashMap<Integer, Double>();
        for(int i=0;i<spGameArray.length;i++){
            double  cnt = 1;
            if(! map.containsKey(spGameArray[i])){
                map.put(spGameArray[i], cnt);
                for(int j=i+1;j<spGameArray.length;j++){
                    if(spGameArray[i] == spGameArray[j]){
                        cnt ++;
                    }
                }
                map.put(spGameArray[i], cnt/spGameArray.length);
            }
        }
        Object[] c = map.keySet().toArray();
        for(int i=0;i<map.size();i++){
            System.out.println("β" +(Integer.valueOf(c[i].toString())+1)+"在局中人 Ⅱ所佔比例 為"+map.get(c[i]));
        }
    }

    public void  proportion1(){
        HashMap<Integer,Double> map = new HashMap<Integer, Double>();
        for(int i=0;i<fpGameArray.length;i++){
            double  cnt = 1;
            if(! map.containsKey(fpGameArray[i])){
                map.put(fpGameArray[i], cnt);
                for(int j=i+1;j<fpGameArray.length;j++){
                    if(fpGameArray[i] == fpGameArray[j]){
                        cnt ++;
                    }
                }
                map.put(fpGameArray[i], cnt/fpGameArray.length);
            }
        }
        Object[] c = map.keySet().toArray();
        for(int i=0;i<map.size();i++){
            System.out.println("α" +(Integer.valueOf(c[i].toString())+1)+"在局中人Ⅰ中 所佔比例 為"+map.get(c[i]));
        }
    }

    public static void main(String[] args) {
        MyMatrix matrix = new MyMatrix(3);
        //matrix.cnt = 6;
        matrix.print();
        matrix.pt();
        matrix.proportion1();
        matrix.proportion2();

    }

}

對你有幫助的話歡迎下載原始碼並給分,謝謝,畢竟我下載別人東西也需要分的
o(╥﹏╥)o

相關文章