-
Notifications
You must be signed in to change notification settings - Fork 1
/
commons.py
157 lines (138 loc) · 5.69 KB
/
commons.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
from os import name as osname, path, sep
# from pyomo.opt import SolverStatus, TerminationCondition
# pyomo.util.timing.report_timing() to timeit the pyomo objects.
"""
Caution: Always use the intrinsic functions that are part of the Pyomo package.
from pyomo.environ import * # imports, e.g., pyomo versions of exp, log, etc.)
from math import * # overrides the pyomo versions with math versions
expr = acos(model.x)
"""
import logging
import sys
from inspect import stack
from pyomo import environ as pe
from pyomo.version.info import version_info as pyomoversion
modpath = path.abspath(__file__)
dir_path = path.dirname(modpath)
sys.path.append(dir_path)
initpathlist = path.realpath(__file__).split(sep)
PROJ_PATH = sep.join(initpathlist[:-1])
EXP = initpathlist[-2]
SOLVERS_PATH = sep.join(initpathlist[:-1] + ['solvers'])
print(SOLVERS_PATH)
scippath = path.join(SOLVERS_PATH, 'scipampl601')
winscippath = path.join(SOLVERS_PATH, 'scipampl700.exe')
def check_expr(exprarg):
# supports Pyomo 5.6.5
# pyomo >= 5.6: is_expression_type(); pyomo <=5.5: is_expression()
if pyomoversion[1] >= 6:
if exprarg.is_expression_type():
return True
elif pyomoversion[1] == 5:
if exprarg.is_expression():
return True
logging.warning('No expression returned by expr_method: %s called by the parent %s' %
(stack()[0][3], stack()[1][3]))
return False
def print_vars_debug(m, vartype=pe.Constraint, c1='$', c2='%'):
# Displays constraints upper bound lower bound and expression.
# m.display()
# debugging: constraints defined in the model.
# logging.debug(peobj.model.display())
s1 = c1 * 80
s2 = c2 * 80
for con in m.component_map(vartype).itervalues():
# for con in m.component_map(vartype).itervalues():
print(s1)
con.pprint()
print(s2)
def print_constr_debug(m, constr_name, c1='$', c2='%'):
# Displays constraints upper bound lower bound and expression.
# m.display()
# debugging: constraints defined in the model.
# logging.debug(peobj.model.display())
s1 = c1 * 80
s2 = c2 * 80
con = getattr(m, constr_name)
print(s1)
con.pprint()
print(s2)
def print_bad_constr(model, log_fpath):
print_vars_debug(model, pe.Constraint, '^', '#')
print_vars_debug(model, pe.Var, '&', '+')
# read the log file created by solver and look for text '<_scon[' to find the failed constr.
# this logic is specific to scip solver's log.
f = open(log_fpath, 'r')
lines = f.read()
pos1 = lines.find('<_scon[')
pos2 = lines.find(']', pos1)
try:
# result_dict['failed_constr_num'] = int(lines[pos1 + 7: pos2])
print('@' * 20)
print(lines)
print('@' * 30)
except ValueError as e:
logging.warning('parser failed to find questionable constraint in logfile: %s' % log_fpath)
pass
def run_solver(model, stype='scip', tee=False):
if osname == 'nt':
if stype =='scip':
solver = pe.SolverFactory('scip6', executable=winscippath, solver_io='nl')
else:
solver = pe.SolverFactory("gurobi", solver_io="python", executable=r'C:\gurobi902\win64\bin\gurobi_cl.exe')
else:
if stype =='scip':
solver = pe.SolverFactory('scip6', executable=scippath, solver_io='nl')
else:
print('gurobi not installed for linux. exiting')
raise ValueError
logging.info('starting solver...')
results = solver.solve(model, keepfiles=True, tee=tee, report_timing=False, load_solutions=False)
logging.info('finished running solver...')
log_fpath = solver._log_file
solver_status = results.solver.status.__str__()
solver_term_cond = results.solver.termination_condition.__str__()
print('solver message:%s' % results.solver.message)
print('solver_status:', solver_status)
print('solver_term_cond:', solver_term_cond)
if solver_term_cond == 'optimal':
if len(results.solution) > 0:
model.solutions.load_from(results)
print('objective value: %d' % model.objective())
return results, log_fpath
else:
# read the log file created by solver and look for text '<_scon[' to find the failed constr.
# this logic is specific to scip solver's log.
print('optimization failed.')
f = open(log_fpath, 'r')
lines = f.read()
pos1 = lines.find('<_scon[')
pos2 = lines.find(']', pos1)
try:
print('failed_constr_num', int(lines[pos1 + 7: pos2]))
print('SOLVER LOG:\n', '<>'*10, lines, '\n', '<>'*10 )
except ValueError as e:
logging.warning('parser failed to find questionable constraint in logfile: %s' % log_fpath)
pass
return None, None
# result_dict['term_cond'] = results.solver.termination_condition.__str__()
# result_dict['solver_status'] = results.solver.status.__str__()
# logging.info('solvername #: %s termination_condition: %s; status: %s' %
# (slvid, result_dict['term_cond'], result_dict['solver_status']))
# if result_dict['solver_status'] == 'ok':
# print('SUCCESS WITH SOLVER: %s' % slvid)
# if len(results.solution) > 0:
# pebidobj.model.solutions.load_from(results)
# result_dict['succ_solverid'] = slvid
def output_to_display(m, list_varnames):
from pprint import pprint
output_dict = {}
for vname in list_varnames:
varval = getattr(m, vname)
output_tuples = []
for key in varval:
# output_tuples.append((key + (varval[key].value,)))
output_tuples.append(([key] + [varval[key].value, ]))
output_dict[vname] = output_tuples
pprint(output_dict)