Storing gap and runtime
Awaiting user inputGood morning, I was hoping to as a question on storing gap, and runtime. I use the following command:
#storing computation time and gap
def data_cb(model, where):
if where == gp.GRB.Callback.MIP:
cur_obj = model.cbGet(gp.GRB.Callback.MIP_OBJBST)
cur_bd = model.cbGet(gp.GRB.Callback.MIP_OBJBND)
cur_time = model.cbGet(GRB.Callback.RUNTIME)
model._data.append([cur_time, cur_obj, cur_bd])
network_size = [200, 500]
running_count = 10 # number of different seeds used
for ns in network_size:
for r in range(1,running_count+1):
m.Params.TimeLimit = 6*60*60 #run for 6 hours maximum
m.Params.MIPGap = 0
# Solve the model
m._obj = None
m._bd = None
m._data = []
m._start=time.time()
m.optimize(callback = data_cb)
#m.terminate()
# Output Reesults
m.write('linear_model.lp')
gap_and_bound = m._data[-1]
final_gap = (abs(gap_and_bound[1] - gap_and_bound[2])/abs(gap_and_bound[1]))*100
duration1 = gap_and_bound[0]
if m.Status != GRB.Status.OPTIMAL:
print('Optimization was terminated before the optimal with status ' + str(m.Status))
# sys.exit(0)
# Print number of solutions stored
nSolutions = m.SolCount
print('Number of solutions found: ' + str(nSolutions))
m.params.ObjNumber = 0
welfare_sheltering_occupancy_level = - m.ObjNVal
print('Total welfare-oriented sheltering capacity =', welfare_sheltering_occupancy_level, end='\n')
m.params.ObjNumber = 1
Total_cost = round(m.ObjNVal, 2)
print('Total cost =', Total_cost, end='\n')
Overall_obj_value = W2*Total_cost - W1*welfare_sheltering_occupancy_level
print('Overall obj value =', Overall_obj_value)
model_output.loc[r-1] = summary_result
print(model_output.loc[r-1])
model_output.to_excel(my_folder+"/Outcome_Summary_Location" + str(ns) + ".xlsx", sheet_name = 'Network'+str(ns))
When I run the model, I get this error: list index out of range for m._data[-1]. It does not happen all the time, but it does happen for some occasions.
Any help would be appreciated!
Another question I have is that the r loop completely stops when there is no optimal solution. Is there a way to jump to the next iteration even when no solution can be found?
Thank you so much for your time and help.
Sincerely,
Min
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Min,
When I run the model, I get this error: list index out of range for m._data[-1]. It does not happen all the time, but it does happen for some occasions.
It is possible that a model is solved in Presolve and thus will never reach the MIP callback. Could you show the log output of a case where this error occurs?
In such cases you should always first check the size of \(\texttt{m._data}\) via \(\texttt{len(m.data)}\). If the length is \(0\) but the status is \(\texttt{GRB.OPTIMAL}\), you can still compute one data point via accessing the model attributes ObjVal, ObjBound, and RunTime.
Another question I have is that the r loop completely stops when there is no optimal solution. Is there a way to jump to the next iteration even when no solution can be found?
I don't see why the loop should completely stop. The sys.exit line is commented out. Couldn't you just use the \(\texttt{continue}\) keyword in the non-optimal case?
Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments