厌外 发表于 2025-5-29 20:08:07

博弈论中的简单合作博弈 C#实现

     最近在看一本关于博弈的书.有个比较简单的合作不合作的博弈.挺有意思,大意是这样的:
    
      这个博弈是对现实生活中人与人之间是否合作的简单抽象,具体内容和规则可以概括为“如果A与B都是合作态度,则是双赢,每人得3分;如果A合作态度,B玩阴的,则A欺骗了B,取得了B本该得到的利益,则B得5分,A扣3分,反之亦然。最后如果A和B都不合作,则一拍两散,两个人都白费劲,则每人扣一分”在这个游戏里,每个人都和除了自己之外的人合作100次,则得分最高的人胜利.

     我抽象到C#代码里是用一个接口来规范参与者,让他们实现自己的算法,并通过泛型列表保存和对手之间以往的合作记录,并可以根据合作记录来返回采取的策略..废话不说接口代码如下:
    

Code
1 public interface ActorBase
2 {
3       bool Gamble(string OpponentName);//你的策略封装在这个函数里,true是合作false是不合作
4        string GetUniqueCode();//用于返回你的名字来让对手确认你的身份
5        int Score { get; set; }//记录总分
6        void AddRecord(string OpponentName,bool record);  //用于添加以往对战的记录
7 }
   对于我的策略,我在第一次合作时保持合作态度,在以后是否合作都根据对手和自己上一步的情况来确定是否合作
    具体代码如下:
     1public class CareySon : ActorBase
 2        {
 3            Dictionary Record;//用于保存和对手以往的记录
 4            public Songyunjian()//构造函数,用于构造记录
 5            {
 6                Record = new Dictionary();
 7            }
 8            public string GetUniqueCode()  //返回你的唯一标识
 9            {
10                return "CareySon";
11            }
12            public void AddRecord(string OpponentName, bool record)
13            {
14                if (!Record.ContainsKey(OpponentName))//如果没合作过,创建合作记录
15                {
16                    List l = new List();
17                    l.Add(record);
18                    Record.Add(OpponentName, l);
19                }
20                else
21                {
22                    Record.Add(record);//利用索引器把记录添加到list里
23                }
24            }
25            public bool Gamble(string name)
26            {
27                if (!Record.ContainsKey(name))//如果是第一次合作,则保持合作态度
28                {
29                    return true;
30                }
31                else
32                {
33                    List t = Record;
34                    if (t.Count >= 1)
35                    {
36                        if (t)//如果最后一次是合作则返回合作
37                        {
38                            return true;
39                        }
40                        else//否则返回不合作
41                        {
42                            return false;
43                        }
44                    }
45                    return true;
46                    
47
48                }
49            }
50            public int Score
51            {
52                get { return _score; }
53                set{_score=value;}
54            }
55            public int _score=0;//用于记录每个人的分数
56
57        }
下面是一个我加进去的随机选手,即合作和不合作的态势是随机的,这里只展示Gamble()方法,其他同
 1 public bool Gamble(string name)
 2             {
 3                 Random rd=new Random();
 4                 int i=rd.Next(2);
 5                 if (i == 1)
 6                 {
 7                     return true;
 8                 }
 9                 return false;
10             }
下面是我一个舍友的策略,即根据后3次的合作记录来返回是否合作,Gamble()方法如下:
 1 public bool Gamble(string name)
 2             {
 3                 int z=0;
 4                 if (!Record.ContainsKey(name))
 5                 {
 6                     return true;
 7                 }
 8                 else
 9                 {
10                     List l = Record;
11                     if (l.Count == 1)
12                     {
13                         if (l)
14                         {
15                             return true;
16                         }
17                         else
18                         {
19                             return false;
20                         }
21                     }
22                     else if(l.Count==2)
23                     {
24                         if (l && l)
25                         {
26                             return true;
27                         }
28                         else
29                         {
30                             return false;
31                         }
32                     }
33                     else if (l.Count >= 3)
34                     {
35                         if (l) z++;
36                         if (l) z++;
37                         if (l) z++;
38                         if (z >= 2) return true;
39                         else {
40                             return false;
41                         }
42                     }
43                 }
44                 return false;
45             }
我的另一个舍友的策略是通过以往所有的合作记录来决定,如果合作次数大于不合作的,则合作,如果小于,则不合作,等于也不合作,代码如下:
 1 public bool Gamble(string name)
 2             {
 3                 int z = 0, c = 0;//z是合作次数,c是不合作次数
 4                 if (!Record.ContainsKey(name))
 5                 {
 6                     return true;
 7                 }
 8                 else
 9                 {
10                     foreach (bool b in Record)
11                     {
12                         if (b) z++;
13                         else
14                         {
15                             c++; 
16                         }
17                     }
18                     if (z == c)
19                     {
20                         return false;
21                     }
22                     if (z > c)
23                     {
24                         return true;
25                     }
26                     if (z 
页: [1]
查看完整版本: 博弈论中的简单合作博弈 C#实现