Changeset 2281


Ignore:
Timestamp:
Feb 8, 2010 1:26:54 PM (10 years ago)
Author:
jwatson
Message:

Adding a suffixes attribute to the base solver class. This is a list of suffixes that the solver will - assuming it can - load into a solution. Examples are rc, dual, slack, etc. I modified the derived solver plugin classes to throw an exception if provided a suffix that it can't handle. I have little doubt that this change will cause some tests to fail - I'll look at this in a bit. The change was motivated by the expense of querying a solution, loading a solution, and shipping a lot of unnecessary information across the network.

Location:
coopr.plugins/trunk/coopr/plugins
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • coopr.plugins/trunk/coopr/plugins/mip/CBC.py

    r2201 r2281  
    257257    def process_soln_file(self,results):
    258258
     259        # the only suffixes that we extract from CBC are
     260        # constraint duals and variable reduced-costs. scan
     261        # through the solver suffix list and throw an
     262        # exception if the user has specified any others.
     263        extract_duals = False
     264        extract_reduced_costs = False
     265        for suffix in self.suffixes:
     266           if suffix == "dual":
     267              extract_duals = True
     268           elif suffix == "rc":
     269              extract_reduced_costs = True
     270           else:
     271              raise RuntimeError,"***CBC solver plugin cannot extract solution suffix="+suffix
     272
    259273        # if dealing with SOL format files, we've already read
    260274        # this via the base class reader functionality.
     
    286300                raise RuntimeError, "CBC encountered unexpected line=("+line.strip()+") in solution file="+self.soln_file+"; constraint and variable sections already processed!"
    287301
    288           if processing_constraints is True:
     302          if (processing_constraints is True) and (extract_duals is True):
    289303             constraint = tokens[1]
    290304             constraint_ax = eval(tokens[2]) # CBC reports the constraint row times the solution vector - not the slack.
     
    296310             variable_name = tokens[1]
    297311             variable_value = eval(tokens[2])
    298              variable_reduced_cost = eval(tokens[3]) # currently ignored.
    299 
    300312             solution.variable[variable_name].value = variable_value
    301              solution.variable[variable_name].rc = variable_reduced_cost
     313
     314             if extract_reduced_costs is True:
     315                variable_reduced_cost = eval(tokens[3]) # currently ignored.
     316                solution.variable[variable_name].rc = variable_reduced_cost
    302317          else:
    303318             raise RuntimeError, "CBC encountered unexpected line=("+line.strip()+") in solution file="+self.soln_file+"; expecting header, but found data!"
  • coopr.plugins/trunk/coopr/plugins/mip/CPLEX.py

    r2212 r2281  
    341341
    342342    def process_soln_file(self,results):
    343         lp_solution=False
     343
     344        # the only suffixes that we extract from CPLEX are
     345        # constraint duals, constraint slacks, and variable
     346        # reduced-costs. scan through the solver suffix list
     347        # and throw an exception if the user has specified
     348        # any others.
     349        extract_duals = False
     350        extract_reduced_costs = False
     351        for suffix in self.suffixes:
     352           if suffix == "dual":
     353              extract_duals = True
     354           elif suffix == "rc":
     355              extract_reduced_costs = True
     356           else:
     357              raise RuntimeError,"***CPLEX solver plugin cannot extract solution suffix="+suffix
     358
     359        lp_solution = False
    344360        if not os.path.exists(self.soln_file):
    345361           return
     
    368384                   elif field_name == "value":
    369385                      variable_value = field_value
    370                    elif field_name == "reducedCost":
     386                   elif (extract_reduced_costs is True) and (field_name == "reducedCost"):
    371387                      variable_reduced_cost = field_value
    372                    elif field_name == "status":
     388                   elif (extracted_reduced_costs is True) and (field_name == "status"):
    373389                      variable_status = field_value
    374390
     
    381397                   except:
    382398                       variable.value = variable_value
    383                    if not variable_reduced_cost is None:
     399                   if (variable_reduced_cost is not None) and (extract_reduced_costs is True):
    384400                        try:
    385401                            variable.rc = eval(variable_reduced_cost)
     
    395411                        except:
    396412                            raise ValueError, "Unexpected reduced-cost value="+str(variable_reduced_cost)+" encountered for variable="+variable_name
    397             elif tokens[0] == "constraint":
     413            elif (tokens[0] == "constraint") and ((extract_duals is True) or (extract_slacks is True)):
    398414                constraint_name = None
    399415                constraint_dual = None
     
    405421                      constraint_name = field_value
    406422                      constraint = soln.constraint[constraint_name]
    407                    elif field_name == "dual": # for LPs
     423                   elif (extract_duals is True) and (field_name == "dual"): # for LPs
    408424                      # assumes the name field is first.
    409425                      if eval(field_value) != 0.0:
    410426                        constraint.dual = eval(field_value)
    411                    elif field_name == "slack": # for MIPs
     427                   elif (extract_slacks is True) and (field_name == "slack"): # for MIPs
    412428                      # assumes the name field is first.
    413429                      if eval(field_value) != 0.0:
  • coopr.plugins/trunk/coopr/plugins/mip/GLPK.py

    r2201 r2281  
    213213    def process_soln_file(self,results):
    214214
     215        # the only suffixes that we extract from GLPK are
     216        # constraint duals. scan through the solver suffix
     217        # list and throw an exception if the user has
     218        # specified any others.
     219        extract_duals = False
     220        for suffix in self.suffixes:
     221           if suffix != "dual":
     222              raise RuntimeError,"***GLPK solver plugin cannot extract solution suffix="+suffix
     223           else:
     224              extract_duals = True
     225
    215226        lp_solution = True # if false, we're dealing with a MIP!
    216227        if not os.path.exists(self.soln_file):
     
    361372                   if index != number_of_constraints_read:
    362373                      raise ValueError,"***ERROR: Unexpected constraint index encountered on line="+line+"; expected value="+str(number_of_constraints_read)+"; actual value="+str(index)
    363 
     374   
    364375                if lp_solution is True:
    365376                   # GLPK doesn't report slacks directly.
     
    372383                      raise ValueError, "Unknown status="+tokens[0]+" encountered for constraint="+active_constraint_name+" in line="+line+" of solution file="+self.soln_file
    373384
    374                    soln.constraint[constraint_name].dual = constraint_dual
     385                   if extract_duals is True:
     386                      soln.constraint[constraint_name].dual = constraint_dual
    375387                 
    376388                else:
  • coopr.plugins/trunk/coopr/plugins/mip/PICO.py

    r2201 r2281  
    188188
    189189    def process_soln_file(self,results):
    190         #print "HERE",self._results_format, self._results_format is ResultsFormat.sol, self.results_reader
     190
    191191        if self._results_format is ResultsFormat.sol:
    192192           return
     193
     194        # the only suffixes that we extract from PICO are
     195        # constraint duals. scan through the solver suffix
     196        # list and throw an exception if the user has
     197        # specified any others.
     198        extract_duals = False
     199        for suffix in self.suffixes:
     200           if suffix == "dual":
     201              extract_duals = True
     202           else:
     203              raise RuntimeError,"***PICO solver plugin cannot extract solution suffix="+suffix
    193204
    194205        #if os.path.exists(self.sol_file):
     
    225236                print "ERROR", line,tokens
    226237            tmp.append( (tokens[0],eval(tokens[2])) )
    227         if lp_flag:
     238        if (lp_flag is True) and (extract_duals is True):
    228239            for (var,val) in tmp:
    229240                soln.constraint[var].dual = val
  • coopr.plugins/trunk/coopr/plugins/smanager/pyro.py

    r2201 r2281  
    8787                                 file=problem_file_string, filename=self._opt._problem_files[0], \
    8888                                 warmstart_file=warm_start_file_string, warmstart_filename=warm_start_file_name, \
    89                                  kwds=kwds, solver_options=solver_options, mipgap=self._opt.mipgap)
     89                                 kwds=kwds, solver_options=solver_options, mipgap=self._opt.mipgap, suffixes=self._opt.suffixes)
    9090        task = pyutilib.pyro.Task(data=data, id=ah.id)
    9191        self.client.add_task(task)
Note: See TracChangeset for help on using the changeset viewer.