|
| 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 | +from pyomo.common import unittest |
| 13 | +from pyomo.environ import ( |
| 14 | + ConcreteModel, |
| 15 | + Var, |
| 16 | + Objective, |
| 17 | + Constraint, |
| 18 | + Suffix, |
| 19 | + NonNegativeIntegers, |
| 20 | + NonNegativeReals, |
| 21 | + value, |
| 22 | +) |
| 23 | +from pyomo.opt import SolverFactory, TerminationCondition |
| 24 | + |
| 25 | +knitroampl_available = SolverFactory('knitroampl').available(False) |
| 26 | + |
| 27 | + |
| 28 | +class TestKNITROAMPLInterface(unittest.TestCase): |
| 29 | + @unittest.skipIf( |
| 30 | + not knitroampl_available, "The 'knitroampl' command is not available" |
| 31 | + ) |
| 32 | + def test_infeasible_lp(self): |
| 33 | + with SolverFactory('knitroampl') as opt: |
| 34 | + model = ConcreteModel() |
| 35 | + model.X = Var(within=NonNegativeReals) |
| 36 | + model.C1 = Constraint(expr=model.X == 1) |
| 37 | + model.C2 = Constraint(expr=model.X == 2) |
| 38 | + model.Obj = Objective(expr=model.X) |
| 39 | + |
| 40 | + results = opt.solve(model) |
| 41 | + |
| 42 | + self.assertEqual( |
| 43 | + results.solver.termination_condition, TerminationCondition.infeasible |
| 44 | + ) |
| 45 | + |
| 46 | + @unittest.skipIf( |
| 47 | + not knitroampl_available, "The 'knitroampl' command is not available" |
| 48 | + ) |
| 49 | + def test_unbounded_lp(self): |
| 50 | + with SolverFactory('knitroampl') as opt: |
| 51 | + model = ConcreteModel() |
| 52 | + model.X = Var() |
| 53 | + model.Obj = Objective(expr=model.X) |
| 54 | + |
| 55 | + results = opt.solve(model) |
| 56 | + |
| 57 | + self.assertIn( |
| 58 | + results.solver.termination_condition, |
| 59 | + ( |
| 60 | + TerminationCondition.unbounded, |
| 61 | + TerminationCondition.infeasibleOrUnbounded, |
| 62 | + ), |
| 63 | + ) |
| 64 | + |
| 65 | + @unittest.skipIf( |
| 66 | + not knitroampl_available, "The 'knitroampl' command is not available" |
| 67 | + ) |
| 68 | + def test_optimal_lp(self): |
| 69 | + with SolverFactory('knitroampl') as opt: |
| 70 | + model = ConcreteModel() |
| 71 | + model.X = Var(within=NonNegativeReals) |
| 72 | + model.C1 = Constraint(expr=model.X >= 2.5) |
| 73 | + model.Obj = Objective(expr=model.X) |
| 74 | + |
| 75 | + results = opt.solve(model, load_solutions=True) |
| 76 | + |
| 77 | + self.assertEqual( |
| 78 | + results.solver.termination_condition, TerminationCondition.optimal |
| 79 | + ) |
| 80 | + self.assertAlmostEqual(value(model.X), 2.5) |
| 81 | + |
| 82 | + @unittest.skipIf( |
| 83 | + not knitroampl_available, "The 'knitroampl' command is not available" |
| 84 | + ) |
| 85 | + def test_get_duals_lp(self): |
| 86 | + with SolverFactory('knitroampl') as opt: |
| 87 | + model = ConcreteModel() |
| 88 | + model.X = Var(within=NonNegativeReals) |
| 89 | + model.Y = Var(within=NonNegativeReals) |
| 90 | + |
| 91 | + model.C1 = Constraint(expr=2 * model.X + model.Y >= 8) |
| 92 | + model.C2 = Constraint(expr=model.X + 3 * model.Y >= 6) |
| 93 | + |
| 94 | + model.Obj = Objective(expr=model.X + model.Y) |
| 95 | + |
| 96 | + results = opt.solve(model, suffixes=['dual'], load_solutions=False) |
| 97 | + |
| 98 | + model.dual = Suffix(direction=Suffix.IMPORT) |
| 99 | + model.solutions.load_from(results) |
| 100 | + |
| 101 | + self.assertAlmostEqual(model.dual[model.C1], 0.4) |
| 102 | + self.assertAlmostEqual(model.dual[model.C2], 0.2) |
| 103 | + |
| 104 | + @unittest.skipIf( |
| 105 | + not knitroampl_available, "The 'knitroampl' command is not available" |
| 106 | + ) |
| 107 | + def test_infeasible_mip(self): |
| 108 | + with SolverFactory('knitroampl') as opt: |
| 109 | + model = ConcreteModel() |
| 110 | + model.X = Var(within=NonNegativeIntegers) |
| 111 | + model.C1 = Constraint(expr=model.X == 1) |
| 112 | + model.C2 = Constraint(expr=model.X == 2) |
| 113 | + model.Obj = Objective(expr=model.X) |
| 114 | + |
| 115 | + results = opt.solve(model) |
| 116 | + |
| 117 | + self.assertEqual( |
| 118 | + results.solver.termination_condition, TerminationCondition.infeasible |
| 119 | + ) |
| 120 | + |
| 121 | + @unittest.skipIf( |
| 122 | + not knitroampl_available, "The 'knitroampl' command is not available" |
| 123 | + ) |
| 124 | + def test_optimal_mip(self): |
| 125 | + with SolverFactory('knitroampl') as opt: |
| 126 | + model = ConcreteModel() |
| 127 | + model.X = Var(within=NonNegativeIntegers) |
| 128 | + model.C1 = Constraint(expr=model.X >= 2.5) |
| 129 | + model.Obj = Objective(expr=model.X) |
| 130 | + |
| 131 | + results = opt.solve(model, load_solutions=True) |
| 132 | + |
| 133 | + self.assertEqual( |
| 134 | + results.solver.termination_condition, TerminationCondition.optimal |
| 135 | + ) |
| 136 | + self.assertAlmostEqual(value(model.X), 3) |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == "__main__": |
| 140 | + unittest.main() |
0 commit comments