eyounx / zoopt Goto Github PK
View Code? Open in Web Editor NEWA python package of Zeroth-Order Optimization (ZOOpt)
Home Page: https://github.com/polixir/ZOOpt
License: MIT License
A python package of Zeroth-Order Optimization (ZOOpt)
Home Page: https://github.com/polixir/ZOOpt
License: MIT License
Hello, I encountered the following problem while implementing Zoopt. Please help!
When I try to use SRE, the solved solution from Zoopt is outside of the constrained region of my problem (the unit box (0,1]**dim_size). Some elements in the solution is larger than 1 and some others are negative. In the paper cited in the documentation (https://doi.org/10.1613/jair.4806), it is said that if it is outside the feasible region it should be automatically projected back to this feasible region. Can you please verify?
This is my code where my objective function is to find the smallest eigenvalue of a matrix whose entries are computed via the variables:
_dim_size = 400
dim = Dimension(dim_size, [[1e-10, 1]] * dim_size, [True] * dim_size )
objective = Objective(smallest_eigen, dim)
low_dim = 100
uniform = list([1/2]*(low_dim+1))
initial = [uniform]
budget =5000
par = Parameter(budget=budget, init_samples = initial, precision = 1e-8, intermediate_result=True, high_dim_handling=True, reducedim=True, num_sre=5, low_dimension=Dimension(low_dim, [[1e-10, 1]] * low_dim, [True] * low_dim))
sol = Opt.min(objective, par)
optimal = sol.get_x()
optval= - sol.get_value()_
您好,请教一个问题,我对您的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
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())
请问您知道这是怎么回事嘛 谢谢
Hello,
Just wondering if it's possible to set an initial X0 as the starting point.
Thanks
ImportError: cannot import name 'Dimension '
Hello,
As indicated in the little, is it possible to include such stopping criterion? For example, if the intermediate objective values do not update (within precision 1e-5) more than 1000 iterations, stop and print the current best solution.
Thank you in advance.
Hi, I implemented the following hypothetical minimization objective. The first part of the objective function is just a l1-norm while the second part ensures that the sum of first 3 entries of x equals to 1.
def fn(solution):
x = solution.get_x()
x1 = [0,3,1,0,0]
val = sum([abs(v-x1[i]) for i,v in enumerate(x)])+ ((np.sum([i for i in x[0:3]])-1.0)**2)
return val
dim = 5
dimobj = Dimension(dim, regs=[[0,5]]*dim, tys=[False]*dim)
obj = Objective(fn, dimobj)
param = Parameter(budget=50)
solution = Opt.min(obj, param)
print(solution.get_x(), solution.get_value())
The above simple program works fine when the budget is less than 50. When I make it more than 50, it throws the following error:
ValueError: empty range for randrange() (0,0, 0)
Further, this problem occurs only when the search space is discrete.
Any help much appreciated. Thanks!
If I already have a set {X,y} of evaluated samples, can I warm start the optimization without having the optimizer re-evaluate all of these points?
I tried discrete optimization with Ackley function from example, with dim equals to 2
def ackley(solution):
x = solution.get_x()
print(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.0*np.pi*(i-bias)) for i in x]) / len(x)) + 20.0 + np.e
return value
with budget=20*dim works fine
dim = 2 # dimension
obj = Objective(ackley, Dimension(dim, [[-10, 10]] * dim, [False] * dim, [True] * dim))
# perform optimization
solution = ExpOpt.min(obj, Parameter(budget=20 * dim))
# print result
solution
this prints
[10, -10]
[-10, -10]
[10, 10]
[-10, 10]
[3, 10]
[-8, 10]
[-7, 10]
[3, -5]
[4, -5]
[3, -2]
[3, 4]
[-4, -2]
[-2, -2]
[-3, -2]
[-1, -2]
[-1, 8]
[-1, 4]
[1, -2]
[7, -2]
[1, 10]
[1, -4]
[1, 1]
[1, -5]
[3, 1]
[1, 0]
[7, 0]
[2, 0]
[4, 0]
[7, 0]
[4, 0]
[-4, 0]
[1, 8]
[4, 0]
[-9, 0]
[-2, 0]
[1, 2]
[1, 8]
[4, 0]
[-6, 0]
[5, 0]
[zoopt] solved solution is:
[zoopt] x: [1, 0]
[zoopt] value: 3.5577131909328554
[zoopt] Best 1 result: 3.5577131909328554 +- 0.0
but with budget=30*dim, I get this and nothing more
[-10, 10]
[10, 10]
[10, -10]
[-10, -10]
Hi all,
1 - sometimes optimization just get stuck at some point and does not end (with dimension size < 50 - it's always the case). Don't know, maybe it is doing something, but after 10 minutes I just gave up (
2 - Moreover, 'poss' optimization fails with an error "AttributeError: 'NoneType' object has no attribute 'print_solution'" at different places.
3 - is there a way to pass additional arguments to objective function?
Best regards,
Oleg
Hello,
Can you please specify how to initialize the sample? I follow the Documents but cannot succeed.
Here is a part of my Python code, where do I get it wrong? Thank you very much.
objective = Objective(smallest_eigen, dim)
uniform = list([1]*E)
initial = [Solution(x=uniform)] # Initial as a Uniform Distribution
par = Parameter(budget=5000, intermediate_result=True,init_samples = initial)
sol = Opt.min(objective, par)
print(sol.get_x(), sol.get_value())
The error is
File "C:\Users\qvu\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\qvu\Anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/qvu/Google Drive/2018 Works/8. AAAI submission/Implementation/Black_box_Explore.py", line 100, in
sol = Opt.min(objective, par)
File "C:\Users\qvu\Anaconda\lib\site-packages\zoopt\opt.py", line 50, in min
result = optimizer.opt(objective, parameter)
File "C:\Users\qvu\Anaconda\lib\site-packages\zoopt\algos\opt_algorithms\racos\racos_optimization.py", line 54, in opt
objective, parameter, strategy, ub)
File "C:\Users\qvu\Anaconda\lib\site-packages\zoopt\algos\opt_algorithms\racos\sracos.py", line 44, in opt
self.init_attribute()
File "C:\Users\qvu\Anaconda\lib\site-packages\zoopt\algos\opt_algorithms\racos\racos_common.py", line 66, in init_attribute
self._objective.eval(x)
File "C:\Users\qvu\Anaconda\lib\site-packages\zoopt\objective.py", line 83, in eval
val = self.__func(solution)
File "C:/Users/qvu/Google Drive/2018 Works/8. AAAI submission/Implementation/Black_box_Explore.py", line 81, in smallest_eigen
H =update_H(w)
File "C:/Users/qvu/Google Drive/2018 Works/8. AAAI submission/Implementation/Black_box_Explore.py", line 61, in update_H
H[i,j] = H[i,j]+ w[edges.index((i,k))]*H[k,j]
TypeError: 'Solution' object does not support indexing
According to sparse_regression the constraint function should return a positive value if the constraint is met and a negative value if the constraint is violated (at least that is how I understand it).
When this is employed the following error is recorded
zoopt\lib\site-packages\zoopt\algos\paretoopt\paretoopt.pyc in opt(self, objective, parameter)
78 continue
79 else:
---> 80 if (fitness[i][0] < offSpringFit[0] and fitness[i][1] >= offSpringFit[1]) or
81 (fitness[i][0] <= offSpringFit[0] and fitness[i][1] > offSpringFit[1]):
82 hasBetter = True
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
How I found the bug:
I ran an example to test, I changed the func code as
def myfunc(x):
b=np.array(x)
y = -20 * np.exp(-0.2 * np.sqrt(((b - 0.2)*(b - 0.2)).mean()))
- np.exp((np.cos(2 * math.pi * (b - 0.2))).mean()) + math.e + 20
return y
and rerun the example in README. It got a different but better result than the original one, and then I found why original code is wrong:
T.dot return a scalar but not a list, so its mean is itself.
Good luck.
When running the sparse_regression example I get the following error:
AttributeError: 'module' object has no attribute 'ArffDecoder'
I got a RuntimeWarning when run the optimizer
lib\site-packages\zoopt-0.3.0-py3.7.egg\zoopt\solution.py:84: RuntimeWarning: invalid value encountered in greater
zoopt\solution.py:84
if sol_value != nan and self.__value != nan:
the sol_value
is nan
and the self.__value
is nan
too
but the expression return True
in python 3.7
while math.isnan(sol_value)
and math.isnan(self.__value)
return True in python 3.7
Hi guys,
I am a learner of PyTorch. So I am wondering whether ZOOpt would support PyTorch.
Any answer and guide will be appreciated!
I tried to install using pip
. Seems like the pareoopt
file is not included in the installation as I keep getting this error:
from paretoopt import ParetoOpt
ModuleNotFoundError: No module named 'paretoopt'
I used the following command: pip install zoopt
on Windows 10.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.