Changeset 3479


Ignore:
Timestamp:
Jan 15, 2011 7:41:24 PM (9 years ago)
Author:
khunter
Message:

Drive-by cleanup of PH option parsing, implement logging facility, mild rework of imports

Location:
coopr.pysp/trunk/coopr/pysp
Files:
2 edited

Legend:

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

    r2990 r3479  
    99#  _________________________________________________________________________
    1010
     11import logging
    1112import pyutilib.component.core
    1213
     
    2526from solutionwriter import *
    2627
     28import logging
     29from pyutilib.misc import LogHandler
     30
     31from os.path import abspath, dirname
     32coopr_base = dirname(dirname(dirname(dirname(dirname(abspath(__file__))))))
     33
     34Logger = logging.getLogger('coopr.pysp')
     35Logger.addHandler( LogHandler(coopr_base) )
     36Logger.setLevel( 30 )  # magic number, that /really/ needs to be set
     37                       # in a .conf file.  see coopr.pyomo/scripts/pyomo
     38
    2739pyutilib.component.core.PluginGlobals.pop_env()
    2840
  • coopr.pysp/trunk/coopr/pysp/ph.py

    r3478 r3479  
    99#  _________________________________________________________________________
    1010
     11import copy
     12import gc
     13import logging
     14import pickle
    1115import sys
     16import traceback
    1217import types
    13 from coopr.pyomo import *
    14 from coopr.pyomo.base.expr import *
    15 import copy
    16 import os.path
    17 import traceback
    18 import copy
    19 from coopr.opt import SolverResults,SolverStatus
     18import time
     19import types
     20
     21from math import fabs, log, exp
     22from os import path
     23
     24from coopr.opt import SolverResults, SolverStatus
    2025from coopr.opt.base import SolverFactory
    2126from coopr.opt.parallel import SolverManagerFactory
    22 import time
    23 import types
    24 import pickle
    25 import gc
    26 from math import fabs, log, exp
    27 
    28 from scenariotree import *
     27from coopr.pyomo import *
     28from coopr.pyomo.base.expr import Expression, _IdentityExpression
     29from coopr.pysp.phextension import IPHExtension
     30
    2931from phutils import *
    3032from phobjective import *
     33from scenariotree import *
    3134
    3235from pyutilib.component.core import ExtensionPoint
    3336
    34 from coopr.pysp.phextension import IPHExtension
     37Logger = logging.getLogger('coopr.pysp')
    3538
    3639class ProgressiveHedging(object):
     
    471474
    472475      # process the keyword options
     476      self._max_iterations         = kwds.pop( 'max_iterations', self._max_iterations )
     477      self._rho                    = kwds.pop( 'rho', self._rho )
     478      self._rho_setter             = kwds.pop( 'rho_setter', self._rho_setter )
     479      self._bounds_setter          = kwds.pop( 'bounds_setter', self._bounds_setter )
     480      self._solver_type            = kwds.pop( 'solver', self._solver_type )
     481      self._solver_manager_type    = kwds.pop( 'solver_manager', self._solver_manager_type )
     482      scenario_solver_options      = kwds.pop( 'scenario_solver_options', scenario_solver_options )
     483      self._mipgap                 = kwds.pop( 'scenario_mipgap', self._mipgap )
     484      self._keep_solver_files      = kwds.pop( 'keep_solver_files', self._keep_solver_files )
     485      self._output_solver_results  = kwds.pop( 'output_solver_results', self._output_solver_results )
     486      self._output_solver_log      = kwds.pop( 'output_solver_log', self._output_solver_log )
     487      self._verbose                = kwds.pop( 'verbose', self._verbose )
     488      self._report_solutions       = kwds.pop( 'report_solutions', self._report_solutions )
     489      self._report_weights         = kwds.pop( 'report_weights', self._report_weights )
     490      self._report_only_statistics = kwds.pop( 'report_only_statistics', self._report_only_statistics )
     491      self._output_times           = kwds.pop( 'output_times', self._output_times )
     492      self._disable_warmstarts     = kwds.pop( 'disable_warmstarts', self._disable_warmstarts )
     493      self._drop_proximal_terms    = kwds.pop( 'drop_proximal_terms', self._drop_proximal_terms )
     494      self._retain_quadratic_binary_terms     = kwds.pop( 'retain_quadratic_binary_terms', self._retain_quadratic_binary_terms )
     495      self._linearize_nonbinary_penalty_terms = kwds.pop( 'linearize_nonbinary_penalty_terms', self._linearize_nonbinary_penalty_terms )
     496      self._breakpoint_strategy    = kwds.pop( 'breakpoint_strategy', self._breakpoint_strategy )
     497      self._checkpoint_interval    = kwds.pop( 'checkpoint_interval', self._checkpoint_interval )
     498      self._output_scenario_tree_solution = kwds.pop( 'output_scenario_tree_solution', self._output_scenario_tree_solution )
     499      self._simplify_expressions   = kwds.pop( 'simplify_expressions', self._simplify_expressions )
     500
    473501      for key in kwds.keys():
    474          if key == "max_iterations":
    475             self._max_iterations = kwds[key]
    476          elif key == "rho":
    477             self._rho = kwds[key]
    478          elif key == "rho_setter":
    479             self._rho_setter = kwds[key]
    480          elif key == "bounds_setter":
    481             self._bounds_setter = kwds[key]
    482          elif key == "solver":
    483             self._solver_type = kwds[key]
    484          elif key == "solver_manager":
    485             self._solver_manager_type = kwds[key]
    486          elif key == "scenario_solver_options":
    487             scenario_solver_options = kwds[key]
    488          elif key == "scenario_mipgap":
    489             self._mipgap = kwds[key]
    490          elif key == "keep_solver_files":
    491             self._keep_solver_files = kwds[key]
    492          elif key == "output_solver_results":
    493             self._output_solver_results = kwds[key]
    494          elif key == "output_solver_log":
    495             self._output_solver_log = kwds[key]
    496          elif key == "verbose":
    497             self._verbose = kwds[key]
    498          elif key == "report_solutions":
    499             self._report_solutions = kwds[key]
    500          elif key == "report_weights":
    501             self._report_weights = kwds[key]
    502          elif key == "report_only_statistics":
    503             self._report_only_statistics = kwds[key]
    504          elif key == "output_times":
    505             self._output_times = kwds[key]
    506          elif key == "disable_warmstarts":
    507             self._disable_warmstarts = kwds[key]
    508          elif key == "drop_proximal_terms":
    509             self._drop_proximal_terms = kwds[key]
    510          elif key == "retain_quadratic_binary_terms":
    511             self._retain_quadratic_binary_terms = kwds[key]
    512          elif key == "linearize_nonbinary_penalty_terms":
    513             self._linearize_nonbinary_penalty_terms = kwds[key]
    514          elif key == "breakpoint_strategy":
    515             self._breakpoint_strategy = kwds[key]
    516          elif key == "checkpoint_interval":
    517             self._checkpoint_interval = kwds[key]
    518          elif key == "output_scenario_tree_solution":
    519             self._output_scenario_tree_solution = kwds[key]
    520          elif key == "simplify_expressions":
    521             self._simplify_expressions = kwds[key]
    522          else:
    523             print "Unknown option=" + key + " specified in call to PH constructor"
     502         msg = "PH constructor received Unknown option: %s"
     503         Logger.warning( msg % key )
    524504
    525505      # validate all "atomic" options (those that can be validated independently)
     
    541521      # validate rho setter file if specified.
    542522      if self._rho_setter is not None:
    543          if os.path.exists(self._rho_setter) is False:
     523         if path.exists(self._rho_setter) is False:
    544524            raise ValueError, "The rho setter script file="+self._rho_setter+" does not exist"
    545525
    546526      # validate bounds setter file if specified.
    547527      if self._bounds_setter is not None:
    548          if os.path.exists(self._bounds_setter) is False:
     528         if path.exists(self._bounds_setter) is False:
    549529            raise ValueError, "The bounds setter script file="+self._bounds_setter+" does not exist"
    550530
     
    608588         print "   Scenario data directory=" + scenario_data_directory_name
    609589
    610       if not os.path.exists(scenario_data_directory_name):
     590      if not path.exists(scenario_data_directory_name):
    611591         raise ValueError, "Scenario data directory=" + scenario_data_directory_name + " either does not exist or cannot be read"
    612592
Note: See TracChangeset for help on using the changeset viewer.