在博弈论中,简单合作博弈C的特点有哪些?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1664个文字,预计阅读时间需要7分钟。
最近读了一本关于博弈的书。书中对比了简单和不合作的博弈者。有趣的是,大意是这样的:这个博弈者是对现实生活人际互动是否合作的简单抽象,其内容规则可以概括为如果。
最近在看一本关于博弈的书.有个比较简单的合作不合作的博弈.挺有意思,大意是这样的:
这个博弈是对现实生活中人与人之间是否合作的简单抽象,具体内容和规则可以概括为“如果A与B都是合作态度,则是双赢,每人得3分;如果A合作态度,B玩阴的,则A欺骗了B,取得了B本该得到的利益,则B得5分,A扣3分,反之亦然。最后如果A和B都不合作,则一拍两散,两个人都白费劲,则每人扣一分”在这个游戏里,每个人都和除了自己之外的人合作100次,则得分最高的人胜利.
我抽象到C#代码里是用一个接口来规范参与者,让他们实现自己的算法,并通过泛型列表保存和对手之间以往的合作记录,并可以根据合作记录来返回采取的策略..废话不说接口代码如下:
Code
1publicinterfaceActorBase
2{
3boolGamble(stringOpponentName);//你的策略封装在这个函数里,true是合作false是不合作
4stringGetUniqueCode();//用于返回你的名字来让对手确认你的身份
5intScore{get;set;}//记录总分
6voidAddRecord(stringOpponentName,boolrecord);//用于添加以往对战的记录
7}
对于我的策略,我在第一次合作时保持合作态度,在以后是否合作都根据对手和自己上一步的情况来确定是否合作
具体代码如下:
1publicclassCareySon:ActorBase
2{
3Dictionary<string,List<bool>>Record;//用于保存和对手以往的记录
4publicSongyunjian()//构造函数,用于构造记录
5{
6Record=newDictionary<string,List<bool>>();
7}
8publicstringGetUniqueCode()//返回你的唯一标识
9{
10return"CareySon";
11}
12publicvoidAddRecord(stringOpponentName,boolrecord)
13{
14if(!Record.ContainsKey(OpponentName))//如果没合作过,创建合作记录
15{
16List<bool>l=newList<bool>();
17l.Add(record);
18Record.Add(OpponentName,l);
19}
20else
21{
22Record[OpponentName].Add(record);//利用索引器把记录添加到list里
23}
24}
25publicboolGamble(stringname)
26{
27if(!Record.ContainsKey(name))//如果是第一次合作,则保持合作态度
28{
29returntrue;
30}
31else
32{
33List<bool>t=Record[name];
34if(t.Count>=1)
35{
36if(t[t.Count-1])//如果最后一次是合作则返回合作
37{
38returntrue;
39}
40else//否则返回不合作
41{
42returnfalse;
43}
44}
45returntrue;
46
47
48}
49}
50publicintScore
51{
52get{return_score;}
53set{_score=value;}
54}
55publicint_score=0;//用于记录每个人的分数
56
57}
下面是一个我加进去的随机选手,即合作和不合作的态势是随机的,这里只展示Gamble()方法,其他同
1publicboolGamble(stringname)
2{
3Randomrd=newRandom();
4inti=rd.Next(2);
5if(i==1)
6{
7returntrue;
8}
9returnfalse;
10}
下面是我一个舍友的策略,即根据后3次的合作记录来返回是否合作,Gamble()方法如下:
1publicboolGamble(stringname)
2{
3intz=0;
4if(!Record.ContainsKey(name))
5{
6returntrue;
7}
8else
9{
10List<bool>l=Record[name];
11if(l.Count==1)
12{
13if(l[0])
14{
15returntrue;
16}
17else
18{
19returnfalse;
20}
21}
22elseif(l.Count==2)
23{
24if(l[0]&&l[1])
25{
26returntrue;
27}
28else
29{
30returnfalse;
31}
32}
33elseif(l.Count>=3)
34{
35if(l[l.Count-1])z++;
36if(l[l.Count-2])z++;
37if(l[l.Count-3])z++;
38if(z>=2)returntrue;
39else{
40returnfalse;
41}
42}
43}
44returnfalse;
45}
我的另一个舍友的策略是通过以往所有的合作记录来决定,如果合作次数大于不合作的,则合作,如果小于,则不合作,等于也不合作,代码如下:
1publicboolGamble(stringname)
2{
3intz=0,c=0;//z是合作次数,c是不合作次数
4if(!Record.ContainsKey(name))
5{
6returntrue;
7}
8else
9{
10foreach(boolbinRecord[name])
11{
12if(b)z++;
13else
14{
15c++;
16}
17}
18if(z==c)
19{
20returnfalse;
21}
22if(z>c)
23{
24returntrue;
25}
26if(z<c)
27{
28returnfalse;
29}
30}
31returnfalse;
32
33}
最后是客户端调用的代码 1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Web;
5usingSystem.Web.UI;
6usingSystem.Web.UI.WebControls;
7usingTheGame;
8publicpartialclassGameTheory:System.Web.UI.Page
9{
10protectedvoidPage_Load(objectsender,EventArgse)
11{
12List<TheGame.ActorBase>player=newList<TheGame.ActorBase>();//我把接口和实现的代码放入了TheGame命名空间
13TheGame.ActorBaseCareySon=newTheGame.Songyunjian();
14TheGame.ActorBaseRandomPlayer=newTheGame.RandomPlayer();
15TheGame.ActorBaseTony=newTheGame.Tony();
16TheGame.ActorBaseJack=newTheGame.Jack();
17player.Add(CareySon);
18player.Add(RandomPlayer);
19player.Add(Tony);
20player.Add(Jack);
21/*从这里开始下面都是算法部分*/
22for(intx=1;x<=100;x++)//循环合作100次
23{
24for(inti=0;i<player.Count;i++)//让选手和其他所有选手进行博弈
25{
26for(intj=i+1;j<player.Count;j++)
27{
28
29boolabBool=player[i].Gamble(player[j].GetUniqueCode());
30ActorBaseab=player[i];
31boolabsBool=player[j].Gamble(player[i].GetUniqueCode());
32ActorBaseabs=player[j];
33if(abBool&&absBool)//当AB合作的时候
34{
35ab.Score+=3;
36abs.Score+=3;
37ab.AddRecord(abs.GetUniqueCode(),true);
38abs.AddRecord(ab.GetUniqueCode(),true);
39}
40elseif(abBool&!absBool)//当AB合作而ABS不合作
41{
42ab.Score-=3;
43abs.Score+=5;
44ab.AddRecord(abs.GetUniqueCode(),false);
45abs.AddRecord(ab.GetUniqueCode(),true);
46}
47elseif(absBool&!abBool)//当abs合作而AB不合作的情况
48{
49ab.Score+=5;
50abs.Score-=3;
51ab.AddRecord(abs.GetUniqueCode(),true);
52abs.AddRecord(ab.GetUniqueCode(),false);
53}
54elseif(!absBool&&!abBool)//当双方都不合作的情况下
55{
56ab.Score-=1;
57abs.Score-=1;
58ab.AddRecord(abs.GetUniqueCode(),false);
59abs.AddRecord(ab.GetUniqueCode(),false);
60}
61
62}
63}
64}
65OutputResult(player);//输出并打印到屏幕上成绩
66
67}
68privatevoidOutputResult(List<TheGame.ActorBase>l)
69{
70foreach(ActorBaseabinl)
71{
72HttpContext.Current.Response.Write("Player:<spanstyle='background-color:#cdcdcd;color:blue;border:solid1px#3cdcad'>"+ab.GetUniqueCode()+"</span>andtheScoreis<spanstyle='background-color:#cdcdcd;color:blue;border:solid1px#3cdcad'>"+ab.Score.ToString()+"</span><br/>");
73}
74}
75}
76
自此代码就完了,代码有点粗糙,各位看官切勿仍板砖-.-!!
某一次的运行结果如下,我就不抓图了:
-----------------------------------------------------
Player:CareySon and the Score is 686
Player: RandomPlayer and the Score is 570
Player:Tony and the Score is 670
Player:Jack and the Score is 734
-----------------------------------------------------
园子里有很多博弈学高手,有什么好的策略发上来一起探讨下:-)
本文共计1664个文字,预计阅读时间需要7分钟。
最近读了一本关于博弈的书。书中对比了简单和不合作的博弈者。有趣的是,大意是这样的:这个博弈者是对现实生活人际互动是否合作的简单抽象,其内容规则可以概括为如果。
最近在看一本关于博弈的书.有个比较简单的合作不合作的博弈.挺有意思,大意是这样的:
这个博弈是对现实生活中人与人之间是否合作的简单抽象,具体内容和规则可以概括为“如果A与B都是合作态度,则是双赢,每人得3分;如果A合作态度,B玩阴的,则A欺骗了B,取得了B本该得到的利益,则B得5分,A扣3分,反之亦然。最后如果A和B都不合作,则一拍两散,两个人都白费劲,则每人扣一分”在这个游戏里,每个人都和除了自己之外的人合作100次,则得分最高的人胜利.
我抽象到C#代码里是用一个接口来规范参与者,让他们实现自己的算法,并通过泛型列表保存和对手之间以往的合作记录,并可以根据合作记录来返回采取的策略..废话不说接口代码如下:
Code
1publicinterfaceActorBase
2{
3boolGamble(stringOpponentName);//你的策略封装在这个函数里,true是合作false是不合作
4stringGetUniqueCode();//用于返回你的名字来让对手确认你的身份
5intScore{get;set;}//记录总分
6voidAddRecord(stringOpponentName,boolrecord);//用于添加以往对战的记录
7}
对于我的策略,我在第一次合作时保持合作态度,在以后是否合作都根据对手和自己上一步的情况来确定是否合作
具体代码如下:
1publicclassCareySon:ActorBase
2{
3Dictionary<string,List<bool>>Record;//用于保存和对手以往的记录
4publicSongyunjian()//构造函数,用于构造记录
5{
6Record=newDictionary<string,List<bool>>();
7}
8publicstringGetUniqueCode()//返回你的唯一标识
9{
10return"CareySon";
11}
12publicvoidAddRecord(stringOpponentName,boolrecord)
13{
14if(!Record.ContainsKey(OpponentName))//如果没合作过,创建合作记录
15{
16List<bool>l=newList<bool>();
17l.Add(record);
18Record.Add(OpponentName,l);
19}
20else
21{
22Record[OpponentName].Add(record);//利用索引器把记录添加到list里
23}
24}
25publicboolGamble(stringname)
26{
27if(!Record.ContainsKey(name))//如果是第一次合作,则保持合作态度
28{
29returntrue;
30}
31else
32{
33List<bool>t=Record[name];
34if(t.Count>=1)
35{
36if(t[t.Count-1])//如果最后一次是合作则返回合作
37{
38returntrue;
39}
40else//否则返回不合作
41{
42returnfalse;
43}
44}
45returntrue;
46
47
48}
49}
50publicintScore
51{
52get{return_score;}
53set{_score=value;}
54}
55publicint_score=0;//用于记录每个人的分数
56
57}
下面是一个我加进去的随机选手,即合作和不合作的态势是随机的,这里只展示Gamble()方法,其他同
1publicboolGamble(stringname)
2{
3Randomrd=newRandom();
4inti=rd.Next(2);
5if(i==1)
6{
7returntrue;
8}
9returnfalse;
10}
下面是我一个舍友的策略,即根据后3次的合作记录来返回是否合作,Gamble()方法如下:
1publicboolGamble(stringname)
2{
3intz=0;
4if(!Record.ContainsKey(name))
5{
6returntrue;
7}
8else
9{
10List<bool>l=Record[name];
11if(l.Count==1)
12{
13if(l[0])
14{
15returntrue;
16}
17else
18{
19returnfalse;
20}
21}
22elseif(l.Count==2)
23{
24if(l[0]&&l[1])
25{
26returntrue;
27}
28else
29{
30returnfalse;
31}
32}
33elseif(l.Count>=3)
34{
35if(l[l.Count-1])z++;
36if(l[l.Count-2])z++;
37if(l[l.Count-3])z++;
38if(z>=2)returntrue;
39else{
40returnfalse;
41}
42}
43}
44returnfalse;
45}
我的另一个舍友的策略是通过以往所有的合作记录来决定,如果合作次数大于不合作的,则合作,如果小于,则不合作,等于也不合作,代码如下:
1publicboolGamble(stringname)
2{
3intz=0,c=0;//z是合作次数,c是不合作次数
4if(!Record.ContainsKey(name))
5{
6returntrue;
7}
8else
9{
10foreach(boolbinRecord[name])
11{
12if(b)z++;
13else
14{
15c++;
16}
17}
18if(z==c)
19{
20returnfalse;
21}
22if(z>c)
23{
24returntrue;
25}
26if(z<c)
27{
28returnfalse;
29}
30}
31returnfalse;
32
33}
最后是客户端调用的代码 1usingSystem;
2usingSystem.Collections.Generic;
3usingSystem.Linq;
4usingSystem.Web;
5usingSystem.Web.UI;
6usingSystem.Web.UI.WebControls;
7usingTheGame;
8publicpartialclassGameTheory:System.Web.UI.Page
9{
10protectedvoidPage_Load(objectsender,EventArgse)
11{
12List<TheGame.ActorBase>player=newList<TheGame.ActorBase>();//我把接口和实现的代码放入了TheGame命名空间
13TheGame.ActorBaseCareySon=newTheGame.Songyunjian();
14TheGame.ActorBaseRandomPlayer=newTheGame.RandomPlayer();
15TheGame.ActorBaseTony=newTheGame.Tony();
16TheGame.ActorBaseJack=newTheGame.Jack();
17player.Add(CareySon);
18player.Add(RandomPlayer);
19player.Add(Tony);
20player.Add(Jack);
21/*从这里开始下面都是算法部分*/
22for(intx=1;x<=100;x++)//循环合作100次
23{
24for(inti=0;i<player.Count;i++)//让选手和其他所有选手进行博弈
25{
26for(intj=i+1;j<player.Count;j++)
27{
28
29boolabBool=player[i].Gamble(player[j].GetUniqueCode());
30ActorBaseab=player[i];
31boolabsBool=player[j].Gamble(player[i].GetUniqueCode());
32ActorBaseabs=player[j];
33if(abBool&&absBool)//当AB合作的时候
34{
35ab.Score+=3;
36abs.Score+=3;
37ab.AddRecord(abs.GetUniqueCode(),true);
38abs.AddRecord(ab.GetUniqueCode(),true);
39}
40elseif(abBool&!absBool)//当AB合作而ABS不合作
41{
42ab.Score-=3;
43abs.Score+=5;
44ab.AddRecord(abs.GetUniqueCode(),false);
45abs.AddRecord(ab.GetUniqueCode(),true);
46}
47elseif(absBool&!abBool)//当abs合作而AB不合作的情况
48{
49ab.Score+=5;
50abs.Score-=3;
51ab.AddRecord(abs.GetUniqueCode(),true);
52abs.AddRecord(ab.GetUniqueCode(),false);
53}
54elseif(!absBool&&!abBool)//当双方都不合作的情况下
55{
56ab.Score-=1;
57abs.Score-=1;
58ab.AddRecord(abs.GetUniqueCode(),false);
59abs.AddRecord(ab.GetUniqueCode(),false);
60}
61
62}
63}
64}
65OutputResult(player);//输出并打印到屏幕上成绩
66
67}
68privatevoidOutputResult(List<TheGame.ActorBase>l)
69{
70foreach(ActorBaseabinl)
71{
72HttpContext.Current.Response.Write("Player:<spanstyle='background-color:#cdcdcd;color:blue;border:solid1px#3cdcad'>"+ab.GetUniqueCode()+"</span>andtheScoreis<spanstyle='background-color:#cdcdcd;color:blue;border:solid1px#3cdcad'>"+ab.Score.ToString()+"</span><br/>");
73}
74}
75}
76
自此代码就完了,代码有点粗糙,各位看官切勿仍板砖-.-!!
某一次的运行结果如下,我就不抓图了:
-----------------------------------------------------
Player:CareySon and the Score is 686
Player: RandomPlayer and the Score is 570
Player:Tony and the Score is 670
Player:Jack and the Score is 734
-----------------------------------------------------
园子里有很多博弈学高手,有什么好的策略发上来一起探讨下:-)

