-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.py
More file actions
315 lines (279 loc) · 12.7 KB
/
Util.py
File metadata and controls
315 lines (279 loc) · 12.7 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import pandas as pd
import numpy as np
import os
import gc
from os.path import exists
from scipy.stats import norm
import matplotlib.mlab as mlab
from jssp.JSSPFactory import JSSPFactory
class Util:
@staticmethod
def createAllResultsFile(resultFolder):
allDict = {
'instance': [],
'run': [],
'jobs': [],
'machines': [],
'hc': [],
'ffa': [],
'bks': [],
}
library = Util.readFullLibrary('files/jssp-instances/full_full_library.txt')
bestKnown = Util.readFullLibraryBestKnown('files/jssp-instances/full_full_library.txt')
for run in os.listdir(resultFolder):
for subFolder in os.listdir(resultFolder + run):
jssp = JSSPFactory.generateJSSPFromFormat(library[subFolder])
hcFile = open(resultFolder + run + "/" + subFolder + "/hc.txt")
ffaFile = open(resultFolder + run + "/" + subFolder + "/fhc.txt")
hcResult = int(hcFile.read())
ffaResult = int(ffaFile.read())
allDict['instance'].append(subFolder)
allDict['run'].append(run)
allDict['jobs'].append(jssp.amountJobs)
allDict['machines'].append(jssp.amountMachines)
allDict['hc'].append(hcResult)
allDict['ffa'].append(ffaResult)
allDict['bks'].append(bestKnown[subFolder])
df = pd.DataFrame(allDict)
df.to_csv('allResults.csv', index=False)
@staticmethod
def createFunctionEvaluationsPlot():
populationsFolder = 'files/output/hc2/populations/'
library = Util.readFullLibrary('files/jssp-instances/full_full_library.txt')
bestKnown = Util.readFullLibraryBestKnown('files/jssp-instances/full_full_library.txt')
for run in os.listdir(populationsFolder):
for subFolder in os.listdir(populationsFolder + run):
if (exists('files/output/hc2/results/' + run + "/" + subFolder + "/plot.png")):
print(subFolder + " exists!")
continue
jssp = JSSPFactory.generateJSSPFromFormat(library[subFolder])
fileName = populationsFolder + run + "/" + subFolder + "/"
fileHC = open(fileName + 'hc.txt')
fileFHC = open(fileName + 'fhc.txt')
recordsHc = fileHC.readline().split(', ')[:-1]
xHc = []
yHc = []
for record in recordsHc:
xHc.append(int(record.split(':')[0]))
yHc.append(int(record.split(':')[1]))
recordsFfa = fileFHC.readline().split(', ')[:-1]
xFfa = []
yFfa = []
for record in recordsFfa:
xFfa.append(int(record.split(':')[0]))
yFfa.append(int(record.split(':')[1]))
xBks = [0, pow(2, 30)]
yBks = [bestKnown[subFolder], bestKnown[subFolder]]
plt.plot(xHc, yHc, label='Hill Climber', c='orange')
plt.plot(xFfa, yFfa, label='FFA', c='blue')
plt.plot(xBks, yBks, label='Best known solution', c='black')
plt.xscale('log')
plt.legend()
plt.xlabel('Function Evaluations')
plt.ylabel('Objective Value (Makespan)')
plt.title(subFolder + "(" + str(jssp.amountMachines) + " machines and " + str(jssp.amountJobs) + " jobs)")
plt.savefig('files/output/hc2/results/' + run + '/' + subFolder + '/plot.png')
plt.figure().clear()
plt.close('all')
print(subFolder + " done!")
@staticmethod
def createFunctionEvaluationsPlotPpa(resultFolder, library, instanceName):
jssp = JSSPFactory.generateJSSPFromFormat(library[instanceName])
fileName = resultFolder + instanceName + "/"
filePpa = open(fileName + 'ppaAllBest.txt')
fileFfaSelect = open(fileName + 'ffaSelectAllBest.txt')
fileFfaComplete = open(fileName + 'ffaCompleteAllBest.txt')
recordsPpa = filePpa.readline().split(', ')[:-1]
xPpa = []
yPpa = []
for record in recordsPpa:
xPpa.append(int(record.split(':')[0]))
yPpa.append(int(record.split(':')[1]))
recordsFfaSelect = fileFfaSelect.readline().split(', ')[:-1]
xFfaSelect = []
yFfaSelect = []
for record in recordsFfaSelect:
xFfaSelect.append(int(record.split(':')[0]))
yFfaSelect.append(int(record.split(':')[1]))
recordsFfaComplete = fileFfaComplete.readline().split(', ')[:-1]
xFfaComplete = []
yFfaComplete = []
for record in recordsFfaComplete:
xFfaComplete.append(int(record.split(':')[0]))
yFfaComplete.append(int(record.split(':')[1]))
plt.plot(xPpa, yPpa, label='PPA')
plt.plot(xFfaSelect, yFfaSelect, label='FFA Select')
plt.plot(xFfaComplete, yFfaComplete, label='FFA Complete')
plt.xscale('log')
plt.legend()
plt.xlabel('Function Evaluations')
plt.ylabel('Objective Value (Execution time)')
plt.title(instanceName + "(" + str(jssp.amountMachines) + " machines and " + str(jssp.amountJobs) + " jobs)")
plt.show()
@staticmethod
def plotBoth(library, instanceNames):
yPPAs = []
yFFAs = []
yFFACompletes = []
xPPAs = []
xFFAs = []
xFFACompletes = []
intermediateHCs = []
intermediateFHCs = []
xAxises = []
titles = []
for instanceName in instanceNames:
resultFolderPPA = 'files/output/ppa/populations/1/'
resultFolderHC = 'files/output/hc/populations/1/'
jssp = JSSPFactory.generateJSSPFromFormat(library[instanceName])
fileName = resultFolderPPA + instanceName + "/"
filePpa = open(fileName + 'ppaAllBest.txt')
fileFfaSelect = open(fileName + 'ffaSelectAllBest.txt')
fileFfaComplete = open(fileName + 'ffaCompleteAllBest.txt')
fileName = resultFolderHC + instanceName + "/"
fileHC = open(fileName + 'hc.txt')
fileFHC = open(fileName + 'fhc.txt')
recordsPpa = filePpa.readline().split(', ')[:-1]
xPpa = []
yPpa = []
for record in recordsPpa:
xPpa.append(int(record.split(':')[0]))
yPpa.append(int(record.split(':')[1]))
recordsFfaSelect = fileFfaSelect.readline().split(', ')[:-1]
xFfaSelect = []
yFfaSelect = []
for record in recordsFfaSelect:
xFfaSelect.append(int(record.split(':')[0]))
yFfaSelect.append(int(record.split(':')[1]))
recordsFfaComplete = fileFfaComplete.readline().split(', ')[:-1]
xFfaComplete = []
yFfaComplete = []
for record in recordsFfaComplete:
xFfaComplete.append(int(record.split(':')[0]))
yFfaComplete.append(int(record.split(':')[1]))
yPPAs.append(yPpa)
yFFAs.append(yFfaSelect)
yFFACompletes.append(yFfaComplete)
xPPAs.append(xPpa)
xFFAs.append(xFfaSelect)
xFFACompletes.append(xFfaComplete)
intermediateResultsHC = list(map(int, fileHC.readline().split(', ')[:-2]))
intermediateResultsFHC = list(map(int, fileFHC.readline().split(', ')[:-1]))
intermediateResultsHC.insert(0, None)
xAxis = []
for i in range(len(intermediateResultsFHC)):
xAxis.append((i * 1000) + 1)
intermediateHCs.append(intermediateResultsHC)
intermediateFHCs.append(intermediateResultsFHC)
xAxises.append(xAxis)
titles.append(instanceName + "(" + str(jssp.amountMachines) + " machines and " + str(jssp.amountJobs) + " jobs)")
fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True)
ax1.plot(xPPAs[0], yPPAs[0], label='PPA Standard')
ax1.plot(xFFAs[0], yFFAs[0], label='PPA FFA Select')
ax1.plot(xFFACompletes[0], yFFACompletes[0], label='PPA FFA Complete')
ax1.plot(xAxises[0], intermediateHCs[0], label='HC')
ax1.plot(xAxises[0], intermediateFHCs[0], label='HC FFA')
ax1.set_title(titles[0])
ax2.plot(xPPAs[1], yPPAs[1])
ax2.plot(xFFAs[1], yFFAs[1])
ax2.plot(xFFACompletes[1], yFFACompletes[1])
ax2.plot(xAxises[1], intermediateHCs[1])
ax2.plot(xAxises[1], intermediateFHCs[1])
ax2.set_title(titles[1])
plt.xscale('log')
fig.text(0.5, 0.04, 'Function Evaluations', ha='center', fontsize=16)
fig.text(0.04, 0.5, 'Objective Value (Execution time)', va='center', rotation='vertical', fontsize=16)
handles, labels = ax1.get_legend_handles_labels()
fig.legend(handles, labels, loc=7)
plt.show()
@staticmethod
def resultsToLatex(resultsFileName):
df = pd.read_csv(resultsFileName)
df['hc'] = df['hc'].astype('string')
df['bks'] = df['bks'].astype('string')
df['ffa'] = df['ffa'].astype('string')
df['hc'] = np.where(df['hc'] == df['bks'], '\\textbf{' + df['hc'] + "}", df['hc'])
df['ffa'] = np.where(df['ffa'] == df['bks'], '\\textbf{' + df['ffa'] + "}", df['ffa'])
print(df.to_latex(index=False, longtable=False, escape=False,
caption='Caption this is a table be happy with it please.'))
@staticmethod
def showGanttChart(instance, amountJobs):
dataFrame = instance.toDataFrame()
sortedDataframe = dataFrame.sort_values(by=['machineId', 'jobId'])
fig, ax = plt.subplots(1, figsize=(16, 6))
chart = ax.barh(sortedDataframe.machineId, sortedDataframe.executionTime, left=sortedDataframe.startTime,
color=cm.hot(sortedDataframe.jobId / amountJobs))
plt.legend(chart, ["Job " + str(number) for number in list(sortedDataframe.jobId)[:amountJobs]])
plt.show()
@staticmethod
def showTimeHistogram(executionTimes):
fig, ax = plt.subplots()
n, bins, patches = plt.hist(x=executionTimes, color='blue',
alpha=0.7, rwidth=0.9, bins=30)
(average, stdv) = np.round(norm.fit(executionTimes), 2)
y = norm.pdf(bins, average, stdv)
text = '\n'.join((
r'$\mu=%.2f$' % (average,),
r'$\sigma=%.2f$' % (stdv,)))
plt.text(0.6, 0.85, text, horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
plt.plot(bins, y * 0.03333 * sum(executionTimes), 'r--', linewidth=2)
plt.grid(axis='y', alpha=0.75)
plt.xlabel('Objective Value')
plt.ylabel('Number of Solutions')
plt.title("Objective value distribution for instance ft06\n6 jobs and 6 machines")
plt.show()
@staticmethod
def readSimpleFile(fileName):
file = open(fileName)
text = file.read()
return {"demo": text}
@staticmethod
def readFullLibrary(fileName):
file = open(fileName)
result = {}
instanceName = ''
instance = ''
copyLines = False
bks = {}
for line in file:
line = line[1:]
if instanceName == '' and 'instance' in line:
instanceName = line.replace('instance', '').replace('\n', '').replace(' ', '')
continue
if ('instance' in line) or ('best known solution' in line):
copyLines = True
bestKnown = int(line.split('best known solution: ')[1])
bks[instanceName] = bestKnown
continue
if '+++' in line and copyLines is True:
result[instanceName] = instance
copyLines = False
instanceName = ''
instance = ''
continue
if copyLines:
instance += line
return result
@staticmethod
def readFullLibraryBestKnown(fileName):
file = open(fileName)
instanceName = ''
copyLines = False
bks = {}
for line in file:
line = line[1:]
if instanceName == '' and 'instance' in line:
instanceName = line.replace('instance', '').replace('\n', '').replace(' ', '')
continue
if 'best known solution' in line:
copyLines = True
bestKnown = int(line.split('best known solution: ')[1])
bks[instanceName] = bestKnown
continue
if '+++' in line and copyLines is True:
copyLines = False
instanceName = ''
continue
return bks