-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLO_EMO_parallel.py
155 lines (124 loc) · 4.73 KB
/
GLO_EMO_parallel.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
"""
Author: André Ulrich
Test parallelization of GLO and EMO
"""
import os
from EMO import *
from optimization import GridLineOptimizer as GLO
from battery_electric_vehicle import BatteryElectricVehicle as BEV
from household import Household as HH
from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
import multiprocessing as mp
import threading as thr
import os
import time
CPUS = os.cpu_count()
#### GridLineOptimizer ########################################################
resolution = 15
buses = 6
bevs = 6
bev_lst = list(range(bevs))
bus_lst = list(range(buses))
s_trafo = 150 #kVA
t_steps = 96
t_counter = 0
# BEVs
home_buses = [0, 1, 2, 3, 4, 5]
start_socs = [20, 20, 30, 20, 25, 40]
target_socs = [80, 70, 80, 90, 80, 70]
target_times = [10, 16, 18, 18, 17, 18]
start_times = [2, 2, 2, 2, 2, 2]
bat_energies = [50, 50, 50, 50, 50, 50]
# Households
ann_dems = [3000, 3500, 3000, 4000, 3000, 3000]
# BEVs erzeugen
bev_list = []
for car in bev_lst:
bev = BEV(soc_start=start_socs[car], soc_target=target_socs[car],
t_target=target_times[car], e_bat=bat_energies[car],
resolution=resolution, home_bus=home_buses[car],
t_start=start_times[car])
bev_list.append(bev)
# Households erzeugen
household_list = []
for bus in bus_lst:
household = HH(home_bus=bus, annual_demand=ann_dems[bus], resolution=resolution)
household.raise_demand(11, 19, 23500)
household_list.append(household)
test = GLO(number_buses=buses, bevs=bev_list, resolution=resolution, s_trafo_kVA=s_trafo,
households=household_list, horizon_width=24)
# export grid as excel
grid_excel_file = 'optimized_grid'
test.export_grid(grid_excel_file)
grid_specs = test.get_grid_specs()
hh_data = test.export_household_profiles()
#wb_data = test.export_I_results() erst nach Optimierung sinnvoll! sonst nur None
#### EMO ######################################################################
system_1 = Low_Voltage_System(line_type='NAYY 4x120 SE', transformer_type="0.25 MVA 10/0.4 kV")
system_1.grid_from_GLO('grids/optimized_grid.xlsx', grid_specs)
sim_handler_1 = Simulation_Handler(system_1,
start_minute=60 * 12,
end_minute=60 * 12 + 24 * 60,
rapid=False)
queue = mp.Queue()
#func = test.run_optimization_single_timestep
def func_opt(tee, marker, queue):
pid = os.getpid()
global t_counter
for t in range(t_steps):
print('optimization round', t, 'running in process', pid)
test.run_optimization_single_timestep(tee=tee)
I_res = test.export_I_results()
print(I_res)
queue.put(I_res) # vielleicht ohne block?
test._store_results()
test._prepare_next_timestep()
test._setup_model()
#t_counter += 1 # is ja eigener Prozess, sieht die global t_counter von main gar nicht!
time.sleep(1.5)
queue.put('done')
#test.plot_results(marker=marker)
def func_sim(queue):
pid = os.getpid()
global t_counter
# function to be run in own thread, to get the data (I_res) out of the
# queue
def monitor_queue():
nonlocal queue, res_I
while True:
res_I = queue.get()
# first run optimization for first timestep to have results
test.run_optimization_single_timestep(tee=False)
res_I = test.export_I_results()
last_I_res = res_I
# start the thread to monitor the queue
thr.Thread(target=monitor_queue, daemon=True).start()
while True:
if res_I == last_I_res:
print('no new results, will continue with the old one')
else:
print('received new results!')
last_I_res = res_I
#t_counter += 1
#print(t_counter)
if res_I == 'done':
break
# run simulation with only the results for the first timestep
sim_handler_1.run_GLO_sim(hh_data, res_I, timesteps=2, parallel=True)
time.sleep(0.5)
#sim_handler_1.plot_EMO_sim_results(resolution, element='buses')
#sim_handler_1.plot_EMO_sim_results(freq=resolution, element='lines')
#sim_handler_1.plot_EMO_sim_results(freq=resolution, element='trafo')
#plt.show()
if __name__ == '__main__':
p_opt = mp.Process(target=func_opt, kwargs={'tee': False, 'marker': 'x', 'queue': queue}, daemon=True)
p_opt.start()
func_sim(queue)
p_opt.join()
print('done!')
# hier müsste jetzt eigentlich nur vom letzten timestep die Ergebnisse der simulation
# drin sein => noch Methode, die nach jedem Simulationsdurchlauf die Ergebnisse vom
# jeweils ersten timestep abfragt und speichert
plt.plot(range(len(sim_handler_1.res_GLO_sim_trafo)), sim_handler_1.res_GLO_sim_U[3])
plt.show()