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
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."))
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)
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)
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))