-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_run_scenario.py
128 lines (120 loc) · 4.86 KB
/
script_run_scenario.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
import random
import os
import pandas as pd
import json
import multiprocessing as mp
from scenario_setup import scenario_input_data, run_calculation, setup_original_grid
def callback_result(result):
return result
def setup_scenario_config():
ev_base_dir = r"H:\no_HP"
run_full = {
"result_dir": r"H:\cost_functions\results",
"run": "test_extreme_days_full_gamma",
"penetration": 1.0,
"seed": 544}
# user input for scenario
run = "Powertech_2022" #"Powertech_2022"
grid_ids = [176] # 176, 177, 1056, 1690, 1811, 2534
resources = ["EV"] # "RES", "BESS", "EV", "HP"
strategy = "optimised"
sizing_mode = "gamma"
nr_iterations = 1
max_day = None
penetrations = [1.0]
# 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0
# 1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1
ts_mode = "reduced"
num_ts_loading = None
num_ts_voltage = None
use_seed_optimised = True
# setup of scenario - do not change from here
scenario = "_".join(resources + [strategy])
orig_seed = 2022
random.seed(orig_seed)
# if optimised, get the right representative seed
if use_seed_optimised:
seeds_tmp = pd.read_csv(os.path.join(results_path, "Powertech_2022", "representative_seeds.csv"), index_col=0)
seeds = [int(seed) for seed in seeds_tmp.loc[grid_ids, "_".join(resources + ["reference"])].values]
else:
seeds = random.sample(range(0, 1000), nr_iterations)
scenario_dict = scenario_input_data()
scenario_dict.update({
"scenario": scenario,
"orig_seed": orig_seed,
"seeds": seeds,
"run": run,
"grid_ids": grid_ids,
"resources": resources,
"strategy": strategy,
"sizing_mode": sizing_mode,
"max_day": max_day,
"penetrations": penetrations,
"ts_mode": ts_mode,
"run_full": run_full,
"num_ts_loading": num_ts_loading,
"num_ts_voltage": num_ts_voltage,
"ev_base_dir": ev_base_dir,
})
return scenario_dict
if __name__ == "__main__":
nr_threads = 1
grids_orig_path = \
r"U:\Software\Cost-functions\distribution-grid-expansion-cost-functions\data\ding0_elia_grids"
grids_path = r"H:\cost_functions\grids_final_reinforced"
results_path = r"H:\cost_functions\results"
scenario_dir = None
if type(scenario_dir) is str:
try:
with open(
os.path.join(scenario_dir, "scenario_dict.json"), 'r',
encoding='utf-8') as f:
scenario_dict = json.load(f)
except json.decoder.JSONDecodeError:
raise ValueError("Defined scenario_dict seems to be empty. Please check.")
elif scenario_dir is None:
scenario_dict = setup_scenario_config()
else:
raise ValueError("Unknwon type of scenario_dir")
os.makedirs(os.path.join(results_path, scenario_dict["run"], scenario_dict["scenario"]), exist_ok=True)
with open(os.path.join(results_path, scenario_dict["run"], scenario_dict["scenario"], "scenario_dict.json"),
'w', encoding='utf-8') as f:
json.dump(scenario_dict, f, ensure_ascii=False, indent=4)
scenario_dict["grids_orig_path"] = grids_orig_path
for grid_id in scenario_dict["grid_ids"]:
edisgo_obj_orig = setup_original_grid(
grids_path=grids_path,
grid_id=grid_id,
scenario_dict=scenario_dict
)
if nr_threads == 1:
# iterate through penetrations
for penetration in scenario_dict["penetrations"]:
for seed in scenario_dict["seeds"]:
costs_tmp = run_calculation(
grids_path=grids_path,
results_path=results_path,
scenario_dict=scenario_dict,
seed=seed,
penetration=penetration,
grid_id=grid_id,
edisgo_obj_orig=edisgo_obj_orig
)
elif nr_threads > 1:
pool = mp.Pool(nr_threads)
results = [pool.apply_async(func=run_calculation,
args=(grids_path,
results_path,
scenario_dict,
seed,
penetration,
grid_id),
callback=callback_result)
for penetration in scenario_dict["penetrations"]
for seed in scenario_dict["seeds"]]
costs = pd.concat([result.get() for result in results], axis=1)
pool.close()
pool.join()
else:
raise ValueError("nr_threads has to be larger or equal to 1.")
print("Success")