我有以下代码
# Random Strategy Selection: ramdomly choose a strategy within each player's strategy profile for each game in the input list
def RandomStrategySelection(): # Return the vectors with the selected
P = pool_of_games
j=10 #number of iterations
s=1 #current round
random_strategy=[] #combination of randomly chosen strategies for each player
random_pool=[] #pool of selected random strategies for the input vector games
rp=random_pool
while s<=j:
for game in range (0, len(P)):
p1=random.choice(P[game][0][0:3]) #random choice within p1(row)'s strategy profile
p2=random.choice(P[game][0][4:8]) #random choice within p2(column)'s strategy profile
random_strategy=[p1,p2]
random_pool.append(random_strategy)
s=s+1
return(rp)
def FitnessEvaluation(): # Return the rank of fitness of all evaluated games
for game in range (0,len(rp)):
pf1=rp[game][0]
pf2=rp[game][1]
fitness=payoff1+payoff2
return(fitness)
#fitness: f(G)=(F(G)*j+s)/j - F(G)=pf1+pf2
RandomStrategySelection 会生成一个对象列表,例如
[[0,2][3,1]]
FitnessEvaluation 应该使用该列表,但我不能让它运行。 FitnessEvaluation 似乎无法识别创建的列表,即使我将其存储在 rp 变量中也是如此。有什么想法吗?谢谢!
答案 0 :(得分:0)
您将其存储在本地 rp 变量中。这与 RandomStrategySelection 中的本地 rp 变量不同。
处理此问题的标准方法是保存返回值(在调用程序中)并将其传递给下一个,例如:
pool = RandomStrategySelection()
how_fit = FitnessEvaluation(pool)
...并为第二个函数提供一个声明参数的签名:
FitnessEvaluation(rp):
for game in range (0,len(rp)):
....