-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptimizationWrapper.py
253 lines (212 loc) · 7.67 KB
/
optimizationWrapper.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
# import libraries
import os
import sys
import toml
import pathlib
from math import *
from borg import *
from functools import partial
from importlib import import_module
args = sys.argv
# args = [
# "",
# # "/Users/kylasemmendinger/Library/CloudStorage/[email protected]/My Drive/loslrRegulation",
# "/Users/kylasemmendinger/Documents/github/loslr_regulation_optimization",
# # "config/glam/juneWorkshop.toml",
# "config/test.toml",
# "1",
# ]
# -----------------------------------------------------------------------------
# set up experimental design from command line inputs
# -----------------------------------------------------------------------------
# [1]: location to run [mac_loc, glhpc]
wd = args[1]
os.chdir(wd)
# [2]: path to configuration file
configFile = args[2]
# [3]: seed [int]
nseed = int(args[3])
# -----------------------------------------------------------------------------
# set variables for experiment form config file
# -----------------------------------------------------------------------------
# load configuration file from folder
with open(configFile, "r") as f:
config = toml.load(f)
# get optimization parameters from config file
nvars = config["optimizationParameters"]["numDV"]
nobjs = config["optimizationParameters"]["numObj"]
nconstrs = config["optimizationParameters"]["numCon"]
nfe = config["optimizationParameters"]["nfe"]
popSize = config["optimizationParameters"]["popSize"]
metFreq = config["optimizationParameters"]["metFreq"]
# get simulation parameters from config file
releaseFunctionName = config["experimentalDesign"]["releaseFunction"]
planLimitsName = config["experimentalDesign"]["limitType"]
septemberRule = config["experimentalDesign"]["septemberRule"]
stlawRoutingName = config["experimentalDesign"]["stlawRouting"]
version = config["experimentalDesign"]["trace"]
inputFile = config["experimentalDesign"]["inputFile"]
# get decision variable info from config file
decisionVariables = config["decisionVariables"]
# release function parameters
releaseFunInputs = config["releaseFunction"]
# get objective function parameters from config file
epsilon = config["performanceIndicators"]["epsilonValue"]
piWeighting = config["performanceIndicators"]["metricWeighting"]
objectiveFormulation = config["performanceIndicators"]["objectiveFormulation"]
objectiveModelNames = config["performanceIndicators"]["objectiveModels"]
# -----------------------------------------------------------------------------
# load functions for simulation - specified in config file
# -----------------------------------------------------------------------------
sys.path.append(".")
# import policy simulation function
import optimizationSimulation
# import config specified simulation functions
formatDecisionVariables = import_module(
"functions.release." + releaseFunctionName
).formatDecisionVariables
getReleaseFunctionInputs = import_module(
"functions.release." + releaseFunctionName
).getReleaseFunctionInputs
releaseFunction = import_module(
"functions.release." + releaseFunctionName
).releaseFunction
getPlanLimitsInputs = import_module(
"functions.limits." + planLimitsName
).getPlanLimitsInputs
planLimits = import_module("functions.limits." + planLimitsName).planLimits
getStLawrenceRoutingInputs = import_module(
"functions.routing." + stlawRoutingName
).getStLawrenceRoutingInputs
stLawrenceRouting = import_module(
"functions.routing." + stlawRoutingName
).stLawrenceRouting
if septemberRule != "off":
septemberRule = import_module("functions.limits.septemberRule").septemberRule
# import objective function simulation script
objectiveFunctions = import_module(
"objectiveFunctions." + objectiveFormulation + ".objectiveSimulation"
)
# import individual objecive function modules
piModels = []
for x in range(len(objectiveModelNames)):
tmpPI = objectiveModelNames[x]
tmp = import_module(
"objectiveFunctions." + objectiveFormulation + ".functions." + tmpPI
)
piModels.append(tmp)
# -----------------------------------------------------------------------------
# file pointers
# -----------------------------------------------------------------------------
# input data to load
# dataName = version + "/" + trace + "/" + leadtime + "_" + skill
dataName = version + "/" + inputFile
# output folder name
folderName = (
releaseFunctionName
+ "_"
+ planLimitsName
+ "_"
+ septemberRule
+ "SepRule"
+ "_"
+ piWeighting
+ "_"
+ inputFile.split("/")[1]
+ "_"
+ str(nvars)
+ "dv_"
+ str(nobjs)
+ "obj_"
+ inputFile.split("/")[0]
+ "_"
+ str(nfe)
+ "nfe"
)
# create output directory
path = pathlib.Path("output/data/" + folderName + "/raw/")
path.mkdir(parents=True, exist_ok=True)
# -----------------------------------------------------------------------------
# set up borg
# -----------------------------------------------------------------------------
# set seed
Configuration.seed(nseed)
# initialize borg with problem --> partial(fn, data_name)(*vars)
borg = Borg(
nvars,
nobjs,
nconstrs,
partial(
optimizationSimulation.optimization,
formatDecisionVariables,
decisionVariables,
dataName,
releaseFunInputs,
getReleaseFunctionInputs,
releaseFunction,
septemberRule,
getPlanLimitsInputs,
planLimits,
getStLawrenceRoutingInputs,
stLawrenceRouting,
objectiveFunctions,
piModels,
piWeighting,
),
)
# set decision variable bounds
if str(decisionVariables["normalized"]) == True:
lowerBounds = [decisionVariables["normalizedRange"][0]] * nvars
upperBounds = [decisionVariables["normalizedRange"][1]] * nvars
else:
lowerBounds = decisionVariables["lowerBounds"]
upperBounds = decisionVariables["upperBounds"]
borg.setBounds(*[list(x) for x in list(zip(lowerBounds, upperBounds))])
# set objective tolerance epsilons - defines "meaningful" improvement
borg.setEpsilons(*epsilon)
# set up configuration
borgConfig = {
"maxEvaluations": nfe,
"initialPopulationSize": popSize,
"runtimefile": "output/data/" + folderName + "/raw/runtime_S" + str(nseed) + ".txt",
"runtimeformat": "borg",
"frequency": metFreq,
}
# -----------------------------------------------------------------------------
# write config file
# -----------------------------------------------------------------------------
# for first seed, copy config file to output directory
if nseed == 1:
with open("output/data/" + folderName + "/config.toml", "w") as f:
toml.dump(config, f)
# -----------------------------------------------------------------------------
# run borg
# -----------------------------------------------------------------------------
# run borg
result = borg.solve(borgConfig)
# -----------------------------------------------------------------------------
# save output
# -----------------------------------------------------------------------------
output_location = (
"output/data/" + folderName + "/raw/pareto_front_S" + str(nseed) + ".txt"
)
# write objective values and decision variable values to output file
with open(output_location, "w") as f:
f.write("# Borg Optimization Results\n")
f.write(
"# First "
+ str(nvars)
+ " are the decision variables, "
+ "last "
+ str(nobjs)
+ " are the "
+ "objective values\n"
)
for solution in result:
line = ""
for i in range(len(solution.getVariables())):
line = line + (str(solution.getVariables()[i])) + " "
for i in range(len(solution.getObjectives())):
line = line + (str(solution.getObjectives()[i])) + " "
line = line + "\n"
f.write(line)