-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_primals.py
More file actions
69 lines (52 loc) · 1.71 KB
/
get_primals.py
File metadata and controls
69 lines (52 loc) · 1.71 KB
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
import os
import re
import numpy as np
import scipy.io
def parseLog(logpath):
lines = []
with open(logpath,'r') as f:
lines = f.readlines()
primals = []
ts = []
for line in lines:
if 'Found incumbent of value' in line:
ss = re.findall('\d+.\d+',line)
primal,t = float(ss[0]),float(ss[1])
t = int(t+0.5)
if t>=len(primals) and len(primals)>0:
for i in range(t - len(primals)-1):
primals.append(primals[-1])
primals.append(primal)
return primals
dirs = os.listdir('.')
for di in dirs:
if not os.path.isdir(di) or di[0]=='.':
continue
if '-r' not in di:
continue
fileDirs = os.listdir(di)
for fileDir in fileDirs:
if (not os.path.isdir(os.path.join(di,fileDir))) or ('exp-' not in fileDir):
continue
file_list = os.listdir(os.path.join(di,fileDir))
allPrimals = []
for filename in file_list:
if '.log' not in filename:
continue
logpath = os.path.join(di,fileDir,filename)
primals = parseLog(logpath)
allPrimals.append(primals)
# align
maxT = max([len(prims) for prims in allPrimals])
maxT = 3600
for ind,prim in enumerate(allPrimals):
if len(prim)<maxT:
prim += [prim[-1]]*(maxT-len(prim))
allPrimals[ind] = np.array(prim)
elif len(prim)>maxT:
allPrimals[ind] = np.array(prim[0:maxT])
allPrimals = np.array(allPrimals)
scipy.io.savemat(os.path.join(di,fileDir,'primals.mat'),{
'primals':allPrimals
})
print('done')