-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolveByCplex.py
60 lines (50 loc) · 1.89 KB
/
solveByCplex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from __future__ import print_function
import sys
import cplex
from cplex.exceptions import CplexSolverError
def Solve(filename):
c = cplex.Cplex(filename)
try:
#c.set_results_stream(None)#close display
c.parameters.mip.tolerances.absmipgap.set(0)
c.solve()
except CplexSolverError:
print("Exception raised during solve")
return
# solution.get_status() returns an integer code
status = c.solution.get_status()
print(c.solution.status[status])
if status == c.solution.status.unbounded:
print("Model is unbounded")
return
if status == c.solution.status.infeasible:
print("Model is infeasible")
return
if status == c.solution.status.infeasible_or_unbounded:
print("Model is infeasible or unbounded")
return
s_method = c.solution.get_method()
s_type = c.solution.get_solution_type()
print("Solution status = ", status, ":", end=' ')
# the following line prints the status as a string
print(c.solution.status[status])
if s_type == c.solution.type.none:
print("No solution available")
return
print("Objective value = ", c.solution.get_objective_value())
print()
x = c.solution.get_values(0, c.variables.get_num() - 1)
# because we're querying the entire solution vector,
# x = c.solution.get_values()
# would have the same effect
#for j in range(c.variables.get_num()):
# if x[j]==1:
# print("Column %d: Value = %17.10g" % (j, x[j]))
#if __name__ == "__main__":
# if len(sys.argv) != 2:
# print("Usage: mipex2.py filename")
# print(" filename Name of a file, with .mps, .lp, or .sav")
# print(" extension, and a possible, additional .gz")
# print(" extension")
# sys.exit(-1)
# mipex2(sys.argv[1])