model.write("solution.mst") only works once Gurobi has
found an optimal solution. It won't write out incumbent solutions.
> Is GRB.Callback.MIPSOL always invoked, when a new feasible solution is
> deiscovered, or only when a new "best solution" is found?
As described in the Callback code section of the documentation
http://www.gurobi.com/doc/46/refman/node596.html#sec:CallbackCodes
the MIPSOL callback is only called when a new incumbent, or
a new "best solution" is found.
> Has anyone a better idea to make my problem crash-resistent?
Gurobi shouldn't crash when performing long solves. But
sometimes I lose my network connection, and accidentally
lose results from a solve on a remote machine. The .mst format is
very simple. See the description here:
http://www.gurobi.com/doc/46/refman/node604.html
So it is easy to write your own function in python
to save MIP start file.
Here is something I threw together by modifying the
python example callback.py
http://www.gurobi.com/doc/46/examples/node112.html
The function simple_mst_writer creates a .mst file
with the incumbent solution.
Hope that helps,
Chris
#!/usr/bin/python
import sys
from gurobipy import *
# Define callback function
def mycallback(model, where):
if where == GRB.callback.MIPSOL:
obj = model.cbGet(GRB.callback.MIPSOL_OBJ)
nodecnt = int(model.cbGet(GRB.callback.MIPSOL_NODCNT))
print 'Found incumbent soln at node', nodecnt, 'objective', obj
simple_mst_writer(model, 'solution.mst', nodecnt, obj)
# A simple MIP start file writer
def simple_mst_writer(model, mstfilename, nodecnt, obj):
mstfile = open(mstfilename, 'w')
varlist = model.getVars()
soln = model.cbGetSolution(varlist)
mstfile.write('# MIP start from soln at node %d objective %e\n' %
(nodecnt, obj))
for var, soln in zip(varlist, soln):
mstfile.write('%s %.16e\n' % (var.VarName, soln))
mstfile.close()
if len(sys.argv) < 2:
print 'Usage: callback.py filename'
quit()
# Read and solve model
model = read(sys.argv[1])
model.optimize(mycallback)
# Throw away solution
model.reset()
print '\n\nOptimizing from previous incumbent solution\n'
# load the MIP start file
model.read('solution.mst')
# Reoptimize
model.optimize()
Actually that is not quite true. If you terminate the solve early
with a sub-optimal solution, model.write() will write the
solution to a start. The issue is that you can't call model.write()
from within a callback. This is why you need to write your
own MIP start file using python code.
> Gurobi shouldn't crash when performing long solves.
And I should add, if Gurobi does really crash, please report it.
Cheers,
Chris
Yep. In version 4.5 it is GRB.Callback. Version 4.6 is backwards
compatible so you can still use GRB.Callback, or the new
GRB.callback.
Chris