Skip to content

Commit

Permalink
Merge pull request #2760 from Robbybp/cyipopt-subclass
Browse files Browse the repository at this point in the history
Refactor CyIpopt interface to subclass `cyipopt.Problem`
  • Loading branch information
blnicho authored Apr 25, 2023
2 parents 2128df4 + 74aadec commit 9f57f07
Show file tree
Hide file tree
Showing 10 changed files with 601 additions and 379 deletions.
442 changes: 80 additions & 362 deletions pyomo/contrib/pynumero/algorithms/solvers/cyipopt_solver.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
# Use attempt_import here due to unguarded NumPy import in these files
pyomo_nlp = attempt_import('pyomo.contrib.pynumero.interfaces.pyomo_nlp')[0]
nlp_proj = attempt_import('pyomo.contrib.pynumero.interfaces.nlp_projections')[0]
from pyomo.contrib.pynumero.algorithms.solvers.cyipopt_solver import (
from pyomo.contrib.pynumero.algorithms.solvers.cyipopt_solver import CyIpoptSolver
from pyomo.contrib.pynumero.interfaces.cyipopt_interface import (
cyipopt_available,
CyIpoptNLP,
CyIpoptSolver,
)
from pyomo.contrib.pynumero.algorithms.solvers.scipy_solvers import (
FsolveNlpSolver,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ def __init__(

# currently, this interface does not do anything with Hessians

# Call CyIpoptProblemInterface.__init__, which calls
# cyipopt.Problem.__init__
super(PyomoExternalCyIpoptProblem, self).__init__()

def load_x_into_pyomo(self, primals):
"""
Use this method to load a numpy array of values into the corresponding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@

from pyomo.contrib.pynumero.interfaces.pyomo_nlp import PyomoNLP

from pyomo.contrib.pynumero.algorithms.solvers.cyipopt_solver import cyipopt_available
from pyomo.contrib.pynumero.interfaces.cyipopt_interface import cyipopt_available

if not cyipopt_available:
raise unittest.SkipTest("Pynumero needs cyipopt to run CyIpoptSolver tests")

from pyomo.contrib.pynumero.algorithms.solvers.cyipopt_solver import CyIpoptNLP
from pyomo.contrib.pynumero.interfaces.cyipopt_interface import CyIpoptNLP


def create_model1():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,13 @@

from pyomo.contrib.pynumero.interfaces.pyomo_nlp import PyomoNLP

from pyomo.contrib.pynumero.algorithms.solvers.cyipopt_solver import cyipopt_available

if not cyipopt_available:
raise unittest.SkipTest("Pynumero needs cyipopt to run CyIpoptSolver tests")

from pyomo.contrib.pynumero.algorithms.solvers.cyipopt_solver import (
CyIpoptSolver,
from pyomo.contrib.pynumero.interfaces.cyipopt_interface import (
cyipopt_available,
CyIpoptNLP,
)

from pyomo.contrib.pynumero.algorithms.solvers.cyipopt_solver import CyIpoptSolver


def create_model1():
m = pyo.ConcreteModel()
Expand Down Expand Up @@ -158,6 +155,17 @@ def f(model):
return model


@unittest.skipIf(cyipopt_available, "cyipopt is available")
class TestCyIpoptNotAvailable(unittest.TestCase):
def test_not_available_exception(self):
model = create_model1()
nlp = PyomoNLP(model)
msg = "cyipopt is required"
with self.assertRaisesRegex(RuntimeError, msg):
solver = CyIpoptSolver(CyIpoptNLP(nlp))


@unittest.skipUnless(cyipopt_available, "cyipopt is not available")
class TestCyIpoptSolver(unittest.TestCase):
def test_model1(self):
model = create_model1()
Expand Down Expand Up @@ -192,6 +200,7 @@ def test_model1_with_scaling(self):

with open('_cyipopt-scaling.log', 'r') as fd:
solver_trace = fd.read()
cynlp.close()
os.remove('_cyipopt-scaling.log')

# check for the following strings in the log and then delete the log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ def test_pyomo_external_model_scaling(self):

with open('_cyipopt-pyomo-ext-scaling.log', 'r') as fd:
solver_trace = fd.read()
cyipopt_problem.close()
os.remove('_cyipopt-pyomo-ext-scaling.log')

self.assertIn('nlp_scaling_method = user-scaling', solver_trace)
Expand Down Expand Up @@ -253,6 +254,7 @@ def test_pyomo_external_model_ndarray_scaling(self):

with open('_cyipopt-pyomo-ext-scaling-ndarray.log', 'r') as fd:
solver_trace = fd.read()
cyipopt_problem.close()
os.remove('_cyipopt-pyomo-ext-scaling-ndarray.log')

self.assertIn('nlp_scaling_method = user-scaling', solver_trace)
Expand Down
Loading

0 comments on commit 9f57f07

Please sign in to comment.