Beispiel #1
0
    def _get_base_model(self):
        model = aml.ConcreteModel()
        model.x = aml.Var()
        model.y = aml.Var()
        model.d1 = aml.Param(mutable=True, initialize=0.0)
        model.d2 = aml.Param(mutable=True, initialize=0.0)
        model.d3 = aml.Param(mutable=True, initialize=0.0)
        model.cost = aml.Expression([1, 2])
        model.cost[1].expr = model.x
        model.cost[2].expr = model.d1 * model.y
        model.o = aml.Objective(expr=model.cost[1] + model.cost[2])
        model.c1 = aml.Constraint(expr=model.x >= 0)
        model.c2 = aml.Constraint(expr=model.y * model.d2 >= model.d3)
        model.varstage = VariableStageAnnotation()
        model.varstage.declare(model.x, 1)
        model.varstage.declare(model.y, 2)
        model.stagecost = StageCostAnnotation()
        model.stagecost.declare(model.cost[1], 1)
        model.stagecost.declare(model.cost[2], 2)
        model.stochdata = StochasticDataAnnotation()
        model.stochdata.declare(model.d1,
                                distribution=TableDistribution([0.0, 1.0]))
        model.stochdata.declare(model.d2,
                                distribution=TableDistribution([0.0, 1.0]))
        model.stochdata.declare(model.d3,
                                distribution=TableDistribution([0.0, 1.0]))

        return model
Beispiel #2
0
    def _get_baa99_sp(self):
        model = baa99_basemodel.model.clone()
        model.varstage = VariableStageAnnotation()
        model.varstage.declare(model.x1, 1)
        model.varstage.declare(model.x2, 1)

        model.stagecost = StageCostAnnotation()
        model.stagecost.declare(model.FirstStageCost, 1)
        model.stagecost.declare(model.SecondStageCost, 2)

        model.stochdata = StochasticDataAnnotation()
        model.stochdata.declare(model.d1_rhs,
                                distribution=TableDistribution(
                                    model.d1_rhs_table))
        model.stochdata.declare(model.d2_rhs,
                                distribution=TableDistribution(
                                    model.d2_rhs_table))

        return EmbeddedSP(model)
Beispiel #3
0
 def test_stoch_range_constraint(self):
     model = self._get_base_model()
     model.q = aml.Param(mutable=True, initialize=0.0)
     model.stochdata.declare(model.q,
                             distribution=TableDistribution([0.0, 1.0]))
     model.c3 = aml.Constraint(expr=aml.inequality(model.q, model.y, 0))
     sp = EmbeddedSP(model)
     with self.assertRaises(ValueError) as cm:
         pyomo.pysp.convert.smps.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."))
Beispiel #4
0
 def test_stoch_data_nontrivial_expression_constraint2(self):
     model = self._get_base_model()
     model.q = aml.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:
         pyomo.pysp.convert.smps.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']"))
Beispiel #5
0
 def test_stoch_constraint_body_constant(self):
     model = self._get_base_model()
     model.q = aml.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:
         pyomo.pysp.convert.smps.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."))
Beispiel #6
0
 def test_TableDistrubtion(self):
     with self.assertRaises(ValueError):
         TableDistribution([])
     with self.assertRaises(ValueError):
         TableDistribution([1],weights=[])
     with self.assertRaises(ValueError):
         TableDistribution([1],weights=[0.5])
     d = TableDistribution([1,2,3])
     v = d.sample()
     self.assertTrue(v in d.values)
     d = TableDistribution([1,2], weights=[0.5, 0.5])
     v = d.sample()
     self.assertTrue(v in d.values)
     d = TableDistribution([1,2], weights=[1, 0])
     v = d.sample()
     self.assertEqual(v, 1)
def create_embedded():

    model = aml.ConcreteModel()
    model.d1 = aml.Param(mutable=True, initialize=0)
    model.d2 = aml.Param(mutable=True, initialize=0)
    model.d3 = aml.Param(mutable=True, initialize=0)
    model.d4 = aml.Param(mutable=True, initialize=0)
    # first stage
    model.x = aml.Var(bounds=(0, 10))
    # first stage derived
    model.y = aml.Expression(expr=model.x + 1)
    model.fx = aml.Var()
    # second stage
    model.z = aml.Var(bounds=(-10, 10))
    # second stage derived
    model.q = aml.Expression(expr=model.z**2)
    model.fz = aml.Var()
    model.r = aml.Var()
    # stage costs
    model.StageCost = aml.Expression([1, 2])
    model.StageCost.add(1, model.fx)
    model.StageCost.add(2, -model.fz + model.r + model.d1)
    model.o = aml.Objective(expr=aml.sum_product(model.StageCost))

    model.c_first_stage = aml.Constraint(expr=model.x >= 0)

    # test our handling of intermediate variables that
    # are created by Piecewise but can not necessarily
    # be declared on the scenario tree
    model.p_first_stage = aml.Piecewise(model.fx,
                                        model.x,
                                        pw_pts=[0., 2., 5., 7., 10.],
                                        pw_constr_type='EQ',
                                        pw_repn='INC',
                                        f_rule=[10., 10., 9., 10., 10.],
                                        force_pw=True)

    model.c_second_stage = aml.Constraint(
        expr=model.x + model.r * model.d2 >= -100)
    model.cL_second_stage = aml.Constraint(expr=model.d3 >= -model.r)
    model.cU_second_stage = aml.Constraint(expr=model.r <= 0)

    # exercise more of the code by making this an indexed
    # block
    model.p_second_stage = aml.Piecewise([1],
                                         model.fz,
                                         model.z,
                                         pw_pts=[-10, -5., 0., 5., 10.],
                                         pw_constr_type='EQ',
                                         pw_repn='INC',
                                         f_rule=[0., 0., -1., model.d4, 1.],
                                         force_pw=True)

    # annotate the model
    model.varstage = VariableStageAnnotation()
    # first stage
    model.varstage.declare(model.x, 1)
    model.varstage.declare(model.y, 1, derived=True)
    model.varstage.declare(model.fx, 1, derived=True)
    model.varstage.declare(model.p_first_stage, 1, derived=True)
    # second stage
    model.varstage.declare(model.z, 2)
    model.varstage.declare(model.q, 2, derived=True)
    model.varstage.declare(model.fz, 2, derived=True)
    model.varstage.declare(model.r, 2, derived=True)
    model.varstage.declare(model.p_second_stage, 2, derived=True)

    model.stagecost = StageCostAnnotation()
    for i in [1, 2]:
        model.stagecost.declare(model.StageCost[i], i)

    model.stochdata = StochasticDataAnnotation()
    model.stochdata.declare(model.d1,
                            distribution=TableDistribution([0.0, 1.0, 2.0]))
    model.stochdata.declare(model.d2,
                            distribution=TableDistribution([0.0, 1.0, 2.0]))
    model.stochdata.declare(model.d3,
                            distribution=TableDistribution([0.0, 1.0, 2.0]))
    model.stochdata.declare(model.d4,
                            distribution=TableDistribution([0.0, 1.0, 2.0]))

    return EmbeddedSP(model)
Beispiel #8
0
 def test_TableDistrubtion(self):
     with self.assertRaises(ValueError):
         TableDistribution([])
     with self.assertRaises(ValueError):
         TableDistribution([1], weights=[])
     with self.assertRaises(ValueError):
         TableDistribution([1], weights=[0.5])
     d = TableDistribution([1, 2, 3])
     v = d.sample()
     self.assertTrue(v in d.values)
     d = TableDistribution([1, 2], weights=[0.5, 0.5])
     v = d.sample()
     self.assertTrue(v in d.values)
     d = TableDistribution([1, 2], weights=[1, 0])
     v = d.sample()
     self.assertEqual(v, 1)