Skip to content

Commit

Permalink
ensure variables and parameters keep their values when constraints ar…
Browse files Browse the repository at this point in the history
…e removed
  • Loading branch information
michaelbynum committed Jul 9, 2023
1 parent cbeb1fb commit 8c6d4c2
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions wntr/tests/test_aml.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import numpy as np
import wntr.sim.aml as aml
from wntr.sim.solvers import NewtonSolver, SolverStatus


def compare_evaluation(self, m, true_r, true_j):
Expand All @@ -17,6 +18,30 @@ def compare_evaluation(self, m, true_r, true_j):
self.assertAlmostEqual(true_j[c][v], j[c.index, v.index], 10)


class TestModel(unittest.TestCase):
def test_var_value_with_decrement(self):
m = aml.Model()
m.x = aml.Var()
m.p = aml.Param(val=1)
m.c = aml.Constraint(m.x - m.p)
m.set_structure()
opt = NewtonSolver({'TOL': 1e-8})
status, msg, num_iter = opt.solve(m)
self.assertEqual(status, SolverStatus.converged)
self.assertAlmostEqual(m.x.value, 1)
m.p.value = 2
status, msg, num_iter = opt.solve(m)
self.assertEqual(status, SolverStatus.converged)
self.assertAlmostEqual(m.x.value, 2)
del m.c
m.c = aml.Constraint(m.x**0.5 - m.p)
m.set_structure()
self.assertAlmostEqual(m.x.value, 2) # this is the real test here
status, msg, num_iter = opt.solve(m)
self.assertEqual(status, SolverStatus.converged)
self.assertAlmostEqual(m.x.value, 4)


class TestExpression(unittest.TestCase):
def test_add(self):
m = aml.Model()
Expand Down

0 comments on commit 8c6d4c2

Please sign in to comment.