矩阵博弈代码:自动驾驶与攻击者车辆的对抗
定义两辆车的动作及其对应的矩阵
AV_actions = { 'up': [[1,0,0],[0,0,0],[0,0,0]], 'down': [[0,0,0],[0,0,0],[1,0,0]], 'left': [[0,0,1],[0,0,0],[0,0,0]], 'right': [[0,0,0],[0,0,1],[0,0,0]], }
BV_actions = { 'up': [[0,0,0],[0,0,0],[0,1,0]], 'down': [[0,1,0],[0,0,0],[0,0,0]], 'left': [[0,0,0],[1,0,0],[0,0,0]], 'right': [[0,0,0],[0,0,0],[0,0,1]], }
定义两辆车的初始矩阵
AV_matrix = [[1,0,0],[0,0,0],[0,0,0]] BV_matrix = [[0,0,0],[0,0,0],[0,0,0]]
进行博弈
while True: # AV选择一个动作 AV_action = input('AV's action (up/down/left/right): ') AV_matrix = matrix_multiply(AV_actions[AV_action], AV_matrix)
# BV选择一个动作
BV_action = input('BV's action (up/down/left/right): ')
BV_matrix = matrix_multiply(BV_actions[BV_action], BV_matrix)
# 判断是否已经结束
if AV_matrix[2][1] == 1: # AV到达终点,安全
print('AV wins! BV fails to make it dangerous.')
break
elif BV_matrix[2][1] == 1: # BV到达终点,危险
print('BV wins! AV failed to stay safe.')
break
定义矩阵相乘函数
def matrix_multiply(A, B): C = [[0,0,0],[0,0,0],[0,0,0]] for i in range(3): for j in range(3): for k in range(3): C[i][j] += A[i][k] * B[k][j] return C
原文地址: https://www.cveoy.top/t/topic/nttj 著作权归作者所有。请勿转载和采集!