I am using the scipy.optimize module to find optimal input weights that would minimize my output. From the examples I've seen, we define the constraint with a one-sided equation; then we create a variable that's of the type 'inequality'. My question is how does the optimization package know whether the sum of the variables in my constraint need to be smaller than 1 or larger than 1?
...
def constraint1(x):
return x[0]+x[1]+x[2]+x[3]-1
....
con1 = {'type': 'ineq', 'fun': constraint1}
link to full solution I'm using in my example: http://apmonitor.com/che263/index.php/Main/PythonOptimization
An inequality constraint g(x, y) ≤ b is called binding (or active) at a point. (x, y) if g(x, y) = b and not binding (or inactive) if g(x, y) < b.
SciPy optimize provides functions for minimizing (or maximizing) objective functions, possibly subject to constraints. It includes solvers for nonlinear problems (with support for both local and global optimization algorithms), linear programing, constrained and nonlinear least-squares, root finding, and curve fitting.
Objective functions in scipy. optimize expect a numpy array as their first parameter which is to be optimized and must return a float value. The exact calling signature must be f(x, *args) where x represents a numpy array and args a tuple of additional arguments supplied to the objective function.
Refer to https://docs.scipy.org/doc/scipy-0.18.1/reference/tutorial/optimize.html and scroll down to Constrained minimization of multivariate scalar functions (minimize), you can find that
This algorithm allows to deal with constrained minimization problems of the form:
[![enter image description here][1]][1]
where the inequalities are of the form C_j(x) >= 0
.
So when you define the constraint as
def constraint1(x):
return x[0]+x[1]+x[2]+x[3]-1
and specify the type of the constraint as
con1 = {'type': 'ineq', 'fun': constraint1}
it automatically assumes that the constraint is in the standard form x[0]+x[1]+x[2]+x[3]-1>=0
i.e., x[0]+x[1]+x[2]+x[3]>=1
[1]: https://i.stack.imgur.com/E3LPs.png
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With