I’m having trouble with global optimisation for a function that has specific constraints on the values. Let me explain the situation.
We have a function F(x1, x2,…, xn, a, b), and our goal is to find its global minimum. We also have constraints from physics: x[i] <= x[i+1] for all values of x. All values in x[i] and a,b have boundaries (0,1).
How can I set up the correct nonlinear constraint for x in SciPy?
I’ve already tried this option:
def con(x):
out = np.ones(N)
for i in range(N-1):
if x[i] > x[i + 1]:
out[i:] = -1
return out
else:
out[i] = 1
if x[N - 1] < x[N - 2]:
out[N - 1] = -1
else:
out[N - 1] = 1
return out
const = NonlinearConstraint(con, 0, 1)
bounds = [(0, 1) for i in range(N+2)]
solution = differential_evolution(f, bounds, workers=-1, constraints=const)
But it doesn’t work. Some values in x are outside the bounds (< 0, how???) and it ignores the constraint x[i]<=x[i+1]. I think I completely misunderstand how the NonlinearConstraint works. I would appreciate any help!
Vladimir Sinitsyn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.