Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APPSI Interface to WNTR #2902

Merged
merged 18 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/test_branches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ jobs:
|| echo "WARNING: Gurobi is not available"
python -m pip install --cache-dir cache/pip xpress \
|| echo "WARNING: Xpress Community Edition is not available"
if [[ ${{matrix.python}} == pypy* ]]; then
echo "skipping wntr for pypy"
else
python -m pip install wntr \
|| echo "WARNING: WNTR is not available"
fi
fi
python -c 'import sys; print("PYTHON_EXE=%s" \
% (sys.executable,))' >> $GITHUB_ENV
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/test_pr_and_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,12 @@ jobs:
|| echo "WARNING: Gurobi is not available"
python -m pip install --cache-dir cache/pip xpress \
|| echo "WARNING: Xpress Community Edition is not available"
if [[ ${{matrix.python}} == pypy* ]]; then
echo "skipping wntr for pypy"
else
python -m pip install wntr \
|| echo "WARNING: WNTR is not available"
fi
fi
python -c 'import sys; print("PYTHON_EXE=%s" \
% (sys.executable,))' >> $GITHUB_ENV
Expand Down
2 changes: 1 addition & 1 deletion pyomo/contrib/appsi/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@

def set_instance(self, model):
saved_update_config = self.update_config
self.__init__()
self.__init__(only_child_vars=self._only_child_vars)

Check warning on line 936 in pyomo/contrib/appsi/base.py

View check run for this annotation

Codecov / codecov/patch

pyomo/contrib/appsi/base.py#L936

Added line #L936 was not covered by tests
self.update_config = saved_update_config
self._model = model
if self.use_extensions and cmodel_available:
Expand Down
1 change: 1 addition & 0 deletions pyomo/contrib/appsi/solvers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from .cbc import Cbc
from .cplex import Cplex
from .highs import Highs
from .wntr import Wntr, WntrResults
182 changes: 182 additions & 0 deletions pyomo/contrib/appsi/solvers/tests/test_wntr_persistent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import pyomo.environ as pe
import pyomo.common.unittest as unittest
from pyomo.contrib.appsi.base import TerminationCondition, Results, PersistentSolver
from pyomo.contrib.appsi.solvers.wntr import Wntr, wntr_available
import math


_default_wntr_options = dict(TOL=1e-8)


@unittest.skipUnless(wntr_available, 'wntr is not available')
class TestWntrPersistent(unittest.TestCase):
def test_param_updates(self):
m = pe.ConcreteModel()
m.x = pe.Var()
m.p = pe.Param(initialize=1, mutable=True)
m.c = pe.Constraint(expr=m.x == m.p)
opt = Wntr()
opt.wntr_options.update(_default_wntr_options)
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, 1)

m.p.value = 2
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, 2)

def test_remove_add_constraint(self):
m = pe.ConcreteModel()
m.x = pe.Var()
m.y = pe.Var()
m.c1 = pe.Constraint(expr=m.y == (m.x - 1) ** 2)
m.c2 = pe.Constraint(expr=m.y == pe.exp(m.x))
opt = Wntr()
opt.config.symbolic_solver_labels = True
opt.wntr_options.update(_default_wntr_options)
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, 0)
self.assertAlmostEqual(m.y.value, 1)

del m.c2
m.c2 = pe.Constraint(expr=m.y == pe.log(m.x))
m.x.value = 0.5
m.y.value = 0.5
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, 1)
self.assertAlmostEqual(m.y.value, 0)

def test_fixed_var(self):
m = pe.ConcreteModel()
m.x = pe.Var()
m.y = pe.Var()
m.c1 = pe.Constraint(expr=m.y == (m.x - 1) ** 2)
m.x.fix(0.5)
opt = Wntr()
opt.wntr_options.update(_default_wntr_options)
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, 0.5)
self.assertAlmostEqual(m.y.value, 0.25)

m.x.unfix()
m.c2 = pe.Constraint(expr=m.y == pe.exp(m.x))
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, 0)
self.assertAlmostEqual(m.y.value, 1)

m.x.fix(0.5)
del m.c2
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, 0.5)
self.assertAlmostEqual(m.y.value, 0.25)

def test_remove_variables_params(self):
m = pe.ConcreteModel()
m.x = pe.Var()
m.y = pe.Var()
m.z = pe.Var()
m.z.fix(0)
m.px = pe.Param(mutable=True, initialize=1)
m.py = pe.Param(mutable=True, initialize=1)
m.c1 = pe.Constraint(expr=m.x == m.px)
m.c2 = pe.Constraint(expr=m.y == m.py)
opt = Wntr()
opt.wntr_options.update(_default_wntr_options)
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, 1)
self.assertAlmostEqual(m.y.value, 1)
self.assertAlmostEqual(m.z.value, 0)

del m.c2
del m.y
del m.py
m.z.value = 2
m.px.value = 2
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, 2)
self.assertAlmostEqual(m.z.value, 2)

del m.z
m.px.value = 3
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, 3)

def test_get_primals(self):
m = pe.ConcreteModel()
m.x = pe.Var()
m.y = pe.Var()
m.c1 = pe.Constraint(expr=m.y == (m.x - 1) ** 2)
m.c2 = pe.Constraint(expr=m.y == pe.exp(m.x))
opt = Wntr()
opt.config.load_solution = False
opt.wntr_options.update(_default_wntr_options)
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, None)
self.assertAlmostEqual(m.y.value, None)
primals = opt.get_primals()
self.assertAlmostEqual(primals[m.x], 0)
self.assertAlmostEqual(primals[m.y], 1)

def test_operators(self):
m = pe.ConcreteModel()
m.x = pe.Var(initialize=1)
m.c1 = pe.Constraint(expr=2 / m.x == 1)
opt = Wntr()
opt.wntr_options.update(_default_wntr_options)
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, 2)

del m.c1
m.x.value = 0
m.c1 = pe.Constraint(expr=pe.sin(m.x) == math.sin(math.pi / 4))
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, math.pi / 4)

del m.c1
m.c1 = pe.Constraint(expr=pe.cos(m.x) == 0)
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, math.pi / 2)

del m.c1
m.c1 = pe.Constraint(expr=pe.tan(m.x) == 1)
m.x.value = 0
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, math.pi / 4)

del m.c1
m.c1 = pe.Constraint(expr=pe.asin(m.x) == math.asin(0.5))
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, 0.5)

del m.c1
m.c1 = pe.Constraint(expr=pe.acos(m.x) == math.acos(0.6))
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, 0.6)

del m.c1
m.c1 = pe.Constraint(expr=pe.atan(m.x) == math.atan(0.5))
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, 0.5)

del m.c1
m.c1 = pe.Constraint(expr=pe.sqrt(m.x) == math.sqrt(0.6))
res = opt.solve(m)
self.assertEqual(res.termination_condition, TerminationCondition.optimal)
self.assertAlmostEqual(m.x.value, 0.6)
Loading