0

我正在尝试使用scipy.optimize.minimze来最小化目标函数,如下所示。

import numpy as np
from scipy import optimize
max_q_fun = lambda q : -q     
def max_q_min(args):
        cons = args 
        res = optimize.minimize(max_q_fun, (0.1), method='SLSQP', bounds = ((0,1),), constraints=cons)
        q = res.x
        return q 
total_counts = np.arange(0,10)
num_actions = 10         
args = [({'type': 'ineq', 'fun': lambda q: total_counts[act] * 
                 (p[act] * np.log(p[act] / q) + (1-p[act]) * np.log((1-p[act]) / (1-q))) - np.log(10)}) 
                for act in range(num_actions)]
pl = Pool(num_actions)
actions = pl.map(max_q_min,args)
current_action = np.argmax(actions)

但是,如果我使用Pool错误from pathos.multiprocessing import ProcessingPool as Pool表明

NameError: name 'np' is not defined

如果我使用Poolby from multiprocessing import Pool,则错误显示 AttributeError: Can't pickle local object 'kl_ucb.act.<locals>.max_q_min'.

知道如何将并行计算用于不同的约束吗?

4

0 回答 0