Skip to content

Commit

Permalink
Fix PyNumero tests to use TempfileManager
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmundt committed Jan 13, 2025
1 parent 2a50773 commit c861491
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import pyomo.common.unittest as unittest
import pyomo.environ as pyo
import os
from pyomo.common.tempfiles import TempfileManager

from pyomo.contrib.pynumero.dependencies import (
numpy as np,
Expand Down Expand Up @@ -219,24 +219,25 @@ def test_model1_with_scaling(self):
m.scaling_factor[m.d] = 3.0 # scale the inequality constraint
m.scaling_factor[m.x[1]] = 4.0 # scale one of the x variables

cynlp = CyIpoptNLP(PyomoNLP(m))
options = {
'nlp_scaling_method': 'user-scaling',
'output_file': '_cyipopt-scaling.log',
'file_print_level': 10,
'max_iter': 0,
}
solver = CyIpoptSolver(cynlp, options=options)
x, info = solver.solve()

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
with TempfileManager.new_context() as temp:
cynlp = CyIpoptNLP(PyomoNLP(m))
logfile = temp.create_tempfile('_cyipopt-scaling.log')
options = {
'nlp_scaling_method': 'user-scaling',
'output_file': logfile,
'file_print_level': 10,
'max_iter': 0,
}
solver = CyIpoptSolver(cynlp, options=options)
x, info = solver.solve()
cynlp.close()

with open(logfile, 'r') as fd:
solver_trace = fd.read()

# check for the following strings in the log
self.assertIn('nlp_scaling_method = user-scaling', solver_trace)
self.assertIn('output_file = _cyipopt-scaling.log', solver_trace)
self.assertIn(f"output_file = {logfile}", solver_trace)
self.assertIn('objective scaling factor = 1e-06', solver_trace)
self.assertIn('x scaling provided', solver_trace)
self.assertIn('c scaling provided', solver_trace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________

import os
import pyomo.common.unittest as unittest
import pyomo.environ as pyo
from pyomo.common.tempfiles import TempfileManager

from pyomo.contrib.pynumero.dependencies import (
numpy as np,
Expand Down Expand Up @@ -157,33 +157,33 @@ def test_pyomo_external_model_scaling(self):
m.scaling_factor[m.F_con] = 8.0 # scale the pyomo constraint
m.scaling_factor[m.Pin_con] = 9.0 # scale the pyomo constraint

cyipopt_problem = PyomoExternalCyIpoptProblem(
pyomo_model=m,
ex_input_output_model=PressureDropModel(),
inputs=[m.Pin, m.c1, m.c2, m.F],
outputs=[m.P1, m.P2],
outputs_eqn_scaling=[10.0, 11.0],
nl_file_options={'file_determinism': 2},
)

# solve the problem
options = {
'hessian_approximation': 'limited-memory',
'nlp_scaling_method': 'user-scaling',
'output_file': '_cyipopt-pyomo-ext-scaling.log',
'file_print_level': 10,
'max_iter': 0,
}
solver = CyIpoptSolver(cyipopt_problem, options=options)
x, info = solver.solve(tee=False)

with open('_cyipopt-pyomo-ext-scaling.log', 'r') as fd:
solver_trace = fd.read()
cyipopt_problem.close()
os.remove('_cyipopt-pyomo-ext-scaling.log')
with TempfileManager.new_context() as temp:
cyipopt_problem = PyomoExternalCyIpoptProblem(
pyomo_model=m,
ex_input_output_model=PressureDropModel(),
inputs=[m.Pin, m.c1, m.c2, m.F],
outputs=[m.P1, m.P2],
outputs_eqn_scaling=[10.0, 11.0],
nl_file_options={'file_determinism': 2},
)
logfile = temp.create_tempfile('_cyipopt-pyomo-ext-scaling.log')
# solve the problem
options = {
'hessian_approximation': 'limited-memory',
'nlp_scaling_method': 'user-scaling',
'output_file': logfile,
'file_print_level': 10,
'max_iter': 0,
}
solver = CyIpoptSolver(cyipopt_problem, options=options)
x, info = solver.solve(tee=False)
cyipopt_problem.close()

with open(logfile, 'r') as fd:
solver_trace = fd.read()

self.assertIn('nlp_scaling_method = user-scaling', solver_trace)
self.assertIn('output_file = _cyipopt-pyomo-ext-scaling.log', solver_trace)
self.assertIn(f"output_file = {logfile}", solver_trace)
self.assertIn('objective scaling factor = 0.1', solver_trace)
self.assertIn('x scaling provided', solver_trace)
self.assertIn('c scaling provided', solver_trace)
Expand Down Expand Up @@ -232,35 +232,33 @@ def test_pyomo_external_model_ndarray_scaling(self):
m.scaling_factor[m.Pin_con] = 9.0 # scale the pyomo constraint

# test that this all works with ndarray input as well
cyipopt_problem = PyomoExternalCyIpoptProblem(
pyomo_model=m,
ex_input_output_model=PressureDropModel(),
inputs=[m.Pin, m.c1, m.c2, m.F],
outputs=[m.P1, m.P2],
outputs_eqn_scaling=np.asarray([10.0, 11.0], dtype=np.float64),
nl_file_options={'file_determinism': 2},
)

# solve the problem
options = {
'hessian_approximation': 'limited-memory',
'nlp_scaling_method': 'user-scaling',
'output_file': '_cyipopt-pyomo-ext-scaling-ndarray.log',
'file_print_level': 10,
'max_iter': 0,
}
solver = CyIpoptSolver(cyipopt_problem, options=options)
x, info = solver.solve(tee=False)

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')
with TempfileManager.new_context() as temp:
cyipopt_problem = PyomoExternalCyIpoptProblem(
pyomo_model=m,
ex_input_output_model=PressureDropModel(),
inputs=[m.Pin, m.c1, m.c2, m.F],
outputs=[m.P1, m.P2],
outputs_eqn_scaling=np.asarray([10.0, 11.0], dtype=np.float64),
nl_file_options={'file_determinism': 2},
)
logfile = temp.create_tempfile('_cyipopt-pyomo-ext-scaling-ndarray.log')
# solve the problem
options = {
'hessian_approximation': 'limited-memory',
'nlp_scaling_method': 'user-scaling',
'output_file': logfile,
'file_print_level': 10,
'max_iter': 0,
}
solver = CyIpoptSolver(cyipopt_problem, options=options)
x, info = solver.solve(tee=False)
cyipopt_problem.close()

with open(logfile, 'r') as fd:
solver_trace = fd.read()

self.assertIn('nlp_scaling_method = user-scaling', solver_trace)
self.assertIn(
'output_file = _cyipopt-pyomo-ext-scaling-ndarray.log', solver_trace
)
self.assertIn(f'output_file = {logfile}', solver_trace)
self.assertIn('objective scaling factor = 0.1', solver_trace)
self.assertIn('x scaling provided', solver_trace)
self.assertIn('c scaling provided', solver_trace)
Expand Down
50 changes: 27 additions & 23 deletions pyomo/contrib/pynumero/examples/tests/test_cyipopt_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
# ___________________________________________________________________________

import os.path
from io import StringIO
import logging

from pyomo.common.fileutils import this_file_dir, import_file
from pyomo.common.tempfiles import TempfileManager
import pyomo.common.unittest as unittest
import pyomo.environ as pyo
from pyomo.common.dependencies import attempt_import
from pyomo.common.log import LoggingIntercept
from pyomo.opt import TerminationCondition
from io import StringIO
import logging

from pyomo.contrib.pynumero.dependencies import (
numpy as np,
Expand Down Expand Up @@ -87,30 +89,32 @@ def test_external_grey_box_react_example_maximize_cb_outputs_scaling(self):
'maximize_cb_ratio_residuals.py',
)
)
aoptions = {
'nlp_scaling_method': 'user-scaling',
'output_file': '_cyipopt-external-greybox-react-scaling.log',
'file_print_level': 10,
}
m = ex.maximize_cb_ratio_residuals_with_output_scaling(
additional_options=aoptions
)
self.assertAlmostEqual(pyo.value(m.reactor.inputs['sv']), 1.26541996, places=3)
self.assertAlmostEqual(
pyo.value(m.reactor.inputs['cb']), 1071.7410089, places=2
)
self.assertAlmostEqual(
pyo.value(m.reactor.outputs['cb_ratio']), 0.15190409266, places=3
)

with open('_cyipopt-external-greybox-react-scaling.log', 'r') as fd:
solver_trace = fd.read()
os.remove('_cyipopt-external-greybox-react-scaling.log')
with TempfileManager.new_context() as temp:
logfile = temp.create_tempfile('_cyipopt-external-greybox-react-scaling.log')
aoptions = {
'nlp_scaling_method': 'user-scaling',
'output_file': logfile,
'file_print_level': 10,
}
m = ex.maximize_cb_ratio_residuals_with_output_scaling(
additional_options=aoptions
)
self.assertAlmostEqual(
pyo.value(m.reactor.inputs['sv']), 1.26541996, places=3
)
self.assertAlmostEqual(
pyo.value(m.reactor.inputs['cb']), 1071.7410089, places=2
)
self.assertAlmostEqual(
pyo.value(m.reactor.outputs['cb_ratio']), 0.15190409266, places=3
)

with open(logfile, 'r') as fd:
solver_trace = fd.read()

self.assertIn('nlp_scaling_method = user-scaling', solver_trace)
self.assertIn(
'output_file = _cyipopt-external-greybox-react-scaling.log', solver_trace
)
self.assertIn(f'output_file = {logfile}', solver_trace)
self.assertIn('objective scaling factor = 1', solver_trace)
self.assertIn('x scaling provided', solver_trace)
self.assertIn('c scaling provided', solver_trace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________

import os
import pyomo.common.unittest as unittest
import pyomo.environ as pyo
from pyomo.common.tempfiles import TempfileManager

from pyomo.contrib.pynumero.dependencies import (
numpy as np,
Expand All @@ -31,8 +31,11 @@

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

from ..external_grey_box import ExternalGreyBoxModel, ExternalGreyBoxBlock
from ..pyomo_nlp import PyomoGreyBoxNLP
from pyomo.contrib.pynumero.interfaces.external_grey_box import (
ExternalGreyBoxModel,
ExternalGreyBoxBlock,
)
from pyomo.contrib.pynumero.interfaces.pyomo_nlp import PyomoGreyBoxNLP
from pyomo.contrib.pynumero.interfaces.tests.compare_utils import (
check_vectors_specific_order,
check_sparse_matrix_specific_order,
Expand Down Expand Up @@ -2074,24 +2077,23 @@ def test_external_greybox_solve_scaling(self):
m.scaling_factor[m.mu] = 1.9
m.scaling_factor[m.pincon] = 2.2

solver = pyo.SolverFactory('cyipopt')
solver.config.options = {
'hessian_approximation': 'limited-memory',
'nlp_scaling_method': 'user-scaling',
'output_file': '_cyipopt-external-greybox-scaling.log',
'file_print_level': 10,
'max_iter': 0,
}
status = solver.solve(m, tee=False)

with open('_cyipopt-external-greybox-scaling.log', 'r') as fd:
solver_trace = fd.read()
os.remove('_cyipopt-external-greybox-scaling.log')
with TempfileManager.new_context() as temp:
logfile = temp.create_tempfile('_cyipopt-external-greybox-scaling.log')
solver = pyo.SolverFactory('cyipopt')
solver.config.options = {
'hessian_approximation': 'limited-memory',
'nlp_scaling_method': 'user-scaling',
'output_file': logfile,
'file_print_level': 10,
'max_iter': 0,
}
status = solver.solve(m, tee=False)

with open(logfile, 'r') as fd:
solver_trace = fd.read()

self.assertIn('nlp_scaling_method = user-scaling', solver_trace)
self.assertIn(
'output_file = _cyipopt-external-greybox-scaling.log', solver_trace
)
self.assertIn(f'output_file = {logfile}', solver_trace)
self.assertIn('objective scaling factor = 0.1', solver_trace)
self.assertIn('x scaling provided', solver_trace)
self.assertIn('c scaling provided', solver_trace)
Expand Down Expand Up @@ -2149,4 +2151,4 @@ def test_external_greybox_solve_scaling(self):


if __name__ == '__main__':
TestPyomoGreyBoxNLP().test_external_greybox_solve(self)
TestPyomoGreyBoxNLP().test_external_greybox_solve()
33 changes: 16 additions & 17 deletions pyomo/contrib/pynumero/interfaces/tests/test_pyomo_grey_box_nlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________

import os
import pyomo.common.unittest as unittest
import pyomo.environ as pyo
from pyomo.common.tempfiles import TempfileManager

from pyomo.contrib.pynumero.dependencies import (
numpy as np,
Expand Down Expand Up @@ -2495,24 +2495,23 @@ def test_external_greybox_solve_scaling(self):
m.scaling_factor[m.mu] = 1.9
m.scaling_factor[m.pincon] = 2.2

solver = pyo.SolverFactory('cyipopt')
solver.config.options = {
'hessian_approximation': 'limited-memory',
'nlp_scaling_method': 'user-scaling',
'output_file': '_cyipopt-external-greybox-scaling.log',
'file_print_level': 10,
'max_iter': 0,
}
status = solver.solve(m, tee=False)

with open('_cyipopt-external-greybox-scaling.log', 'r') as fd:
solver_trace = fd.read()
os.remove('_cyipopt-external-greybox-scaling.log')
with TempfileManager.new_context() as temp:
logfile = temp.create_tempfile('_cyipopt-external-greybox-scaling.log')
solver = pyo.SolverFactory('cyipopt')
solver.config.options = {
'hessian_approximation': 'limited-memory',
'nlp_scaling_method': 'user-scaling',
'output_file': logfile,
'file_print_level': 10,
'max_iter': 0,
}
status = solver.solve(m, tee=False)

with open(logfile, 'r') as fd:
solver_trace = fd.read()

self.assertIn('nlp_scaling_method = user-scaling', solver_trace)
self.assertIn(
'output_file = _cyipopt-external-greybox-scaling.log', solver_trace
)
self.assertIn(f'output_file = {logfile}', solver_trace)
self.assertIn('objective scaling factor = 0.1', solver_trace)
self.assertIn('x scaling provided', solver_trace)
self.assertIn('c scaling provided', solver_trace)
Expand Down

0 comments on commit c861491

Please sign in to comment.