def test_two_stage(self): G = networkx.DiGraph() G.add_node("Root") G.add_node("Child1") G.add_edge("Root", "Child1", weight=0.8) G.add_node("Child2") G.add_edge("Root", "Child2", weight=0.2) model = ScenarioTreeModelFromNetworkX(G) self.assertEqual(sorted(list(model.Stages)), sorted(["Stage1", "Stage2"])) self.assertEqual(sorted(list(model.Nodes)), sorted(["Root", "Child1", "Child2"])) self.assertEqual(sorted(list(model.Children["Root"])), sorted(["Child1", "Child2"])) self.assertEqual(sorted(list(model.Children["Child1"])), sorted([])) self.assertEqual(sorted(list(model.Children["Child2"])), sorted([])) self.assertEqual(sorted(list(model.Scenarios)), sorted(["Child1", "Child2"])) self.assertEqual(model.ConditionalProbability["Root"], 1.0) self.assertEqual(model.ConditionalProbability["Child1"], 0.8) self.assertEqual(model.ConditionalProbability["Child2"], 0.2) model.StageCost["Stage1"] = "c1" model.StageCost["Stage2"] = "c2" model.StageVariables["Stage1"].add("x") self.assertEqual(model.Bundling.value, False) self.assertEqual(list(model.Bundles), []) self.assertEqual(len(model.BundleScenarios), 0) ScenarioTree(scenariotreeinstance=model)
def test_not_directed(self): G = networkx.Graph() G.add_node("1") G.add_node("2") G.add_edge("1", "2") with self.assertRaises(TypeError): ScenarioTreeModelFromNetworkX(G)
def test_bad_custom_stage_names1(self): G = networkx.DiGraph() G.add_node("R", ) G.add_node("C1") G.add_edge("R", "C1", weight=1.0) with self.assertRaises(ValueError): ScenarioTreeModelFromNetworkX(G, stage_names=["Stage1"])
def test_missing_scenario_name(self): G = networkx.DiGraph() G.add_node("R", name="Root") G.add_node("C") G.add_edge("R", "C", weight=1) with self.assertRaises(KeyError): ScenarioTreeModelFromNetworkX(G, scenario_name_attribute="name")
def pysp_scenario_tree_model_callback(): from pyomo.pysp.scenariotree.tree_structure_model import ScenarioTreeModelFromNetworkX ## build a decision tree object Tree_Graph = DT(dec_GLOBAL,MD_GLOBAL) builtins.Tree_Graph_GLOBAL = Tree_Graph ### Initialize scenario tree model try: stm = ScenarioTreeModelFromNetworkX(Tree_Graph.G,edge_probability_attribute='probability', stage_names = Tree_Graph.stage_names) except: pdb.set_trace() ### Define Variables for each Node for N in Tree_Graph.G.node: if N.startswith('s') or N.startswith('pn'): stm.NodeVariables[N].add(str(N)+'VarXX') stm.NodeDerivedVariables[N].add(str(N)+'VarY') stm.NodeDerivedVariables[N].add(str(N)+'VarZ') else: ###NAC Constrained Decision Variables stm.NodeVariables[N].add(str(N)+'VarX') stm.NodeVariables[N].add(str(N)+'VarXX') stm.NodeDerivedVariables[N].add(str(N)+'VarY') stm.NodeDerivedVariables[N].add(str(N)+'VarZ') for stage_name in Tree_Graph.stage_names: stm.StageCost[stage_name] = str(stage_name) + "StageCost" return stm
def test_bad_weight2(self): G = networkx.DiGraph() G.add_node("R") G.add_node("C1") G.add_edge("R", "C1", weight=0.8) G.add_node("C2") G.add_edge("R", "C2", weight=0.1) with self.assertRaises(ValueError): ScenarioTreeModelFromNetworkX(G)
def __init__(self, scenario_creator, tree_model=None, scenarios_dir=None, scenario_creator_callback_name=None, tree_model_callback_name=None): ## first, attempt to determine abstract vs concrete ## and get a scenario instance creator ## if callable, a instance creator if callable(scenario_creator): self.pysp_instance_creator = scenario_creator self.abstract = False else: ## else, either and abstract model or a module with a callback if scenario_creator_callback_name is None: scenario_creator_callback_name = 'pysp_instance_creation_callback' module = import_file(scenario_creator) if hasattr(module, scenario_creator_callback_name): self.pysp_instance_creator = \ getattr(module, scenario_creator_callback_name) self.abstract = False else: self.pysp_instance_creator = module.model.create_instance self.abstract = True ## attempt to find and construct a tree model if tree_model is None: if tree_model_callback_name is None: tree_model_callback_name = 'pysp_scenario_tree_model_callback' tree_maker = getattr(module, tree_model_callback_name) tree_model = tree_maker() ## if we get a *.dat file, assume the scenarios are here unless ## otherwise specified if isinstance(tree_model, str): self.tree_model = CreateAbstractScenarioTreeModel(\ ).create_instance(tree_model) self.scenarios_dir = os.path.dirname(tree_model) elif hasnetworkx and isinstance(tree_model, networkx.DiGraph): self.tree_model = ScenarioTreeModelFromNetworkX(tree_model) elif isinstance(tree_model, pyo.ConcreteModel): self.tree_model = tree_model else: raise RuntimeError("Type of tree_model {} unrecongized".format( type(tree_model))) ## set the scenarios_dir if specified, but complain if ## we don't have an abstract model if scenarios_dir is not None: if not self.abstract: raise RuntimeError("An abstract model is required for " "scenarios_dir") self.scenarios_dir = scenarios_dir self._init()
def test_two_stage_more_node_attributes(self): G = networkx.DiGraph() G.add_node("Root", cost="c1", variables=["x"], derived_variables=["y"]) G.add_node("Child1", cost="c2", variables=["q"], derived_variables=["z"]) G.add_edge("Root", "Child1", weight=0.8) G.add_node("Child2", cost="c2", variables=["q"], derived_variables=["z"]) G.add_edge("Root", "Child2", weight=0.2) model = ScenarioTreeModelFromNetworkX(G) self.assertEqual(sorted(list(model.Stages)), sorted(["Stage1", "Stage2"])) self.assertEqual(sorted(list(model.Nodes)), sorted(["Root", "Child1", "Child2"])) self.assertEqual(sorted(list(model.Children["Root"])), sorted(["Child1", "Child2"])) self.assertEqual(sorted(list(model.Children["Child1"])), sorted([])) self.assertEqual(sorted(list(model.Children["Child2"])), sorted([])) self.assertEqual(sorted(list(model.Scenarios)), sorted(["Child1", "Child2"])) self.assertEqual(model.ConditionalProbability["Root"], 1.0) self.assertEqual(model.ConditionalProbability["Child1"], 0.8) self.assertEqual(model.ConditionalProbability["Child2"], 0.2) # FIXME: #300 on GitHub self.assertEqual(model.StageCost["Stage1"]._value, None) self.assertEqual(list(model.StageVariables["Stage1"]), []) self.assertEqual(list(model.StageDerivedVariables["Stage1"]), []) self.assertEqual(model.NodeCost["Root"].value, "c1") self.assertEqual(list(model.NodeVariables["Root"]), ["x"]) self.assertEqual(list(model.NodeDerivedVariables["Root"]), ["y"]) # FIXME: #300 on GitHub self.assertEqual(model.StageCost["Stage2"]._value, None) self.assertEqual(list(model.StageVariables["Stage2"]), []) self.assertEqual(list(model.StageDerivedVariables["Stage2"]), []) self.assertEqual(model.NodeCost["Child1"].value, "c2") self.assertEqual(list(model.NodeVariables["Child1"]), ["q"]) self.assertEqual(list(model.NodeDerivedVariables["Child1"]), ["z"]) self.assertEqual(model.NodeCost["Child2"].value, "c2") self.assertEqual(list(model.NodeVariables["Child2"]), ["q"]) self.assertEqual(list(model.NodeDerivedVariables["Child2"]), ["z"]) self.assertEqual(model.Bundling.value, False) self.assertEqual(list(model.Bundles), []) self.assertEqual(len(model.BundleScenarios), 0) ScenarioTree(scenariotreeinstance=model)
def test_unbalanced(self): G = networkx.DiGraph() G.add_node("R") G.add_node("0") G.add_node("1") G.add_edge("R", "0") G.add_edge("R", "1") G.add_node("00") G.add_node("01") G.add_edge("0", "00") G.add_edge("0", "01") model = ScenarioTreeModelFromNetworkX( G, edge_probability_attribute=None) self.assertEqual( sorted(list(model.Stages)), sorted(["Stage1", "Stage2", "Stage3"])) self.assertEqual( sorted(list(model.Nodes)), sorted(["R","0","1","00","01"])) self.assertEqual( sorted(list(model.Children["R"])), sorted(["0", "1"])) self.assertEqual( sorted(list(model.Children["0"])), sorted(["00","01"])) self.assertEqual( sorted(list(model.Children["1"])), sorted([])) self.assertEqual( sorted(list(model.Children["00"])), sorted([])) self.assertEqual( sorted(list(model.Children["01"])), sorted([])) self.assertEqual( sorted(list(model.Scenarios)), sorted(["00", "01", "1"])) self.assertEqual(model.ConditionalProbability["R"], 1.0) self.assertEqual(model.ConditionalProbability["0"], 0.5) self.assertEqual(model.ConditionalProbability["1"], 0.5) self.assertEqual(model.ConditionalProbability["00"], 0.5) self.assertEqual(model.ConditionalProbability["01"], 0.5) model.StageCost["Stage1"] = "c1" model.StageCost["Stage2"] = "c2" model.StageCost["Stage3"] = "c3" model.StageVariables["Stage1"].add("x") model.StageVariables["Stage2"].add("x") self.assertEqual(model.Bundling.value, False) self.assertEqual(list(model.Bundles), []) self.assertEqual(len(model.BundleScenarios), 0) ScenarioTree(scenariotreeinstance=model)
def simple_threestage_scenario_tree(): from pyomo.pysp.scenariotree.tree_structure_model \ import CreateConcreteTwoStageScenarioTreeModel import networkx G = networkx.balanced_tree(2, 2, networkx.DiGraph()) st_model = ScenarioTreeModelFromNetworkX(G, edge_probability_attribute=None) first_stage = st_model.Stages.first() second_stage = st_model.Stages.next(first_stage) third_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') # Third Stage st_model.StageCost[thrid_stage] = 'StageCost[3]' st_model.StageVariables[second_stage].add('z') return st_model