vferraz
vferraz

Reputation: 469

Return maximum value from a multi-dimensional array (ndarray)

So I have the following code:

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)

I am using the numpy.dot function here to obtain the product of two same-sized matrixes (created by the nash functions). I believe this function returns the data in an ndarray of data. What I do next is summing them up.

The (partial) output of the function, as an example, is:

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

As you can see, the results and the sum are being computed correctly, however the MAXIMUM value (MX) is not. This would be the main result from this part of the code.

Does anyone know what I'm doing wrong here?

Upvotes: 0

Views: 118

Answers (1)

user6655984
user6655984

Reputation:

Your fitness is not an array, it's a scalar to which you assign in a loop. First it's assigned 5; then 5 again, then 3. At the end of the loop fitness is 3. And the maximum of a single number 3 is of course 3. The following would be a correct way to find the maximal fitness:

  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)

The main point is that maxfit is updated within the loop.

Upvotes: 2

Related Questions