Changeset 2449


Ignore:
Timestamp:
Mar 24, 2010 7:26:28 PM (9 years ago)
Author:
jwatson
Message:

Added method to PySP to load the extensive form solution into the master binding instance and the individual scenario instances.

Required by some of the sampling procedures we are developing, and generally useful.

File:
1 edited

Legend:

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

    r2439 r2449  
    1515
    1616from coopr.pyomo.base.var import _VarValue, _VarBase
     17
     18from coopr.opt.results.solution import Solution
    1719
    1820#
     
    687689   print ""
    688690   print "Total execution time=%8.2f seconds" %(end_time - start_time)
    689    print ""   
     691   print ""
     692
     693#
     694# a utility to load an EF solution into the corresponding instances.
     695#
     696
     697def load_ef_solution(ef_results, binding_instance, scenario_instances):
     698
     699   # type is coopr.opt.results.results.SolverResults
     700   if len(ef_results.solution) == 0:
     701      raise RuntimeError, "Method load_ef_solution invoked with results object containing no solutions!"
     702   elif len(ef_results.solution) > 1:
     703      raise RuntimeError, "Method load_ef_solution invoked with results object containing more than one solution!"
     704   
     705   # type is coopr.opt.results.solution.Solution
     706   solution = ef_results.solution[0]
     707
     708   # shotgun the ef solution into individual solutions for the binding and scenario instances.
     709   sub_solutions = {} # map between instance name and the corresponding Solution
     710   sub_solutions[binding_instance.name] = Solution()
     711   for scenario_name, scenario in scenario_instances.items():
     712      sub_solutions[scenario_name] = Solution()
     713
     714   for key, attr_dictionary in solution.variable.items():
     715      tokens = string.split(key, '_', 1)
     716      instance_name = tokens[0]
     717      variable_name = tokens[1]
     718      subsolution_variable = sub_solutions[instance_name].variable[variable_name]
     719      for attr_name in attr_dictionary.keys():
     720         attr_value = attr_dictionary[attr_name]
     721         setattr(subsolution_variable, attr_name, attr_value)
     722
     723   # load the sub-solutions into the appropriate instances.
     724   for instance_name, sub_solution in sub_solutions.items():
     725      if instance_name == binding_instance.name:
     726         binding_instance.load(sub_solution)
     727      else:
     728         scenario_instances[instance_name].load(sub_solution)
Note: See TracChangeset for help on using the changeset viewer.