Exemple #1
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:
         pyomo.pysp.convert.smps.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 "
          "pyomo.pysp.embeddedsp.TableDistribution."))
Exemple #2
0
 def test_UniformDistrubtion(self):
     d = UniformDistribution(1,10)
     v = d.sample()
     self.assertTrue(1 <= v <= 10)
Exemple #3
0
#     and stochastic parameters.
# (2) Obtains a solution using the deterministic
#     approximation, where stochastic parameters
#     are set to the expected value of their
#     distribution.
# (3) Generates an "external" SP by sampling from
#     the EmbeddedSP and uses DDSIP solver interface.
#     The DDSIP solver requires explicit annotations
#     that describe the locations of the stochastic data.
#     These annotations are automatically added when
#     the SP is created from an EmbeddedSP object.
# (4) The EmbeddedSP object is then re-sampled to
#     creating a test set that is used to evaluate
#     the solutions from steps (2) and (3).

d_dist = UniformDistribution(0, 100)
train_N = 150
test_N = 150
cplex = SolverFactory("cplex")
saa_solver = SPSolverFactory("ddsip")  # "ef"

#
# Define the reference model
#
model = ConcreteModel()
model.x = Var(domain=Integers)
model.y = Var(domain=Integers)
model.t = Var(domain=Integers)
model.c = 1.0
model.b = 1.5
model.h = 0.1
Exemple #4
0
 def test_UniformDistrubtion(self):
     d = UniformDistribution(1, 10)
     v = d.sample()
     self.assertTrue(1 <= v <= 10)
Exemple #5
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))