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_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 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 simple_twostage_scenario_tree(): from 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 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(): from 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
from 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')