博弈论中的简单合作博弈C#实现
最近在看⼀本关于博弈的书.有个⽐较简单的合作不合作的博弈.挺有意思,⼤意是这样的:
这个博弈是对现实⽣活中⼈与⼈之间是否合作的简单抽象,具体内容和规则可以概括为“如果A与B都是合作态度,则是双赢,每⼈得3分;如果A合作态
度,B玩阴的,则A欺骗了B,取得了B本该得到的利益,则B得5分,A扣3分,反之亦然。最后如果A和B都不合作,则⼀拍两散,两个⼈都⽩费劲,则每⼈扣⼀分”在这个游戏⾥,每个⼈都和除了⾃⼰之外的⼈合作100次,则得分最⾼的⼈胜利.
我抽象到C#代码⾥是⽤⼀个接⼝来规范参与者,让他们实现⾃⼰的算法,并通过泛型列表保存和对⼿之间以往的合作记录,并可以根据合作记录来返回采取的策略..废话不说接⼝代码如下:
Code
对于我的策略,我在第⼀次合作时保持合作态度,在以后是否合作都根据对⼿和⾃⼰上⼀步的情况来确定是否合作
具体代码如下:
1public class CareySon : ActorBa
2 {
3 Dictionary<string, List<bool>> Record;//⽤于保存和对⼿以往的记录
4public Songyunjian()//构造函数,⽤于构造记录
5 {
6 Record = new Dictionary<string, List<bool>>();
7 }
苹果原产地
8public string GetUniqueCode() //返回你的唯⼀标识
9 {
10return"CareySon";
11 }
12public void AddRecord(string OpponentName, bool record)
13 {
14if (!Record.ContainsKey(OpponentName))//如果没合作过,创建合作记录
15 {
16 List<bool> l = new List<bool>();
17 l.Add(record);
18 Record.Add(OpponentName, l);
19 }
20el
21 {
22 Record[OpponentName].Add(record);//利⽤索引器把记录添加到list⾥
23 }
24 }
25public bool Gamble(string name)
26 {
27if (!Record.ContainsKey(name))//如果是第⼀次合作,则保持合作态度
28 {
29return true;
对联怎么写30 }
31el
32 {
33 List<bool> t = Record[name];
34if (t.Count >= 1)
35 {
36if (t[t.Count - 1])//如果最后⼀次是合作则返回合作
37 {湖北莲藕排骨汤
38return true;
39 }
40el//否则返回不合作
41 {
42return fal;
43 }
44 }
45return true;
46
47
48 }
49 }
50public int Score
51 {
52get { return _score; }
53t{_score=value;}
54 }
55public int _score=0;//⽤于记录每个⼈的分数
56
57 }
下⾯是⼀个我加进去的随机选⼿,即合作和不合作的态势是随机的,这⾥只展⽰Gamble()⽅法,其他同 1public bool Gamble(string name)
2 {
3 Random rd=new Random();
4int i=rd.Next(2);
5if (i == 1)
6 {
7return true;
8 }
9return fal;
10 }
下⾯是我⼀个舍友的策略,即根据后3次的合作记录来返回是否合作,Gamble()⽅法如下:
1public bool Gamble(string name)
2 {
3int z=0;
4if (!Record.ContainsKey(name))
5 {
6return true;
7 }
8el
9 {
10 List<bool> l = Record[name];
11if (l.Count == 1)
12 {
13if (l[0])
14 {
咏梅的古诗
15return true;
16 }
17el
18 {
19return fal;
20 }
21 }
22el if(l.Count==2)
23 {
24if (l[0] && l[1])
25 {
26return true;
27 }
28el
29 {
30return fal;
31 }
32 }
33el if (l.Count >= 3)
34 {
35if (l[l.Count - 1]) z++;ix是数字几
36if (l[l.Count - 2]) z++;
37if (l[l.Count - 3]) z++;
38if (z >= 2) return true;
39el {
40return fal;
41 }
42 }
43 }
44return fal;
45 }
我的另⼀个舍友的策略是通过以往所有的合作记录来决定,如果合作次数⼤于不合作的,则合作,如果⼩于,则不合作,等于也不合作,代码如下: 1public bool Gamble(string name)
2 {
3int z = 0, c = 0;//z是合作次数,c是不合作次数
4if (!Record.ContainsKey(name))
5 {
6return true;
7 }
8el
9 {
10foreach (bool b in Record[name])
11 {
12if (b) z++;
13el
14 {
15 c++;
16 }
17 }
18if (z == c)
19 {
20return fal;
21 }
22if (z > c)
23 {
24return true;
25 }
26if (z < c)
27 {
28return fal;
29 }
30 }
31return fal;
32
33 }
最后是客户端调⽤的代码
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
警署警长5using System.Web.UI;
6using System.Web.UI.WebControls;
7using TheGame;
8public partial class GameTheory : System.Web.UI.Page
9{
10protected void Page_Load(object nder, EventArgs e)
11 {
12 List<TheGame.ActorBa> player = new List<TheGame.ActorBa>();//我把接⼝和实现的代码放⼊了TheGame命名空间
13 TheGame.ActorBa CareySon = new TheGame.Songyunjian();
14 TheGame.ActorBa RandomPlayer = new TheGame.RandomPlayer();
15 TheGame.ActorBa Tony = new TheGame.Tony();
16 TheGame.ActorBa Jack = new TheGame.Jack();
17 player.Add(CareySon);
18 player.Add(RandomPlayer);
19 player.Add(Tony);
20 player.Add(Jack);
21/*从这⾥开始下⾯都是算法部分*/
22for (int x = 1; x <= 100; x++)//循环合作100次
23 {
24for (int i = 0; i < player.Count; i++)//让选⼿和其他所有选⼿进⾏博弈
25 {
26for (int j = i + 1; j < player.Count; j++)
27 {
28
29bool abBool = player[i].Gamble(player[j].GetUniqueCode());
30 ActorBa ab = player[i];
31bool absBool = player[j].Gamble(player[i].GetUniqueCode());
32 ActorBa abs = player[j];
33if (abBool && absBool)//当AB合作的时候
34 {
35 ab.Score += 3;
36 abs.Score += 3;
37 ab.AddRecord(abs.GetUniqueCode(), true);
38 abs.AddRecord(ab.GetUniqueCode(), true);
39 }
40el if (abBool & !absBool) //当AB合作⽽ABS不合作
41 {
42 ab.Score -= 3;
43 abs.Score += 5;
44 ab.AddRecord(abs.GetUniqueCode(), fal);
45 abs.AddRecord(ab.GetUniqueCode(), true);
46 }
47el if (absBool & !abBool)//当abs合作⽽AB不合作的情况
48 {
49 ab.Score += 5;
50 abs.Score -= 3;
51 ab.AddRecord(abs.GetUniqueCode(), true);
52 abs.AddRecord(ab.GetUniqueCode(), fal);
53 }
54el if (!absBool && !abBool)//当双⽅都不合作的情况下
55 {
56 ab.Score -= 1;
57 abs.Score -= 1;
58 ab.AddRecord(abs.GetUniqueCode(), fal);ssd硬盘
59 abs.AddRecord(ab.GetUniqueCode(), fal);
60 }
61
62 }
63 }
64 }
65 OutputResult(player);//输出并打印到屏幕上成绩
66
67 }
68private void OutputResult(List<TheGame.ActorBa> l)
69 {
70foreach (ActorBa ab in l)
71 {
72 HttpContext.Current.Respon.Write("Player: <span style='background-
color:#cdcdcd;color:blue;border:solid 1px #3cdcad'>" + ab.GetUniqueCode() + "</span> and the Score is <span style='background-color:#cdcdcd;color:blue;border:solid 1px #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
格力总裁
-----------------------------------------------------
园⼦⾥有很多博弈学⾼⼿,有什么好的策略发上来⼀起探讨下:-)