def run(self): """ This method execute the optimization on the knapsack problem. @ In, None @ Out, None """ outputDict = {} if self.uncertainties is None: inputData = self.generateModelInputData() # specifying the path to a solver # with SolverFactory(self.solver, executable=self.executable) as opt: with SolverFactory(self.solver) as opt: opt.options.update(self.sopts) # add solver options model = self.createInstance(inputData) results = opt.solve(model, load_solutions=False, tee=self.tee, **{'use_signal_handling':False}) if results.solver.termination_condition != TerminationCondition.optimal: raise RuntimeError("Solver did not report optimality:\n%s" %(results.solver)) model.solutions.load_from(results) outputDict.update(self.printSolution(model)) self.output.update(outputDict) # TODO: Add collect output and return a dictionary for raven to retrieve information else: tree_model = self.pysp_scenario_tree_model_callback() if self.stochSolver == 'ef': # fsfct the callback function for pysp_instance_creation_callback # tree_model generated by pysp_scenario_tree_model_callback stsolver = rapper.StochSolver("", fsfct=self.pysp_instance_creation_callback, tree_model=tree_model) ef_sol = stsolver.solve_ef(self.solver, sopts=self.sopts, tee=self.tee) if ef_sol.solver.termination_condition != TerminationCondition.optimal: raise RuntimeError("Solver did not report optimality:\n%s" %(ef_sol.solver)) # TODO: Add collect output and return a dictionary for raven to retrieve information self.printScenarioSolution(stsolver) outputDict.update(self.getScenarioSolution(stsolver.scenario_tree)) self.output.update(outputDict) elif self.stochSolver == 'ph': # fsfct the callback function for pysp_instance_creation_callback # tree_model generated by pysp_scenario_tree_model_callback stsolver = rapper.StochSolver("", fsfct=self.pysp_instance_creation_callback, tree_model=tree_model, phopts=self.phopts) ph_sol = stsolver.solve_ph(subsolver=self.solver, default_rho=self.phRho, phopts=self.phopts, sopts=self.sopts, tee=self.tee) # TODO: Add collect output and return a dictionary for raven to retrieve information # first retrieve the xhat solution from solver, then print the solution obj, xhat = rapper.xhat_from_ph(ph_sol) for nodeName, varName, varValue in rapper.xhat_walker(xhat): print (nodeName, varName, varValue) if ph_sol.solver.termination_condition != TerminationCondition.optimal: raise RuntimeError("Solver did not report optimality:\n%s" %(ph_sol.solver)) self.printScenarioSolution(stsolver) outputDict.update(self.getScenarioSolution(stsolver.scenario_tree)) self.output.update(outputDict) return outputDict
def test_ph_solve(self): """ use ph""" phopts = {'--max-iterations': '2'} stsolver = rapper.StochSolver("ReferenceModel.py", tree_model = self.farmer_concrete_tree, fsfct = "pysp_instance_creation_callback", phopts = phopts) ph = stsolver.solve_ph(subsolver = solvername, default_rho = 1, phopts=phopts) obj = stsolver.root_E_obj() # E[xbar] obj, xhat = rapper.xhat_from_ph(ph) for nodename, varname, varvalue in rapper.xhat_walker(xhat): pass assert(nodename == 'RootNode')
def test_ph_solve(self): """ use ph; assumes concrete two-stage json passes""" phopts = {'--max-iterations': '2'} stsolver = rapper.StochSolver("ReferenceModel.py", tree_model=self.farmer_concrete_tree, phopts=phopts) ph = stsolver.solve_ph(subsolver=solvername, default_rho=1, phopts=phopts) obj = stsolver.root_E_obj() # E[xbar] obj, xhat = rapper.xhat_from_ph(ph) for nodename, varname, varvalue in rapper.xhat_walker(xhat): pass assert (nodename == 'RootNode')