Nelder-Mead optimization in python with constraints

Viewed 2112

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?

1 Answers

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.

Related