symopt
makes it easy to define and solve (non)linear constrained optimization problems in Python.
It uses the power of SymPy to automate the error-prone and
time-consuming task of writing functions to evaluate an optimization problem's objective function
and nonlinear constraints (to say nothing of their first and second derivatives!).
symopt
then provides a standardized interface to solve the problem through nonlinear
optimization backends including SciPy and
Ipopt.
The easiest way to install symopt
and its dependencies is through conda
conda install -c conda-forge symopt
Optimization problems can be defined using the OptimizationProblem
class, which has a similar constructor to scipy.optimize.minimize
.
For example, consider
where the p's are parameters defining the upper bounds for each variable. This can be defined by:
>>> from symopt import OptimizationProblem
>>> from sympy import MatrixSymbol, symarray
>>> x1, x2 = symarray('x', 2)
>>> p = MatrixSymbol('p', 2, 1)
>>> prob = OptimizationProblem(mode='min')
>>> prob.add_parameter(p)
>>> prob.add_variable(x1, lb=2, ub=p[0])
>>> prob.add_variable(x2, lb=0, ub=p[1])
>>> prob.obj = x1**2/100 + x2**2
>>> prob.add_constraints_from([x1 * x2 >= 25,
x1**2 + x2**2 >= 25])
That's it. From here, symopt
will automatically:
- derive all necessary derivatives (gradients and Hessians for the objective function/constraints)
- create functions to numerically evaluate these quantities
(using SymPy's
lambdify
orautowrap
)
One can then solve the problem for specified parameters using solve
:
>>> x = [2, 2]
>>> p = [20.0, 50.0]
>>> res = prob.solve(x, p, solver='ipopt')
>>> print(res['success'])
True
>>> print(res['x'])
array([15.8113883 , 1.58113883])
>>> print(res['fun'])
5.000000000505797
Auto-generated API documentation for latest stable release can be found at https://symopt.readthedocs.io/en/stable/. For the latest development version, see https://symopt.readthedocs.io/en/latest/.
- numpy
- scipy
- sympy
- orderedset
- cyipopt (for optimization using the Ipopt backend)
- A fortran compiler (for code generation using
autowrap
)
symopt
is released under the MIT license. See LICENSE for details.
Sean P. Cornelius (gmail address: spcornelius).