Changeset 2317
- Timestamp:
- Feb 11, 2010 11:33:46 AM (11 years ago)
- Location:
- coopr.pysp/stable/2.3
- Files:
-
- 23 deleted
- 9 edited
- 436 copied
Legend:
- Unmodified
- Added
- Removed
-
coopr.pysp/stable/2.3
- Property svnmerge-integrated changed
/coopr.pysp/trunk merged: 2246-2249,2254-2256,2260-2262,2267-2272,2275,2277,2288,2290-2291,2296-2301
- Property svnmerge-integrated changed
-
coopr.pysp/stable/2.3/README.txt
r1806 r2317 1 ================ 1 ================= 2 2 coopr.pysp README 3 ================ 3 ================= 4 4 5 5 This Python package defines stochastic programming extensions for the Pyomo -
coopr.pysp/stable/2.3/coopr/pysp/__init__.py
r2201 r2317 20 20 from ef import * 21 21 from ef_writer_script import * 22 from ph _script import *22 from phinit import * 23 23 24 24 pyutilib.component.core.PluginGlobals.pop_env() -
coopr.pysp/stable/2.3/coopr/pysp/ef.py
r2110 r2317 186 186 node_data_filename = scenario_data_directory_name + os.sep + current_node._name + ".dat" 187 187 # print "Node data for scenario=" + scenario._name + " partially loading from file=" + node_data_filename 188 scenario_data.add _data_file(node_data_filename)188 scenario_data.add(node_data_filename) 189 189 current_node = current_node._parent 190 190 scenario_data.read(model=scenario_instance) … … 222 222 for (stage_variable, index_template, stage_variable_indices) in stage._variables: 223 223 224 print "Creating master variable and blending constraints for decision variable=", stage_variable, ", indices=", 224 print "Creating master variable and blending constraints for decision variable=", stage_variable, ", indices=",stage_variable_indices 225 225 226 226 for tree_node in stage._tree_nodes: … … 237 237 master_variable = getattr(master_binding_instance, master_variable_name) 238 238 except: 239 # the deepcopy is probably too expensive (and unnecessary) computationally - 240 # easier to just use the constructor with the stage variable index/bounds/etc. 241 # NOTE: need to re-assign the master variables for each _varval - they probably 242 # point to a bogus model. 243 new_master_variable = copy.deepcopy(stage_variable) 244 new_master_variable.name = master_variable_name 239 new_master_variable_index = stage_variable._index 240 new_master_variable = None 241 if (len(new_master_variable_index) is 1) and (None in new_master_variable_index): 242 new_master_variable = Var(name=stage_variable.name) 243 else: 244 new_master_variable = Var(new_master_variable_index, name=stage_variable.name) 245 new_master_variable.construct() 245 246 new_master_variable._model = master_binding_instance 246 247 setattr(master_binding_instance, master_variable_name, new_master_variable) 248 249 # TBD - TECHNICALLY, WE NEED TO COPY BOUNDS - BUT WE REALLY DON'T, AS THEY ARE ON THE PER-INSTNACE VARS! 247 250 248 251 master_variable = new_master_variable … … 313 316 for tree_node in stage._tree_nodes: 314 317 318 # TBD - the following is bad - check to see if it's already there (I suspect some of them are!!!) 319 320 # this is undoubtedly wasteful, in that a cost variable 321 # for each tree node is created with *all* indices. 315 322 new_cost_variable_name = tree_node._name + "_" + cost_variable.name 316 317 # TBD - the following is bad - check to see if it's already there (I suspect some of them are!!!)318 319 # this is undoubtedly wasteful, in that a cost variable320 # for each tree node is created with *all* indices.321 new_cost_variable = copy.deepcopy(cost_variable)322 new_cost_variable. name = new_cost_variable_name323 new_cost_variable_index = cost_variable._index 324 new_cost_variable = None 325 if (len(new_cost_variable_index) is 1) and (None in new_cost_variable_index): 326 new_cost_variable = Var(name=new_cost_variable_name) 327 else: 328 new_cost_variable = Var(new_cost_variable_index, name=new_cost_variable_name) 329 new_cost_variable.construct() 323 330 new_cost_variable._model = master_binding_instance 324 setattr(master_binding_instance, new_cost_variable_name, new_cost_variable) 331 setattr(master_binding_instance, new_cost_variable_name, new_cost_variable) 325 332 326 333 # the following is necessary, specifically to get the name - deepcopy won't reset these attributes. … … 644 651 master_variable = getattr(master_binding_instance, master_variable_name) 645 652 except: 646 # the deepcopy is probably too expensive (and unnecessary) computationally - 647 # easier to just use the constructor with the stage variable index/bounds/etc. 648 # NOTE: need to re-assign the master variables for each _varval - they probably 649 # point to a bogus model. 650 new_master_variable = copy.deepcopy(stage_variable) 651 new_master_variable.name = master_variable_name 653 new_master_variable_index = stage_variable._index 654 new_master_variable = None 655 if (len(new_master_variable_index) is 1) and (None in new_master_variable_index): 656 new_master_variable = Var(name=stage_variable.name) 657 else: 658 new_master_variable = Var(new_master_variable_index, name=stage_variable.name) 659 new_master_variable.construct() 652 660 new_master_variable._model = master_binding_instance 653 661 setattr(master_binding_instance, master_variable_name, new_master_variable) 662 663 # TBD - TECHNICALLY, WE NEED TO COPY BOUNDS - BUT WE REALLY DON'T, AS THEY ARE ON THE PER-INSTNACE VARS! 654 664 655 665 master_variable = new_master_variable … … 711 721 # this is undoubtedly wasteful, in that a cost variable 712 722 # for each tree node is created with *all* indices. 713 new_cost_variable = copy.deepcopy(cost_variable) 714 new_cost_variable.name = new_cost_variable_name 723 new_cost_variable_name = tree_node._name + "_" + cost_variable.name 724 new_cost_variable_index = cost_variable._index 725 new_cost_variable = None 726 if (len(new_cost_variable_index) is 1) and (None in new_cost_variable_index): 727 new_cost_variable = Var(name=new_cost_variable_name) 728 else: 729 new_cost_variable = Var(new_cost_variable_index, new_cost_variable_name) 730 new_cost_variable.construct() 715 731 new_cost_variable._model = master_binding_instance 716 setattr(master_binding_instance, new_cost_variable_name, new_cost_variable) 732 setattr(master_binding_instance, new_cost_variable_name, new_cost_variable) 717 733 718 734 # the following is necessary, specifically to get the name - deepcopy won't reset these attributes. -
coopr.pysp/stable/2.3/coopr/pysp/ef_writer_script.py
r1979 r2317 84 84 risk_alpha = options.risk_alpha 85 85 86 write_ef_from_scratch(o ptions.model_directory, options.instance_directory, options.output_file, \86 write_ef_from_scratch(os.path.expanduser(options.model_directory), os.path.expanduser(options.instance_directory), os.path.expanduser(options.output_file), \ 87 87 generate_weighted_cvar, cvar_weight, risk_alpha) 88 88 -
coopr.pysp/stable/2.3/coopr/pysp/ph.py
r2201 r2317 851 851 if self._verbose is True: 852 852 print "Node data for scenario=" + scenario._name + " partially loading from file=" + node_data_filename 853 scenario_data.add _data_file(node_data_filename)853 scenario_data.add(node_data_filename) 854 854 current_node = current_node._parent 855 855 scenario_data.read(model=scenario_instance) … … 1009 1009 1010 1010 action_handles = [] 1011 action_handle_instance_map = {} 1011 scenario_action_handle_map = {} # maps scenario names to action handles 1012 action_handle_scenario_map = {} # maps action handles to scenario names 1012 1013 1013 1014 for scenario in self._scenario_tree._scenarios: … … 1030 1031 1031 1032 new_action_handle = self._solver_manager.queue(instance, opt=self._solver, tee=self._output_solver_log) 1032 action_handle_instance_map[scenario._name] = new_action_handle 1033 scenario_action_handle_map[scenario._name] = new_action_handle 1034 action_handle_scenario_map[new_action_handle] = scenario._name 1033 1035 1034 1036 action_handles.append(new_action_handle) 1035 1037 1036 # STEP 2: barrier sync for all scenario sub-problem solves. 1038 # STEP 2: loop for the solver results, reading them and loading 1039 # them into instances as they are available. 1040 1037 1041 if self._verbose is True: 1038 1042 print "Waiting for scenario sub-problem solves" 1039 self._solver_manager.wait_all(action_handles) 1040 if self._verbose is True: 1041 print "Scenario sub-problem solves completed" 1042 1043 solve_end_time = time.time() 1044 self._cumulative_solve_time += (solve_end_time - solve_start_time) 1045 1046 if self._output_times is True: 1047 print "Aggregate sub-problem solve time=%8.2f" % (solve_end_time - solve_start_time) 1048 1049 # STEP 3: Load the results! 1050 for scenario_name, action_handle in action_handle_instance_map.items(): 1051 1052 if self._verbose is True: 1053 print "Successfully processed results for scenario="+scenario_name 1054 1055 instance = self._instances[scenario_name] 1056 results = self._solver_manager.get_results(action_handle) 1043 1044 num_results_so_far = 0 1045 1046 while (num_results_so_far < len(self._scenario_tree._scenarios)): 1047 1048 action_handle = self._solver_manager.wait_any() 1049 results = self._solver_manager.get_results(action_handle) 1050 scenario_name = action_handle_scenario_map[action_handle] 1051 instance = self._instances[scenario_name] 1052 1053 if self._verbose is True: 1054 print "Results obtained for scenario="+scenario_name 1057 1055 1058 1056 if len(results.solution) == 0: … … 1062 1060 if self._output_solver_results is True: 1063 1061 print "Results for scenario=",scenario_name 1064 results.write(num=1) 1062 results.write(num=1) 1065 1063 1066 1064 start_time = time.time() … … 1068 1066 end_time = time.time() 1069 1067 if self._output_times is True: 1070 print "Time loading results ="+str(end_time-start_time)+" seconds"1068 print "Time loading results into instance="+str(end_time-start_time)+" seconds" 1071 1069 1072 1070 if self._verbose is True: 1073 1071 print "Successfully loaded solution for scenario="+scenario_name 1072 1073 num_results_so_far = num_results_so_far + 1 1074 1075 if self._verbose is True: 1076 print "Scenario sub-problem solves completed" 1077 1078 solve_end_time = time.time() 1079 self._cumulative_solve_time += (solve_end_time - solve_start_time) 1080 1081 if self._output_times is True: 1082 print "Aggregate sub-problem solve time this iteration=%8.2f" % (solve_end_time - solve_start_time) 1074 1083 1075 1084 if self._verbose is True: … … 1352 1361 def iteration_k_solve(self): 1353 1362 1354 if self._verbose is True: 1355 print "------------------------------------------------" 1356 print "Starting PH iteration " + str(self._current_iteration) + " solves" 1357 1358 # cache the objective values generated by PH for output at the end of this function. 1359 ph_objective_values = {} 1360 1361 solve_start_time = time.time() 1362 1363 # STEP 0: set up all global solver options. 1364 self._solver.mipgap = self._mipgap 1365 1366 # STEP 1: queue up the solves for all scenario sub-problems and 1367 # grab all of the action handles for the subsequent barrier sync. 1368 1369 action_handles = [] 1370 action_handle_instance_map = {} 1371 1372 for scenario in self._scenario_tree._scenarios: 1373 1374 instance = self._instances[scenario._name] 1375 1376 if self._verbose is True: 1377 print "Queuing solve for scenario=" + scenario._name 1378 1379 # IMPT: You have to re-presolve, as the simple presolver collects the linear terms together. If you 1380 # don't do this, you won't see any chance in the output files as you vary the problem parameters! 1381 # ditto for instance fixing! 1382 instance.presolve() 1383 1384 # once past iteration 0, there is always a feasible solution from which to warm-start. 1385 # however, you might want to disable warm-start when the solver is behaving badly (which does happen). 1386 new_action_handle = None 1387 if (self._disable_warmstarts is False) and (self._solver.warm_start_capable() is True): 1388 new_action_handle = self._solver_manager.queue(instance, opt=self._solver, warmstart=True, tee=self._output_solver_log) 1389 else: 1390 new_action_handle = self._solver_manager.queue(instance, opt=self._solver, tee=self._output_solver_log) 1391 1392 action_handle_instance_map[scenario._name] = new_action_handle 1393 1394 action_handles.append(new_action_handle) 1395 1396 # STEP 2: barrier sync for all scenario sub-problem solves. 1397 if self._verbose is True: 1398 print "Waiting for scenario sub-problem solves" 1399 self._solver_manager.wait_all(action_handles) 1400 if self._verbose is True: 1401 print "Scenario sub-problem solves completed" 1402 1403 solve_end_time = time.time() 1404 self._cumulative_solve_time += (solve_end_time - solve_start_time) 1405 1406 if self._output_times is True: 1407 print "Aggregate sub-problem solve time=%8.2f" % (solve_end_time - solve_start_time) 1408 1409 # STEP 3: Load the results! 1410 for scenario_name, action_handle in action_handle_instance_map.items(): 1411 1412 if self._verbose is True: 1413 print "Successfully processed results for scenario="+scenario_name 1414 1415 instance = self._instances[scenario_name] 1416 results = self._solver_manager.get_results(action_handle) 1417 1418 if len(results.solution) == 0: 1419 results.write() 1420 raise RuntimeError, "Solve failed for scenario="+scenario_name+"; no solutions generated" 1421 1422 if self._output_solver_results is True: 1423 print "Results for scenario=", scenario_name 1424 results.write(num=1) 1425 1426 start_time = time.time() 1427 instance.load(results) 1428 end_time = time.time() 1429 1430 if self._output_times is True: 1431 print "Time loading results="+str(end_time-start_time)+" seconds" 1432 1433 if self._verbose is True: 1434 print "Successfully loaded solution for scenario="+scenario_name 1435 1436 # we're assuming there is a single solution. 1437 # the "value" attribute is a pre-defined feature of any solution - it is relative to whatever 1438 # objective was selected during optimization, which of course should be the PH objective. 1439 ph_objective_values[instance.name] = float(results.solution(0).objective['f'].value) 1440 1441 if self._verbose is True: 1442 print "Successfully completed PH iteration " + str(self._current_iteration) + " solves - solution statistics:" 1443 print " Scenario PH Objective Cost Objective" 1444 for scenario in self._scenario_tree._scenarios: 1445 instance = self._instances[scenario._name] 1446 for objective_name in instance.active_components(Objective): 1447 objective = instance.active_components(Objective)[objective_name] 1448 print "%20s %18.4f %14.4f" % (scenario._name, ph_objective_values[scenario._name], 0.0) 1363 if self._verbose is True: 1364 print "------------------------------------------------" 1365 print "Starting PH iteration " + str(self._current_iteration) + " solves" 1366 1367 # cache the objective values generated by PH for output at the end of this function. 1368 ph_objective_values = {} 1369 1370 solve_start_time = time.time() 1371 1372 # STEP 0: set up all global solver options. 1373 self._solver.mipgap = self._mipgap 1374 1375 # STEP 1: queue up the solves for all scenario sub-problems and 1376 # grab all of the action handles for the subsequent barrier sync. 1377 1378 action_handles = [] 1379 scenario_action_handle_map = {} # maps scenario names to action handles 1380 action_handle_scenario_map = {} # maps action handles to scenario names 1381 1382 for scenario in self._scenario_tree._scenarios: 1383 1384 instance = self._instances[scenario._name] 1385 1386 if self._verbose is True: 1387 print "Queuing solve for scenario=" + scenario._name 1388 1389 # IMPT: You have to re-presolve, as the simple presolver collects the linear terms together. If you 1390 # don't do this, you won't see any chance in the output files as you vary the problem parameters! 1391 # ditto for instance fixing! 1392 instance.presolve() 1393 1394 # once past iteration 0, there is always a feasible solution from which to warm-start. 1395 # however, you might want to disable warm-start when the solver is behaving badly (which does happen). 1396 new_action_handle = None 1397 if (self._disable_warmstarts is False) and (self._solver.warm_start_capable() is True): 1398 new_action_handle = self._solver_manager.queue(instance, opt=self._solver, warmstart=True, tee=self._output_solver_log) 1399 else: 1400 new_action_handle = self._solver_manager.queue(instance, opt=self._solver, tee=self._output_solver_log) 1401 1402 scenario_action_handle_map[scenario._name] = new_action_handle 1403 action_handle_scenario_map[new_action_handle] = scenario._name 1404 1405 action_handles.append(new_action_handle) 1406 1407 # STEP 2: loop for the solver results, reading them and loading 1408 # them into instances as they are available. 1409 if self._verbose is True: 1410 print "Waiting for scenario sub-problem solves" 1411 1412 num_results_so_far = 0 1413 1414 while (num_results_so_far < len(self._scenario_tree._scenarios)): 1415 1416 action_handle = self._solver_manager.wait_any() 1417 results = self._solver_manager.get_results(action_handle) 1418 scenario_name = action_handle_scenario_map[action_handle] 1419 instance = self._instances[scenario_name] 1420 1421 if self._verbose is True: 1422 print "Results obtained for scenario="+scenario_name 1423 1424 if len(results.solution) == 0: 1425 results.write(num=1) 1426 raise RuntimeError, "Solve failed for scenario="+scenario_name+"; no solutions generated" 1427 1428 if self._output_solver_results is True: 1429 print "Results for scenario=",scenario_name 1430 results.write(num=1) 1431 1432 start_time = time.time() 1433 instance.load(results) 1434 end_time = time.time() 1435 if self._output_times is True: 1436 print "Time loading results into instance="+str(end_time-start_time)+" seconds" 1437 1438 if self._verbose is True: 1439 print "Successfully loaded solution for scenario="+scenario_name 1440 1441 # we're assuming there is a single solution. 1442 # the "value" attribute is a pre-defined feature of any solution - it is relative to whatever 1443 # objective was selected during optimization, which of course should be the PH objective. 1444 ph_objective_values[instance.name] = float(results.solution(0).objective['f'].value) 1445 1446 num_results_so_far = num_results_so_far + 1 1447 1448 if self._verbose is True: 1449 print "Scenario sub-problem solves completed" 1450 1451 solve_end_time = time.time() 1452 self._cumulative_solve_time += (solve_end_time - solve_start_time) 1453 1454 if self._output_times is True: 1455 print "Aggregate sub-problem solve time this iteration=%8.2f" % (solve_end_time - solve_start_time) 1456 1457 if self._verbose is True: 1458 print "Successfully completed PH iteration " + str(self._current_iteration) + " solves - solution statistics:" 1459 print " Scenario PH Objective Cost Objective" 1460 for scenario in self._scenario_tree._scenarios: 1461 instance = self._instances[scenario._name] 1462 for objective_name in instance.active_components(Objective): 1463 objective = instance.active_components(Objective)[objective_name] 1464 print "%20s %18.4f %14.4f" % (scenario._name, ph_objective_values[scenario._name], 0.0) 1449 1465 1450 1466 def solve(self): -
coopr.pysp/stable/2.3/coopr/pysp/tests/unit/test_ph.py
r1768 r2317 5 5 import os 6 6 import sys 7 import string 7 8 from os.path import abspath, dirname 8 9 sys.path.insert(0, dirname(dirname(abspath(__file__)))+"/../..") 9 currdir = dirname(abspath(__file__))+os.sep 10 11 current_directory = dirname(abspath(__file__))+os.sep 12 13 pysp_examples_dir = dirname(dirname(dirname(dirname(dirname(abspath(__file__))))))+os.sep+"examples"+os.sep+"pysp"+os.sep 14 15 coopr_bin_dir = dirname(dirname(dirname(dirname(dirname(dirname(dirname(abspath(__file__))))))))+os.sep+"bin"+os.sep 16 10 17 # 11 18 # Import the testing packages … … 14 21 import pyutilib.misc 15 22 import pyutilib.th 23 import pyutilib.subprocess 24 import coopr.pysp 25 import coopr.plugins.mip 26 import coopr.pysp.phinit 27 28 def filter_time(line): 29 return "seconds" in line 30 31 cplex = None 32 cplex_available = False 33 try: 34 cplex = coopr.plugins.mip.CPLEX(keepFiles=True) 35 cplex_available = (not cplex.executable() is None) and cplex.available(False) 36 except pyutilib.common.ApplicationError: 37 cplex_available=False 38 39 glpk = None 40 glpk_available = False 41 try: 42 glpk = coopr.plugins.mip.GLPK(keepFiles=True) 43 glpk_available = (not glpk.executable() is None) and glpk.available(False) 44 except pyutilib.common.ApplicationError: 45 glpk_available=False 16 46 17 47 # … … 22 52 class TestPH(pyutilib.th.TestCase): 23 53 24 # 25 # A test which redirects IO to a file, prints stuff, resets the 26 # redirection, and then compares with a baseline file. 27 # 28 def test_dummy(self): 29 pyutilib.misc.setup_redirect(currdir+"dummy.out") 30 print "HELLO WORLD" 54 def cleanup(self): 55 56 # IMPT: This step is key, as Python keys off the name of the module, not the location. 57 # So, different reference models in different directories won't be detected. 58 # If you don't do this, the symptom is a model that doesn't have the attributes 59 # that the data file expects. 60 if "ReferenceModel" in sys.modules: 61 del sys.modules["ReferenceModel"] 62 63 def test_quadratic_farmer(self): 64 65 if cplex_available is False: 66 return 67 68 pyutilib.misc.setup_redirect(current_directory+"farmer_quadratic.out") 69 farmer_examples_dir = pysp_examples_dir + "farmer" 70 model_dir = farmer_examples_dir + os.sep + "models" 71 instance_dir = farmer_examples_dir + os.sep + "scenariodata" 72 argstring = "runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir 73 args = string.split(argstring) 74 coopr.pysp.phinit.run(args=args) 31 75 pyutilib.misc.reset_redirect() 32 self.failUnlessFileEqualsBaseline(currdir+"dummy.out",currdir+"dummy.txt") 76 self.cleanup() 77 self.failUnlessFileEqualsBaseline(current_directory+"farmer_quadratic.out",current_directory+"farmer_quadratic.baseline", filter=filter_time) 78 79 def test_linearized_farmer(self): 80 81 if cplex_available is False: 82 return 83 84 pyutilib.misc.setup_redirect(current_directory+"farmer_linearized.out") 85 farmer_examples_dir = pysp_examples_dir + "farmer" 86 model_dir = farmer_examples_dir + os.sep + "models" 87 instance_dir = farmer_examples_dir + os.sep + "scenariodata" 88 argstring = "runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+" --linearize-nonbinary-penalty-terms=10" 89 args = string.split(argstring) 90 coopr.pysp.phinit.run(args=args) 91 pyutilib.misc.reset_redirect() 92 self.cleanup() 93 self.failUnlessFileEqualsBaseline(current_directory+"farmer_linearized.out",current_directory+"farmer_linearized.baseline", filter=filter_time) 94 95 def test_quadratic_sizes3(self): 96 97 if cplex_available is False: 98 return 99 100 pyutilib.misc.setup_redirect(current_directory+"sizes3_quadratic.out") 101 102 sizes_example_dir = pysp_examples_dir + "sizes" 103 model_dir = sizes_example_dir + os.sep + "models" 104 instance_dir = sizes_example_dir + os.sep + "SIZES3" 105 argstring = "runph --solver=cplex --solver-manager=serial --model-directory="+model_dir+" --instance-directory="+instance_dir+ \ 106 " --max-iterations=200"+ \ 107 " --rho-cfgfile="+sizes_example_dir+os.sep+"config"+os.sep+"rhosetter.cfg"+ \ 108 " --scenario-solver-options=mip_tolerances_integrality=1e-7"+ \ 109 " --enable-ww-extensions"+ \ 110 " --ww-extension-cfgfile="+sizes_example_dir+os.sep+"config"+os.sep+"wwph.cfg"+ \ 111 " --ww-extension-suffixfile="+sizes_example_dir+os.sep+"config"+os.sep+"wwph.suffixes" 112 args = string.split(argstring) 113 coopr.pysp.phinit.run(args=args) 114 pyutilib.misc.reset_redirect() 115 self.failUnlessFileEqualsBaseline(current_directory+"sizes3_quadratic.out",current_directory+"sizes3_quadratic.baseline", filter=filter_time) 116 self.cleanup() 117 118 def run(self, result=None): 119 global cplex_available 120 if not cplex_available: 121 return 122 pyutilib.th.TestCase.run(self,result) 33 123 34 124 if __name__ == "__main__": -
coopr.pysp/stable/2.3/coopr/pysp/wwphextension.py
r2201 r2317 29 29 return cmp(value_b, value_a) 30 30 31 #==================================================32 31 #================================================== 33 32 class wwphextension(SingletonPlugin): … … 266 265 tree_node._fixed_var_flag = {} 267 266 268 # next, create parameters for each variable in the corresponding tree node.269 270 267 for (variable, index_template, variable_indices) in stage._variables: 271 268 272 269 variable_name = variable.name 273 270 variable_type = variable.domain 271 272 # create the parameters for all indices, even though this might be a bit 273 # wasteful. in practice, this probably won't be the case, and determining 274 # the reduced index set would probably take longer that it is worth. 275 276 new_stat_index = variable._index 277 new_stat_parameter_name = "NODESTAT_NUM_ITERS_CONVERGED_"+variable.name 278 new_stat_parameter = None 279 # this bit of ugliness is due to Pyomo not correctly handling the Param construction 280 # case when the supplied index set consists strictly of None, i.e., the source variable 281 # is a singleton. this case be cleaned up when the source issue in Pyomo is fixed. 282 if (len(new_stat_index) is 1) and (None in new_stat_index): 283 new_stat_parameter = Param(name=new_stat_parameter_name) 284 else: 285 new_stat_parameter = Param(new_stat_index,name=new_stat_parameter_name) 286 for newindex in new_stat_index: 287 new_stat_parameter[newindex] = 0 288 tree_node._num_iters_converged[variable.name] = new_stat_parameter 289 290 # need to know to what we have most recently converged 291 new_conv_index = variable._index 292 new_conv_parameter_name = "NODESTAT_LAST_CONVERGED_VAL_"+variable.name 293 new_conv_parameter = None 294 if (len(new_conv_index) is 1) and (None in new_conv_index): 295 new_conv_parameter = Param(name=new_conv_parameter_name) 296 else: 297 new_conv_parameter = Param(new_conv_index,name=new_conv_parameter_name) 298 for newindex in new_conv_index: 299 new_conv_parameter[newindex] = 0.5 # not an int, so harmless 300 tree_node._last_converged_val[variable.name] = new_conv_parameter 301 302 # need to know to what has been fixed 303 new_fix_index = variable._index 304 new_fix_parameter_name = "NODESTAT_FIXED_FLAG_VAL_"+variable.name 305 new_fix_parameter = None 306 if (len(new_fix_index) is 1) and (None in new_fix_index): 307 new_fix_parameter = Param(name=new_fix_parameter_name) 308 else: 309 new_fix_parameter = Param(new_fix_index,name=new_fix_parameter_name) 310 for newindex in new_fix_index: 311 new_fix_parameter[newindex] = False 312 tree_node._fixed_var_flag[variable.name] = new_fix_parameter 313 314 # now make the w hash value storage array 315 new_hash_index = variable._index 316 new_hash_parameter_name = "W_HASH_STORAGE_"+variable.name 317 new_hash_parameter = None 318 if (len(new_hash_index) is 1) and (None in new_hash_index): 319 new_hash_parameter = Param(ph._iteration_index_set, name=new_hash_parameter_name) 320 else: 321 new_hash_parameter = Param(new_hash_index, ph._iteration_index_set, name=new_hash_parameter_name) 322 for newindex in new_hash_index: 323 for i in range(0, ph._max_iterations+1): 324 # the following if-then block is a complete hack, due to the 325 # fact that we can't index by None if the Param is unary. 326 if new_hash_parameter.dim() == 1: 327 new_hash_parameter[i] = 0 328 else: 329 new_hash_parameter[newindex,i] = 0 330 tree_node._w_hash[variable.name] = new_hash_parameter 274 331 332 # JPW has no idea why the following code block is here, or if it is necessary. 275 333 for index in variable_indices: 276 334 … … 278 336 # variable values in the last stage of a stochastic program will *not* 279 337 # have a defined _stage attribute. 280 # print "dlw debug create stage association ",variable_name, index, stage._name281 338 variable[index]._stage = stage 282 339 283 new_stat_index = variable._index284 new_stat_parameter_name = "NODESTAT_NUM_ITERS_CONVERGED_"+variable.name285 new_stat_parameter = None286 # this bit of ugliness is due to Pyomo not correctly handling the Param construction287 # case when the supplied index set consists strictly of None, i.e., the source variable288 # is a singleton. this case be cleaned up when the source issue in Pyomo is fixed.289 if (len(new_stat_index) is 1) and (None in new_stat_index):290 new_stat_parameter = Param(name=new_stat_parameter_name)291 else:292 new_stat_parameter = Param(new_stat_index,name=new_stat_parameter_name)293 for newindex in new_stat_index:294 new_stat_parameter[newindex] = 0295 tree_node._num_iters_converged[variable.name] = new_stat_parameter296 297 # need to know to what we have most recently converged298 new_conv_index = variable._index299 new_conv_parameter_name = "NODESTAT_LAST_CONVERGED_VAL_"+variable.name300 new_conv_parameter = None301 if (len(new_conv_index) is 1) and (None in new_conv_index):302 new_conv_parameter = Param(name=new_conv_parameter_name)303 else:304 new_conv_parameter = Param(new_conv_index,name=new_conv_parameter_name)305 for newindex in new_conv_index:306 new_conv_parameter[newindex] = 0.5 # not an int, so harmless307 tree_node._last_converged_val[variable.name] = new_conv_parameter308 309 # need to know to what has been fixed310 new_fix_index = variable._index311 new_fix_parameter_name = "NODESTAT_FIXED_FLAG_VAL_"+variable.name312 new_fix_parameter = None313 if (len(new_fix_index) is 1) and (None in new_fix_index):314 new_fix_parameter = Param(name=new_fix_parameter_name)315 else:316 new_fix_parameter = Param(new_fix_index,name=new_fix_parameter_name)317 for newindex in new_fix_index:318 new_fix_parameter[newindex] = False319 tree_node._fixed_var_flag[variable.name] = new_fix_parameter320 321 # now make the w hash value storage array322 new_hash_index = variable._index323 new_hash_parameter_name = "W_HASH_STORAGE_"+variable.name324 new_hash_parameter = None325 if (len(new_hash_index) is 1) and (None in new_hash_index):326 new_hash_parameter = Param(ph._iteration_index_set, name=new_hash_parameter_name)327 else:328 new_hash_parameter = Param(new_hash_index, ph._iteration_index_set, name=new_hash_parameter_name)329 for newindex in new_hash_index:330 for i in range(0, ph._max_iterations+1):331 # the following if-then block is a complete hack, due to the332 # fact that we can't index by None if the Param is unary.333 if new_hash_parameter.dim() == 1:334 new_hash_parameter[i] = 0335 else:336 new_hash_parameter[newindex,i] = 0337 tree_node._w_hash[variable.name] = new_hash_parameter338 340 339 341 # store the total variable counts for future reporting. -
coopr.pysp/stable/2.3/scripts/runph
r1979 r2317 46 46 curr = os.path.dirname(curr) 47 47 48 import coopr.pysp.ph _script48 import coopr.pysp.phinit 49 49 50 50 try: 51 coopr.pysp.ph _script.run()51 coopr.pysp.phinit.run() 52 52 except ValueError, str: 53 53 print "VALUE ERROR:"
Note: See TracChangeset
for help on using the changeset viewer.