-
Notifications
You must be signed in to change notification settings - Fork 141
Open
Description
I’m trying to optimise a black-box function which needs to obey a constraint. However, I noticed that the objective function is evaluated with values that don’t obey the constraint.
I created a toy problem to verify the behaviour and indeed it seems to be sampling the function while violating constraints.
!pip install --upgrade pyswarm
import numpy as np
iteration = 0
def objective(val):
global iteration
x, y = val
if (x+y)>3 :
print("Sum of X and Y is: ", x+y)
print("Iteration: ", iteration)
iteration=iteration+1
return -20.0 * np.exp(-0.2 * np.sqrt(0.5 * (x**2 + y**2))) - np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y))) + np.e + 20
def condition(val):
x, y = val
sum = x + y
return [3 - sum]
ub = [5, 5]
lb = [-5, -5]
xopt, fopt = pso(objective, lb, ub, f_ieqcons=condition, debug=True, maxiter=20)
xopt
Ideally "Sum of X and Y is: " shouldn't be printed, but it is. I cannot evaluate the black-box function outside the constraints since it involves changing a parameters of a live system (And system simply wouldn’t allow those combinations).
Also, I get around 620 "iterations" although I've set maxiter=20.
Was wondering what I'm doing wrong here..
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels