ZOOpt icon indicating copy to clipboard operation
ZOOpt copied to clipboard

关于跑示例代码报错

Open SunRockLife opened this issue 1 month ago • 1 comments

您好,请教一个问题,我对您的zooPT这个无梯度优化非常感兴趣,最近尝试在玩一玩并且想解决一些实际问题,但是我跑了一下示例代码,报错了,报错信息如下: Traceback (most recent call last): File "D:\pythonProject\main.py", line 28, in Traceback (most recent call last): File "", line 1, in solution = Opt.min(obj, Parameter(budget=100 * dim_size, parallel=True, server_num=3)) File "D:\pythonProject\venv\lib\site-packages\zoopt\opt.py", line 50, in min result = optimizer.opt(objective, parameter) ForkingPickler(file, protocol).dump(obj) AttributeError: Can't pickle local object 'Parameter.init..'

代码是这个: import numpy as np from zoopt import Dimension, ValueType, Dimension2, Objective, Parameter, Opt, ExpOpt def ackley(solution): x = solution.get_x() bias = 0.2 value = -20 * np.exp(-0.2 * np.sqrt(sum([(i - bias) * (i - bias) for i in x]) / len(x))) -
np.exp(sum([np.cos(2.0np.pi(i-bias)) for i in x]) / len(x)) + 20.0 + np.e return value

Press the green button in the gutter to run the script.

if name == 'main': dim_size = 100 # dimension size dim = Dimension(dim_size, [[-1, 1]] * dim_size, [True] * dim_size) # dim = Dimension2([(ValueType.CONTINUOUS, [-1, 1], 1e-6)]*dim_size) obj = Objective(ackley, dim) # perform optimization solution = Opt.min(obj, Parameter(budget=100 * dim_size)) # print the solution print(solution.get_x(), solution.get_value()) # parallel optimization for time-consuming tasks solution = Opt.min(obj, Parameter(budget=100 * dim_size, parallel=True, server_num=3)) import matplotlib.pyplot as plt

plt.plot(obj.get_history_bestsofar())
plt.savefig('figure.png')
solution_list = ExpOpt.min(obj, Parameter(budget=100 * dim_size), repeat=3,
                           plot=True, plot_file="progress.png")
for solution in solution_list:
    print(solution.get_x(), solution.get_value())

请问您知道这是怎么回事嘛 谢谢

SunRockLife avatar May 21 '24 09:05 SunRockLife