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

时间:2017-12-26 21:46:42

标签: arrays python-3.x numpy multidimensional-array game-theory

所以我有以下代码:

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)

我在这里使用numpy.dot函数来获得两个相同大小的矩阵(由nash函数创建)的乘积。我相信这个函数返回数据的ndarray中的数据。我接下来要做的就是总结它们。

作为一个例子,函数的(部分)输出是:

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

如您所见,结果和总和正在正确计算,但 MAXIMUM 值(MX)不是。这将是这部分代码的主要结果。

有谁知道我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

您的fitness不是数组,它是您在循环中指定的标量。首先它被分配5;然后是5,然后是3.在循环结束时fitness是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)

重点是maxfit在循环中更新。