def pysp_scenario_tree_model_callback(): from pyomo.pysp.scenariotree.tree_structure_model \ import CreateConcreteTwoStageScenarioTreeModel st_model = CreateConcreteTwoStageScenarioTreeModel(scenarios) first_stage = st_model.Stages.first() second_stage = st_model.Stages.last() # First Stage st_model.StageCost[first_stage] = 'FirstStageCost' st_model.StageVariables[first_stage].add('k1') st_model.StageVariables[first_stage].add('k2') st_model.StageVariables[first_stage].add('E1') st_model.StageVariables[first_stage].add('E2') # Second Stage st_model.StageCost[second_stage] = 'SecondStageCost' # st_model.StageVariables[second_stage].add('Ca[*]') # st_model.StageVariables[second_stage].add('Cb[*]') # st_model.StageVariables[second_stage].add('Cc[*]') # st_model.StageVariables[second_stage].add('Tr[*]') #st_model.StageVariables[second_stage].add('QuantitySuperQuotaSold[*]') #st_model.StageVariables[second_stage].add('QuantityPurchased[*]') return st_model
def test_singletonblock_noindextemplate(self): st_model = CreateConcreteTwoStageScenarioTreeModel(1) st_model.StageVariables['Stage1'].add("b1") st_model.StageDerivedVariables['Stage1'].add("b2") st_model.NodeVariables['RootNode'].add("b3") st_model.NodeDerivedVariables['RootNode'].add("b4") st_model.StageCost['Stage1'] = "FirstStageCost" st_model.StageCost['Stage2'] = "SecondStageCost" scenario_tree = ScenarioTree(scenariotreeinstance=st_model) self.assertEqual(len(scenario_tree.stages), 2) self.assertEqual(len(scenario_tree.nodes), 2) self.assertEqual(len(scenario_tree.scenarios), 1) model = self._get_block_model() scenario_tree.linkInInstances({'Scenario1': model}) root = scenario_tree.findRootNode() self.assertEqual(len(root._variable_ids), 12) self.assertEqual(len(root._standard_variable_ids), 6) self.assertEqual(len(root._derived_variable_ids), 6) for name in ("b1.x", "b2.x", "b3.x", "b4.x"): for index in [None]: self.assertEqual( (name,index) in root._name_index_to_id, True) for name in ("b1.X", "b2.X", "b3.X", "b4.X"): for index in model.s: self.assertEqual( (name,index) in root._name_index_to_id, True)
def test_indexedblock_wildcardtemplate(self): st_model = CreateConcreteTwoStageScenarioTreeModel(1) st_model.StageVariables['Stage1'].add("B1[*]") st_model.StageDerivedVariables['Stage1'].add("B2[*]") st_model.NodeVariables['RootNode'].add("B3[*]") st_model.NodeDerivedVariables['RootNode'].add("B4[*]") st_model.StageCost['Stage1'] = "FirstStageCost" st_model.StageCost['Stage2'] = "SecondStageCost" scenario_tree = ScenarioTree(scenariotreeinstance=st_model) self.assertEqual(len(scenario_tree.stages), 2) self.assertEqual(len(scenario_tree.nodes), 2) self.assertEqual(len(scenario_tree.scenarios), 1) model = self._get_block_model() scenario_tree.linkInInstances({'Scenario1': model}) root = scenario_tree.findRootNode() self.assertEqual(len(root._variable_ids), 24) self.assertEqual(len(root._standard_variable_ids), 12) self.assertEqual(len(root._derived_variable_ids), 12) for name in ("B1[1].x", "B1[2].x", "B2[1].x", "B2[2].x", "B3[1].x", "B3[2].x", "B4[1].x", "B4[2].x"): for index in [None]: self.assertEqual((name, index) in root._name_index_to_id, True) for name in ("B1[1].X", "B1[2].X", "B2[1].X", "B2[2].X", "B3[1].X", "B3[2].X", "B4[1].X", "B4[2].X"): for index in model.s: self.assertEqual((name, index) in root._name_index_to_id, True)
def test_singletonblock_noindextemplate(self): st_model = CreateConcreteTwoStageScenarioTreeModel(1) st_model.StageVariables['Stage1'].add("b1") st_model.StageDerivedVariables['Stage1'].add("b2") st_model.NodeVariables['RootNode'].add("b3") st_model.NodeDerivedVariables['RootNode'].add("b4") st_model.StageCost['Stage1'] = "FirstStageCost" st_model.StageCost['Stage2'] = "SecondStageCost" scenario_tree = ScenarioTree(scenariotreeinstance=st_model) self.assertEqual(len(scenario_tree.stages), 2) self.assertEqual(len(scenario_tree.nodes), 2) self.assertEqual(len(scenario_tree.scenarios), 1) model = self._get_block_model() scenario_tree.linkInInstances({'Scenario1': model}) root = scenario_tree.findRootNode() self.assertEqual(len(root._variable_ids), 12) self.assertEqual(len(root._standard_variable_ids), 6) self.assertEqual(len(root._derived_variable_ids), 6) for name in ("b1.x", "b2.x", "b3.x", "b4.x"): for index in [None]: self.assertEqual((name, index) in root._name_index_to_id, True) for name in ("b1.X", "b2.X", "b3.X", "b4.X"): for index in model.s: self.assertEqual((name, index) in root._name_index_to_id, True)
def test_singletonvar_wildcardtemplate(self): st_model = CreateConcreteTwoStageScenarioTreeModel(1) st_model.StageVariables['Stage1'].add("x[*]") st_model.StageDerivedVariables['Stage1'].add("y[*]") st_model.NodeVariables['RootNode'].add("z[*]") st_model.NodeDerivedVariables['RootNode'].add("q[*]") st_model.StageCost['Stage1'] = "FirstStageCost" st_model.StageCost['Stage2'] = "SecondStageCost" scenario_tree = ScenarioTree(scenariotreeinstance=st_model) self.assertEqual(len(scenario_tree.stages), 2) self.assertEqual(len(scenario_tree.nodes), 2) self.assertEqual(len(scenario_tree.scenarios), 1) model = ConcreteModel() model.x = Var() model.y = Var() model.z = Var() model.q = Var() model.FirstStageCost = Expression(expr=0.0) model.SecondStageCost = Expression(expr=0.0) model.obj = Objective(expr=0.0) scenario_tree.linkInInstances({'Scenario1': model}) root = scenario_tree.findRootNode() self.assertEqual(len(root._variable_ids), 4) self.assertEqual(len(root._standard_variable_ids), 2) self.assertEqual(len(root._derived_variable_ids), 2) for name in ("x", "y", "z", "q"): for index in [None]: self.assertEqual((name, index) in root._name_index_to_id, True)
def pysp_scenario_tree_model_callback(): from pyomo.pysp.scenariotree.tree_structure_model import \ CreateConcreteTwoStageScenarioTreeModel st_model = CreateConcreteTwoStageScenarioTreeModel(num_scenarios) first_stage = st_model.Stages.first() second_stage = st_model.Stages.last() # First Stage st_model.StageCost[first_stage] = 'FirstStageCost' st_model.StageVariables[first_stage].add('x1') st_model.StageVariables[first_stage].add('x2') st_model.StageVariables[first_stage].add('x3') # Second Stage st_model.StageCost[second_stage] = 'SecondStageCost' st_model.StageVariables[second_stage].add('f11') st_model.StageVariables[second_stage].add('f12') st_model.StageVariables[second_stage].add('f21') st_model.StageVariables[second_stage].add('f22') st_model.StageVariables[second_stage].add('f31') st_model.StageVariables[second_stage].add('f32') st_model.StageVariables[second_stage].add('s1') st_model.StageVariables[second_stage].add('s2') st_model.StageVariables[second_stage].add('s3') # st_model.StageVariables[second_stage].add('d1_rhs') # st_model.StageVariables[second_stage].add('d2_rhs') # st_model.StageVariables[second_stage].add('d3_rhs') return st_model
def pysp_scenario_tree_model_callback(): from pyomo.pysp.scenariotree.tree_structure_model import \ CreateConcreteTwoStageScenarioTreeModel st_model = CreateConcreteTwoStageScenarioTreeModel(num_scenarios) first_stage = st_model.Stages.first() second_stage = st_model.Stages.last() # First Stage st_model.StageCost[first_stage] = 'FirstStageCost' st_model.StageVariables[first_stage].add('x1') st_model.StageVariables[first_stage].add('x2') # Second Stage st_model.StageCost[second_stage] = 'SecondStageCost' st_model.StageVariables[second_stage].add('v1') st_model.StageVariables[second_stage].add('v2') st_model.StageVariables[second_stage].add('u1') st_model.StageVariables[second_stage].add('u2') st_model.StageVariables[second_stage].add('w11') st_model.StageVariables[second_stage].add('w12') st_model.StageVariables[second_stage].add('w22') return st_model
def test_singletonvar_wildcardtemplate(self): st_model = CreateConcreteTwoStageScenarioTreeModel(1) st_model.StageVariables['Stage1'].add("x[*]") st_model.StageDerivedVariables['Stage1'].add("y[*]") st_model.NodeVariables['RootNode'].add("z[*]") st_model.NodeDerivedVariables['RootNode'].add("q[*]") st_model.StageCost['Stage1'] = "FirstStageCost" st_model.StageCost['Stage2'] = "SecondStageCost" scenario_tree = ScenarioTree(scenariotreeinstance=st_model) self.assertEqual(len(scenario_tree.stages), 2) self.assertEqual(len(scenario_tree.nodes), 2) self.assertEqual(len(scenario_tree.scenarios), 1) model = ConcreteModel() model.x = Var() model.y = Var() model.z = Var() model.q = Var() model.FirstStageCost = Expression(expr=0.0) model.SecondStageCost = Expression(expr=0.0) model.obj = Objective(expr=0.0) scenario_tree.linkInInstances({'Scenario1': model}) root = scenario_tree.findRootNode() self.assertEqual(len(root._variable_ids), 4) self.assertEqual(len(root._standard_variable_ids), 2) self.assertEqual(len(root._derived_variable_ids), 2) for name in ("x", "y", "z", "q"): for index in [None]: self.assertEqual( (name,index) in root._name_index_to_id, True)
def test_indexedvar_noindextemplate(self): st_model = CreateConcreteTwoStageScenarioTreeModel(1) st_model.StageVariables['Stage1'].add("x") st_model.StageDerivedVariables['Stage1'].add("y") st_model.NodeVariables['RootNode'].add("z") st_model.NodeDerivedVariables['RootNode'].add("q") st_model.StageCost['Stage1'] = "FirstStageCost" st_model.StageCost['Stage2'] = "SecondStageCost" scenario_tree = ScenarioTree(scenariotreeinstance=st_model) self.assertEqual(len(scenario_tree.stages), 2) self.assertEqual(len(scenario_tree.nodes), 2) self.assertEqual(len(scenario_tree.scenarios), 1) model = ConcreteModel() model.s = Set(initialize=[1,2,3]) model.x = Var(model.s) model.y = Var(model.s) model.z = Var(model.s) model.q = Var(model.s) model.FirstStageCost = Expression(expr=0.0) model.SecondStageCost = Expression(expr=0.0) model.obj = Objective(expr=0.0) scenario_tree.linkInInstances({'Scenario1': model}) root = scenario_tree.findRootNode() self.assertEqual(len(root._variable_ids), 12) self.assertEqual(len(root._standard_variable_ids), 6) self.assertEqual(len(root._derived_variable_ids), 6) for name in ("x", "y", "z", "q"): for index in model.s: self.assertEqual( (name,index) in root._name_index_to_id, True)
def test_indexedvar_noindextemplate(self): st_model = CreateConcreteTwoStageScenarioTreeModel(1) st_model.StageVariables['Stage1'].add("x") st_model.StageDerivedVariables['Stage1'].add("y") st_model.NodeVariables['RootNode'].add("z") st_model.NodeDerivedVariables['RootNode'].add("q") st_model.StageCost['Stage1'] = "FirstStageCost" st_model.StageCost['Stage2'] = "SecondStageCost" scenario_tree = ScenarioTree(scenariotreeinstance=st_model) self.assertEqual(len(scenario_tree.stages), 2) self.assertEqual(len(scenario_tree.nodes), 2) self.assertEqual(len(scenario_tree.scenarios), 1) model = ConcreteModel() model.s = Set(initialize=[1, 2, 3]) model.x = Var(model.s) model.y = Var(model.s) model.z = Var(model.s) model.q = Var(model.s) model.FirstStageCost = Expression(expr=0.0) model.SecondStageCost = Expression(expr=0.0) model.obj = Objective(expr=0.0) scenario_tree.linkInInstances({'Scenario1': model}) root = scenario_tree.findRootNode() self.assertEqual(len(root._variable_ids), 12) self.assertEqual(len(root._standard_variable_ids), 6) self.assertEqual(len(root._derived_variable_ids), 6) for name in ("x", "y", "z", "q"): for index in model.s: self.assertEqual((name, index) in root._name_index_to_id, True)
def simple_twostage_scenario_tree(): from pyomo.pysp.scenariotree.tree_structure_model import CreateConcreteTwoStageScenarioTreeModel st_model = CreateConcreteTwoStageScenarioTreeModel(2) first_stage = st_model.Stages.first() second_stage = st_model.Stages.last() # First Stage st_model.StageCost[first_stage] = "StageCost[1]" st_model.StageVariables[first_stage].add("x") # Second Stage st_model.StageCost[second_stage] = "StageCost[2]" st_model.StageVariables[second_stage].add("y") return st_model
def simple_twostage_scenario_tree(): from pyomo.pysp.scenariotree.tree_structure_model \ import CreateConcreteTwoStageScenarioTreeModel st_model = CreateConcreteTwoStageScenarioTreeModel(2) first_stage = st_model.Stages.first() second_stage = st_model.Stages.last() # First Stage st_model.StageCost[first_stage] = 'StageCost[1]' st_model.StageVariables[first_stage].add('x') # Second Stage st_model.StageCost[second_stage] = 'StageCost[2]' st_model.StageVariables[second_stage].add('y') return st_model
def pysp_scenario_tree_model_callback(): from pyomo.pysp.scenariotree.tree_structure_model \ import CreateConcreteTwoStageScenarioTreeModel st_model = CreateConcreteTwoStageScenarioTreeModel(3) first_stage = st_model.Stages.first() second_stage = st_model.Stages.last() # First Stage st_model.StageCost[first_stage] = 'StageCost[1]' st_model.StageVariables[first_stage].add('x') st_model.StageDerivedVariables[first_stage].add('y') # Second Stage st_model.StageCost[second_stage] = 'StageCost[2]' st_model.StageVariables[second_stage].add('z') st_model.StageDerivedVariables[second_stage].add('q') return st_model
def pysp_scenario_tree_model_callback(): from pyomo.pysp.scenariotree.tree_structure_model \ import CreateConcreteTwoStageScenarioTreeModel st_model = CreateConcreteTwoStageScenarioTreeModel(scenarios) first_stage = st_model.Stages.first() second_stage = st_model.Stages.last() # First Stage st_model.StageCost[first_stage] = 'FirstStageCost' st_model.StageVariables[first_stage].add('DevotedAcreage[*]') # Second Stage st_model.StageCost[second_stage] = 'SecondStageCost' st_model.StageVariables[second_stage].add('QuantitySubQuotaSold[*]') st_model.StageVariables[second_stage].add('QuantitySuperQuotaSold[*]') st_model.StageVariables[second_stage].add('QuantityPurchased[*]') return st_model
def pysp_scenario_tree_model_callback(): scenarios=len(ScData) from pyomo.pysp.scenariotree.tree_structure_model \ import CreateConcreteTwoStageScenarioTreeModel st_model = CreateConcreteTwoStageScenarioTreeModel(scenarios) first_stage = st_model.Stages.first() second_stage = st_model.Stages.last() # First Stage definitions st_model.StageCost[first_stage] = 'SV[0]' st_model.StageCost[second_stage]= 'SS[1]' #Stage Variables for i in t1: #First stage variable st_model.StageVariables[first_stage].add('x['+str(i)+']') for i in t2: #Second stage variable st_model.StageVariables[second_stage].add('x['+str(i)+']') #Scenario Probability st_model.ConditionalProbability['RootNode']=1 for i in range(0, len(ScData)): st_model.ConditionalProbability['LeafNode_Scenario'+str(i+1)]=Pro[i] return st_model
def pysp_scenario_tree_model_callback(): from pyomo.pysp.scenariotree.tree_structure_model import \ CreateConcreteTwoStageScenarioTreeModel st_model = CreateConcreteTwoStageScenarioTreeModel(num_scenarios) first_stage = st_model.Stages.first() second_stage = st_model.Stages.last() # First Stage st_model.StageCost[first_stage] = 'FirstStageCost' st_model.StageVariables[first_stage].add('delta1') st_model.StageVariables[first_stage].add('delta2') st_model.StageVariables[first_stage].add('delta3') st_model.StageVariables[first_stage].add('delta4') #st_model.StageVariables[first_stage].add('delta5') # Second Stage st_model.StageCost[second_stage] = 'SecondStageCost' st_model.StageVariables[second_stage].add('y1') st_model.StageVariables[second_stage].add('y2') st_model.StageVariables[second_stage].add('y3') st_model.StageVariables[second_stage].add('y4') st_model.StageVariables[second_stage].add('y5') st_model.StageVariables[second_stage].add('z1') st_model.StageVariables[second_stage].add('z2') st_model.StageVariables[second_stage].add('z3') st_model.StageVariables[second_stage].add('z4') st_model.StageVariables[second_stage].add('z5') st_model.StageVariables[second_stage].add('x1') st_model.StageVariables[second_stage].add('x2') st_model.StageVariables[second_stage].add('x3') st_model.StageVariables[second_stage].add('x4') st_model.StageVariables[second_stage].add('x5') return st_model
def test_indexedblock_wildcardtemplate(self): st_model = CreateConcreteTwoStageScenarioTreeModel(1) st_model.StageVariables['Stage1'].add("B1[*]") st_model.StageDerivedVariables['Stage1'].add("B2[*]") st_model.NodeVariables['RootNode'].add("B3[*]") st_model.NodeDerivedVariables['RootNode'].add("B4[*]") st_model.StageCost['Stage1'] = "FirstStageCost" st_model.StageCost['Stage2'] = "SecondStageCost" scenario_tree = ScenarioTree(scenariotreeinstance=st_model) self.assertEqual(len(scenario_tree.stages), 2) self.assertEqual(len(scenario_tree.nodes), 2) self.assertEqual(len(scenario_tree.scenarios), 1) model = self._get_block_model() scenario_tree.linkInInstances({'Scenario1': model}) root = scenario_tree.findRootNode() self.assertEqual(len(root._variable_ids), 24) self.assertEqual(len(root._standard_variable_ids), 12) self.assertEqual(len(root._derived_variable_ids), 12) for name in ("B1[1].x", "B1[2].x", "B2[1].x", "B2[2].x", "B3[1].x", "B3[2].x", "B4[1].x", "B4[2].x"): for index in [None]: self.assertEqual( (name,index) in root._name_index_to_id, True) for name in ("B1[1].X", "B1[2].X", "B2[1].X", "B2[2].X", "B3[1].X", "B3[2].X", "B4[1].X", "B4[2].X"): for index in model.s: self.assertEqual( (name,index) in root._name_index_to_id, True)
from pyomo.pysp.scenariotree.tree_structure_model import \ CreateConcreteTwoStageScenarioTreeModel model = CreateConcreteTwoStageScenarioTreeModel(3) #### helps with testing across python versions for key in model.ConditionalProbability: if key != "RootNode": model.ConditionalProbability[key] = 0.3333333333 ##### first_stage = model.Stages.first() second_stage = model.Stages.last() # First Stage model.StageCost[first_stage] = 'StageCost[1]' model.StageVariables[first_stage].add('x') model.StageDerivedVariables[first_stage].add('y') model.StageDerivedVariables[first_stage].add('fx') model.StageDerivedVariables[first_stage].add('p_first_stage') # Second Stage model.StageCost[second_stage] = 'StageCost[2]' model.StageVariables[second_stage].add('z') model.StageDerivedVariables[second_stage].add('q') model.StageDerivedVariables[second_stage].add('fz') model.StageDerivedVariables[second_stage].add('r') model.StageDerivedVariables[second_stage].add('p_second_stage')