Пример #1
0
 def _run(self, sp, basename, **kwds):
     class_name, test_name = self.id().split('.')[-2:]
     output_directory = join(thisdir, class_name + "." + test_name)
     shutil.rmtree(output_directory, ignore_errors=True)
     os.makedirs(output_directory)
     convert_embedded(output_directory, basename, sp, **kwds)
     return output_directory
Пример #2
0
 def test_too_many_stages(self):
     sp = EmbeddedSP(self._get_base_model())
     sp.time_stages = [1, 2, 3]
     with self.assertRaises(ValueError) as cm:
         convert_embedded(self.tmpdir, 'test', sp)
     self.assertEqual(str(cm.exception),
                      ("SMPS conversion does not yet handle more "
                       "than 2 time-stages"))
Пример #3
0
 def test_makes_directory(self):
     tmpdir = tempfile.mkdtemp(dir=thisdir)
     self.assertTrue(os.path.exists(tmpdir))
     shutil.rmtree(tmpdir, ignore_errors=True)
     self.assertFalse(os.path.exists(tmpdir))
     sp = EmbeddedSP(self._get_base_model())
     convert_embedded(tmpdir, 'test', sp)
     self.assertTrue(os.path.exists(tmpdir))
     shutil.rmtree(tmpdir, ignore_errors=True)
Пример #4
0
 def test_nonlinear_stoch_constraint(self):
     model = self._get_base_model()
     model.c2._body = model.d2 * model.y**2
     sp = EmbeddedSP(model)
     with self.assertRaises(ValueError) as cm:
         convert_embedded(self.tmpdir, 'test', sp)
     self.assertTrue(
         str(cm.exception).startswith(
             "Cannot output embedded SP representation for component "
             "'c2'. The embedded SMPS writer does not yet handle "
             "stochastic nonlinear expressions. Invalid expression: "))
Пример #5
0
 def test_stoch_range_constraint(self):
     model = self._get_base_model()
     model.q = pyo.Param(mutable=True, initialize=0.0)
     model.stochdata.declare(model.q,
                             distribution=TableDistribution([0.0, 1.0]))
     model.c3 = pyo.Constraint(expr=pyo.inequality(model.q, model.y, 0))
     sp = EmbeddedSP(model)
     with self.assertRaises(ValueError) as cm:
         convert_embedded(self.tmpdir, 'test', sp)
     self.assertEqual(
         str(cm.exception),
         ("Cannot output embedded SP representation for component "
          "'c3'. The embedded SMPS writer does not yet handle range "
          "constraints that have stochastic data."))
Пример #6
0
 def test_bad_distribution_constraint(self):
     model = self._get_base_model()
     del model.stochdata
     model.stochdata = StochasticDataAnnotation()
     model.stochdata.declare(model.d2,
                             distribution=UniformDistribution(0.0, 1.0))
     sp = EmbeddedSP(model)
     with self.assertRaises(ValueError) as cm:
         convert_embedded(self.tmpdir, 'test', sp)
     self.assertEqual(
         str(cm.exception),
         ("Invalid distribution type 'UniformDistribution' for stochastic "
          "parameter 'd2'. The embedded SMPS writer currently "
          "only supports discrete table distributions of type "
          "pysp.embeddedsp.TableDistribution."))
Пример #7
0
 def test_stoch_data_nontrivial_expression_constraint1(self):
     model = self._get_base_model()
     model.c2._body = -model.d2 * model.y
     sp = EmbeddedSP(model)
     with self.assertRaises(ValueError) as cm:
         convert_embedded(self.tmpdir, 'test', sp)
     self.assertTrue(
         str(cm.exception).startswith(
             "Cannot output embedded SP representation for component "
             "'c2'. The embedded SMPS writer does not yet handle the "
             "case where a stochastic data component appears "
             "in an expression that defines a single variable's "
             "coefficient. The coefficient for variable 'y' must be "
             "exactly set to parameter 'd2' in the expression. Invalid "
             "expression: "))
Пример #8
0
 def test_stoch_data_nontrivial_expression_constraint2(self):
     model = self._get_base_model()
     model.q = pyo.Param(mutable=True, initialize=0.0)
     model.stochdata.declare(model.q,
                             distribution=TableDistribution([0.0, 1.0]))
     model.c2._body = (model.d2 + model.q) * model.y
     sp = EmbeddedSP(model)
     with self.assertRaises(ValueError) as cm:
         convert_embedded(self.tmpdir, 'test', sp)
     self.assertEqual(
         str(cm.exception),
         ("Cannot output embedded SP representation for component "
          "'c2'. The embedded SMPS writer does not yet handle the "
          "case where multiple stochastic data components appear "
          "in an expression that defines a single variable's "
          "coefficient. The coefficient for variable 'y' involves "
          "stochastic parameters: ['d2', 'q']"))
Пример #9
0
 def test_stoch_data_too_many_uses_constraint(self):
     model = self._get_base_model()
     model.c2._lower = model.d2
     sp = EmbeddedSP(model)
     with self.assertRaises(ValueError) as cm:
         convert_embedded(self.tmpdir, 'test', sp)
     self.assertEqual(
         str(cm.exception),
         ("Cannot output embedded SP representation for component "
          "'c2'. The embedded SMPS writer does not yet handle the "
          "case where a stochastic data component appears in "
          "multiple expressions or locations within a single "
          "expression (e.g., multiple constraints, or multiple "
          "variable coefficients within a constraint). The "
          "parameter 'd2' appearing in component 'c2' was "
          "previously encountered in another location in "
          "component 'c2'."))
Пример #10
0
 def test_stoch_constraint_body_constant(self):
     model = self._get_base_model()
     model.q = pyo.Param(mutable=True, initialize=0.0)
     model.stochdata.declare(model.q,
                             distribution=TableDistribution([0.0, 1.0]))
     model.c2._body = model.d2 * model.y + model.q
     sp = EmbeddedSP(model)
     with self.assertRaises(ValueError) as cm:
         convert_embedded(self.tmpdir, 'test', sp)
     self.assertEqual(
         str(cm.exception),
         ("Cannot output embedded SP representation for component "
          "'c2'. The embedded SMPS writer does not yet handle the "
          "case where a stochastic data appears in the body of a "
          "constraint expression that must be moved to the bounds. "
          "The constraint must be written so that the stochastic "
          "element 'q' is a simple bound or a simple variable "
          "coefficient."))
Пример #11
0
    def test_stoch_variable_bounds(self):
        # Note there is no way to test this starting from a real model
        # because the Pyomo AML does not return what is assigned
        # to the bounds without converting it to a raw value (e.g.,
        # we can't detect if a mutable Param was used in a bound)
        class _Junk(object):
            pass

        sp = _Junk()
        sp.time_stages = [1, 2]
        sp.has_stochastic_variable_bounds = True
        self.assertEqual(sp.has_stochastic_variable_bounds, True)
        with self.assertRaises(ValueError) as cm:
            convert_embedded(self.tmpdir, 'test', sp)
        self.assertEqual(str(cm.exception),
                         ("Problems with stochastic variables bounds "
                          "can not be converted into an embedded "
                          "SMPS representation"))