def pysp_instance_creation_callback(scenario_name, node_names): model = create_instance(scenario_name) # # SMPS Related Annotations # model.constraint_stage = PySP_ConstraintStageAnnotation() model.constraint_stage.declare(model.c_first_stage, 1) model.constraint_stage.declare(model.p_first_stage, 1) model.constraint_stage.declare(model.c_second_stage, 2) model.constraint_stage.declare(model.r_second_stage, 2) model.constraint_stage.declare(model.p_second_stage, 2) # The difficulty with Piecewise is that it hides the # structure of the underlying constraints (there may be # more than one). It doesn't seem possible to direct a # modeler on how to go about tagging specific # constraints. For this reason, we allow the # PySP_StochasticRHS and PySP_StochasticMatrix suffixes # to contain entries for entire blocks, where we # interpret this as meaning all rhs and constraint # matrix entries should be treated as stochastic. model.stoch_rhs = PySP_StochasticRHSAnnotation() model.stoch_rhs.declare(model.p_second_stage) model.stoch_rhs.declare(model.r_second_stage) model.stoch_matrix = PySP_StochasticMatrixAnnotation() # exercise more of the code by testing this with an # indexed block and a single block model.stoch_matrix.declare(model.c_second_stage, variables=[model.r]) model.stoch_matrix.declare(model.p_second_stage[1]) model.stoch_objective = PySP_StochasticObjectiveAnnotation() return model
def test_deprecated(self): self.assertIs(StageCostAnnotation, type(PySP_StageCostAnnotation())) self.assertIs(VariableStageAnnotation, type(PySP_VariableStageAnnotation())) self.assertIs(_ConstraintStageAnnotation, type(PySP_ConstraintStageAnnotation())) self.assertIs(_ConstraintStageAnnotation, type(ConstraintStageAnnotation())) self.assertIs(StochasticDataAnnotation, type(PySP_StochasticDataAnnotation())) self.assertIs(StochasticConstraintBoundsAnnotation, type(PySP_StochasticRHSAnnotation())) self.assertIs(StochasticConstraintBodyAnnotation, type(PySP_StochasticMatrixAnnotation())) self.assertIs(StochasticObjectiveAnnotation, type(PySP_StochasticObjectiveAnnotation()))
def declare_annotations_rule(model): # # Annotate constraint stages # model.constraint_stage = PySP_ConstraintStageAnnotation() # first-stage constraints model.constraint_stage.declare(model.ConstrainTotalAcreage, 1) # second-stage constraints model.constraint_stage.declare(model.EnforceQuotas, 2) model.constraint_stage.declare(model.EnforceCattleFeedRequirement, 2) model.constraint_stage.declare(model.LimitAmountSold, 2) # # Annotate stochastic constraint matrix coefficients # model.stoch_matrix = PySP_StochasticMatrixAnnotation() # DevotedAcreage[i] has a stochastic coefficient in two constraints for i in model.CROPS: model.stoch_matrix.declare(model.EnforceCattleFeedRequirement[i], variables=[model.DevotedAcreage[i]]) model.stoch_matrix.declare(model.LimitAmountSold[i], variables=[model.DevotedAcreage[i]])
from pyomo.pysp.annotations import (PySP_ConstraintStageAnnotation, PySP_StochasticRHSAnnotation) # # Import the reference model # from baa99_basemodel import model # # Annotate the model to enable SMPS conversion # of the explicit scenario tree defined later # in this file # model.constraint_stage = PySP_ConstraintStageAnnotation() model.constraint_stage.declare(model.s1, 2) model.constraint_stage.declare(model.s2, 2) model.constraint_stage.declare(model.d1, 2) model.constraint_stage.declare(model.d2, 2) model.stoch_rhs = PySP_StochasticRHSAnnotation() model.stoch_rhs.declare(model.d1) model.stoch_rhs.declare(model.d2) # # Define the scenario tree and provide a scenario instance # creation callback # num_scenarios = len(model.d1_rhs_table) * len(model.d2_rhs_table) scenario_data = dict(
def pysp_instance_creation_callback(scenario_name, node_names): cnt = d[scenario_name] model = ConcreteModel() # first stage model.x = Var(bounds=(0, 10)) # first stage derived model.y = Expression(expr=model.x + 1) model.fx = Var() # second stage model.z = Var(bounds=(-10, 10)) # second stage derived model.q = Expression(expr=model.z**2) model.fz = Var() model.r = Var() # stage costs model.StageCost = Expression([1, 2]) model.StageCost.add(1, model.fx) model.StageCost.add(2, -model.fz + model.r - cnt) model.o = Objective(expr=summation(model.StageCost)) model.ZERO = Expression(expr=0) if cnt == 0: cnt = model.ZERO model.c_first_stage = 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 = 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 = Constraint(expr=model.x + model.r * cnt >= -100) model.r_second_stage = Constraint(expr=-cnt <= model.r <= 0) # exercise more of the code by making this an indexed # block model.p_second_stage = 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., 2. + cnt, 1.], force_pw=True) # # SMPS Related Annotations # model.constraint_stage = PySP_ConstraintStageAnnotation() model.constraint_stage.declare(model.c_first_stage, 1) model.constraint_stage.declare(model.p_first_stage, 1) model.constraint_stage.declare(model.c_second_stage, 2) model.constraint_stage.declare(model.r_second_stage, 2) model.constraint_stage.declare(model.p_second_stage, 2) # The difficulty with Piecewise is that it hides the # structure of the underlying constraints (there may be # more than one). It doesn't seem possible to direct a # modeler on how to go about tagging specific # constraints. For this reason, we allow the # PySP_StochasticRHS and PySP_StochasticMatrix suffixes # to contain entries for entire blocks, where we # interpret this as meaning all rhs and constraint # matrix entries should be treated as stochastic. model.stoch_rhs = PySP_StochasticRHSAnnotation() model.stoch_rhs.declare(model.p_second_stage) model.stoch_rhs.declare(model.r_second_stage) model.stoch_matrix = PySP_StochasticMatrixAnnotation() model.stoch_objective = PySP_StochasticObjectiveAnnotation() # exercise more of the code by testing this with an # indexed block and a single block model.stoch_matrix.declare(model.c_second_stage, variables=[model.r]) model.stoch_matrix.declare(model.p_second_stage[1]) return model