def _create_scenario_tree_model(self, size): assert size > 0 stm = CreateAbstractScenarioTreeModel() stm.Stages.add('t1') stm.Stages.add('t2') stm.Nodes.add('root') for i in xrange(1, size+1): stm.Nodes.add('n'+str(i)) stm.Scenarios.add('s'+str(i)) stm = stm.create_instance() stm.NodeStage['root'] = 't1' stm.ConditionalProbability['root'] = 1.0 weight = 1.0/float(size) for i in xrange(1, size+1): node_name = 'n'+str(i) scen_name = 's'+str(i) stm.NodeStage[node_name] = 't2' stm.Children['root'].add(node_name) stm.Children[node_name].clear() stm.ConditionalProbability[node_name] = weight stm.ScenarioLeafNode[scen_name] = node_name stm.StageCost['t1'] = self._stage1_cost.name stm.StageCost['t2'] = self._stage2_cost.name for var, (stagenum, derived) in \ self._variable_stage_assignments.items(): stage_name = 't'+str(stagenum) if not derived: stm.StageVariables[stage_name].add(var.name) else: stm.StageDerivedVariables[stage_name].add(var.name) return stm
def _treemaker(scenlist): """Makes a scenario tree (avoids dependence on daps) Parameters ---------- scenlist (list of `int`): experiment (i.e. scenario) numbers Returns ------- a `ConcreteModel` that is the scenario tree """ num_scenarios = len(scenlist) m = CreateAbstractScenarioTreeModel() m.Stages.add('Stage1') m.Stages.add('Stage2') m.Nodes.add('RootNode') for i in scenlist: m.Nodes.add('LeafNode_Experiment' + str(i)) m.Scenarios.add('Experiment' + str(i)) m = m.create_instance() m.NodeStage['RootNode'] = 'Stage1' m.ConditionalProbability['RootNode'] = 1.0 for node in m.Nodes: if node != 'RootNode': m.NodeStage[node] = 'Stage2' m.Children['RootNode'].add(node) m.Children[node].clear() m.ConditionalProbability[node] = 1.0 / num_scenarios m.ScenarioLeafNode[node.replace('LeafNode_', '')] = node return m
def createScenarioTreeModel(self): """ Construct scenario tree based on abstract scenario tree model for stochastic programming @ In, None @ Out, treeModel, Instance, pyomo scenario tree model """ treeModel = CreateAbstractScenarioTreeModel() if pyoVersion: treeModel = treeModel.create_instance() treeModel.Stages.add('FirstStage') treeModel.Stages.add('SecondStage') treeModel.Nodes.add('RootNode') for i in self.scenarios['scenario_name']: leafNode = 'leaf_' + i treeModel.Nodes.add(leafNode) treeModel.Scenarios.add(i) if not pyoVersion: treeModel = treeModel.create_instance() treeModel.NodeStage['RootNode'] = 'FirstStage' treeModel.ConditionalProbability['RootNode'] = 1.0 for node in treeModel.Nodes: if node != 'RootNode': treeModel.NodeStage[node] = 'SecondStage' treeModel.Children['RootNode'].add(node) treeModel.Children[node].clear() treeModel.ConditionalProbability[node] = self.scenarios['probabilities'][node.replace('leaf_','')] treeModel.ScenarioLeafNode[node.replace('leaf_','')] = node return treeModel
def setUp(self): """ Get ready for tests""" ######## make a temp dir to which files can be copied ##### self.tdir = tempfile.mkdtemp() #TemporaryDirectory().name sys.path.insert(1, self.tdir) """ During debugging, local files might get in the way of finding the file in the temp dir, so we cd there.""" self.savecwd = os.getcwd() os.chdir(self.tdir) p = str(pyomoroot.__path__) l = p.find("'") r = p.find("'", l + 1) pyomorootpath = p[l + 1:r] farmpath = pyomorootpath + os.sep + ".." + os.sep + "examples" + \ os.sep + "pysp" + os.sep + "farmer" farmpath = os.path.abspath(farmpath) self.farmer_concrete_file = farmpath + os.sep + \ "concrete" + os.sep + "ReferenceModel.py" shutil.copyfile(self.farmer_concrete_file, self.tdir + os.sep + "ReferenceModel.py") abstract_tree = CreateAbstractScenarioTreeModel() shutil.copyfile( farmpath + os.sep + "scenariodata" + os.sep + "ScenarioStructure.dat", self.tdir + os.sep + "ScenarioStructure.dat") self.farmer_concrete_tree = \ abstract_tree.create_instance("ScenarioStructure.dat")
def test_init12(self): self.assertTrue("reference_test_model" not in sys.modules) scenario_tree_model = CreateAbstractScenarioTreeModel().\ create_instance( join(testdatadir, "reference_test_scenario_tree.dat")) def scenario_model_callback(scenario_tree, scenario_name, node_list): self.assertIs(scenario_tree, scenario_tree_model) instance = reference_test_model.create_instance() if scenario_name == "s1": instance.p = 1.0 elif scenario_name == "s2": instance.p = 2.0 else: assert scenario_name == "s3" instance.p = 3.0 return instance with ScenarioTreeInstanceFactory( model=scenario_model_callback, scenario_tree=scenario_tree_model) as factory: self.assertTrue(factory.model_directory() is None) self.assertTrue(factory.scenario_tree_directory() is None) self._check_factory(factory) self.assertEqual(factory._closed, True) self.assertEqual(len(factory._archives), 0)
def test_init7(self): self.assertTrue("reference_test_model" not in sys.modules) scenario_tree_model = CreateAbstractScenarioTreeModel().\ create_instance( join(testdatadir, "reference_test_scenario_tree.dat")) with self.assertRaises(ValueError): with ScenarioTreeInstanceFactory( model=reference_test_model, scenario_tree=scenario_tree_model) as factory: pass with ScenarioTreeInstanceFactory(model=reference_test_model, scenario_tree=scenario_tree_model, data=testdatadir) as factory: self.assertTrue(factory.model_directory() is None) self.assertTrue(factory.scenario_tree_directory() is None) self._check_factory(factory) self.assertEqual(factory._closed, True) with ScenarioTreeInstanceFactory( model=reference_test_model, scenario_tree=join( testdatadir, "reference_test_scenario_tree.dat")) as factory: self.assertTrue(factory.model_directory() is None) self.assertTrue(factory.scenario_tree_directory() is not None) self._check_factory(factory) self.assertEqual(len(factory._archives), 0)
def solve_sto(f, IndexReturn, Tbill): #The next two lines show one way to create a concrete scenario tree. There are #others that can be found in `pyomo.pysp.scenariotree.tree_structure_model`. abstract_tree = CreateAbstractScenarioTreeModel() concrete_tree = abstract_tree.create_instance("/Users/xiaoshiguo/Desktop/model"+str(J)+"/ScenarioStructure"+str(J)+".dat") concrete_tree.IndexReturn = IndexReturn # line added by DLW concrete_tree.Tbill = Tbill stsolver = rapper.StochSolver("ReferenceModel"+str(J)+".py", fsfct = "pysp_instance_creation_callback", tree_model = concrete_tree) #stsolver = rapper.StochSolver("/Users/xiaoshiguo/Desktop/portfolio/models/ReferenceModel.py", tree_model = concrete_tree) ef_sol = stsolver.solve_ef('glpk', tee=False) # ef_sol = stsolver.solve_ph(subsolver = solvername, default_rho = 1) if ef_sol.solver.termination_condition != TerminationCondition.optimal: print ("oops! not optimal:",ef_sol.solver.termination_condition) #There is an iterator to loop over the root node solution: for varname, varval in stsolver.root_Var_solution(): if varname == "weight": weight = varval else: eta = varval #print (varname, str(varval)) #a function to compute compute the objective function value obj = stsolver.root_E_obj() #print ("Expecatation take over scenarios=", obj) # write down scenario tree in testcref file #csvw.write_csv_soln(stsolver.scenario_tree, str(f)) return obj, weight, eta
def _generate_base_scenario_tree(self, model, variable_stage_assignments): stage_cost_annotation = locate_annotations(model, PySP_StageCostAnnotation, max_allowed=1) if len(stage_cost_annotation) == 0: raise ValueError("Reference model is missing stage cost " "annotation: %s" % (PySP_StageCostAnnotation.__name__)) else: assert len(stage_cost_annotation) == 1 stage_cost_annotation = stage_cost_annotation[0][1] stage_cost_assignments = ComponentMap( stage_cost_annotation.expand_entries()) stage1_cost = None stage2_cost = None for cdata, stagenum in stage_cost_assignments.items(): if stagenum == 1: stage1_cost = cdata elif stagenum == 2: stage2_cost = cdata if stage1_cost is None: raise ValueError("Missing stage cost annotation for time stage: 1") if stage2_cost is None: raise ValueError("Missing stage cost annotation for time stage: 2") assert stage1_cost != stage2_cost # # Create a dummy 1-scenario scenario tree # stm = CreateAbstractScenarioTreeModel() stm.Stages.add('Stage1') stm.Stages.add('Stage2') stm.Nodes.add('RootNode') stm.Nodes.add('LeafNode') stm.Scenarios.add('ReferenceScenario') stm = stm.create_instance() stm.NodeStage['RootNode'] = 'Stage1' stm.ConditionalProbability['RootNode'] = 1.0 stm.NodeStage['LeafNode'] = 'Stage2' stm.Children['RootNode'].add('LeafNode') stm.Children['LeafNode'].clear() stm.ConditionalProbability['LeafNode'] = 1.0 stm.ScenarioLeafNode['ReferenceScenario'] = 'LeafNode' stm.StageCost['Stage1'] = stage1_cost.name stm.StageCost['Stage2'] = stage2_cost.name for var, (stagenum, derived) in variable_stage_assignments.items(): stagelabel = 'Stage' + str(stagenum) if not derived: stm.StageVariables[stagelabel].add(var.name) else: stm.StageDerivedVariables[second_stage].add(var.name) scenario_tree = ScenarioTree(scenariotreeinstance=stm) scenario_tree.linkInInstances( {'ReferenceScenario': self.reference_model}) return scenario_tree
def _import_model_and_scenario_tree(self): model_import, module_name = \ load_external_module(self._model_filename, clear_cache=True) self._model_module = model_import dir_model_import = dir(model_import) self._model_object = None self._model_callback = None if "pysp_instance_creation_callback" in dir_model_import: callback = model_import.pysp_instance_creation_callback if not hasattr(callback, "__call__"): raise TypeError("'pysp_instance_creation_callback' object is " "not callable in model file: %s" % (self._model_filename)) self._model_callback = callback elif "model" in dir_model_import: model = model_import.model if not isinstance(model, (_BlockData, Block)): raise TypeError("'model' object has incorrect type " "in model file: %s" % (self._model_filename)) self._model_object = model else: raise AttributeError( "No 'model' or 'pysp_instance_creation_callback' " "object found in model file: %s" % (self._model_filename)) if self._scenario_tree_filename is None: assert self._scenario_tree_location is None assert self._scenario_tree_directory is None if self._model_object is not None: self._data_directory = self._model_directory if "pysp_scenario_tree_model_callback" in dir_model_import: callback = model_import.pysp_scenario_tree_model_callback if not hasattr(callback, "__call__"): raise TypeError( "'pysp_scenario_tree_model_callback' object is " "not callable in model file: %s" % (self._model_filename)) self._scenario_tree_instance = callback() if not isinstance(self._scenario_tree_instance, (_BlockData, Block)): raise TypeError( "'pysp_scenario_tree_model_callback' returned " "an object that is not of the correct type for " "a Pyomo model (e.g, _BockData, Block): %s" % (type(self._scenario_tree_instance))) else: raise ValueError( "No scenario tree file was given but no function " "named 'pysp_scenario_tree_model_callback' was " "found in the reference model file.") else: self._data_directory = self._scenario_tree_directory self._scenario_tree_instance = \ CreateAbstractScenarioTreeModel().\ create_instance(filename=self._scenario_tree_filename)
def pysp_scenario_tree_model_callback(): from pyomo.pysp.scenariotree.tree_structure_model import \ CreateAbstractScenarioTreeModel #Model st_model = CreateAbstractScenarioTreeModel() #---------------------------------------- #Basic Spectification #---------------------------------------- #Stages------------------- for i in Root.stages: st_model.Stages.add(i) #Nodes-------------------- for i in sorted(Case.tree.nodes()): st_model.Nodes.add(i) #Scenarios---------------- for i in sorted(Case.names): st_model.Scenarios.add(i) st_model = st_model.create_instance() #---------------------------------------- #Advanced Spectification #---------------------------------------- #Scenario Leaf Node for i in sorted(Case.names): st_model.ScenarioLeafNode[i] = Case.scenario_node[i] #Node attributes for i in st_model.Nodes: #Conditional probability st_model.ConditionalProbability[i] = Case.tree.node[i]['probability'] #Stage st_model.NodeStage[i] = Case.tree.node[i]['stage'] #Children Node for j in Case.tree.edges(i): st_model.Children[i].add(j[1]) #------------------------------------------- #Variable /Objective Spectification #------------------------------------------- for i in Root.variables: st_model.StageVariables[Root.st_to_var[i]].add('x[' + i + ']') for i in Root.stages: st_model.StageCost[i] = 'SC[' + i + ']' return st_model
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_init6(self): self.assertTrue("reference_test_model" not in sys.modules) scenario_tree_model = CreateAbstractScenarioTreeModel().\ create_instance( join(testdatadir, "reference_test_scenario_tree.dat")) with ScenarioTreeInstanceFactory( model=join(testdatadir, "reference_test_model.py"), scenario_tree=scenario_tree_model) as factory: self.assertTrue(factory.model_directory() is not None) self.assertTrue(factory.scenario_tree_directory() is None) self._check_factory(factory) with self.assertRaises(TypeError): with ScenarioTreeInstanceFactory(model=join( testdatadir, "reference_test_model.py"), scenario_tree=int) as f: pass with self.assertRaises(ValueError): with ScenarioTreeInstanceFactory(model=join( testdatadir, "reference_test_model.py"), scenario_tree=None) as f: pass with self.assertRaises(TypeError): with ScenarioTreeInstanceFactory( model=None, scenario_tree=scenario_tree_model) as f: pass with self.assertRaises(IOError): with ScenarioTreeInstanceFactory( model=join(testdatadir, "reference_test_model_does_not_exist.py"), scenario_tree=scenario_tree_model) as f: pass with self.assertRaises(ValueError): with ScenarioTreeInstanceFactory( model=join(testdatadir, "reference_test_model.py"), scenario_tree=CreateAbstractScenarioTreeModel()) as f: pass self.assertEqual(len(factory._archives), 0) self.assertTrue("reference_test_model" in sys.modules)
def test_init11(self): self.assertTrue("reference_test_model" not in sys.modules) scenario_tree_model = CreateAbstractScenarioTreeModel().\ create_instance( join(testdatadir, "reference_test_scenario_tree.dat")) scenario_tree_model.ScenarioBasedData = False with ScenarioTreeInstanceFactory(model=reference_test_model, scenario_tree=scenario_tree_model, data=testdatadir) as factory: self.assertTrue(factory.model_directory() is None) self.assertTrue(factory.scenario_tree_directory() is None) self._check_factory(factory) self.assertEqual(factory._closed, True) self.assertEqual(len(factory._archives), 0)
def test_init14(self): self.assertTrue("reference_test_model" not in sys.modules) scenario_tree_model = CreateAbstractScenarioTreeModel().\ create_instance( join(testdatadir, "reference_test_scenario_tree.dat")) with ScenarioTreeInstanceFactory(model=reference_test_model, scenario_tree=scenario_tree_model, data=testdatadir) as factory: scenario_tree = factory.generate_scenario_tree() self.assertEqual(factory._closed, True) self.assertEqual(len(factory._archives), 0) # start with a scenario tree (not a scenario tree model) with ScenarioTreeInstanceFactory(model=reference_test_model, scenario_tree=scenario_tree, data=testdatadir) as factory: self._check_factory(factory) self.assertEqual(factory._closed, True) self.assertEqual(len(factory._archives), 0)
def test_init13(self): model = reference_test_model.create_instance() scenario_tree_model = CreateAbstractScenarioTreeModel().\ create_instance( join(testdatadir, "reference_test_scenario_tree.dat")) with ScenarioTreeInstanceFactory( model=model, scenario_tree=scenario_tree_model) as factory: self.assertTrue(factory.model_directory() is None) self.assertTrue(factory.scenario_tree_directory() is None) scenario_tree = factory.generate_scenario_tree() instances = factory.construct_instances_for_scenario_tree( scenario_tree, verbose=True) self.assertEqual(len(instances), 3) self.assertEqual(instances["s1"].p(), model.p()) self.assertEqual(instances["s2"].p(), model.p()) self.assertEqual(instances["s3"].p(), model.p()) self.assertEqual(factory._closed, True) self.assertEqual(len(factory._archives), 0)
def _init(self, model, scenario_tree, data): self._model_filename = None self._model_module = None self._model_object = None self._model_callback = None if isinstance(model, six.string_types): logger.debug("A model filename was provided.") self._model_filename, self._archives = \ _extract_pathspec(model, "ReferenceModel.py", archives=self._archives) if not os.path.exists(self._model_filename): logger.error("Failed to extract reference model python file " "from path specification: %s" % (model)) raise IOError("path does not exist: %s" % (self._model_filename)) assert self._model_filename is not None assert self._model_filename.endswith(".py") (self._model_module, self._model_object, self._model_callback) = \ _find_reference_model_or_callback(self._model_filename) if (self._model_object is None) and \ (self._model_callback is None): raise AttributeError("No reference Pyomo model or " "'pysp_instance_creation_callback' " "function object found in src: %s" % (self._model_filename)) elif hasattr(model, "__call__"): logger.debug("A model callback function was provided.") self._model_callback = model else: if not isinstance(model, (_BlockData, Block)): raise TypeError( "model argument object has incorrect type: %s. " "Must be a string type, a callback, or a Pyomo model." % (type(model))) logger.debug("A model object was provided.") self._model_object = model self._scenario_tree_filename = None self._scenario_tree_model = None self._scenario_tree = None if isinstance(scenario_tree, ScenarioTree): for scenario in scenario_tree.scenarios: if scenario.instance is not None: raise ValueError( "The scenario tree can not be linked with instances") if hasattr(scenario_tree, "_scenario_instance_factory"): del scenario_tree._scenario_instance_factory self._scenario_tree = scenario_tree elif has_networkx and \ isinstance(scenario_tree, networkx.DiGraph): self._scenario_tree_model = scenario_tree elif isinstance(scenario_tree, six.string_types): logger.debug("scenario tree input is a string, attempting " "to load file specification: %s" % (scenario_tree)) self._scenario_tree_filename = None if not scenario_tree.endswith(".py"): self._scenario_tree_filename, self._archives = \ _extract_pathspec(scenario_tree, "ScenarioStructure.dat", archives=self._archives) if not os.path.exists(self._scenario_tree_filename): logger.debug("Failed to extract scenario tree structure " ".dat file from path specification: %s" % (scenario_tree)) self._scenario_tree_filename = None if self._scenario_tree_filename is None: self._scenario_tree_filename, self._archives = \ _extract_pathspec(scenario_tree, "ScenarioStructure.py", archives=self._archives) if not os.path.exists(self._scenario_tree_filename): logger.debug("Failed to locate scenario tree structure " ".py file with path specification: %s" % (scenario_tree)) self._scenario_tree_filename = None if self._scenario_tree_filename is None: raise ValueError("Failed to extract scenario tree structure " "file with .dat or .py extension from path " "specification: %s" % (scenario_tree)) elif self._scenario_tree_filename.endswith(".py"): if self._scenario_tree_filename == self._model_filename: # try not to clobber the model import (self._scenario_tree_module, self._scenario_tree, self._scenario_tree_model) = \ _find_scenariotree(module=self._model_module) else: (self._scenario_tree_module, self._scenario_tree, self._scenario_tree_model) = \ _find_scenariotree(src=self._scenario_tree_filename) if (self._scenario_tree is None) and \ (self._scenario_tree_model is None): raise AttributeError("No scenario tree or " "'pysp_scenario_tree_model_callback' " "function found in src: %s" % (self._scenario_tree_filename)) elif self._scenario_tree_filename.endswith(".dat"): self._scenario_tree_model = \ CreateAbstractScenarioTreeModel().\ create_instance(filename=self._scenario_tree_filename) else: assert False elif scenario_tree is None: if self._model_module is not None: self._scenario_tree_filename = self._model_filename (self._scenario_tree_module, self._scenario_tree, self._scenario_tree_model) = \ _find_scenariotree(module=self._model_module) if (self._scenario_tree is None) and \ (self._scenario_tree_model is None): raise ValueError( "No input was provided for the scenario tree " "and no callback or scenario tree object was " "found with the model") else: raise ValueError( "No input was provided for the scenario tree " "but there is no module to search for a " "'pysp_scenario_tree_model_callback' function " "or a ScenarioTree object.") else: self._scenario_tree_model = scenario_tree if self._scenario_tree is None: if (not isinstance(self._scenario_tree_model, (_BlockData, Block))) and \ ((not has_networkx) or \ (not isinstance(self._scenario_tree_model, networkx.DiGraph))): raise TypeError( "scenario tree model object has incorrect type: %s. " "Must be a string type, Pyomo model, or a " "networkx.DiGraph object." % (type(scenario_tree))) if isinstance(self._scenario_tree_model, (_BlockData, Block)): if not self._scenario_tree_model.is_constructed(): raise ValueError("scenario tree model is not constructed") self._data_directory = None if data is None: if self.scenario_tree_directory() is not None: logger.debug("data directory is set to the scenario tree " "directory: %s" % (self.scenario_tree_directory())) self._data_directory = self.scenario_tree_directory() elif self.model_directory() is not None: logger.debug("data directory is set to the reference model " "directory: %s" % (self.model_directory())) self._data_directory = self.model_directory() else: if (self._model_callback is None) and \ isinstance(self._model_object, AbstractModel) and \ (not self._model_object.is_constructed()): raise ValueError( "A data location is required since no model " "callback was provided and no other location could " "be inferred.") logger.debug("no data directory is required") else: logger.debug("data location is provided, attempting " "to load specification: %s" % (data)) self._data_directory, self._archives = \ _extract_pathspec(data, None, archives=self._archives) if not os.path.exists(self._data_directory): logger.error("Failed to extract data directory " "from path specification: %s" % (data)) raise IOError("path does not exist: %s" % (self._data_directory))
def return_CP_and_path(p_data): # return_CP_and_path(p_data) -> dict(), dict() # This function reads the path to the instance directory (p_data) and # returns conditional two dictionaries, the first one is the conditional # probability of a scenario, the second one is the path to all files of a # scenario. from collections import deque, defaultdict # from pyomo.pysp.util.scenariomodels import scenario_tree_model from pyomo.pysp.scenariotree.tree_structure_model import \ CreateAbstractScenarioTreeModel pwd = os.getcwd() os.chdir(p_data) s2fp_dict = defaultdict( deque) # Scenario to 'file path' dictionary, .dat not included s2cd_dict = defaultdict(float) # Scenario to conditonal density mapping # sStructure = scenario_tree_model.create_instance( filename='ScenarioStructure.dat' ) sStructure = CreateAbstractScenarioTreeModel().create_instance( filename='ScenarioStructure.dat') # The following code is borrowed from Kevin's temoa_lib.py ########################################################################### # Step 1: find the root node. PySP doesn't make this very easy ... # a child -> parent mapping, because every child has only one parent, but # not vice-versa ctpTree = dict() # Child to parent dict, one to one mapping to_process = deque() to_process.extend(sStructure.Children.keys()) while to_process: node = to_process.pop() if node in sStructure.Children: # it's a parent! new_nodes = set(sStructure.Children[node]) to_process.extend(new_nodes) ctpTree.update({n: node for n in new_nodes}) # parents - children root_node = (set(ctpTree.values()) - set(ctpTree.keys())).pop() # ptcTree = defaultdict( list ) # Parent to child node, one to multiple mapping # for c, p in ctpTree.items(): # ptcTree[ p ].append( c ) # ptcTree = dict( ptcTree ) # be slightly defensive; catch any additions # leaf_nodes = set(ctpTree.keys()) - set(ctpTree.values()) # leaf_nodes = set(sStructure.ScenarioLeafNode.values()) # Try to hack Kevin's code leaf_nodes = sStructure.ScenarioLeafNode.values( ) # Try to hack Kevin's code leaf_nodes_names = list() for n in leaf_nodes: leaf_nodes_names.append(n.value) leaf_nodes_names = set(leaf_nodes_names) scenario_nodes = dict() # Map from leafnode to 'node path' for node in leaf_nodes_names: # e.g.: {Rs0s0: [R, Rs0, Rs0s0]} s = deque() scenario_nodes[node] = s while node in ctpTree: s.append(node) node = ctpTree[node] s.append(node) s.reverse() ########################################################################### for s in sStructure.Scenarios: cp = 1.0 # Starting probability for n in scenario_nodes[value(sStructure.ScenarioLeafNode[s])]: cp = cp * value(sStructure.ConditionalProbability[n]) if not sStructure.ScenarioBasedData.value: s2fp_dict[s].append(n + '.dat') s2cd_dict[s] = cp from pyomo.core import Objective if sStructure.ScenarioBasedData.value: for s in sStructure.Scenarios: s2fp_dict[s].append(s + '.dat') os.chdir(pwd) return (s2cd_dict, s2fp_dict)
root 1.0 n1 0.33333333 n2 0.33333334 n3 0.33333333 ; set Scenarios := s1 s2 s3 ; param ScenarioLeafNode := s1 n1 s2 n2 s3 n3 ; set StageVariables[t1] := x ; param StageCost := t1 cost[1] t2 cost[2] ; """) model = CreateAbstractScenarioTreeModel().create_instance(f.name) os.remove(f.name)