-
Notifications
You must be signed in to change notification settings - Fork 13
/
cost_sensitivity.py
440 lines (380 loc) · 16.7 KB
/
cost_sensitivity.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import numpy as np
from os.path import join as pjoin
import pandas as pd
import os
import pdb
from ncet import fill_scaling_table
from ncet import scale_direct_costs
from ncet import modularize
from ncet import learn
from ncet import get_indirect_costs
from ncet import sum_accounts
from ncet import get_sub_account_iloc
import cProfile
import datetime
def update_input_scaling(fname, case="Base"):
# Read in the two tabs of inputs and ranges
df_inputs = pd.read_excel(fname, sheet_name="input_scaling")
df_scalars = pd.read_excel(fname, sheet_name="param_ranges")
# Create dictionary of parameters for the specific case
scalars_dict = df_scalars.set_index("Parameter")[case + " value"].to_dict()
# Map the new values to the input df
options = [0, 1, 2]
for option in options:
if option == 0:
for idx in df_inputs.index:
if df_inputs["Option 0"].iloc[idx] is not np.nan:
varz = (
df_inputs["Option 0 Formula"]
.iloc[idx]
.replace("[", "")
.replace("]", "")
.split(",")
)
varz = [x.strip() for x in varz]
if len(varz) == 4:
varz[2] = scalars_dict[varz[2]]
varz[3] = scalars_dict[varz[3]]
else:
varz[0] = scalars_dict[varz[0]]
varz = [float(x) for x in varz]
df_inputs.at[idx, "Option 0 Formula"] = varz
else:
aux = (
df_inputs["Option " + str(option) + " Exponent"]
.astype(str)
.str.isnumeric()
)
numbers = df_inputs["Option " + str(option) + " Exponent"].loc[aux].values
df_inputs["Option " + str(option) + " Exponent"] = df_inputs[
"Option " + str(option) + " Exponent"
].map(scalars_dict)
df_inputs.loc[aux, "Option " + str(option) + " Exponent"] = numbers
return df_inputs, scalars_dict
def build_schedule_table(plant_list, id, scheduler_table):
for i, dfNP in enumerate(plant_list):
labor_df = dfNP["Site Labor Hours"]
scheduler_table = scheduler_table.merge(labor_df, how="left", on="Account")
scheduler_table.rename(
columns={"Site Labor Hours": "Hours run" + str(id) + "_unit" + str(i)},
inplace=True,
)
return scheduler_table
def rand_input_scaling(fname):
# Read in the two tabs of inputs and ranges
df_inputs = pd.read_excel(fname, sheet_name="input_scaling")
df_scalars = pd.read_excel(fname, sheet_name="param_ranges", index_col="Parameter")
df_scalars["New value"] = 0
for param in df_scalars.index:
if df_scalars.loc[param, "Distribution"] == "lognormal":
df_scalars.loc[param, "New value"] = np.random.lognormal(
df_scalars.at[param, "mu/lambda"], df_scalars.at[param, "sigma"]
)
elif df_scalars.loc[param, "Distribution"] == "exponential":
df_scalars.loc[param, "New value"] = np.random.exponential(
1 / df_scalars.at[param, "mu/lambda"]
)
elif df_scalars.loc[param, "Distribution"] == "normal":
df_scalars.loc[param, "New value"] = np.random.normal(
df_scalars.at[param, "mu/lambda"], df_scalars.at[param, "sigma"]
)
elif df_scalars.loc[param, "Distribution"] == "uniform":
df_scalars.loc[param, "New value"] = np.random.uniform(
df_scalars.at[param, "Min value"], df_scalars.at[param, "Max value"]
)
# Create dictionary of parameters for the specific case
scalars_dict = df_scalars["New value"].to_dict()
# Map the new values to the input df
options = [0, 1, 2]
for option in options:
if option == 0:
for idx in df_inputs.index:
if df_inputs["Option 0"].iloc[idx] is not np.nan:
varz = (
df_inputs["Option 0 Formula"]
.iloc[idx]
.replace("[", "")
.replace("]", "")
.split(",")
)
varz = [x.strip() for x in varz]
if len(varz) == 4:
varz[2] = scalars_dict[varz[2]]
varz[3] = scalars_dict[varz[3]]
else:
varz[0] = scalars_dict[varz[0]]
varz = [float(x) for x in varz]
df_inputs.at[idx, "Option 0 Formula"] = varz
else:
aux = (
df_inputs["Option " + str(option) + " Exponent"]
.astype(str)
.str.isnumeric()
)
numbers = df_inputs["Option " + str(option) + " Exponent"].loc[aux].values
df_inputs["Option " + str(option) + " Exponent"] = df_inputs[
"Option " + str(option) + " Exponent"
].map(scalars_dict)
df_inputs.loc[aux, "Option " + str(option) + " Exponent"] = numbers
return df_inputs, scalars_dict
def build_mc_output_series(df):
output_rows = ["A.1", "A.2", "A.9", "A.21", "A.22", "A.23", "A.24", "A.25", "A.26"]
output_cols = [
"Factory Equipment Cost",
"Site Labor Cost",
"Site Material Cost",
"Total Cost",
]
output_dict = {} # pd.DataFrame(columns=['Description', 'Value'])
for i in output_rows:
for j in output_cols:
output_dict[i + " " + j] = df.at[i, j]
return output_dict
def get_building_table(plant_characteristics, scaling_table):
bldg_template = pd.read_csv("building_template.csv", index_col="Account")
bldg_template["Area"] = bldg_template.index
bldg_template["Area"] = bldg_template["Area"].map(plant_characteristics)
for account in bldg_template.index:
if pd.isna(bldg_template.at[account, "Area"]):
aux = bldg_template.at[account, "Scaling Account"]
bldg_template.at[account, "Area"] = (
scaling_table.at[aux, "Scaling Factor"]
* bldg_template.at[account, "Area - PWR12"]
)
bldg_template["Staff Limit"] = (
352.82 * (bldg_template["Area"].values / 1534) ** 0.7
) # this was a data fit on workers/m^2
# Scale the site prep by the reference staff limit because the above equation breaks down for open areas
bldg_template.at["A.211.", "Staff Limit"] = np.min(
[
bldg_template.at["A.211.", "Staff Limit - PWR12"],
bldg_template.at["A.211.", "Staff Limit"],
]
)
# Add the map for where SSCs may have moved from one building to another
bldg_template["New Building"] = plant_characteristics["New Bldg"]
bldg_template["Staff Limit"] = bldg_template["Staff Limit"].astype(float).round(0)
return bldg_template
def run_ncet(
plant,
path,
orders,
plant_fname,
param_fname,
BASIS_FNAME,
mc_runs=1,
make_building_table=False,
save_all=False,
):
scheduler_table = pd.read_csv("scheduler_table.csv", index_col=0)
# Make the directories if they don't exist (use the timestamp to avoid overwriting anythingS)
if not os.path.isdir(path + "/out"):
os.mkdir(path + "/out")
if not os.path.isdir(path + "/out/" + plant):
os.mkdir(path + "/out/" + plant)
time_folder = "/" + datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
os.mkdir(path + "/out/" + plant + time_folder)
os.mkdir(path + "/out/" + plant + time_folder + "/raw")
os.mkdir(path + "/out/" + plant + time_folder + "/scaling_tables")
# Run the cost modeling functions
mc_dict_list = [[] for _ in range(orders)]
for i in range(mc_runs):
# Get the scaling table parameters
if mc_runs == 1:
df_scalars, scalars_dict = update_input_scaling(param_fname)
else:
df_scalars, scalars_dict = rand_input_scaling(param_fname)
# Fill out the scaling table with the input file data
scaling_table, plant_characteristics = fill_scaling_table.fill_scaling_table(
path,
plant_fname,
base=BASIS_FNAME,
scaling_table=df_scalars,
scalars_dict=scalars_dict,
)
if mc_runs == 1:
scaling_table.to_csv(
path
+ "/out/"
+ plant
+ time_folder
+ "/base_"
+ plant
+ "scaling_inputs.csv"
)
elif save_all:
scaling_table.to_csv(
path
+ "/out/"
+ plant
+ time_folder
+ "/scaling_tables/mc_output_"
+ plant
+ "scaling_inputs_"
+ str(i)
+ ".csv"
)
# Build the input file for construction scheduler if needed
if make_building_table:
bldg_table = get_building_table(plant_characteristics, scaling_table)
bldg_table.to_csv(
path
+ "/out/"
+ plant
+ time_folder
+ "/base_"
+ plant
+ "buildingtable.csv"
)
# Scale the direct costs based on the scaling table
dfNP = scale_direct_costs.scale_direct_costs(
BASIS_FNAME, scaling_table, plant_characteristics, scalars_dict
)
# Get the subaccount indices to accelerate the summing process below
idx_dict = get_sub_account_iloc.get_sub_account_iloc(dfNP)
# Modularize, learn, and build the scheduler table
dfNP, idx_modules = modularize.modularize(
path, plant_fname, dfNP, orders, scalars_dict
)
newPlant_list, learning_rate = learn.learn(
path, plant_fname, dfNP, orders, scalars_dict, idx_modules
)
scheduler_table = build_schedule_table(newPlant_list, i, scheduler_table)
# Get the indirect costs, sum all costs, and save files
for j, dfNP in enumerate(newPlant_list):
dfNP = get_indirect_costs.get_indirect_costs(
dfNP, plant_characteristics, learning_rate[j], scalars_dict
)
dfNP = sum_accounts.sum_accounts(dfNP, idx_dict)
# Build an output dictionary of all the results for monte carlo
if mc_runs != 1:
if save_all:
dfNP.to_csv(
path
+ "/out/"
+ plant
+ time_folder
+ "/raw/mc_"
+ plant
+ "_run"
+ str(i)
+ "_unit"
+ str(j + 1)
+ ".csv"
)
output_dict = build_mc_output_series(dfNP)
output_dict["Run"] = i
mc_dict_list[j].append(output_dict)
else:
dfNP.to_csv(
path
+ "/out/"
+ plant
+ time_folder
+ "/new"
+ plant
+ "_Base_"
+ str(j)
+ ".csv"
)
# Save all the output dictionaries from montecarlo
if mc_runs != 1:
for i, dict_list in enumerate(mc_dict_list):
pd.DataFrame(dict_list).to_csv(
path
+ "/out/"
+ plant
+ time_folder
+ "/mc_output_"
+ plant
+ "_unit"
+ str(i + 1)
+ ".csv"
)
scheduler_table.to_csv(
path
+ "/out/"
+ plant
+ time_folder
+ "/mc_"
+ plant
+ "_scheduler_table.csv"
)
else:
scheduler_table.to_csv(
path + "/out/" + plant + time_folder + "/" + plant + "_scheduler_table.csv"
)
if __name__ == "__main__":
start_time = datetime.datetime.now()
path = os.path.dirname(__file__) # Get the path to the project directory
print(path)
# Setup parameters:
# plant_fname: name of the plant input file
# BASIS_FNAME: ME or BE (choosing BE starts at NOAK), this is EEDB basis table <---- depends on user
# plant_fname: the EXCEL file containing design inputs. good to use plant variable to define the name.
# param_fname: of the EXCEL sheet with two tabs: input_scaling and param_ranges
# cases: match those on param_ranges
# orders: how many plants to run the learning model out to and capitalize the factory costs over
plant = "PWR12ME"
BASIS_FNAME = (
"./PWR12_ME_inflated_reduced.csv"
)
plant_fname = "inputfile_" + plant + ".xlsx"
param_fname = "input_scaling_exponents.xlsx"
orders = 10
mc_runs = 1 # choose 1 to run the reference values
# cProfile.run('run_ncet(plant, path, orders, plant_fname, param_fname, BASIS_FNAME, mc_runs=mc_runs, make_building_table=True, save_all=True)')
run_ncet(
plant,
path,
orders,
plant_fname,
param_fname,
BASIS_FNAME,
mc_runs=mc_runs,
make_building_table=True,
save_all=True,
)
# mc_runs = 300 # choose 1 to run the reference values
# run_ncet(plant, path, orders, plant_fname, param_fname, BASIS_FNAME, mc_runs=mc_runs, make_building_table=False, save_all=True)
print("--- %s seconds ---" % (datetime.datetime.now() - start_time))
" Old way of running the code"
# -------------------------------------------- Monte carlo run ---------------------------------------------#
# mc_dict_list = [ [] for _ in range(orders)]
# for i in range(monte_carlo_runs):
# df_scalars, scalars_dict = rand_input_scaling(param_fname, i, plant)
# # Get the direct costs
# scaling_table, plant_characteristics = fill_scaling_table.fill_scaling_table(path, plant_fname, base=BASIS_FNAME,
# scaling_table=df_scalars, scalars_dict=scalars_dict)
# dfNP = scale_direct_costs.scale_direct_costs(BASIS_FNAME, scaling_table, plant_characteristics, scalars_dict)
# dfNP, idx_modules = modularize.modularize(path, plant_fname, dfNP, orders, scalars_dict)
# newPlant_list, learning_rate = learn.learn(path, plant_fname, dfNP, orders, scalars_dict, idx_modules)
# scheduler_table = build_schedule_table(newPlant_list, i, scheduler_table)
# for j, dfNP in enumerate(newPlant_list):
# dfNP = get_indirect_costs.get_indirect_costs(dfNP, plant_characteristics, learning_rate[j], scalars_dict)
# dfNP = sum_accounts.sum_accounts(dfNP)
# dfNP.to_csv('./out/' + plant + '/raw/mc_' + plant + '_run' + str(i) + '_unit' + str(j+1) + '.csv')
# output_dict = build_mc_output_series(dfNP)
# output_dict['Run'] = i
# mc_dict_list[j].append(output_dict)
# for i, dict_list in enumerate(mc_dict_list):
# pd.DataFrame(dict_list).to_csv('./out/' + plant + '/mc_output_' + plant + '_unit' + str(i+1) + '.csv')
# scheduler_table.to_csv('./out/' + plant + '/mc_' + plant + '_scheduler_table.csv')
# print("--- %s seconds ---" % (datetime.datetime.now() - start_time))
# #-------------------------------------------- Case by case run ---------------------------------------------#
# # Get direct costs for each case
# for case in cases:
# # Create the input params df
# df_scalars, scalars_dict = update_input_scaling(param_fname, case)
# # Get the direct costs
# scaling_table, plant_characteristics = fill_scaling_table.fill_scaling_table(path, plant_fname, base=BASIS_FNAME,
# scaling_table=df_scalars, scalars_dict=scalars_dict)
# dfNP = scale_direct_costs.scale_direct_costs(BASIS_FNAME, scaling_table, plant_characteristics, scalars_dict)
# dfNP, idx_modules = modularize.modularize(path, plant_fname, dfNP, orders, scalars_dict)
# newPlant_list, learning_rate = learn.learn(path, plant_fname, dfNP, orders, scalars_dict, idx_modules)
# scheduler_table = build_schedule_table(newPlant_list, case, scheduler_table)
# for i, dfNP in enumerate(newPlant_list):
# dfNP = get_indirect_costs.get_indirect_costs(dfNP, plant_characteristics, learning_rate[i], scalars_dict)
# dfNP = sum_accounts.sum_accounts(dfNP)
# dfNP.to_csv('./out/' + plant + '/new' + plant + '_' + case + '_' + str(i) + '.csv')
# scheduler_table.to_csv('./out/' + plant + '/' + plant + '_scheduler_table.csv')