Skip to content

Latest commit

 

History

History
115 lines (87 loc) · 3.06 KB

tutorial.nlp_interfaces.rst

File metadata and controls

115 lines (87 loc) · 3.06 KB

NLP Interfaces

Below are examples of using PyNumero's interfaces to ASL for function and derivative evaluation. More information can be found in the API documentation (pynumero_api).

Relevant imports

>>> import pyomo.environ as pe >>> from pyomo.contrib.pynumero.interfaces.pyomo_nlp import PyomoNLP >>> import numpy as np

Create a Pyomo model

>>> m = pe.ConcreteModel() >>> m.x = pe.Var(bounds=(-5, None)) >>> m.y = pe.Var(initialize=2.5) >>> m.obj = pe.Objective(expr=m.x*2 + m.y2) >>> m.c1 = pe.Constraint(expr=m.y == (m.x - 1)*2) >>> m.c2 = pe.Constraint(expr=m.y >= pe.exp(m.x))

Create a :pypyomo.contrib.pynumero.interfaces.pyomo_nlp.PyomoNLP instance

>>> nlp = PyomoNLP(m)

Get values of primals and duals

>>> nlp.get_primals() array([0. , 2.5]) >>> nlp.get_duals() array([0., 0.])

Get variable and constraint bounds

>>> nlp.primals_lb() array([ -5., -inf]) >>> nlp.primals_ub() array([inf, inf]) >>> nlp.constraints_lb() array([ 0., -inf]) >>> nlp.constraints_ub() array([0., 0.])

Objective and constraint evaluations

>>> nlp.evaluate_objective() 6.25 >>> nlp.evaluate_constraints() array([ 1.5, -1.5])

Derivative evaluations

>>> nlp.evaluate_grad_objective() array([0., 5.]) >>> nlp.evaluate_jacobian() # doctest: +SKIP <2x2 sparse matrix of type '<class 'numpy.float64'>' with 4 stored elements in COOrdinate format> >>> nlp.evaluate_jacobian().toarray() array([[ 2., 1.], [ 1., -1.]]) >>> nlp.evaluate_hessian_lag().toarray() array([[2., 0.], [0., 2.]])

Set values of primals and duals

>>> nlp.set_primals(np.array([0, 1])) >>> nlp.evaluate_constraints() array([0., 0.]) >>> nlp.set_duals(np.array([-2/3, 4/3])) >>> nlp.evaluate_grad_objective() + nlp.evaluate_jacobian().transpose() * nlp.get_duals() array([0., 0.])

Equality and inequality constraints separately

>>> nlp.evaluate_eq_constraints() array([0.]) >>> nlp.evaluate_jacobian_eq().toarray() array([[2., 1.]]) >>> nlp.evaluate_ineq_constraints() array([0.]) >>> nlp.evaluate_jacobian_ineq().toarray() array([[ 1., -1.]]) >>> nlp.get_duals_eq() array([-0.66666667]) >>> nlp.get_duals_ineq() array([1.33333333])