Commit ba9b9f68 authored by Ambi's avatar Ambi
Browse files

[Time Limit] Added timelimit to optimization of both gurobi and cplex. Set...

[Time Limit] Added timelimit to optimization of both gurobi and cplex. Set current time limit to 20s
parent 4d12f53f
......@@ -10,21 +10,25 @@ INFEASIBLE = 1
INF_OR_UNBD = 2
UNBOUNDED = 3
UNKNOWN = 4
TIME_LIMIT_EXCEEDED = 5
status_text = {
OPTIMAL : "optimal",
INFEASIBLE : "infeasible",
INF_OR_UNBD : "infeasible or unbounded",
UNBOUNDED : "unbounded",
UNKNOWN : "unknown"
UNKNOWN : "unknown",
TIME_LIMIT_EXCEEDED : "time limit exceeded",
}
MAX_TIME_LIMIT = 20
class Solver(object):
def __init__(self, verbose = False) -> None:
# by default, the solvers are very talky but we can make it less verbose
self.verbose = verbose
self.stats = dict()
for k in [OPTIMAL, INFEASIBLE, INF_OR_UNBD, UNBOUNDED, UNKNOWN]:
for k in [OPTIMAL, INFEASIBLE, INF_OR_UNBD, UNBOUNDED, UNKNOWN, TIME_LIMIT_EXCEEDED]:
self.stats[k] = 0
def print_solver_stats(self) -> None:
......@@ -36,13 +40,16 @@ class CplexSolver(Solver):
# https://www.tu-chemnitz.de/mathematik/discrete/manuals/cplex/doc/refman/html/appendixB.html
status_code = {
# LP
1 : OPTIMAL,
2 : UNBOUNDED,
3 : INFEASIBLE,
4 : INF_OR_UNBD,
1 : OPTIMAL,
2 : UNBOUNDED,
3 : INFEASIBLE,
4 : INF_OR_UNBD,
11 : TIME_LIMIT_EXCEEDED,
# MIP
101 : OPTIMAL,
102 : OPTIMAL,
107 : TIME_LIMIT_EXCEEDED,
108 : TIME_LIMIT_EXCEEDED,
115 : INFEASIBLE, # https://www-eio.upc.edu/lceio/manuals/cplex-11/html/usrcplex/solveLP19.html
103 : INFEASIBLE,
118 : UNBOUNDED,
......@@ -58,6 +65,9 @@ class CplexSolver(Solver):
model.set_warning_stream(None)
model.set_error_stream(None)
model.set_log_stream(None)
ps = model.create_parameter_set()
ps.add(model.parameters.timelimit, MAX_TIME_LIMIT)
model.set_parameter_set(ps)
with VerbosePrint(self.verbose):
model.read(mps_file)
......@@ -129,7 +139,8 @@ class GurobiSolver(Solver):
2 : OPTIMAL,
3 : INFEASIBLE,
4 : INF_OR_UNBD,
5 : UNBOUNDED
5 : UNBOUNDED,
9 : TIME_LIMIT_EXCEEDED,
}
def __init__(self, verbose = False) -> None:
......@@ -139,6 +150,7 @@ class GurobiSolver(Solver):
with VerbosePrint(self.verbose):
model = gp.read(mps_file)
model.setParam("OutputFlag", False)
model.setParam(gp.GRB.Param.TimeLimit, MAX_TIME_LIMIT)
model.optimize()
if self.get_sol_status(model) == INF_OR_UNBD:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment