首页 > 解决方案 > 使用 Scipy Optimize 进行多处理冻结

问题描述

编辑:错误不在于多处理,而在于另一个库。我会结束这个问题,但@Lukasz Tracewski 使用 joblib 的观点可能对其他人有所帮助。

我尝试在 Windows 上并行化的任务有问题。

它似乎工作了一段时间,然后突然停止在一个特定的实例上(当我得到代码来打印它在哪个迭代上时,我可以知道)。我在任务管理器中注意到,对于 Python 进程来说,CPU 使用率(大约 50%)是最小的。然后,当我在 cmd 提示符下进行键盘中断时,这会突然向前发射一些实例,并且活动会恢复一小会儿,但又会再次下降。

我已经包含了我认为相关的代码部分。我知道它可以工作(我认为它不会被困在问题上),而且它何时冻结似乎存在一定程度的随机性。

我正在使用带有max_iter集合的 COBYLA 求解器。我不确定它是否相关,但是当我尝试使用 BFGS 时,我遇到了冻结问题。


def get_optimal_angles(G,p,coeff,objective_function,initial_gamma_range,initial_beta_range,seed):
    '''
    This performs the classical-quantum interchange, improving the values of beta and gamma by reducing the value of
    < beta, gamma | C | beta, gamma >. Returns the best angles found and the objective value this refers to. Bounds on the minimiser are set as the starting points
    '''
    initial_starting_points = random_intial_values((np.array(initial_gamma_range)),(np.array(initial_beta_range)),p,seed)

    optimiser_function =  minimize(objective_function, initial_starting_points, method='COBYLA', options={'maxiter':1500})
    best_angles = optimiser_function.x
    objective_value = optimiser_function.fun
    return best_angles,objective_value



def find_best_angles_for_graph_6(graph_seed):
    print("6:On graph",graph_seed)
    #graph = gp.unweighted_erdos_graph(10,0.4,graph_seed)
    graph = gp.unweighted_erdos_graph(6,0.4,graph_seed)
    graph_coefficients = quantum_operator_z_coefficients(graph,'Yo')
    exact_energy =get_exact_energy(graph)
    angle_starting_seed = np.arange(1,number_of_angle_points,1)

    objective_function= get_black_box_objective_sv(graph,p,graph_coefficients)
    list_of_results = []
    for angle_seed in angle_starting_seed:
        print("On Angle Seed",angle_seed)
        best_angles_for_seed, best_objective_value_for_seed = get_optimal_angles(graph,p,graph_coefficients,objective_function,[0,np.pi],[0,np.pi],angle_seed)
        success_prob = calculate_energy_success_prob(graph,p,exact_energy, graph_coefficients,best_angles_for_seed,angle_seed)
        angle_seed_data_list = [angle_seed,best_objective_value_for_seed,success_prob,best_angles_for_seed]
        list_of_results.append(angle_seed_data_list)

    list_of_best =  get_best_results(list_of_results)

    full_results = [graph_seed,exact_energy,list_of_best,list_of_results]

    return full_results



import multiprocessing as mp

def main():
    physical_cores = 5
    pool = mp.Pool(physical_cores)




    list_of_results_every_graph_6 = []
    list_of_all_graph_results_6 = pool.map(find_best_angles_for_graph_6,range(1,number_of_graphs+1))
    print(list_of_all_graph_results_6)
    file_name_6 = 'unweighted_erdos_graph_N_6_p_8.pkl'
    pickle_6 = open((file_name_6),'wb')
    pickle.dump(list_of_all_graph_results_6, pickle_6)
    pickle_6.close()

    list_of_results_every_graph_10 = []
    list_of_all_graph_results_10 = pool.map(find_best_angles_for_graph_10,range(1,number_of_graphs+1))
    print(list_of_all_graph_results_10)
    file_name_10 = 'unweighted_erdos_graph_N_9_p_8.pkl'
    pickle_10 = open((file_name_10),'wb')
    pickle.dump(list_of_all_graph_results_10, pickle_10)
    pickle_10.close()


    







if __name__ == "__main__":
    main()

编辑:这是作为 Jupyter 笔记本的完整代码。https://www.dropbox.com/sh/6xb7setjsn1c1o3/AAANfH7mEmhuuf9cxh5QWsRQa?dl=0

标签: pythonscipymultiprocessingscipy-optimize

解决方案


推荐阅读