Changeset 3204


Ignore:
Timestamp:
Oct 29, 2010 4:46:39 PM (10 years ago)
Author:
jwatson
Message:

Flushing out MIP warm-start capability for GUROBI plugin.

Location:
coopr.plugins/trunk/coopr/plugins/mip
Files:
2 edited

Legend:

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

    r3170 r3204  
    6868        self._capabilities.sos2 = True
    6969
     70    def warm_start_capable(self):
     71
     72       return True
     73
    7074    #
    71     # GUROBI warm-start capability is not implemented
     75    # write a warm-start file in the GUROBI MST format, which is *not* the same as the CPLEX MST format.
    7276    #
    73     def warm_start_capable(self):
    74 
    75        return False
     77    def warm_start(self, instance):
     78
     79       self.warm_start_file_name = pyutilib.services.TempfileManager.create_tempfile(suffix = '.gurobi.mst')
     80
     81       mst_file = open(self.warm_start_file_name,'w')
     82
     83       # for each variable, add a child to the variables element.
     84       # both continuous and discrete are accepted (and required,
     85       # depending on other options), according to the CPLEX manual.
     86       for variable in instance.active_components(Var).values():
     87
     88          for index in variable._varval.keys():
     89
     90             if (variable[index].status != VarStatus.unused) and (variable[index].value != None) and (variable[index].fixed == False):
     91
     92                name = variable[index].label
     93                name = name.replace('[','(')
     94                name = name.replace(']',')')
     95                print >>mst_file, name, variable[index].value
     96
     97       mst_file.close()
     98
     99    # over-ride presolve to extract the warm-start keyword, if specified.
     100    def _presolve(self, *args, **kwds):
     101
     102       # if the first argument is a string (representing a filename),
     103       # then we don't have an instance => the solver is being applied
     104       # to a file.
     105
     106       self.warm_start_solve = False               
     107       if "warmstart" in kwds:
     108          self.warm_start_solve = kwds["warmstart"]
     109          del kwds["warmstart"]
     110
     111       if (len(args) > 0) and (isinstance(args[0],basestring) is False):
     112
     113          # write the warm-start file - currently only supports MIPs.
     114          # we only know how to deal with a single problem instance.       
     115          if self.warm_start_solve is True:
     116
     117             if len(args) != 1:
     118                raise ValueError, "GUROBI _presolve method can only handle a single problem instance - "+str(len(args))+" were supplied"                 
     119
     120             if args[0].has_discrete_variables() is True:
     121                start_time = time.time()
     122                self.warm_start(args[0])
     123                end_time = time.time()
     124                if self._report_timing is True:
     125                   print "Warm start write time="+str(end_time-start_time)+" seconds"
     126         
     127       # let the base class handle any remaining keywords/actions.
     128       ILMLicensedSystemCallSolver._presolve(self, *args, **kwds)
    76129
    77130    def executable(self):
     
    108161        problem_filename = self._problem_files[0]
    109162        solution_filename = self.soln_file
     163        warmstart_filename = self.warm_start_file_name
    110164        if sys.platform == 'win32':
    111165           problem_filename  = problem_filename.replace('\\', r'\\')
    112166           solution_filename = solution_filename.replace('\\', r'\\')
     167           if self.warm_start is True:
     168              warmstart_filename = warmstart_filename.replace('\\', r'\\')
    113169
    114170        # translate the options into a normal python dictionary, from a
     
    127183        script += "sys.path.append('%s')\n" % os.path.dirname(__file__)
    128184        script += "from GUROBI_RUN import *\n"
    129         script += "gurobi_run%s\n" % str((problem_filename, solution_filename, self.mipgap, options_dict))
     185        script += "gurobi_run%s\n" % str((problem_filename, warmstart_filename, solution_filename, self.mipgap, options_dict))
    130186        script += "quit()\n"
    131187
     
    139195        if self.keepFiles:
    140196           print "Solver script file: '%s'" % self.gurobi_script_file_name
     197           if (self.warm_start_solve is True) and (self.warm_start_file_name is not None):
     198              print "Solver warm-start file: " + self.warm_start_file_name
    141199
    142200        #
  • coopr.plugins/trunk/coopr/plugins/mip/GUROBI_RUN.py

    r3170 r3204  
    1212from gurobipy import *
    1313
    14 def gurobi_run(model_file, soln_file, mipgap, options):
     14def gurobi_run(model_file, warmstart_file, soln_file, mipgap, options):
    1515
    1616    # Load the lp model
    1717    model = read(model_file)
     18
     19    if warmstart_file is not None:
     20       model.read(warmstart_file)
    1821
    1922    # set the mipgap if specified.
Note: See TracChangeset for help on using the changeset viewer.