CMA-ES / pycma

Python implementation of CMA-ES
Other
1.1k stars 178 forks source link

What should I do to limit the search space to [-1,1]? #94

Closed KeXue-NJU closed 5 years ago

KeXue-NJU commented 5 years ago

Here are my core-codes:

self.strategy = cma.CMAEvolutionStrategy(x_start, 0.5)

es = self.strategy

for count in range(budget):
    solutions = es.ask()
    es.tell(solutions, [cma.ff.rosen(s) for s in solutions])
    es.disp()
nikohansen commented 5 years ago
self.strategy = cma.CMAEvolutionStrategy(x_start, 0.5, {'bounds': [[-1], [1]]})

should do.

Check out the output of

import cma

cma.CMAOptions('boun')
{'BoundaryHandler': 'BoundTransform  # or BoundPenalty, unused when ``bounds in (None, [None, None])``',
 'bounds': '[None, None]  # lower (=bounds[0]) and upper domain boundaries, each a scalar or a list/vector'}

Furthermore,

help(cma.CMAEvolutionStrategy)

gives an example, see also here.

KeXue-NJU commented 5 years ago

Thanks a lot.

unghee commented 2 years ago

hello, can we set a different bounds for each dimension? for example, for 2dim case:

def f(x, y):  # a 1-D function
    return x**2 + y**2

x0 = [0.5, 0.8]
strategy = cma.CMAEvolutionStrategy(x0, 0.5, {'bounds': [[-1, 1], [0, 5]]})

if I do this, for me it throws an error as following :

ValueError: argument of inverse must be within the given bounds

nikohansen commented 2 years ago

The lower bound of the second variable, bounds[0][1], was set to 1 and the initial solution was 0.8, below the lower bound, hence the error.

unghee commented 2 years ago

It worked. Thank you for your help!