Skip to content

Commit

Permalink
Finishing test clean-up for sensitivity_toolbox
Browse files Browse the repository at this point in the history
  • Loading branch information
blnicho committed Oct 27, 2020
1 parent 9f983cb commit 3c6caaf
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 863 deletions.
8 changes: 4 additions & 4 deletions pyomo/contrib/sensitivity_toolbox/examples/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from __future__ import print_function
from pyomo.environ import ConcreteModel, Param, Var, Objective, Constraint, NonNegativeReals, value
from pyomo.contrib.sensitivity_toolbox.sens import sipopt
from pyomo.contrib.sensitivity_toolbox.sens import sensitivity_calculation

def create_model():
''' Create a concrete Pyomo model for this example
Expand Down Expand Up @@ -52,9 +52,9 @@ def run_example(print_flag=True):
m.perturbed_eta2 = Param(initialize = 1.0)


m_sipopt = sipopt(m,[m.eta1,m.eta2],
[m.perturbed_eta1,m.perturbed_eta2],
streamSoln=True)
m_sipopt = sensitivity_calculation('sipopt',m,[m.eta1,m.eta2],
[m.perturbed_eta1,m.perturbed_eta2],
streamSoln=True)



Expand Down
14 changes: 6 additions & 8 deletions pyomo/contrib/sensitivity_toolbox/examples/parameter_kaug.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


from pyomo.environ import *
from pyomo.contrib.sensitivity_toolbox.sens import kaug
from pyomo.contrib.sensitivity_toolbox.sens import sensitivity_calculation

def create_model():
''' Create a concrete Pyomo model for this example
Expand All @@ -35,7 +35,7 @@ def create_model():

return m

def example(print_flag=True):
def run_example(print_flag=True):
'''
Execute the example
Expand All @@ -52,11 +52,9 @@ def example(print_flag=True):
m.perturbed_eta2 = Param(initialize = 1.0)


m_kaug_dsdp = kaug(m,[m.eta1,m.eta2],
[m.perturbed_eta1,m.perturbed_eta2],
streamSoln=True)


m_kaug_dsdp = sensitivity_calculation('kaug',m,[m.eta1,m.eta2],
[m.perturbed_eta1,m.perturbed_eta2],
streamSoln=True)

if print_flag:
print("\nOriginal parameter values:")
Expand Down Expand Up @@ -99,4 +97,4 @@ def example(print_flag=True):
return d

if __name__=='__main__':
d = example()
d = run_example()
9 changes: 5 additions & 4 deletions pyomo/contrib/sensitivity_toolbox/sens.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

_log = logging.getLogger('pyomo.contrib.sensitivity_toolbox')

@deprecated('The sipopt function has been deprecated. Use the sensitivity_calculation() '
'function with method="sipopt" to access this functionality.',
@deprecated("The sipopt function has been deprecated. Use the sensitivity_calculation() "
"function with method='sipopt' to access this functionality.",
logger='pyomo.contrib.sensitivity_toolbox',
version='TBD')
def sipopt(instance, paramSubList, perturbList,
cloneModel=True, streamSoln=False, keepfiles=False):
Expand All @@ -29,8 +30,8 @@ def sipopt(instance, paramSubList, perturbList,

return m

@deprecated('The kaug function has been deprecated. Use the sensitivity_calculation() '
'function with method="kaug" to access this functionality.',
@deprecated("The kaug function has been deprecated. Use the sensitivity_calculation() "
"function with method='kaug' to access this functionality.",
version='TBD')
def kaug(instance, paramSubList, perturbList,
cloneModel=True, streamSoln=False, keepfiles=False, optarg=None):
Expand Down
155 changes: 51 additions & 104 deletions pyomo/contrib/sensitivity_toolbox/tests/test_sens.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from pyomo.core.expr.current import identify_variables
from pyomo.contrib.sensitivity_toolbox.sens import sipopt, kaug, sensitivity_calculation
import pyomo.contrib.sensitivity_toolbox.examples.parameter as param_ex
import pyomo.contrib.sensitivity_toolbox.examples.parameter_kaug as param_kaug_ex
import pyomo.contrib.sensitivity_toolbox.examples.feedbackController as fc
import pyomo.contrib.sensitivity_toolbox.examples.rangeInequality as ri
import pyomo.contrib.sensitivity_toolbox.examples.HIV_Transmission as hiv
Expand All @@ -45,11 +46,12 @@ def test_sipopt_deprecated(self):
with LoggingIntercept(output, 'pyomo.contrib.sensitivity_toolbox', logging.WARNING):
sipopt(m,[m.eta1,m.eta1],
[m.perturbed_eta1,m.perturbed_eta2],
cloneModel=False)
self.assertIn('DEPRECATED: The sipopt function has been deprecated. Use the '
'sensitivity_calculation() function with method="sipopt"',
cloneModel=False)
self.assertIn("DEPRECATED: The sipopt function has been deprecated. Use the "
"sensitivity_calculation() function with method='sipopt' to access",
output.getvalue().replace('\n', ' '))


@unittest.skipIf(not opt.available(False), "ipopt_sens is not available")
def test_sipopt_equivalent(self):
m1 = param_ex.create_model()
Expand All @@ -60,16 +62,16 @@ def test_sipopt_equivalent(self):
m2.perturbed_eta1 = Param(initialize = 4.0)
m2.perturbed_eta2 = Param(initialize = 1.0)

out1 = StringIO()
out2 = StringIO()
sipopt(m1,[m1.eta1,m1.eta2],
m11 = sipopt(m1,[m1.eta1,m1.eta2],
[m1.perturbed_eta1,m1.perturbed_eta2],
cloneModel=False)
sensitivity_calculation('sipopt',m2,[m2.eta1,m2.eta2],
cloneModel=True)
m22 = sensitivity_calculation('sipopt',m2,[m2.eta1,m2.eta2],
[m2.perturbed_eta1,m2.perturbed_eta2],
cloneModel=False)
m1.pprint(ostream=out1)
m2.pprint(ostream=out2)
cloneModel=True)
out1 = StringIO()
out2 = StringIO()
m11._sipopt_data.constList.pprint(ostream=out1)
m22._sipopt_data.constList.pprint(ostream=out2)
self.assertMultiLineEqual(out1.getvalue(), out2.getvalue())

@unittest.skipIf(not opt_kaug.available(False), "k_aug is not available")
Expand All @@ -84,8 +86,8 @@ def test_kaug_deprecated(self):
kaug(m,[m.eta1,m.eta1],
[m.perturbed_eta1,m.perturbed_eta2],
cloneModel=False)
self.assertIn('DEPRECATED: The kaug function has been deprecated. Use the '
'sensitivity_calculation() function with method="kaug"',
self.assertIn("DEPRECATED: The kaug function has been deprecated. Use the "
"sensitivity_calculation() function with method='kaug'",
output.getvalue().replace('\n', ' '))

@unittest.skipIf(not opt_kaug.available(False), "k_aug is not available")
Expand All @@ -99,16 +101,16 @@ def test_kaug_equivalent(self):
m2.perturbed_eta1 = Param(initialize = 4.0)
m2.perturbed_eta2 = Param(initialize = 1.0)

m11 = kaug(m1,[m1.eta1,m1.eta2],
[m1.perturbed_eta1,m1.perturbed_eta2],
cloneModel=True)
m22 = sensitivity_calculation('kaug',m2,[m2.eta1,m2.eta2],
[m2.perturbed_eta1,m2.perturbed_eta2],
cloneModel=True)
out1 = StringIO()
out2 = StringIO()
kaug(m1,[m1.eta1,m1.eta2],
[m1.perturbed_eta1,m1.perturbed_eta2],
cloneModel=False)
sensitivity_calculation('kaug',m2,[m2.eta1,m2.eta2],
[m2.perturbed_eta1,m2.perturbed_eta2],
cloneModel=False)
m1.pprint(ostream=out1)
m2.pprint(ostream=out2)
m11.pprint(ostream=out1)
m22.pprint(ostream=out2)
self.assertMultiLineEqual(out1.getvalue(), out2.getvalue())


Expand All @@ -133,35 +135,30 @@ def test_bad_arg(self):

# verify ValueError thrown when param and perturb list are different
# lengths
try:
Result = sipopt(m,list_one,list_two)
self.fail("Expected ValueError: for different length lists")
except ValueError:
pass
with self.assertRaises(ValueError) as context:
Result = sensitivity_calculation('sipopt',m,list_one,list_two)
self.assertTrue("Length of paramSubList argument does not equal "
"length of perturbList" in str(context.exception))

# verify ValueError thrown when param list has a Var in it
try:
Result = sipopt(m,list_three,list_two)
self.fail("Expected ValueError: variable sent through paramSubList")
except ValueError:
pass
with self.assertRaises(ValueError) as context:
Result = sensitivity_calculation('sipopt',m,list_three,list_one)
self.assertTrue("paramSubList argument is expecting a list of Params" in str(context.exception))

# verify ValueError thrown when perturb list has Var in it
try:
Result = sipopt(m,list_one,list_three)
self.fail("Expected ValueError: variable sent through perturbList")
except ValueError:
pass
with self.assertRaises(ValueError) as context:
Result = sensitivity_calculation('sipopt',m,list_one,list_three)
self.assertTrue("perturbList argument is expecting a list of Params" in str(context.exception))

# verify ValueError thrown when param list has an unmutable param
try:
Result = sipopt(m,list_four,list_one)
self.fail("Expected ValueError:"
"unmutable param sent through paramSubList")
except ValueError:
pass

with self.assertRaises(ValueError) as context:
Result = sensitivity_calculation('sipopt',m,list_four,list_one)
self.assertTrue("parameters within paramSubList must be mutable" in str(context.exception))

# verify ValueError thrown when an invalid method is specified
with self.assertRaises(ValueError) as context:
Result = sensitivity_calculation('foo',m,list_four,list_one)
self.assertTrue("method should be 'sipopt' or 'kaug'" in str(context.exception))

# test feedbackController Solution when the model gets cloned
@unittest.skipIf(not scipy_available, "scipy is required for this test")
Expand All @@ -174,7 +171,7 @@ def test_clonedModel_soln(self):
m_orig.perturbed_a = Param(initialize=-0.25)
m_orig.perturbed_H = Param(initialize=0.55)

m_sipopt = sipopt(m_orig,[m_orig.a,m_orig.H],
m_sipopt = sensitivity_calculation('sipopt',m_orig,[m_orig.a,m_orig.H],
[m_orig.perturbed_a,m_orig.perturbed_H],
cloneModel=True)

Expand Down Expand Up @@ -269,7 +266,7 @@ def test_noClone_soln(self):
m_orig.perturbed_a = Param(initialize=-0.25)
m_orig.perturbed_H = Param(initialize=0.55)

m_sipopt = sipopt(m_orig,[m_orig.a,m_orig.H],
m_sipopt = sensitivity_calculation('sipopt',m_orig,[m_orig.a,m_orig.H],
[m_orig.perturbed_a,m_orig.perturbed_H],
cloneModel=False)

Expand Down Expand Up @@ -341,8 +338,6 @@ def test_noClone_soln(self):
self.assertAlmostEqual(value(m_sipopt.J),0.0048956783,8)




# test indexed param mapping to var and perturbed values
@unittest.skipIf(not scipy_available, "scipy is required for this test")
@unittest.skipIf(not opt.available(False), "ipopt_sens is not available")
Expand All @@ -366,7 +361,7 @@ def test_indexedParamsMapping(self):

m.aaDelta = Param(initialize =0.0001001)

m_sipopt = sipopt(m, [m.eps,m.qq,m.aa],
m_sipopt = sensitivity_calculation('sipopt',m, [m.eps,m.qq,m.aa],
[m.epsDelta,m.qqDelta,m.aaDelta])

# param to var data
Expand Down Expand Up @@ -400,7 +395,7 @@ def test_constraintSub(self):
m.pert_a = Param(initialize=0.01)
m.pert_b = Param(initialize=1.01)

m_sipopt = sipopt(m,[m.a,m.b], [m.pert_a,m.pert_b])
m_sipopt = sensitivity_calculation('sipopt',m,[m.a,m.b], [m.pert_a,m.pert_b])

# verify substitutions in equality constraint
self.assertTrue(m_sipopt.C_equal.lower.ctype is Param and
Expand Down Expand Up @@ -441,8 +436,7 @@ def test_constraintSub(self):
@unittest.skipIf(not opt.available(False), "ipopt_sens is not available")
def test_parameter_example(self):

from pyomo.contrib.sensitivity_toolbox.examples.parameter import run_example
d = run_example()
d = param_ex.run_example()

d_correct = {'eta1':4.5, 'eta2':1.0, 'x1_init':0.15, 'x2_init':0.15, 'x3_init':0.0,
'cost_sln':0.5, 'x1_sln':0.5, 'x2_sln':0.5, 'x3_sln':0.0, 'eta1_pert':4.0,
Expand All @@ -456,53 +450,7 @@ def test_parameter_example(self):


# Test kaug
# Perform the same tests as ipopt_sens
# test arguments
@unittest.skipIf(not opt_kaug.available(False), "k_aug is not available")
@unittest.skipIf(not opt_dotsens.available(False), "dot_sens is not available")
def test_kaug_bad_arg_kaug(self):
m = ConcreteModel()
m.t = ContinuousSet(bounds=(0,1))

m.a = Param(initialize=1, mutable=True)
m.b = Param(initialize=2, mutable=True)
m.c = Param(initialize=3, mutable=False)

m.x = Var(m.t)

list_one = [m.a,m.b]
list_two = [m.a,m.b,m.c]
list_three = [m.a, m.x]
list_four = [m.a,m.c]

# verify ValueError thrown when param and perturb list are different
# lengths
with self.assertRaises(ValueError):
Result = kaug(m, list_one, list_two)

# verify ValueError thrown when param list has a Var in it
try:
Result = kaug(m,list_three,list_two)
self.fail("Expected ValueError: variable sent through paramSubList")
except ValueError:
pass

# verify ValueError thrown when perturb list has Var in it
try:
Result = kaug(m,list_one,list_three)
self.fail("Expected ValueError: variable sent through perturbList")
except ValueError:
pass

# verify ValueError thrown when param list has an unmutable param
try:
Result = kaug(m,list_four,list_one)
self.fail("Expected ValueError:"
"unmutable param sent through paramSubList")
except ValueError:
pass


# Perform the same tests as for sipopt
# test feedbackController Solution when the model gets cloned
@unittest.skipIf(not scipy_available, "scipy is required for this test")
@unittest.skipIf(not opt_kaug.available(False), "k_aug is not available")
Expand All @@ -514,7 +462,7 @@ def test_kaug_clonedModel_soln_kaug(self):
m_orig.perturbed_a = Param(initialize=-0.25)
m_orig.perturbed_H = Param(initialize=0.55)

m_kaug = kaug(m_orig,[m_orig.a,m_orig.H],
m_kaug = sensitivity_calculation('kaug',m_orig,[m_orig.a,m_orig.H],
[m_orig.perturbed_a,m_orig.perturbed_H],
cloneModel=True)

Expand Down Expand Up @@ -609,7 +557,7 @@ def test_noClone_soln_kaug(self):
m_orig.perturbed_a = Param(initialize=-0.25)
m_orig.perturbed_H = Param(initialize=0.55)

m_kaug = kaug(m_orig,[m_orig.a,m_orig.H],
m_kaug = sensitivity_calculation('kaug',m_orig,[m_orig.a,m_orig.H],
[m_orig.perturbed_a,m_orig.perturbed_H],
cloneModel=False)

Expand Down Expand Up @@ -696,7 +644,7 @@ def test_indexedParamsMapping_kaug(self):

m.aaDelta = Param(initialize =0.0001001)

m_kaug = kaug(m, [m.eps,m.qq,m.aa],
m_kaug = sensitivity_calculation('kaug',m, [m.eps,m.qq,m.aa],
[m.epsDelta,m.qqDelta,m.aaDelta])

# param to var data
Expand Down Expand Up @@ -732,16 +680,15 @@ def test_constraintSub_kaug(self):
# m_kaug = kaug(m,[m.a,m.b], [m.pert_a,m.pert_b])
# verify ValueError thrown when param list has an unmutable param
with self.assertRaises(Exception) as context:
m_kaug = kaug(m,[m.a,m.b], [m.pert_a,m.pert_b])
m_kaug = sensitivity_calculation('kaug',m,[m.a,m.b], [m.pert_a,m.pert_b])
self.assertTrue('kaug does not support inequality constraints.' in str(context.exception))

# Test example `parameter_kaug.py`
@unittest.skipIf(not opt_kaug.available(False), "k_aug is not available")
@unittest.skipIf(not opt_dotsens.available(False), "dot_sens is not available")
def test_parameter_example_kaug(self):

from parameter_kaug import example
d = example()
d = param_kaug_ex.run_example()

d_correct = {'eta1':4.5, 'eta2':1.0, 'x1_init':0.15, 'x2_init':0.15, 'x3_init':0.0,
'eta1_pert':4.0, 'eta2_pert':1.0, 'x1_pert':0.3333333,'x2_pert':0.6666667,
Expand Down

0 comments on commit 3c6caaf

Please sign in to comment.