|
| 1 | +# ___________________________________________________________________________ |
| 2 | +# |
| 3 | +# Pyomo: Python Optimization Modeling Objects |
| 4 | +# Copyright (c) 2008-2025 |
| 5 | +# National Technology and Engineering Solutions of Sandia, LLC |
| 6 | +# Under the terms of Contract DE-NA0003525 with National Technology and |
| 7 | +# Engineering Solutions of Sandia, LLC, the U.S. Government retains certain |
| 8 | +# rights in this software. |
| 9 | +# This software is distributed under the 3-clause BSD License. |
| 10 | +# ___________________________________________________________________________ |
| 11 | + |
| 12 | + |
| 13 | +import pyomo.common.unittest as unittest |
| 14 | +from pyomo.common.dependencies import numpy as np, numpy_available |
| 15 | +from pyomo.common.dependencies import scipy_available, attempt_import |
| 16 | + |
| 17 | +import pyomo.environ as pyo |
| 18 | + |
| 19 | +# Use attempt_import here due to unguarded NumPy import in this file |
| 20 | +nlp = attempt_import('pyomo.contrib.pynumero.interfaces.pyomo_nlp')[0] |
| 21 | +import pyomo.contrib.sensitivity_toolbox.pynumero as pnsens |
| 22 | +from pyomo.contrib.pynumero.asl import AmplInterface |
| 23 | + |
| 24 | +if not scipy_available or not numpy_available: |
| 25 | + raise unittest.SkipTest("scipy or numpy is not available") |
| 26 | + |
| 27 | +if not AmplInterface.available(): |
| 28 | + raise unittest.SkipTest("Pynumero needs the ASL extension to run NLP tests") |
| 29 | + |
| 30 | + |
| 31 | +class TestSeriesData(unittest.TestCase): |
| 32 | + def test_dsdp_dfdp_pyomo(self): |
| 33 | + m = pyo.ConcreteModel() |
| 34 | + m.x1 = pyo.Var(initialize=200) |
| 35 | + m.x2 = pyo.Var(initialize=5) |
| 36 | + m.p1 = pyo.Var(initialize=10) |
| 37 | + m.p2 = pyo.Var(initialize=5) |
| 38 | + m.obj = pyo.Objective( |
| 39 | + expr=m.x1 * m.p1 + m.x2 * m.x2 * m.p2 + m.p1 * m.p2, sense=pyo.minimize |
| 40 | + ) |
| 41 | + m.c1 = pyo.Constraint(expr=m.x1 == 2 * m.p1**2) |
| 42 | + m.c2 = pyo.Constraint(expr=m.x2 == m.p2) |
| 43 | + theta = [m.p1, m.p2] |
| 44 | + |
| 45 | + dsdp, dfdp, rmap, cmap = pnsens.get_dsdp_dfdp(m, theta) |
| 46 | + |
| 47 | + # Since x1 = p1 |
| 48 | + np.testing.assert_almost_equal(dsdp[rmap[m.x1], cmap[m.p1]], 40.0) |
| 49 | + np.testing.assert_almost_equal(dsdp[rmap[m.x1], cmap[m.p2]], 0.0) |
| 50 | + np.testing.assert_almost_equal(dsdp[rmap[m.x2], cmap[m.p1]], 0.0) |
| 51 | + np.testing.assert_almost_equal(dsdp[rmap[m.x2], cmap[m.p2]], 1.0) |
| 52 | + |
| 53 | + # if x1 = 2 * p1 and x2 = p2 then |
| 54 | + # df/dp1 = 6 p1**2 + p2 = 45.0 |
| 55 | + # df/dp2 = 3 p2 + p1 = 85.0 |
| 56 | + np.testing.assert_almost_equal(dfdp[0, cmap[m.p1]], 605.0) |
| 57 | + np.testing.assert_almost_equal(dfdp[0, cmap[m.p2]], 85.0) |
| 58 | + |
| 59 | + def test_dsdp_dfdp_pyomo_nlp(self): |
| 60 | + m = pyo.ConcreteModel() |
| 61 | + m.x1 = pyo.Var(initialize=200) |
| 62 | + m.x2 = pyo.Var(initialize=5) |
| 63 | + m.p1 = pyo.Var(initialize=10) |
| 64 | + m.p2 = pyo.Var(initialize=5) |
| 65 | + m.obj = pyo.Objective( |
| 66 | + expr=m.x1 * m.p1 + m.x2 * m.x2 * m.p2 + m.p1 * m.p2, sense=pyo.minimize |
| 67 | + ) |
| 68 | + m.c1 = pyo.Constraint(expr=m.x1 == 2 * m.p1**2) |
| 69 | + m.c2 = pyo.Constraint(expr=m.x2 == m.p2) |
| 70 | + theta = [m.p1, m.p2] |
| 71 | + |
| 72 | + m2 = nlp.PyomoNLP(m) |
| 73 | + dsdp, dfdp, rmap, cmap = pnsens.get_dsdp_dfdp(m2, theta) |
| 74 | + |
| 75 | + # Since x1 = p1 |
| 76 | + assert dsdp.shape == (2, 2) |
| 77 | + np.testing.assert_almost_equal(dsdp[rmap[m.x1], cmap[m.p1]], 40.0) |
| 78 | + np.testing.assert_almost_equal(dsdp[rmap[m.x1], cmap[m.p2]], 0.0) |
| 79 | + np.testing.assert_almost_equal(dsdp[rmap[m.x2], cmap[m.p1]], 0.0) |
| 80 | + np.testing.assert_almost_equal(dsdp[rmap[m.x2], cmap[m.p2]], 1.0) |
| 81 | + |
| 82 | + # if x1 = 2 * p1 and x2 = p2 then |
| 83 | + # df/dp1 = 6 p1**2 + p2 = 45.0 |
| 84 | + # df/dp2 = 3 p2 + p1 = 85.0 |
| 85 | + assert dfdp.shape == (1, 2) |
| 86 | + np.testing.assert_almost_equal(dfdp[0, cmap[m.p1]], 605.0) |
| 87 | + np.testing.assert_almost_equal(dfdp[0, cmap[m.p2]], 85.0) |
| 88 | + |
| 89 | + def test_dydp_pyomo(self): |
| 90 | + m = pyo.ConcreteModel() |
| 91 | + m.x1 = pyo.Var(initialize=200) |
| 92 | + m.x2 = pyo.Var(initialize=5) |
| 93 | + m.p1 = pyo.Var(initialize=10) |
| 94 | + m.p2 = pyo.Var(initialize=5) |
| 95 | + m.obj = pyo.Objective( |
| 96 | + expr=m.x1 * m.p1 + m.x2 * m.x2 * m.p2 + m.p1 * m.p2, sense=pyo.minimize |
| 97 | + ) |
| 98 | + m.c1 = pyo.Constraint(expr=m.x1 == 2 * m.p1**2) |
| 99 | + m.c2 = pyo.Constraint(expr=m.x2 == m.p2) |
| 100 | + theta = [m.p1, m.p2] |
| 101 | + dsdp, dfdp, rmap, cmap = pnsens.get_dsdp_dfdp(m, theta) |
| 102 | + |
| 103 | + dydp, rmap = pnsens.get_dydp([m.x2], dsdp, rmap) |
| 104 | + |
| 105 | + np.testing.assert_almost_equal(dydp[rmap[m.x2], cmap[m.p1]], 0.0) |
| 106 | + np.testing.assert_almost_equal(dydp[rmap[m.x2], cmap[m.p2]], 1.0) |
| 107 | + assert dydp.shape == (1, 2) |
0 commit comments