Skip to content

Commit

Permalink
Add skip statement to doctests; start ipopt unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmundt committed Feb 20, 2024
1 parent 044f847 commit 423b141
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/OnlineDocs/developer_reference/solvers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Backwards Compatible Mode
^^^^^^^^^^^^^^^^^^^^^^^^^

.. testcode::
:skipif: not ipopt_available

import pyomo.environ as pyo
from pyomo.contrib.solver.util import assert_optimal_termination
Expand All @@ -53,6 +54,7 @@ Backwards Compatible Mode
model.pprint()

.. testoutput::
:skipif: not ipopt_available
:hide:

2 Var Declarations
Expand All @@ -66,6 +68,7 @@ There are multiple ways to utilize the future capability mode: direct import
or changed ``SolverFactory`` version.

.. testcode::
:skipif: not ipopt_available

# Direct import
import pyomo.environ as pyo
Expand All @@ -89,6 +92,7 @@ or changed ``SolverFactory`` version.
model.pprint()

.. testoutput::
:skipif: not ipopt_available
:hide:

solution_loader: ...
Expand All @@ -98,6 +102,7 @@ or changed ``SolverFactory`` version.
Changing the ``SolverFactory`` version:

.. testcode::
:skipif: not ipopt_available

# Change SolverFactory version
import pyomo.environ as pyo
Expand All @@ -120,13 +125,15 @@ Changing the ``SolverFactory`` version:
model.pprint()

.. testoutput::
:skipif: not ipopt_available
:hide:

solution_loader: ...
...
3 Declarations: x y obj

.. testcode::
:skipif: not ipopt_available
:hide:

from pyomo.__future__ import solver_factory_v1
Expand Down
73 changes: 73 additions & 0 deletions pyomo/contrib/solver/tests/unit/test_ipopt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# ___________________________________________________________________________
#
# Pyomo: Python Optimization Modeling Objects
# Copyright (c) 2008-2024
# National Technology and Engineering Solutions of Sandia, LLC
# Under the terms of Contract DE-NA0003525 with National Technology and
# Engineering Solutions of Sandia, LLC, the U.S. Government retains certain
# rights in this software.
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________

from pyomo.common import unittest, Executable
from pyomo.repn.plugins.nl_writer import NLWriter
from pyomo.contrib.solver import ipopt


ipopt_available = ipopt.Ipopt().available()


@unittest.skipIf(not ipopt_available, "The 'ipopt' command is not available")
class TestIpoptSolverConfig(unittest.TestCase):
def test_default_instantiation(self):
config = ipopt.IpoptConfig()
# Should be inherited
self.assertIsNone(config._description)
self.assertEqual(config._visibility, 0)
self.assertFalse(config.tee)
self.assertTrue(config.load_solutions)
self.assertTrue(config.raise_exception_on_nonoptimal_result)
self.assertFalse(config.symbolic_solver_labels)
self.assertIsNone(config.timer)
self.assertIsNone(config.threads)
self.assertIsNone(config.time_limit)
# Unique to this object
self.assertIsInstance(config.executable, type(Executable('path')))
self.assertIsInstance(config.writer_config, type(NLWriter.CONFIG()))

def test_custom_instantiation(self):
config = ipopt.IpoptConfig(description="A description")
config.tee = True
self.assertTrue(config.tee)
self.assertEqual(config._description, "A description")
self.assertFalse(config.time_limit)
# Default should be `ipopt`
self.assertIsNotNone(str(config.executable))
self.assertIn('ipopt', str(config.executable))
# Set to a totally bogus path
config.executable = Executable('/bogus/path')
self.assertIsNone(config.executable.executable)
self.assertFalse(config.executable.available())


class TestIpoptResults(unittest.TestCase):
def test_default_instantiation(self):
res = ipopt.IpoptResults()
# Inherited methods/attributes
self.assertIsNone(res.solution_loader)
self.assertIsNone(res.incumbent_objective)
self.assertIsNone(res.objective_bound)
self.assertIsNone(res.solver_name)
self.assertIsNone(res.solver_version)
self.assertIsNone(res.iteration_count)
self.assertIsNone(res.timing_info.start_timestamp)
self.assertIsNone(res.timing_info.wall_time)
# Unique to this object
self.assertIsNone(res.timing_info.ipopt_excluding_nlp_functions)
self.assertIsNone(res.timing_info.nlp_function_evaluations)
self.assertIsNone(res.timing_info.total_seconds)


@unittest.skipIf(not ipopt_available, "The 'ipopt' command is not available")
class TestIpoptInterface(unittest.TestCase):
pass

0 comments on commit 423b141

Please sign in to comment.