Commit a7893b16 authored by Bowen Wu's avatar Bowen Wu
Browse files

Improve the mip solver status recognition

parent 4840ad58
......@@ -7,18 +7,24 @@ import glob
# A simple wrapper to suppress talkative APIs from printing
# Inspired from https://stackoverflow.com/a/45669280
class VerbosePrint:
def __init__(self, verbose) -> None:
def __init__(self, verbose, disable_stderr = False) -> None:
self.verbose = verbose
self.disable_stderr = disable_stderr
def __enter__(self):
if not self.verbose:
self._original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
if self.disable_stderr:
self._original_stderr = sys.stderr
sys.stderr = open(os.devnull, 'w')
def __exit__(self, exc_type, exc_val, exc_tb):
if not self.verbose:
sys.stdout.close()
sys.stdout = self._original_stdout
if self.disable_stderr:
sys.stderr = self._original_stderr
class bcolors:
HEADER = '\033[95m'
......
......@@ -2,7 +2,7 @@ import xml.etree.ElementTree as ET
from cplex import Cplex
import gurobipy as gp
from mps_util import VerbosePrint
from mip import *
import mip
# Solver status
OPTIMAL = 0
......@@ -187,19 +187,23 @@ class CBCSolver(Solver):
def __call__(self, mps_file: str) -> dict:
with VerbosePrint(False):
model = Model(sense=MINIMIZE, solver_name=CBC)
model = mip.Model()
model.read(mps_file)
status = model.optimize()
ret = dict()
if status == OptimizationStatus.OPTIMAL:
if status == mip.OptimizationStatus.OPTIMAL or \
status == mip.OptimizationStatus.FEASIBLE: #for now marking this as optimal as well
ret['status'] = OPTIMAL
ret["obj_val"] = model.objective_value
elif status == OptimizationStatus.FEASIBLE: #for now marking this as optimal as well
ret['status'] = OPTIMAL
ret["obj_val"] = model.objective_value
elif status == OptimizationStatus.NO_SOLUTION_FOUND:
elif status == mip.OptimizationStatus.NO_SOLUTION_FOUND or \
status == mip.OptimizationStatus.INFEASIBLE or \
status == mip.OptimizationStatus.INT_INFEASIBLE:
ret['status'] = INFEASIBLE
elif status == mip.OptimizationStatus.UNBOUNDED:
ret['status'] = UNBOUNDED
else:
ret["status"] = UNKNOWN
return ret
......
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