-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1a_generate_ts_realrandom.py
421 lines (352 loc) · 17.2 KB
/
1a_generate_ts_realrandom.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
# RUN THIS FILE FROM THE TERMINAL OR COMMAND LINE
# THIS IS AN EXAMPLE TO CREATE MULTIPLE TIME SERIES
# BELOW YOU WILL FIND THE CONFIGURATION SECTION
# THIS EXAMPLE CONSIDERS PARALLEL PROCESSING TO SPEED UP THE DRIVING PROFILE GENERATION
from emobpy import Mobility, Availability, Charging, Export, DataBase
import pandas as pd
import numpy as np
import random
import psutil
from multiprocessing import Lock, Process, Queue
nr_workers = min(20, psutil.cpu_count()-1)
try:
import ray
is_ray = True
deco = ray.remote
ray.init(num_cpus=nr_workers, ignore_reinit_error=True, include_webui=False)
except ImportError:
deco = None
is_ray = False
def raydecorator(condition, decorator):
return decorator if condition else lambda x: x
@raydecorator(is_ray, deco)
def evsteps(odir, s1, s2, s3, p, r, seed):
print('evsteps seed:', seed)
np.random.seed(seed)
m = Mobility()
m.setParams(p)
m.setStats(s1, s2, s3)
m.setRules(r)
m.run()
m.save_profile(odir)
del m
return None
def drive_iter(odir,s1,s2,s3,p,r,from_,to_):
prequeue = []
for i in range(from_, to_):
prequeue.append([odir, s1, s2, s3, p, r])
return prequeue
@raydecorator(is_ray, deco)
def gridsteps(whenAtHome,
whenAtWork,
obs,
code,
battery_capacity,
charging_eff,
soc_init,
soc_min,
db,
fold,
battopt):
prob = whenAtHome[obs['whenathome']]
if obs['g_type'] == 'commuter':
prob['prob_charging_point']['workplace'] = whenAtWork[obs['whenatwork']]
a = Availability(code)
a.setScenario(prob)
a.setVehicleFeature(battery_capacity, charging_eff)
a.setBatteryRules(soc_init, soc_min, battopt)
a.loadSettingDriving(db)
a.run()
a.save_profile(fold, obs)
return None
def scen_solve_mov(func, queue, queue_lock, io_lock):
while True:
queue_lock.acquire()
if queue.empty():
queue_lock.release()
return
lsconf, seed = queue.get()
queue_lock.release()
func(*lsconf,seed)
return None
def scen_solve(func, queue, queue_lock, io_lock):
while True:
queue_lock.acquire()
if queue.empty():
queue_lock.release()
return
lsconf = queue.get()
queue_lock.release()
func(*lsconf)
return None
if __name__ == '__main__':
### EDIT THIS SECTION TO CREATE YOUR OWN SCENARIO ###
####################################################
########### Initial configuration parameters #######
####################################################
dbdir = '/home/dbeier/git-projects/emobpy_examples/casestudy/test123' # directory to store the profiles
run_driving = True
run_availability = False
run_charging = False
run_export = False
####################################################
############# Configuration for driving ############
####################################################
if run_driving:
bev_n = 25 # number of driving time series
dist = {'fulltime': 0.485, 'parttime': 0.135, 'freetime': 0.380} # share of profiles
hours = 24*14 # Total hours for each time series
timestep = 0.5 # in hours. e.g. 0.5 means half hour
consumption = 0.18 # kWh/km
refdate = '31/12/2018' # time series starting date. Starting time always 00:00
seeds = list(range(bev_n))
stat1 = pd.read_csv('vehiclestats/1_group_trips_weekdays.csv') # importing statistics
stat2 = pd.read_csv('vehiclestats/2_time_purpose_group_weeks_hrs.csv') # importing statistics. Time steps in the file must match the timestep variable. e.g. If the variable is 0.5 then in the file the time steps should be 0.5 as well
stat3 = pd.read_csv('vehiclestats/3_km_range_purpose_car.csv') # importing statistics
# param dictionary. Person key must match with the stat1 column "group" and with the stat2. Open the csv file and compare
param_comm_fulltime = {'person':'fulltime',
'group':'commuter',
'refdate':refdate,
'energy_consumption':consumption,
'hours':hours,
'timestep_in_hrs':timestep}
param_comm_parttime = {'person':'parttime',
'group':'commuter',
'refdate':refdate,
'energy_consumption':consumption,
'hours':hours,
'timestep_in_hrs':timestep}
param_free = {'person':'freetime',
'group':'freetime',
'refdate':refdate,
'energy_consumption':consumption,
'hours':hours,
'timestep_in_hrs':timestep}
rules_comm_fulltime={'weekday':
{'n_trip_out': [1],
'last_trip_to':{'home':True},
'at_least_one_trip':{'workplace':True},
'overall_min_time_at':{'home':9,'workplace':7.0},
'overall_max_time_at':{'workplace':8.0},
'min_state_duration':{'workplace':3.5}
},
'weekend':
{'n_trip_out': [1],
'last_trip_to':{'home':True},
'overall_min_time_at':{'home':6,'workplace':3},
'overall_max_time_at':{'workplace':4},
'min_state_duration':{'workplace':3}
}
}
rules_comm_parttime={'weekday':
{'n_trip_out': [1],
'last_trip_to':{'home':True},
'at_least_one_trip':{'workplace':True},
'overall_min_time_at':{'home':9,'workplace':3.5},
'overall_max_time_at':{'workplace':4},
'min_state_duration':{'workplace':3.5}
},
'weekend':
{'n_trip_out': [1],
'last_trip_to':{'home':True},
'overall_min_time_at':{'home':6,'workplace':3},
'overall_max_time_at':{'workplace':4},
'min_state_duration':{'workplace':3}
}
}
rules_freetime={'weekday':
{'n_trip_out': [1],
'last_trip_to':{'home':True},
'overall_min_time_at':{'home':9}
},
'weekend':
{'n_trip_out': [1],
'last_trip_to':{'home':True},
'overall_min_time_at':{'home':6}
}
}
####################################################
######## Configuration for grid availability #######
####################################################
if run_availability:
soc_init = 0.5 # initial state of charge. soc_init*100 = %
soc_min = 0.01 # profile can not have hours with state of charge lower than this value. soc_min*100 = %
battery_capacity = 40 # kWh
battopt = list(range(battery_capacity + 5, battery_capacity*4, 5)) # in case the motor electricity demand is high (long distance trips).
# The battery_capacity may be not enough.
# Then this list has several battery sizes that are tested until a size is good enough to fullfil demand requirements.
# However, it may happend that this list values are still not enough.
# Then it creates a file whose name indicate "FAIL".
# This has been done so to avoid stopping the creation of the rest of the profiles.
# There is a Notebook that can be use after this run to help to identify the FAIL files,
# to test more options to create a success profile.
charging_eff = 0.90
parkingathome = {'street': 0.19, 'garage': 0.81} # from the total amount of driving profiles, this share represents the ones who have a garage or not.
commuters_atworkplace = {'workpark': 0.5, 'publicpark': 0.25, 'none': 0.25} # This share applies only for commuters type driving profiles
# This configuration applies for all profiles
whenAtHome ={'street': {'prob_charging_point' :
{'errands': {'public':0.5,'none':0.5},
'escort': {'public':0.5,'none':0.5},
'leisure': {'public':0.5,'none':0.5},
'shopping': {'public':0.5,'none':0.5},
'home': {'public':0.5,'none':0.5},
'workplace':{'public':0.0,'workplace':1.0,'none':0.0},
'driving': {'none':1.0}
},
'capacity_charging_point' :
{'public':22,'home':3.7,'workplace':11,'none':0}
},
'garage': {'prob_charging_point' :
{'errands': {'public':0.5,'none':0.5},
'escort': {'public':0.5,'none':0.5},
'leisure': {'public':0.5,'none':0.5},
'shopping': {'public':0.5,'none':0.5},
'home': {'public':0.0,'home':1.0,'none':0.0},
'workplace':{'public':0.0,'workplace':1.0,'none':0.0},
'driving': {'none':1.0}
},
'capacity_charging_point' :
{'public':22,'home':3.7,'workplace':11,'none':0}
}
}
# this configuration applies only for commuters type and depends on the case ('workpark', 'publicpark', 'none') the above configuration gets replaced the 'workplace' key with the indicated below
whenAtWork = {'workpark':{'public':0.0,'workplace':1.0,'none':0.0},
'publicpark':{'public':0.5,'workplace':0.0,'none':0.5},
'none':{'public':0.0,'workplace':0.0,'none':1.0}}
####################################################
########### Configuration for grid demand ##########
####################################################
if run_charging:
options_list = ['immediate',
'balanced',
'from_0_to_24_at_home',
'from_23_to_8_at_home'
]
####################################################
######### Configuration for csv report file ########
####################################################
if run_export:
report_folder = 'csv'
####################################################
################# END CONFIGURATION ################
####################################################
############# DO NOT MODIFY THIS SECTION ###########
####################################################
if run_driving:
inventory = [[dbdir,stat1,stat2,stat3,param_comm_fulltime,rules_comm_fulltime,0,round(bev_n*dist['fulltime'])],
[dbdir,stat1,stat2,stat3,param_comm_parttime,rules_comm_parttime,0,round(bev_n*dist['parttime'])],
[dbdir,stat1,stat2,stat3,param_free,rules_freetime,0,round(bev_n*dist['freetime'])]]
lista = []
for l in inventory:
for p in drive_iter(*l):
lista.append(p)
if is_ray:
queue = []
else:
queue = Queue()
for q,seed in zip(lista, seeds):
if is_ray:
queue.append(evsteps.remote(*q,seed))
else:
queue.put((q,seed))
if is_ray:
ray.get(queue)
else:
io_lock = Lock()
queue_lock = Lock()
processes = {}
for i in range(nr_workers):
processes[i] = Process(target=scen_solve_mov, args=(evsteps, queue, queue_lock, io_lock))
processes[i].start()
for i in range(nr_workers):
processes[i].join()
print('=:=:=:=:=:=:=:=:=:=:= Driving done! =:=:=:=:=:=:=:=:=:=:')
db = DataBase(dbdir)
if run_availability:
db.update()
iddrivingp = [k for k, v in db.db.items() if v['kind'] == 'driving']
random.shuffle(iddrivingp)
groups = {}
init = 0
for key, val in parkingathome.items():
idx = init + int(round(len(iddrivingp)*val+0.01, 0))
try:
print('When_at_home slice:', init, ':', idx)
for code in iddrivingp[init:idx]:
groups[code] = {'g_type': db.db[code]['g_type'],
'per_str': db.db[code]['per_str'],
'whenathome': key,
'whenatwork': False}
except:
print('When_at_home slice:', init, ':', idx, 'out of range. Total profiles:',len(iddrivingp))
for code in iddrivingp[init:]:
groups[code] = {'g_type': db.db[code]['g_type'],
'per_str': db.db[code]['per_str'],
'whenathome': key,
'whenatwork': False}
init = idx
commuters = []
for code, dict_ in groups.items():
if 'commuter' == dict_['g_type']:
commuters.append(code)
random.shuffle(commuters)
init = 0
for key, val in commuters_atworkplace.items():
idx = init + int(round(len(commuters)*val+0.01, 0))
try:
print('Commuter_at_work slice:', init, ':', idx)
for code in commuters[init:idx]:
groups[code]['whenatwork'] = key
except:
print('Commuter_at_work slice:', init, ':', idx, 'out of range. Total profiles:',len(commuters))
for code in commuters[init:]:
groups[code]['whenatwork'] = key
init = idx
for cod, dic in groups.items():
print(cod, dic)
if is_ray:
queue = []
else:
queue = Queue()
for code, obs in groups.items():
q = [whenAtHome, whenAtWork, obs, code, battery_capacity, charging_eff, soc_init, soc_min, db, dbdir, battopt]
if is_ray:
queue.append(gridsteps.remote(*q))
else:
queue.put(q)
if is_ray:
ray.get(queue)
else:
io_lock = Lock()
queue_lock = Lock()
processes = {}
for i in range(nr_workers):
processes[i] = Process(target=scen_solve, args=(gridsteps, queue, queue_lock, io_lock))
processes[i].start()
for i in range(nr_workers):
processes[i].join()
print('=:=:=:=:=:=:=:=:=:=:= Grid availability done! =:=:=:=:=:=:=:=:=:=:')
if run_charging:
db.update()
for code, v in db.db.items():
if v['kind'] == 'availability':
print(' Success? Availability Profile: ', code, ' : ', v['success'])
for option in options_list:
db.update()
for ids in db.db.keys():
if db.db[ids]['kind'] == 'availability':
c = Charging(ids)
c.loadScenario(db)
c.setSubScenario(option)
c.run()
c.save_profile(dbdir)
print('=:=:=:=:=:=:=:=:=:=:= Charging options done! =:=:=:=:=:=:=:=:=:=:')
if run_export:
db.update()
export = Export()
export.loaddata(db)
print("Creating report... \nIf there is a failed availability file in the database, this process may hang up. \nIn which case check your database folder")
export.to_csv()
export.save_files(report_folder)
print('=:=:=:=:=:=:=:=:=:=:= Export done! =:=:=:=:=:=:=:=:=:=:')
print('==== Program finished ====')