-
Notifications
You must be signed in to change notification settings - Fork 181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
An error occurres when args is specified and ScaleCoordinates is used #205
Comments
Thanks for the report. Using the builtin import functools
import cma
def objective_function(x, a: float = 1., b: float = 0., c: float = 0.):
x[0] /= 2.
return a * x[0] ** 2 + b * x[0] + c + x[1] ** 2
f_partial = functools.partial(objective_function, a=1., b=-4., c=4.) # attach parameters to function
objective_function_scaled = cma.fitness_transformations.ScaleCoordinates(
f_partial, multipliers=[2., 1.])
es = cma.CMAEvolutionStrategy([0., 0.], 1.).optimize(objective_function_scaled) Would that solve your issue? |
Thanks, this solves my problem. |
@ppalcx According to fun2 = cma.ScaleCoordinates(fun, multipliers, zero)
fun2(x) == fun(multipliers * (x - zero)) when |
Then how to do this????? I am having a function f(x1,x2,x3,x4,x5,x6) that has to be minimised for the variable x1,x2,x3,x4,x5,x6. The range for the variables or the search domain is I am getting the function value from a simulator. help(cma.fitness_transformations.ScaleCoordinates) is not clear to me how can I use it for my problem. sigma0 if I am not usingcaleCoordinates I can not use sigma 0 bigger than 10 (x4(0-10) ) which is making the algo not searching in wider domain. Can you please share or show any simpler example. |
I mean what should I write in this part according to my problem objective_function_scaled = cma.fitness_transformations.ScaleCoordinates(objective_function, multipliers=[2., 1.]) |
Assume you want objective_function_scaled = cma.fitness_transformations.ScaleCoordinates(
objective_function, multipliers=[200, 2, 200, 2, 1000, 20],
zero=[-1000/200, -5/2, -1000/200, -5/2, -10000/1000, -100/20],
) When es = cma.CMAEvolutionStrategy([0, 0, 0, 0, 0, 0], 1.)
es.optimize(objective_function_scaled) Or just scale the object function: x0 = [1000, 5, 1000, 5, 10000, 100]
stds = [200, 2, 200, 2, 1000, 20]
objective_function_scaled = cma.fitness_transformations.ScaleCoordinates(objective_function, multipliers=stds)
es = cma.CMAEvolutionStrategy(x0=x0, sigma0=1.0)
es.optimize(objective_function_scaled) |
Also, this could be a solution which I think is more natural: x0 = [1000, 5, 1000, 5, 10000, 100]
stds = [200, 2, 200, 2, 1000, 20]
es = cma.CMAEvolutionStrategy(x0=x0, sigma0=1.0, inopts={'CMA_stds': stds})
es.optimize(objective_function) Even though the cma.CMAOptions()
...
CMA_stds='None # multipliers for sigma0 in each coordinate, not represented in C, better use `cma.ScaleCoordinates` instead'
... |
@haiiliin Thanks for your explanation. Will this take care of my bounds on the variable? Because none of my decision variables should go out of bound and negative. |
You can pass a import cma
cma.CMAOptions()
...
bounds='[None, None] # lower (=bounds[0]) and upper domain boundaries, each a scalar or a list/vector'
...
x0 = [1000, 5, 1000, 5, 10000, 100]
stds = [200, 2, 200, 2, 1000, 20]
lb = [300, 0, 300, 0, 500, 0]
ub = [1500, 10, 1500, 10, 20000, 20]
es = cma.CMAEvolutionStrategy(x0=x0, sigma0=1.0, inopts={'CMA_stds': stds, 'bounds': [lb, ub]})
es.optimize(objective_function) If you use the |
How to do scale import cma
cma.CMAOptions()
...
bounds='[None, None] # lower (=bounds[0]) and upper domain boundaries, each a scalar or a list/vector'
...
x0 = [1000, 5, 1000, 5, 10000, 100]
stds = [200, 2, 200, 2, 1000, 20]
lb = [300, 0, 300, 0, 500, 0]
ub = [1500, 10, 1500, 10, 20000, 20]
es = cma.CMAEvolutionStrategy(x0=x0, sigma0=1.0, inopts={'CMA_stds': stds, 'bounds': [lb, ub] })
es.optimize(objective_function) |
We don't use |
When the args argument of the optimizer is specified and the ScaleCoordinates is used, an error occurres.
For example, I want to optimize the following simple function,
it is a 2D function,
f = (a * x1 ^ 2 + b * x1 + c) + x2 * 2
, the function is scaled but inside the objective function, the variables are scaled again which makes it identical to the original function, the minimal point is(0, 0)
whena = 1, b = c = 0
, when the args is not specified, the result is correct:But when I specified the
args
argument, i.e., to optimize the function when(a = 1, b = -4, c = 4)
, the actual minimal point for this condition is(2, 0)
, but an error occurres:The above example throw the following error message:
It seems that the scaled objective function can not deal with the additional arguments. The
scale_and_offset
method defined in theScaleCoordinates
class takes only one argument (except for the self argument), but the additional arguments are passed to this method:The text was updated successfully, but these errors were encountered: