-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataFormat.py
375 lines (307 loc) · 11.6 KB
/
dataFormat.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# import libraries
import os
import sys
import toml
import numpy as np
import pandas as pd
from pathlib import Path
# set variables from command line input
args = sys.argv
# args = [
# "",
# "/Users/kylasemmendinger/Library/CloudStorage/[email protected]/My Drive/loslrRegulation",
# "flowANN_onlyPhysicalLimits_offSepRule_netAnnualAverage_12month_sqLM_91dv_7obj_1900_2020_75000nfe",
# "5",
# ]
# -----------------------------------------------------------------------------
# experimental design from inputs
# -----------------------------------------------------------------------------
# [1]: path to working directory
os.chdir(args[1])
# print(os.getcwd())
# [2]: folder name of experiment
folderName = args[2]
# print(folderName)
# print(os.listdir())
# load configuration file from folder
with open("output/data/" + folderName + "/config.toml", "r") as f:
config = toml.load(f)
# [3]: number of seeds
nseed = int(args[3])
# -----------------------------------------------------------------------------
# set variables from config file
# -----------------------------------------------------------------------------
# number of decision variables and objectives
numDV = config["optimizationParameters"]["numDV"]
numObj = config["optimizationParameters"]["numObj"]
# # forecast lead-time and skill
# leadtime = config["experimentalDesign"]["forecastLeadTime"]
# skill = config["experimentalDesign"]["forecastSkill"]
# extract pi and dv names
pis = config["performanceIndicators"]["piName"]
dvs = config["decisionVariables"]["dvName"]
norm = config["decisionVariables"]["normalized"]
if norm == "True":
normRange = config["decisionVariables"]["normalizedRange"]
# -----------------------------------------------------------------------------
# create directories for formatted data
# -----------------------------------------------------------------------------
# create folder for clean decision variable and objective files
newpath = "output/data/" + folderName + "/clean/"
if not os.path.exists(newpath):
os.makedirs(newpath)
# create folder for moeaFramework
# parent directory
newpath = "output/data/" + folderName + "/moeaFramework/"
if not os.path.exists(newpath):
os.makedirs(newpath)
# inputs
newpath = "output/data/" + folderName + "/moeaFramework/objs/"
if not os.path.exists(newpath):
os.makedirs(newpath)
# outputs
newpath = "output/data/" + folderName + "/moeaFramework/metrics/"
if not os.path.exists(newpath):
os.makedirs(newpath)
# outputs
newpath = "output/data/" + folderName + "/postAnalysis/"
if not os.path.exists(newpath):
os.makedirs(newpath)
# -----------------------------------------------------------------------------
# functions
# -----------------------------------------------------------------------------
# import custom functions from utils.py script
# sys.path.insert(1, os.getcwd() + "/functions")
sys.path.append(".")
from functions.utils import minmaxNorm
# find non dominated solutions across all pareto fronts generated by all seeds
# adapted from: https://stackoverflow.com/questions/32791911/fast-calculation-of-pareto-front-in-python
def is_pareto_efficient(costs, return_mask=True):
# find the pareto-efficient points
# costs: (n_points, n_costs) array
# return_mask: True to return a mask
# return: array of indices of pareto-efficient points. if return_mask is
# True, this will be an (n_points, ) boolean array. otherwise it will be
# a (n_efficient_points, ) integer array of indices
is_efficient = np.arange(costs.shape[0])
n_points = costs.shape[0]
# next index in the is_efficient array to search for
next_point_index = 0
while next_point_index < len(costs):
nondominated_point_mask = np.any(costs < costs[next_point_index], axis=1)
nondominated_point_mask[next_point_index] = True
# remove dominated points
is_efficient = is_efficient[nondominated_point_mask]
costs = costs[nondominated_point_mask]
next_point_index = np.sum(nondominated_point_mask[:next_point_index]) + 1
if return_mask:
is_efficient_mask = np.zeros(n_points, dtype=bool)
is_efficient_mask[is_efficient] = True
return is_efficient_mask
else:
return is_efficient
# -----------------------------------------------------------------------------
# format raw borg output files for 1) MOEA Framework and 2) Clean version
# -----------------------------------------------------------------------------
pf = []
dvpf = []
for s in range(1, nseed + 1):
# files to read
pfFile = "output/data/" + folderName + "/raw/pareto_front_S" + str(s) + ".txt"
rtFile = "output/data/" + folderName + "/raw/runtime_S" + str(s) + ".txt"
# first, check to see if complete run
path = Path(pfFile)
# if yes, load discovered pareto front for the seed of interest
if path.is_file():
pfSeed = pd.read_csv(
pfFile,
sep=" ",
header=None,
skiprows=2,
)
# if not, take last pareto front from runtime file
else:
import csv
data = []
flag = False
with open(rtFile) as f:
flag = True
for line in reversed(f.readlines()):
# if line.startswith("#"):
# flag = True
# continue
if line.startswith("//"):
flag = False
break
if flag:
data.append(line)
# convert to data frame
pfSeed = pd.DataFrame([[float(x) for x in e.split()] for e in data])
pfSeed = pfSeed.iloc[:, 1:]
tmp = pfSeed[pfSeed.columns[0:]].apply(
lambda x: " ".join(x.dropna().astype(str)), axis=1
)
tmp = pd.DataFrame(tmp)
# add in beginning lines
line1 = "# Borg Optimization Results"
line2 = (
"# First "
+ str(numDV)
+ " are the decision variables, last "
+ str(numObj)
+ " are the objective values"
)
lines = pd.DataFrame([line1, line2])
tmp = pd.concat([lines, tmp])
# save to csv
tmp.to_csv(
pfFile,
index=False,
header=False,
quoting=csv.QUOTE_NONE,
escapechar="\\",
)
# drop columns with NAs (usually just a weird output that sometimes happens from borg)
pfSeed = pfSeed.dropna(axis=1, how="all")
pfSeed = pfSeed.dropna(axis=0, how="any")
# set column names
pfSeed.columns = dvs + pis
# if decision variables are [0, 1] normalized for Borg, backnormalize
if norm == "True":
for d in range(len(dvs)):
tmpDV = dvs[d]
tmpValues = list(pfSeed.loc[:, tmpDV])
dvRange = [
config["decisionVariables"]["lowerBounds"][d],
config["decisionVariables"]["upperBounds"][d],
]
trueVals = [
minmaxNorm(x, dvRange, normRange, method="backtransform")
for x in tmpValues
]
pfSeed.loc[:, tmpDV] = trueVals
# save clean pareto front to file for reference later
cleanPF = "output/data/" + folderName + "/clean/pareto_front_S" + str(s) + ".txt"
pfSeed.to_csv(
cleanPF,
sep=",",
header=True,
index=False,
)
pfSeed.insert(0, "Seed", s)
dvpf.append(pfSeed)
# extract just objectives and save in text file with # at the end for use in the MOEAFramework
moeaFile = (
"output/data/"
+ folderName
+ "/moeaFramework/objs/pareto_front_S"
+ str(s)
+ ".txt"
)
objSeed = pfSeed[pis]
# objSeed = pfSeed.iloc[:, -numObj:]
objSeed.to_csv(
moeaFile,
sep=" ",
header=False,
index=False,
)
with open(moeaFile, "a") as f:
f.write("#")
pf.append(objSeed)
# -----------------------------------------------------------------------------
# update or create reference set for MOEAFramework
# -----------------------------------------------------------------------------
# join all seeded pareto fronts into one data frame
objsDF = pd.concat(pf).reset_index(drop=True)
objs = objsDF.to_numpy(dtype="float")
# find reference set (non-dominated policies across all seeds)
refsetInd = is_pareto_efficient(objs, return_mask=True)
refsetInd = pd.Series(refsetInd)
refsetUpdated = objsDF[refsetInd].reset_index(drop=True)
moeaFile = "output/data/" + folderName + "/moeaFramework/objRefset.txt"
refsetUpdated.to_csv(
moeaFile,
sep=" ",
header=False,
index=False,
)
with open(moeaFile, "a") as f:
f.write("#")
# -----------------------------------------------------------------------------
# save nondominated policies with decision variables
# -----------------------------------------------------------------------------
# join all seeded pareto fronts into one data frame
pols = pd.concat(dvpf).reset_index(drop=True)
objs = pols[pis].to_numpy(dtype="float")
# objs = pols.iloc[:, -numObj:].to_numpy(dtype="float")
# find reference set
refsetInd = is_pareto_efficient(objs, return_mask=True)
refsetInd = pd.Series(refsetInd)
refsetUpdated = pols[refsetInd].reset_index(drop=True)
refsetUpdated.insert(0, "ID", range(0, refsetUpdated.shape[0]))
# save updated reference set
refsetUpdated.to_csv(
"output/data/" + folderName + "/NonDominatedPolicies.txt",
sep="\t",
header=True,
index=False,
)
# -----------------------------------------------------------------------------
# extract objectives from runtime files
# -----------------------------------------------------------------------------
for s in range(1, nseed + 1):
# load runtime dynamics and pareto fronts for seed of interest line by line
rtSeed = []
dynSeed = []
rtFile = "output/data/" + folderName + "/raw/runtime_S" + str(s) + ".txt"
with open(rtFile) as f:
for line in f:
# check if the current line starts with "//" to signal runtime dynamics
if line.startswith("//"):
line = line.replace("\n", "")
dynSeed.append(line)
elif not line.startswith("#"):
line = line.replace("\n", "")
rtSeed.append(line)
# save clean runtime dynamics to file for reference later
dynFile = "output/data/" + folderName + "/clean/dynamics_S" + str(s) + ".txt"
dynSeed = pd.DataFrame(dynSeed)
dynSeed.to_csv(
dynFile,
sep=",",
header=False,
index=False,
)
# format to data frame and set column names
rtSeed = pd.DataFrame([[float(x) for x in e.split()] for e in rtSeed])
rtSeed.columns = ["NFE"] + dvs + pis
# rtSeed.columns = ["NFE"] + list(range(0, numDV + numObj))
# save clean output to file for reference later
rtFile = "output/data/" + folderName + "/clean/runtime_S" + str(s) + ".txt"
rtSeed.to_csv(
rtFile,
sep=",",
header=True,
index=False,
)
# save objectives only to evaluate in MOEAFramework
freq = rtSeed["NFE"].unique()
for fe in range(len(freq)):
# filter by NFE of interest and extract objectives
tmp = rtSeed.loc[rtSeed["NFE"] == freq[fe]]
tmp = tmp[pis]
# tmp = tmp.iloc[:, -numObj:]
filename = (
"output/data/"
+ folderName
+ "/moeaFramework/objs/runtime_S"
+ str(s)
+ ".txt"
)
if fe == 0:
tmp.to_csv(filename, sep=" ", index=False, header=False)
else:
tmp.to_csv(filename, sep=" ", index=False, header=False, mode="a")
with open(filename, "a") as f:
f.write("#\n")