-
Notifications
You must be signed in to change notification settings - Fork 0
/
PySP.py
80 lines (59 loc) · 2.2 KB
/
PySP.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
from Crispin.bash import bash_command
from Crispin.context import temp_file
import Crispin.bash as bash
import time
from GBB import GBB, parse_pysp_output
def mpath(mthd, archive):
return 'models_{}/nodedata/{}.dat'.format(mthd, archive)
def nd_cat(mthd, nf, *ofs):
new_file = mpath(mthd, nf)
old_files = [mpath(mthd, of) for of in ofs]
bash.cat(new_file, *old_files)
def solve_ef(method='BigM', solver='gurobi', verbose=False):
nd_cat(method, 'ScenarioStructure', 'ScenarioStructureBase', 'ScenarioStructureEF')
cmd = ['runef',
'-m models_{}/models'.format(method),
'-i models_{}/nodedata'.format(method),
'--solver={}'.format(solver),
'--solve']
basic_command = ' '.join(cmd)
OBJ = parse_pysp_output(basic_command, verbose)
return OBJ
def solve_ph(method='BigM', solver='gurobi', verbose=False, WW=False):
nd_cat(method, 'ScenarioStructure', 'ScenarioStructureBase', 'ScenarioStructurePH')
cmd = ['runph',
'-m models_{}/models'.format(method),
'-i models_{}/nodedata'.format(method),
'--solver={}'.format(solver),
'--default-rho=1',
'--async',
'--output-solver-logs']
if WW:
cmd += ['--enable-ww-extensions',
'--ww-extension-cfgfile=config/wwph.cfg',
'--ww-extension-suffixfile=config/wwph.suffixes']
basic_command = ' '.join(cmd)
OBJ = parse_pysp_output(basic_command, verbose)
return OBJ
def RLT(method='PH', solver='gurobi', verbose=False):
t1 = time.time()
if method.upper() == 'PH':
results = solve_ph('RLT', solver, verbose)
elif method.upper() == 'EF':
results = solve_ef('RLT', solver, verbose)
else:
raise ValueError
t2 = time.time()
return results['obj'], t2 - t1
def BigM(method='PH', solver='gurobi', verbose=False):
t1 = time.time()
if method.upper() == 'PH':
results = solve_ph('BigM', solver, verbose)
elif method.upper() == 'EF':
results = solve_ef('BigM', solver, verbose)
else:
raise ValueError
t2 = time.time()
return results['obj'], t2 - t1
if '__main__' == __name__:
T1 = GBB(cutoff=True, gap=.0175)