解释下列代码:def p11(history, myhistory): if history[0] < Kb: return 1 else: p = CoopCoop_times(history, myhistory) if p == inf: return 1 else: return p def p12(history, myhistory): if history[0] < Kb: return 0 else: p = CoopBetray_times(history, myhistory) if p == inf: return 1 else: return p
时间: 2023-06-25 09:07:48 浏览: 52
这段代码是对于囚徒困境博弈中的一种策略进行描述,其中p11和p12分别是两个不同的策略函数。这些函数的输入是历史记录和我的历史记录,输出是对于当前局面的决策,即选择合作或背叛。
其中,history[0]是指当前局面中对手的上一步决策,Kb是一个常数。如果对手的上一步决策小于Kb,那么p11函数返回1(即选择合作),否则就计算出当前局面下我和对手共同选择合作的次数(CoopCoop_times)并返回这个次数作为决策。如果CoopCoop_times的值为inf,则返回1作为决策。
p12函数基本上与p11函数相似,只是在对CoopCoop_times的计算改为了CoopBetray_times。如果CoopBetray_times的值为inf,则返回1作为决策。
相关问题
解释下列代码:def p21(history, myhistory): if history[0] < Kb: return 1 else: p = BetrayCoop_times(history, myhistory) if p == inf: return 1 else: return p def p22(history, myhistory): if history[0] < Kb: return 0 else: p = BetrayBetray_times(history, myhistory) if p == inf: return 1 else: return p
这段代码定义了两个函数 `p21` 和 `p22`,它们都接受两个参数 `history` 和 `myhistory`,分别表示对手的历史动作和自己的历史动作。
函数 `p21` 的作用是根据对手的历史动作来决定自己的下一步动作。具体实现如下:
- 如果对手合作的次数小于 `Kb`,则自己选择合作,返回 `1`。
- 否则,调用 `BetrayCoop_times` 函数计算自己背叛次数与对手合作次数的比值 `p`。
- 如果 `p` 等于正无穷,则自己选择背叛,返回 `1`。
- 否则,返回 `p`。
函数 `p22` 的作用类似于 `p21`,只不过它是根据自己和对手的背叛次数来决定自己的下一步动作。具体实现如下:
- 如果对手合作的次数小于 `Kb`,则自己选择背叛,返回 `0`。
- 否则,调用 `BetrayBetray_times` 函数计算自己和对手的背叛次数的比值 `p`。
- 如果 `p` 等于正无穷,则自己选择背叛,返回 `1`。
- 否则,返回 `p`。
解释下列代码:def cBetray_times(history): times = 0 for i in range(history[0] + 1, 2, -1): if history[i] == 1: times += 1 else: break return times def cCoop_times(history): times = 0 for i in range(history[0] + 1, 2, -1): if history[i] == 0: times += 1 else: break return times # Some useful functions def cBetray_times(history): times = 0 for i in range(history[0] + 1, 2, -1): if history[i] == 1: times += 1 else: break return times def cCoop_times(history): times = 0 for i in range(history[0] + 1, 2, -1): if history[i] == 0: times += 1 else: break return times def BetrayBetray_times(history, myhistory): if myhistory[1] == 0: return inf else: m = 0 n = 0 for i in range(2, history[0] + 1): if myhistory[i] == 1: n += 1 if history[i] == 1: m += 1 return m / n def CoopBetray_times(history, myhistory): if myhistory[1] == 0: return inf else: return 1 - BetrayBetray_times(history, myhistory) def BetrayCoop_times(history, myhistory): if myhistory[0] == myhistory[1]: return inf else: m = 0 n = 0 for i in range(2, history[0] + 1): if myhistory[i] == 0: n += 1 if history[i] == 1: m += 1 return m / n def CoopCoop_times(history, myhistory): if myhistory[0] == myhistory[1]: return inf else: return 1 - BetrayCoop_times(history, myhistory)
这段代码定义了几个函数,用于计算博弈中的一些策略和参数。
- cBetray_times(history): 给定历史记录,返回最近的一次背叛之后出现的合作次数。
- cCoop_times(history): 给定历史记录,返回最近的一次合作之后出现的背叛次数。
- BetrayBetray_times(history, myhistory): 给定历史记录和我的历史记录,返回在我背叛的情况下,对手也背叛的概率。
- CoopBetray_times(history, myhistory): 给定历史记录和我的历史记录,返回在我合作的情况下,对手背叛的概率。
- BetrayCoop_times(history, myhistory): 给定历史记录和我的历史记录,返回在我背叛的情况下,对手合作的概率。
- CoopCoop_times(history, myhistory): 给定历史记录和我的历史记录,返回在我合作的情况下,对手也合作的概率。
这些函数都是用来计算策略概率的,可以在博弈中用来选择下一步的行动。其中,函数中的参数history表示历史记录,myhistory表示我的历史记录。函数中的inf表示无限大,用来表示某些情况下的极端情况。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)