2017-10-18 47 views
0

我有以下代码如何使用函数中生成的变量(列表)作为下一个函数的输入?

# 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

您将其存储在本地rp变量中。这与RandomStrategySelection中的当地rp变量不同。

来处理这个标准的方法是,以保存返回值(调用程序),并把它传递给下一个如:

pool = RandomStrategySelection() 
how_fit = FitnessEvaluation(pool) 

...并给第二个函数签名声明参数:

FitnessEvaluation(rp): 
    for game in range (0,len(rp)): 
     .... 
+0

嘿,那么在这种情况下,第一个函数中的how_fit也被视为“已分配且从未使用过”。之后应该在哪里添加? – vferraz

+0

这取决于调用程序。你对最终结果做什么返回形式** FitnessEvaluation **?由于您没有提供[最小,完整,可验证的示例](http://stackoverflow.com/help/mcve),因此我没有语义来告诉您如何处理该问题。 – Prune

相关问题