# 从多维数组 (ndarray) 返回最大值

``````import numpy
import nash

pool_of_games = [[[0,2,1,3,3,2,1,0],"PdPd","PrisonerDilemma"],
[[0,3,1,2,2,3,1,0],"ShSh","StagHunt"],
[[1,2,3,0,3,2,1,0],"ChCh","Chicken"],
[[2,1,0,3,3,1,0,2],"BaBa","Battle"]]

def RandomStrategySelection343(pool_of_games):
gci = 0
fitness_list = []
nash_pool = []
for game in range (0, len(pool_of_games)):
pr1 = pool_of_games[game][0][:2]
pr2 = pool_of_games[game][0][2:4]
pc1 = pool_of_games[game][0][4:6]
pc2 = pool_of_games[game][0][6:]
p1_strategy_vector = [pr1, pr2]
p2_strategy_vector = [pc1, pc2]
game_matrix = nash.Game(p1_strategy_vector,p2_strategy_vector)
print(game_matrix)
nash_array = game_matrix.support_enumeration()
nash_list = list(nash_array)
print("NL",nash_list)
for s1,s2 in nash_list:
row_utility = ((numpy.dot(numpy.dot(s1,p1_strategy_vector),s2)))
print(row_utility)
col_utility = (numpy.dot(numpy.dot(s1,p2_strategy_vector),s2))
print(col_utility)
fitness = row_utility + col_utility
print ("FS",fitness)
maxfit = fitness.max()
print ("MX", maxfit)
``````

``````Row player:
[[2 1]
[0 3]]

Column player:
[[3 1]
[0 2]]
NL [(array([ 1.,  0.]), array([ 1.,  0.])), (array([ 0.,  1.]), array([ 0.,  1.])), (array([ 0.5,  0.5]), array([ 0.5,  0.5]))]
2.0
3.0
FS 5.0
3.0
2.0
FS 5.0
1.5
1.5
FS 3.0
MX 3.0
``````

`fitness`不是一个数组，它是一个你在循环中分配的标量。首先它被分配了 5；然后是 5，然后`fitness`是 3。循环结束时是 3。单个数字 3 的最大值当然是 3。以下是找到最大适应度的正确方法：

``````  maxfit = -np.inf   # negative infinity to initialize
for s1,s2 in nash_list:
row_utility = ((numpy.dot(numpy.dot(s1,p1_strategy_vector),s2)))
col_utility = (numpy.dot(numpy.dot(s1,p2_strategy_vector),s2))
fitness = row_utility + col_utility
maxfit = max(fitness, maxfit)
print(maxfit)
``````

0条评论