您的位置:首页 > 其它

田忌赛马博弈矩阵分析

2017-10-28 00:34 134 查看
之前帮别人写的田忌赛马博弈矩阵分析

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.
bd0b
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  class