Changeset 4346


Ignore:
Timestamp:
07/08/11 10:38:31 (3 years ago)
Author:
dword
Message:

faster, more robust load_ef_solutions in ef.py

File:
1 edited

Legend:

Unmodified
Added
Removed
  • coopr.pysp/trunk/coopr/pysp/ef.py

    r4242 r4346  
    770770        sub_solutions[scenario_name] = Solution() 
    771771 
    772     for key, attr_dictionary in solution.variable.items(): 
    773 #        tokens = string.split(key, '_', 1) 
    774         tokens = string.split(symbol_map[key], '_', 1) 
    775         variable_name = tokens[1] 
    776         instance_name = tokens[0] 
    777         if not instance_name in scenario_instances.keys(): 
    778             instance_name = "MASTER" 
    779             variable_name = tokens[0]+"_"+tokens[1] 
    780         sub_solutions[instance_name].variable[variable_name] = attr_dictionary 
    781  
    782     for key, attr_dictionary in solution.constraint.items(): 
     772    for key, attr_dictionary in solution.constraint.iteritems(): 
    783773        tokens = string.split(symbol_map[key], '_', 1) 
    784774        constraint_name = tokens[1] 
    785775        instance_name = tokens[0] 
    786776        sub_solutions[instance_name].constraint[constraint_name] = attr_dictionary 
     777 
     778    scen_inst_names = scenario_instances.keys() 
     779    for key, attr_dictionary in solution.variable.iteritems(): 
     780        tokens = string.split(symbol_map[key], '_', 1) 
     781        instance_name = tokens[0] 
     782        if not instance_name in scen_inst_names: 
     783            instance_name = "MASTER" 
     784            variable_name = '_'.join(tokens) 
     785        else: 
     786            variable_name = tokens[1] 
     787        sub_solutions[instance_name].variable[variable_name] = attr_dictionary 
     788 
     789    # load the sub-solutions into the appropriate instances. 
     790    for instance_name, sub_solution in sub_solutions.iteritems(): 
     791        if instance_name == binding_instance.name: 
     792            binding_instance.load(sub_solution) 
     793        else: 
     794            scenario_instances[instance_name].load(sub_solution) 
     795 
     796 
     797def load_ef_solution(ef_results, binding_instance, scenario_instances): 
     798 
     799    # type of ef_results is coopr.opt.results.results.SolverResults 
     800    if len(ef_results.solution) == 0: 
     801        raise RuntimeError, "Method load_ef_solution invoked with results object containing no solutions - check solver output!" 
     802    elif len(ef_results.solution) > 1: 
     803        raise RuntimeError, "Method load_ef_solution invoked with results object containing more than one solution - check solver output!" 
     804 
     805    # type is coopr.opt.results.solution.Solution 
     806    solution = ef_results.solution[0] 
     807 
     808    # shotgun the ef solution into individual solutions for the binding and scenario instances. 
     809    sub_solutions = {} # map between instance name and the corresponding Solution 
     810    sub_solutions[binding_instance.name] = Solution() 
     811    for scenario_name, scenario in scenario_instances.items(): 
     812        sub_solutions[scenario_name] = Solution() 
     813 
     814    for key, attr_dictionary in solution.variable.items(): 
     815        tokens = string.split(key, '_', 1) 
     816        instance_name = tokens[0] 
     817        variable_name = tokens[1] 
     818        sub_solutions[instance_name].variable[variable_name] = attr_dictionary 
    787819 
    788820    # load the sub-solutions into the appropriate instances. 
     
    792824        else: 
    793825            scenario_instances[instance_name].load(sub_solution) 
    794  
    795  
    796 def load_ef_solution(ef_results, binding_instance, scenario_instances): 
    797  
    798     # type of ef_results is coopr.opt.results.results.SolverResults 
    799     if len(ef_results.solution) == 0: 
    800         raise RuntimeError, "Method load_ef_solution invoked with results object containing no solutions - check solver output!" 
    801     elif len(ef_results.solution) > 1: 
    802         raise RuntimeError, "Method load_ef_solution invoked with results object containing more than one solution - check solver output!" 
    803  
    804     # type is coopr.opt.results.solution.Solution 
    805     solution = ef_results.solution[0] 
    806  
    807     # shotgun the ef solution into individual solutions for the binding and scenario instances. 
    808     sub_solutions = {} # map between instance name and the corresponding Solution 
    809     sub_solutions[binding_instance.name] = Solution() 
    810     for scenario_name, scenario in scenario_instances.items(): 
    811         sub_solutions[scenario_name] = Solution() 
    812  
    813     for key, attr_dictionary in solution.variable.items(): 
    814         tokens = string.split(key, '_', 1) 
    815         instance_name = tokens[0] 
    816         variable_name = tokens[1] 
    817         sub_solutions[instance_name].variable[variable_name] = attr_dictionary 
    818  
    819     # load the sub-solutions into the appropriate instances. 
    820     for instance_name, sub_solution in sub_solutions.items(): 
    821         if instance_name == binding_instance.name: 
    822             binding_instance.load(sub_solution) 
    823         else: 
    824             scenario_instances[instance_name].load(sub_solution) 
Note: See TracChangeset for help on using the changeset viewer.