CMA-ES / pycma

Python implementation of CMA-ES
Other
1.08k stars 177 forks source link

Could we use Constrains and Bounds together? #218

Closed TongGeTT closed 1 year ago

TongGeTT commented 1 year ago

Hello,this work is great excellent ! However, when I used constrains and bounds together, I got the results that did not obey the constrains, I really hope to know why. The length of x is 84, I want to use the constrain for the first 4 elements like "np.sqrt(sum(np.power(x[0:MK2], 2))) less than 0.0037", in the meanwhile, I hope the rest 80 elements bound between [0, 2Pi]. but I got x = [ 3.11683938e-02 1.06373066e-03 -8.11204831e-02 -1.14136925e-01 2.81821394e+00 2.75432779e+00 3.21580276e-07 2.26921752e+00 ...] I found the first 4 elements did not obey the constrain. Belows are my code. `import cma import numpy as np import math

fun = cma.ff.Rate_single_user_M2_N40_d20 # we could use functools.partial(cma.ff.elli, cond=1e4) to change the condition number to 1e4

def constraints(x): # for 1 user

sum_ = 0

# for i in range(M*K*2):
#     sum_ = sum_ + x[i] * x[i]
return [np.sqrt(sum(np.power(x[0:M*K*2], 2))) - np.sqrt(0.0037)]  # constrain the second variable to <= -1, the second constraint is superfluous

M = 2
K = 1
N = 40
Z = 2

x0 = (MK2+NZ) [0] # initial solution sigma0 = 1 # initial standard deviation to sample new solutions

cfun = cma.ConstrainedFitnessAL(fun, constraints) # unconstrained function with adaptive Lagrange multipliers none_list = [None for i in range(MK2)] l_list = [0] NZ u_list = [2math.pi] N*Z lower = none_list + l_list upper = none_list + u_list es = cma.CMAEvolutionStrategy(x0, sigma0, {'bounds':[lower, upper]})

while not es.stop(): X = es.ask() # sample len(X) candidate solutions es.tell(X, [cfun(x) for x in X]) cfun.update(es) es.logger.add() # for later plotting es.disp() x = es.result_pretty()`

Thanks for your attention !!

nikohansen commented 1 year ago

To "guaranty" a final solution that fully obeys the constraints you have to run a "post-optimization" with cfun.find_feasible as shown in this notebook in cell [2].

TongGeTT commented 1 year ago

Thank you for your advice!Your work is so excellent!