3

Currently, I am looking for a Nelder-Mead optimizer in python that also accepts bounds and constraints for the variables. Scipy has a Nelder-Mead optimizer, but it does not allow any constraints.

During my search I came across the package constrNMPy, which should make this possible.

Here is an example of how to use constrNMPy:

# Define initial guess
x0=[2.5,2.5]

# Define lower and upper bounds (None indicates no bound)
LB=[2,2]
UB=[None,3]

# Call optimizer
import constrNMPy as cNM 
res=cNM.constrNM(cNM.test_funcs.rosenbrock,x0,LB,UB,full_output=True)

# Print results
cNM.printDict(res)

However, this example only explains how to define bounds, but cannot define constraints. In the example above I would like to have the following constraint, so that the variables only accept values where the sum is 5:

cons = {'type':'eq', 'fun':lambda x0: 5 - sum(x0)}

How do I pass this constraint to the constrNM call?

Or are there other packages for a Nelder-Mead optimizer with constraints?

Emma
  • 439
  • 5
  • 17
  • 1
    I am not aware of any Nelder-Mead algorithm that can handle general constraints. You may want to look at other algorithms such as SLSQP or COBYLA. I assume you deal with small problems. – Erwin Kalvelagen Jun 01 '20 at 16:48
  • You may want lo look [trust-constr](https://docs.scipy.org/doc/scipy/reference/optimize.minimize-trustconstr.html) from Scipy too. – Alien Jul 07 '21 at 13:42

1 Answers1

0

You can always add a a constraint by defining a "penalty" function. the missio of this function is to have a value above zero when this constraint is broken. and adding penalty function's value to the objective function (needed to become minimum) will garantee that the optimum solution would be reached with all constraints (if many are used) are satisfied.