Ejemplo n.º 1
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"))
Ejemplo n.º 2
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)
Ejemplo n.º 3
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: "))
Ejemplo n.º 4
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."))
Ejemplo n.º 5
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."))
Ejemplo n.º 6
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: "))
Ejemplo n.º 7
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']"))
Ejemplo n.º 8
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'."))
Ejemplo n.º 9
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."))
Ejemplo n.º 10
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)
Ejemplo n.º 11
0
model.cons = ConstraintList()
model.cons.add(model.t >= (model.c - model.b) * model.x + model.b * model.d)
model.cons.add(model.t >= (model.c + model.h) * model.x - model.h * model.d)
model.cons.add(model.y == -model.x)

#
# Embed an SP in the model using annotations
#
model.varstage = VariableStageAnnotation()
model.stagecost = StageCostAnnotation()
model.stochdata = StochasticDataAnnotation()
model.varstage.declare(model.x, 1)
model.stagecost.declare(model.cost[1], 1)
model.stagecost.declare(model.cost[2], 2)
model.stochdata.declare(model.d, distribution=d_dist)
sp = EmbeddedSP(model)

#
# Solve the deterministic approximation
#
sp.set_expected_value()
status = cplex.solve(sp.reference_model)
assert status.solver.termination_condition == \
    TerminationCondition.optimal
ev_solution = sp.reference_model.x()

#
# Setup an explicit training sample and solve
#
train_sp = sp.generate_sample_sp(train_N)
results = saa_solver.solve(train_sp, output_solver_log=True)
Ejemplo n.º 12
0
    def test_compute_time_stage(self):
        model = ConcreteModel()
        model.x = Var()
        model.y = Var([0, 1])
        model.z = Var()
        model.p = Param(mutable=True)
        model.cost = Expression([0, 1])
        model.cost[0] = model.x + model.y[0]
        model.cost[1] = model.p + model.y[1] + model.y[0] * model.p
        model.o = Objective(expr=model.cost[0] + model.cost[1])
        model.c = ConstraintList()
        model.c.add(model.x >= 1)  # 1
        model.c.add(model.y[0] >= 1)  # 2
        model.c.add(model.p * model.y[0] >= 1)  # 3
        model.c.add(model.y[0] >= model.p)  # 4
        model.c.add(model.p <= model.y[1])  # 5
        model.c.add(model.y[1] <= 1)  # 6
        model.c.add(model.x >= model.p)  # 7
        model.c.add(model.z == 1)  # 8

        model.varstage = VariableStageAnnotation()
        model.varstage.declare(model.x, 1)
        model.varstage.declare(model.y[0], 1, derived=True)
        model.varstage.declare(model.y[1], 2)
        model.varstage.declare(model.z, 2, derived=True)

        model.stagecost = StageCostAnnotation()
        model.stagecost.declare(model.cost[0], 1)
        model.stagecost.declare(model.cost[1], 2)

        model.stochdata = StochasticDataAnnotation()
        model.stochdata.declare(model.p,
                                distribution=UniformDistribution(0, 1))
        sp = EmbeddedSP(model)

        #
        # check variables
        #
        self.assertEqual(sp.compute_time_stage(model.x), min(sp.time_stages))
        self.assertEqual(
            sp.compute_time_stage(model.x, derived_last_stage=True),
            min(sp.time_stages))

        self.assertEqual(sp.compute_time_stage(model.y[0]),
                         min(sp.time_stages))
        self.assertEqual(
            sp.compute_time_stage(model.y[0], derived_last_stage=True),
            max(sp.time_stages))

        self.assertEqual(sp.compute_time_stage(model.y[1]),
                         max(sp.time_stages))
        self.assertEqual(
            sp.compute_time_stage(model.y[1], derived_last_stage=True),
            max(sp.time_stages))

        self.assertEqual(sp.compute_time_stage(model.z), max(sp.time_stages))
        self.assertEqual(
            sp.compute_time_stage(model.z, derived_last_stage=True),
            max(sp.time_stages))

        #
        # check constraints
        #
        self.assertEqual(sp.compute_time_stage(model.c[1]),
                         min(sp.time_stages))
        self.assertEqual(
            sp.compute_time_stage(model.c[1], derived_last_stage=True),
            min(sp.time_stages))

        self.assertEqual(sp.compute_time_stage(model.c[2]),
                         min(sp.time_stages))
        self.assertEqual(
            sp.compute_time_stage(model.c[2], derived_last_stage=True),
            max(sp.time_stages))

        self.assertEqual(sp.compute_time_stage(model.c[3]),
                         max(sp.time_stages))
        self.assertEqual(
            sp.compute_time_stage(model.c[3], derived_last_stage=True),
            max(sp.time_stages))

        self.assertEqual(sp.compute_time_stage(model.c[4]),
                         max(sp.time_stages))
        self.assertEqual(
            sp.compute_time_stage(model.c[4], derived_last_stage=True),
            max(sp.time_stages))

        self.assertEqual(sp.compute_time_stage(model.c[5]),
                         max(sp.time_stages))
        self.assertEqual(
            sp.compute_time_stage(model.c[5], derived_last_stage=True),
            max(sp.time_stages))

        self.assertEqual(sp.compute_time_stage(model.c[6]),
                         max(sp.time_stages))
        self.assertEqual(
            sp.compute_time_stage(model.c[6], derived_last_stage=True),
            max(sp.time_stages))

        self.assertEqual(sp.compute_time_stage(model.c[7]),
                         max(sp.time_stages))
        self.assertEqual(
            sp.compute_time_stage(model.c[7], derived_last_stage=True),
            max(sp.time_stages))

        self.assertEqual(sp.compute_time_stage(model.c[8]),
                         max(sp.time_stages))
        self.assertEqual(
            sp.compute_time_stage(model.c[8], derived_last_stage=True),
            max(sp.time_stages))

        #
        # check objectives and expressions
        #
        self.assertEqual(sp.compute_time_stage(model.cost[0]),
                         min(sp.time_stages))
        self.assertEqual(
            sp.compute_time_stage(model.cost[0], derived_last_stage=True),
            max(sp.time_stages))

        self.assertEqual(sp.compute_time_stage(model.cost[1]),
                         max(sp.time_stages))
        self.assertEqual(
            sp.compute_time_stage(model.cost[1], derived_last_stage=True),
            max(sp.time_stages))

        self.assertEqual(sp.compute_time_stage(model.o), max(sp.time_stages))
        self.assertEqual(
            sp.compute_time_stage(model.o, derived_last_stage=True),
            max(sp.time_stages))
Ejemplo n.º 13
0
    def test_collect_mutable_parameters(self):
        model = pc.ConcreteModel()
        model.p = pc.Param(mutable=True)
        model.q = pc.Param([1], mutable=True, initialize=1.0)
        model.r = pc.Param(initialize=1.1, mutable=False)
        model.x = pc.Var()
        for obj in [model.p, model.q[1]]:

            result = EmbeddedSP._collect_mutable_parameters(obj)
            self.assertTrue(id(obj) in result)
            self.assertEqual(len(result), 1)
            del result

            result = EmbeddedSP._collect_mutable_parameters(obj + 1)
            self.assertTrue(id(obj) in result)
            self.assertEqual(len(result), 1)
            del result

            result = EmbeddedSP._collect_mutable_parameters(2 * (obj + 1))
            self.assertTrue(id(obj) in result)
            self.assertEqual(len(result), 1)
            del result

            result = EmbeddedSP._collect_mutable_parameters(2 * obj)
            self.assertTrue(id(obj) in result)
            self.assertEqual(len(result), 1)
            del result

            result = EmbeddedSP._collect_mutable_parameters(2 * obj + 1)
            self.assertTrue(id(obj) in result)
            self.assertEqual(len(result), 1)
            del result

            result = EmbeddedSP._collect_mutable_parameters(2 * obj + 1 +
                                                            model.x)
            self.assertTrue(id(obj) in result)
            self.assertEqual(len(result), 1)
            del result

            result = EmbeddedSP._collect_mutable_parameters(obj * model.x)
            self.assertTrue(id(obj) in result)
            self.assertEqual(len(result), 1)
            del result

            result = EmbeddedSP._collect_mutable_parameters(model.x / obj)
            self.assertTrue(id(obj) in result)
            self.assertEqual(len(result), 1)
            del result

            result = EmbeddedSP._collect_mutable_parameters(model.x /
                                                            (2 * obj))
            self.assertTrue(id(obj) in result)
            self.assertEqual(len(result), 1)
            del result

            result = EmbeddedSP._collect_mutable_parameters(
                obj * pc.log(2 * model.x))
            self.assertTrue(id(obj) in result)
            self.assertEqual(len(result), 1)
            del result

            result = EmbeddedSP._collect_mutable_parameters(
                obj * pc.sin(model.r)**model.x)
            self.assertTrue(id(obj) in result)
            self.assertEqual(len(result), 1)
            del result

            result = EmbeddedSP._collect_mutable_parameters(
                model.x**(obj * pc.sin(model.r)))
            self.assertTrue(id(obj) in result)
            self.assertEqual(len(result), 1)
            del result

        result = EmbeddedSP._collect_mutable_parameters(1.0)
        self.assertEqual(len(result), 0)
        del result

        result = EmbeddedSP._collect_mutable_parameters(model.p + model.q[1] +
                                                        model.r)
        self.assertTrue(id(model.p) in result)
        self.assertTrue(id(model.q[1]) in result)
        self.assertEqual(len(result), 2)
        del result

        result = EmbeddedSP._collect_mutable_parameters(model.p + 1 + model.r +
                                                        model.q[1])
        self.assertTrue(id(model.p) in result)
        self.assertTrue(id(model.q[1]) in result)
        self.assertEqual(len(result), 2)

        result = EmbeddedSP._collect_mutable_parameters(model.q[1] * 2 *
                                                        (model.p + model.r) +
                                                        model.r)
        self.assertTrue(id(model.p) in result)
        self.assertTrue(id(model.q[1]) in result)
        self.assertEqual(len(result), 2)
        del result

        result = EmbeddedSP._collect_mutable_parameters(2 * model.x * model.p *
                                                        model.q[1] * model.r)
        self.assertTrue(id(model.p) in result)
        self.assertTrue(id(model.q[1]) in result)
        self.assertEqual(len(result), 2)
        del result

        result = EmbeddedSP._collect_mutable_parameters(2 * obj * model.q[1] *
                                                        model.r + 1)
        self.assertTrue(id(model.q[1]) in result)
        self.assertEqual(len(result), 1)
        del result

        result = EmbeddedSP._collect_mutable_parameters(2 * model.q[1] + 1 +
                                                        model.x - model.p)
        self.assertTrue(id(model.p) in result)
        self.assertTrue(id(model.q[1]) in result)
        self.assertEqual(len(result), 2)
        del result

        result = EmbeddedSP._collect_mutable_parameters(model.r * model.x)
        self.assertEqual(len(result), 0)
        del result

        result = EmbeddedSP._collect_mutable_parameters(model.x / obj)
        self.assertTrue(id(obj) in result)
        self.assertEqual(len(result), 1)
        del result

        result = EmbeddedSP._collect_mutable_parameters(
            model.x / (2 * model.q[1] / model.p))
        self.assertTrue(id(model.p) in result)
        self.assertTrue(id(model.q[1]) in result)
        self.assertEqual(len(result), 2)
        del result

        result = EmbeddedSP._collect_mutable_parameters(
            (model.p / model.q[1]) * pc.log(2 * model.x))
        self.assertTrue(id(model.p) in result)
        self.assertTrue(id(model.q[1]) in result)
        self.assertEqual(len(result), 2)
        del result

        result = EmbeddedSP._collect_mutable_parameters(
            model.q[1] * pc.sin(model.p)**(model.x + model.r))
        self.assertTrue(id(model.p) in result)
        self.assertTrue(id(model.q[1]) in result)
        self.assertEqual(len(result), 2)
        del result

        result = EmbeddedSP._collect_mutable_parameters(
            (model.p + model.x)**(model.q[1] * pc.sin(model.r)))
        self.assertTrue(id(model.p) in result)
        self.assertTrue(id(model.q[1]) in result)
        self.assertEqual(len(result), 2)
        del result
Ejemplo n.º 14
0
def create_embedded():

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

    model.c_first_stage = pyo.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 = pyo.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 = pyo.Constraint(expr= model.x + model.r * model.d2 >= -100)
    model.cL_second_stage = pyo.Constraint(expr= model.d3 >= -model.r)
    model.cU_second_stage = pyo.Constraint(expr= model.r <= 0)

    # exercise more of the code by making this an indexed
    # block
    model.p_second_stage = pyo.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)