-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathems_functions.py
949 lines (693 loc) · 26.2 KB
/
ems_functions.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
"""
Date: 2016-Mar-26
User-defined functions for EMS
"""
import numpy as np
import sqlite3 as sq
from scipy.optimize import minimize
from sklearn.externals import joblib
import neurolab as nl
import time
import sys
def load_data_solar(train_start, train_end, test_start, test_end):
"""
Load data from database
"""
con = sq.connect("ems.sqlite")
cur = con.cursor()
cur.execute("select rad, solar from forecast where date >= ? and date <= ?", (train_start, train_end))
train_data = cur.fetchall() # list
train_len = len(train_data) # no. of training data
cur.execute("select rad, solar from forecast where date >= ? and date <= ?", (test_start, test_end))
test_data = cur.fetchall()
test_len = len(test_data) # no. of testing data
train_data0 = np.asarray(train_data) # list to array
test_data0 = np.asarray(test_data) # list to array
data = np.vstack((train_data0, test_data0))
rad = data[:, 0].reshape(-1, 1)
solar = data[:, 1].reshape(-1, 1)
return solar, rad, train_len, test_len
def load_data_load(train_start, train_end, test_start, test_end):
"""
Load data from database
"""
con = sq.connect("ems.sqlite")
cur = con.cursor()
cur.execute("select temp, load from forecast where date >= ? and date <= ?", (train_start, train_end))
train_data = cur.fetchall() # list
train_len = len(train_data) # no. of training data
cur.execute("select temp, load from forecast where date >= ? and date <= ?", (test_start, test_end))
test_data = cur.fetchall()
test_len = len(test_data) # no. of testing data
train_data0 = np.asarray(train_data) # list to array
test_data0 = np.asarray(test_data) # list to array
data = np.vstack((train_data0, test_data0))
temp = data[:, 0].reshape(-1, 1)
eload = data[:, 1].reshape(-1, 1)
return eload, temp, train_len, test_len
def load_data_wind(train_start, train_end, test_start, test_end):
"""
Load data from database
"""
con = sq.connect("ems.sqlite")
cur = con.cursor()
cur.execute("select speed, wind from forecast where date >= ? and date <= ?", (train_start, train_end))
train_data = cur.fetchall() # list
train_len = len(train_data) # no. of training data
cur.execute("select speed, wind from forecast where date >= ? and date <= ?", (test_start, test_end))
test_data = cur.fetchall()
test_len = len(test_data) # no. of testing data
train_data0 = np.asarray(train_data) # list to array
test_data0 = np.asarray(test_data) # list to array
data = np.vstack((train_data0, test_data0))
speed = data[:, 0].reshape(-1, 1)
wind = data[:, 1].reshape(-1, 1)
return wind, speed, train_len, test_len
def prepare_data(lag_power, lag_rad, power, rad, train_len, test_len):
"""
Prepare data for SVR
"""
# training set
trainY = power[24:train_len]
X1 = np.zeros((len(trainY), len(lag_power)))
h = range(24, train_len)
for i in range(0, len(lag_power)):
X1[:, i] = power[h - lag_power[i]].reshape(len(trainY))
X2 = np.zeros((len(trainY), len(lag_rad)))
for j in range(0, len(lag_rad)):
X2[:, j] = rad[h - lag_rad[j]].reshape(len(trainY))
# testing set
test_from = len(power) - test_len
testY = power[test_from:]
TX1 = np.zeros((len(testY), len(lag_power)))
h = range(test_from, len(power))
for i in range(0, len(lag_power)):
TX1[:, i] = power[h - lag_power[i]].reshape(len(testY))
TX2 = np.zeros((len(testY), len(lag_rad)))
for j in range(0, len(lag_rad)):
TX2[:, j] = rad[h - lag_rad[j]].reshape(len(testY))
if lag_rad == []:
trainX = X1
testX = TX1
elif lag_power == []:
trainX = X2
testX = TX2
else:
trainX = np.hstack((X1, X2))
testX = np.hstack((TX1, TX2))
return trainX, trainY, testX, testY
def prepare_data_load(lag_power, lag_rad, power, rad, train_len, test_len):
"""
Prepare data for SVR
"""
# training set
trainY = power[168:train_len]
X1 = np.zeros((len(trainY), len(lag_power)))
h = range(168, train_len)
for i in range(0, len(lag_power)):
X1[:, i] = power[h - lag_power[i]].reshape(len(trainY))
X2 = np.zeros((len(trainY), len(lag_rad)))
for j in range(0, len(lag_rad)):
X2[:, j] = rad[h - lag_rad[j]].reshape(len(trainY))
# testing set
test_from = len(power) - test_len
testY = power[test_from:]
TX1 = np.zeros((len(testY), len(lag_power)))
h = range(test_from, len(power))
for i in range(0, len(lag_power)):
TX1[:, i] = power[h - lag_power[i]].reshape(len(testY))
TX2 = np.zeros((len(testY), len(lag_rad)))
for j in range(0, len(lag_rad)):
TX2[:, j] = rad[h - lag_rad[j]].reshape(len(testY))
if lag_rad == []:
trainX = X1
testX = TX1
elif lag_power == []:
trainX = X2
testX = TX2
else:
trainX = np.hstack((X1, X2))
testX = np.hstack((TX1, TX2))
return trainX, trainY, testX, testY
def y_in_range(y, maxPower):
"""
Force predictions in range, used for solar and wind
"""
for i in range(0, len(y)):
if y[i] > maxPower:
y[i] = maxPower
elif y[i] < 0:
y[i] = 0
return y
def dispatch(test_start, test_end):
"""
Economic dispatch
--------------------------------
Cost functions:
f_diesel = 2.6975 + 1.1153 * p + 0.05 * p * p
f_bat_discharge = 0.1154 + 0.7975 * p + 0.1409 * p * p
--------------------------------
Marginal cost functions:
df_diesel = 1.1153 + 0.1 * p
df_bat_discharge = 0.7975 + 0.2818 * p
df_bat_charge = 1.381
--------------------------------
Remark:
1. battery is cheaper than diesel
2. Using battery only is cheaper than using the combination of battery and diesel
"""
con = sq.connect('ems.sqlite')
cur = con.cursor()
cur.execute("select load_f, solar_f, wind_f from forecast "
"where date >= ? and date <= ?", (test_start, test_end))
forecasted_data = np.asarray(cur.fetchall()) # a list
eload = forecasted_data[:, 0]
solar = forecasted_data[:, 1]
wind = forecasted_data[:, 2]
# net load for power dispatch
netload = eload - solar - wind
# diesel generator cost function
# df_diesel = 1.1153 + 0.1*P
maxDiesel = 5.0
# battery charging: make money
# df_bat_charge = 1.381
maxBat = 2.0
# battery discharging: cost money
# df_bat_discharge = 0.7975 + 0.2818*P
# objective: min cost
len0 = len(eload)
set_diesel = np.zeros(len0)
set_bat = np.zeros(len0)
bat_start = 180.0 # kW*min
bat_max = 270.0
bat_min = 30.0
bat_now = bat_start # instant SOC, should get it outside actually
for i in range(0, len0):
if netload[i] < 0: # not happen in this case
pass
elif netload[i] == 0: # not happen in this case
pass
else: # solar + wind < load, diesel and battery supply
if bat_now > bat_min:
bat_gap = bat_now - bat_min
bat_ub = min(maxBat, bat_gap)
if netload[i] <= bat_ub: # battery is cheaper
set_bat[i] = netload[i]
else: # diesel is needed, call SLSQP to optimize
def obj2(x): # objective function 2
return 2.6975 + 1.1153 * x[0] + 0.05 * x[0] * x[0] \
+ 0.1154 + 0.7975 * x[1] + 0.1409 * x[1] * x[1]
def obj2_deriv(x): # derivative of obj2
dfdx0 = 1.1153 + 0.05 * x[0] * 2
dfdx1 = 0.7975 + 0.1409 * x[1] * 2
return np.array([dfdx0, dfdx1])
# add constraints and bounds
cons = ({'type': 'eq',
'fun': lambda x: np.array([x[0] + x[1] - netload[i]]),
'jac': lambda x: np.array([1.0, 1.0])})
bnds = ((0, maxDiesel), (0, bat_ub))
# optimize
res = minimize(obj2, [1.0, 1.0], jac=obj2_deriv, constraints=cons,
method='SLSQP', bounds=bnds)
set_diesel[i] = res.x[0]
set_bat[i] = res.x[1] # discharge is positive
# update battery status
bat_now = bat_now - set_bat[i]
else: # battery at lowest energy, cannot discharge, diesel supplied solely
set_diesel[i] = min(maxDiesel, netload[i])
print('Unit commitment and economic dispatch completed\n')
# save set points to database
cur.execute("select rowid from dispatch where date = '2010-01-04 12:01:00'")
rowid = cur.fetchone()[0]
for i in range(0, len0):
cur.execute("update dispatch set diesel = ?, battery = ? where rowid = ?", (set_diesel[i], set_bat[i], rowid))
rowid = rowid + 1
con.commit()
return set_diesel, set_bat, netload
def load_svm(train_start, train_end, test_start, test_end):
"""
Use saved model to predict load
"""
eload, temp, train_len, test_len = load_data_load(train_start, train_end, test_start, test_end)
# normalization
norm_temp = nl.tool.Norm(temp)
tempn = norm_temp(temp)
norm_power = nl.tool.Norm(eload)
loadn = norm_power(eload)
# prepare data for SVM
horizon = 1
lag_load = np.array([1, 2, 3, 23, 24, 25, 48, 72, 144, 168])
lag_load = lag_load + horizon - 1
lag_temp = []
trainX, trainY, testX, testY = prepare_data_load(lag_load, lag_temp, loadn, tempn, train_len, test_len)
# load saved model
modelname = 'load_predict.pkl'
model = joblib.load(modelname)
TY = model.predict(testX)
TY = norm_power.renorm(TY) # reverse to real scale
ybar = TY.reshape(len(testY))
testY = norm_power.renorm(testY) # actual output
y = testY.reshape(len(testY))
maxPower = 8.0
test_rmse = np.sqrt(np.mean((y - ybar) ** 2)) / maxPower * 100
print('Load forecasting completed\n')
# print('Forecasting nRMSE is: %.2f\n' % test_rmse)
# save forecasted value to database
con = sq.connect('ems.sqlite')
cur = con.cursor()
cur.execute("select rowid from forecast where date = '2010-01-04 12:01:00'")
rowid = cur.fetchone()[0]
for item in ybar:
cur.execute("update forecast set load_f = ? where rowid = ?", (item, rowid))
rowid = rowid + 1
con.commit()
def solar_svm(train_start, train_end, test_start, test_end):
"""
Use saved model to predict solar energy
"""
solar, rad, train_len, test_len = load_data_solar(train_start, train_end, test_start, test_end)
# normalization
norm_rad = nl.tool.Norm(rad)
radn = norm_rad(rad)
norm_power = nl.tool.Norm(solar)
solarn = norm_power(solar)
# prepare data for SVR
horizon = 1
lag_solar = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
lag_solar = lag_solar + horizon - 1
lag_rad = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
trainX, trainY, testX, testY = prepare_data(lag_solar, lag_rad, solarn, radn, train_len, test_len)
# load saved model
modelname = 'solar_predict.pkl'
model = joblib.load(modelname)
TY = model.predict(testX)
TY = norm_power.renorm(TY) # reverse to real scale
ybar = TY.reshape(len(testY))
testY = norm_power.renorm(testY) # actual output
y = testY.reshape(len(testY))
maxPower = 2.0
test_rmse = np.sqrt(np.mean((y - ybar) ** 2)) / maxPower * 100
print('Solar forecasting completed\n')
# print('Forecasting nRMSE is: %.2f\n' % test_rmse)
# save forecasted value to database
con = sq.connect('ems.sqlite')
cur = con.cursor()
cur.execute("select rowid from forecast where date = '2010-01-04 12:01:00'")
rowid = cur.fetchone()[0]
for item in ybar:
cur.execute("update forecast set solar_f = ? where rowid = ?", (item, rowid))
rowid = rowid + 1
con.commit()
def wind_svm(train_start, train_end, test_start, test_end):
"""
Use saved model to predict wind energy
"""
wind, speed, train_len, test_len = load_data_wind(train_start, train_end, test_start, test_end)
# normalization
norm_speed = nl.tool.Norm(speed)
radn = norm_speed(speed)
norm_power = nl.tool.Norm(wind)
windn = norm_power(wind)
# prepare data for SVR
horizon = 1
lag_wind = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
lag_wind = lag_wind + horizon - 1
lag_speed = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
trainX, trainY, testX, testY = prepare_data(lag_wind, lag_speed, windn, radn, train_len, test_len)
# load saved model
modelname = 'wind_predict.pkl'
model = joblib.load(modelname)
TY = model.predict(testX)
TY = norm_power.renorm(TY) # reverse to real scale
ybar = TY.reshape(len(testY))
testY = norm_power.renorm(testY) # actual output
y = testY.reshape(len(testY))
maxPower = 2.0
test_rmse = np.sqrt(np.mean((y - ybar) ** 2)) / maxPower * 100
print('Wind forecasting completed\n')
# print('Forecasting nRMSE is: %.2f\n' % test_rmse)
# save forecasted value to database
con = sq.connect('ems.sqlite')
cur = con.cursor()
cur.execute("select rowid from forecast where date = '2010-01-04 12:01:00'")
rowid = cur.fetchone()[0]
for item in ybar:
cur.execute("update forecast set wind_f = ? where rowid = ?", (item, rowid))
rowid = rowid + 1
con.commit()
def showResult(test_start, test_end):
con = sq.connect('ems.sqlite')
cur = con.cursor()
cur.execute("select load_f, solar_f, wind_f from forecast "
"where date >= ? and date <= ?", (test_start, test_end))
forecasted_data = cur.fetchall() # a list
print('-------------------- Forecasting Results ---------------------------\n')
print('%-20s%-20s%-20s%-20s' % ('Minute', 'Predicted Load', 'Predicted Solar', 'Predicted Wind'))
minute = 1
for row in forecasted_data:
print('%.2d %.4f %.4f %.4f' % (minute, row[0], row[1], row[2]))
minute += 1
print('\n')
cur.execute("select diesel, battery from dispatch "
"where date >= ? and date <= ?", (test_start, test_end))
dispatch_data = cur.fetchall() # a list
print('-------------------- Unit Commitment Results ---------------------\n')
print('%-20s%-20s%-20s%-20s%-20s' % ('Minute', 'Solar PV', 'Wind Turbine', 'Diesel Gen', 'Battery'))
minute = 1
for row in dispatch_data:
diesel_status = int(row[0] > 1e-4)
bat_status = int(row[1] > 1e-4)
print('%.2d %d %d %d %d' % (minute, 1, 1, diesel_status, bat_status))
minute += 1
print('\n')
print('-------------------- Economic Dispatch Results ---------------------\n')
print('%-20s%-20s%-20s%-20s%-20s' % ('Minute', 'Solar PV', 'Wind Turbine', 'Diesel Gen', 'Battery'))
minute = 1
for row in dispatch_data:
x = np.asarray(forecasted_data)
print('%.2d %.4f %.4f %.4f %.4f' % (minute, x[minute-1, 1], x[minute-1, 2], row[0], row[1]))
minute += 1
print('\n')
'''
Date: 2016-Apr-12
Forecast and dispatch step by step
'''
def load_svm_one(test_time):
"""
Use saved model to predict load point by point
"""
# read data from database
# need data prior to test_start to setup the testX
time_int = int(time.mktime(time.strptime(test_time, '%Y-%m-%d %H:%M:%S')))
before_test = 170
before_start = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time_int - before_test * 60))
con = sq.connect("ems.sqlite")
cur = con.cursor()
cur.execute("select temp, load from forecast where date >= ? and date <= ?", (before_start, test_time))
selected_data = cur.fetchall()
data = np.asarray(selected_data)
temp = data[:, 0].reshape(-1, 1)
eload = data[:, 1].reshape(-1, 1)
# normalization
# norm_load = joblib.load('norm_load.pkl')
# norm_temp = joblib.load('norm_temp.pkl')
norm_load = nl.tool.Norm(eload)
norm_temp = nl.tool.Norm(temp)
tempn = norm_temp(temp)
loadn = norm_load(eload)
# lagged values used as input variables
horizon = 1
lag_load = np.array([1, 2, 3, 23, 24, 25, 48, 72, 144, 168])
lag_load = lag_load + horizon - 1
lag_temp = []
# form testX
test_from = len(loadn) - 1
TX1 = np.zeros((1, len(lag_load)))
for i in range(0, len(lag_load)):
TX1[0, i] = loadn[test_from - lag_load[i]]
TX2 = np.zeros((1, len(lag_temp)))
for j in range(0, len(lag_temp)):
TX2[0, j] = tempn[test_from - lag_temp[j]]
if lag_temp == []:
testX = TX1
elif lag_load == []:
testX = TX2
else:
testX = np.hstack((TX1, TX2))
# load saved model
m = 'load_predict.pkl'
model = joblib.load(m)
TY = model.predict(testX)
ybar = norm_load.renorm(TY) # reverse to real scale
# save forecasted value to database, only 1 data
cur.execute("update forecast set load_f = ? where date = ?", (ybar[0, 0], test_time))
con.commit()
return ybar
def solar_svm_one(test_time):
"""
Use saved model to predict solar point by point
"""
# read data from database
# need data prior to test_start to setup the testX
time_int = int(time.mktime(time.strptime(test_time, '%Y-%m-%d %H:%M:%S')))
before_test = 11
before_start = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time_int - before_test * 60))
con = sq.connect("ems.sqlite")
cur = con.cursor()
cur.execute("select rad, solar from forecast where date >= ? and date <= ?", (before_start, test_time))
selected_data = cur.fetchall()
data = np.asarray(selected_data)
rad = data[:, 0].reshape(-1, 1)
solar = data[:, 1].reshape(-1, 1)
# normalization
norm_rad = nl.tool.Norm(rad)
radn = norm_rad(rad)
norm_solar = nl.tool.Norm(solar)
solarn = norm_solar(solar)
# lagged values used as input variables
horizon = 1
lag_solar = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
lag_solar = lag_solar + horizon - 1
lag_rad = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# form testX
test_from = len(solarn) - 1
TX1 = np.zeros((1, len(lag_solar)))
for i in range(0, len(lag_solar)):
TX1[0, i] = solarn[test_from - lag_solar[i]]
TX2 = np.zeros((1, len(lag_rad)))
for j in range(0, len(lag_rad)):
TX2[0, j] = radn[test_from - lag_rad[j]]
if lag_rad == []:
testX = TX1
elif lag_solar == []:
testX = TX2
else:
testX = np.hstack((TX1, TX2))
# load saved model
m = 'solar_predict.pkl'
model = joblib.load(m)
TY = model.predict(testX)
ybar = norm_solar.renorm(TY) # reverse to real scale
# save forecasted value to database, only 1 data
cur.execute("update forecast set solar_f = ? where date = ?", (ybar[0, 0], test_time))
con.commit()
return ybar
def wind_svm_one(test_time):
"""
Use saved model to predict wind point by point
"""
# read data from database
# need data prior to test_start to setup the testX
time_int = int(time.mktime(time.strptime(test_time, '%Y-%m-%d %H:%M:%S')))
before_test = 11
before_start = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time_int - before_test * 60))
con = sq.connect("ems.sqlite")
cur = con.cursor()
cur.execute("select speed, wind from forecast where date >= ? and date <= ?", (before_start, test_time))
selected_data = cur.fetchall()
data = np.asarray(selected_data)
speed = data[:, 0].reshape(-1, 1)
wind = data[:, 1].reshape(-1, 1)
# normalization
norm_speed = nl.tool.Norm(speed)
speedn = norm_speed(speed)
norm_wind = nl.tool.Norm(wind)
windn = norm_wind(wind)
# lagged values used as input variables
horizon = 1
lag_wind = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
lag_wind = lag_wind + horizon - 1
lag_speed = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
# form testX
test_from = len(windn) - 1
TX1 = np.zeros((1, len(lag_wind)))
for i in range(0, len(lag_wind)):
TX1[0, i] = windn[test_from - lag_wind[i]]
TX2 = np.zeros((1, len(lag_speed)))
for j in range(0, len(lag_speed)):
TX2[0, j] = speedn[test_from - lag_speed[j]]
if lag_speed == []:
testX = TX1
elif lag_wind == []:
testX = TX2
else:
testX = np.hstack((TX1, TX2))
# load saved model
m = 'wind_predict.pkl'
model = joblib.load(m)
TY = model.predict(testX)
ybar = norm_wind.renorm(TY) # reverse to real scale
# save forecasted value to database, only 1 data
cur.execute("update forecast set wind_f = ? where date = ?", (ybar[0, 0], test_time))
con.commit()
return ybar
def dispatch_one(test_time):
"""
dispatch step by step
"""
con = sq.connect('ems.sqlite')
cur = con.cursor()
cur.execute("select load_f, solar_f, wind_f from forecast "
"where date >= ? and date <=?", (test_time, test_time))
forecasted_data = np.asarray(cur.fetchall()) # a list
eload = forecasted_data[0, 0]
solar = forecasted_data[0, 1]
wind = forecasted_data[0, 2]
# net load for power dispatch
netload = eload - solar - wind
# diesel generator cost function
# df_diesel = 1.1153 + 0.1*P
maxDiesel = 5.0
# battery charging: make money
# df_bat_charge = 1.381
maxBat = 2.0
# battery discharging: cost money
# df_bat_discharge = 0.7975 + 0.2818*P
# objective: min cost
set_diesel = 0
set_bat = 0
bat_start = 180.0 # kW*min
bat_max = 270.0
bat_min = 30.0
bat_now = bat_start # instant SOC, should get it outside actually
if netload < 0: # not happen in this case
pass
elif netload == 0: # not happen in this case
pass
else: # solar + wind < load, diesel and battery supply
if bat_now > bat_min:
bat_gap = bat_now - bat_min
bat_ub = min(maxBat, bat_gap)
if netload <= bat_ub: # battery is cheaper
set_bat = netload
else: # diesel is needed, call SLSQP to optimize
def obj2(x): # objective function 2
return 2.6975 + 1.1153 * x[0] + 0.05 * x[0] * x[0] \
+ 0.1154 + 0.7975 * x[1] + 0.1409 * x[1] * x[1]
def obj2_deriv(x): # derivative of obj2
dfdx0 = 1.1153 + 0.05 * x[0] * 2
dfdx1 = 0.7975 + 0.1409 * x[1] * 2
return np.array([dfdx0, dfdx1])
# add constraints and bounds
cons = ({'type': 'eq',
'fun': lambda x: np.array([x[0] + x[1] - netload]),
'jac': lambda x: np.array([1.0, 1.0])})
bnds = ((0, maxDiesel), (0, bat_ub))
# optimize
res = minimize(obj2, [1.0, 1.0], jac=obj2_deriv, constraints=cons,
method='SLSQP', bounds=bnds)
set_diesel = res.x[0]
set_bat = res.x[1] # discharge is positive
# update battery status
bat_now = bat_now - set_bat
else: # battery at lowest energy, cannot discharge, diesel supplied solely
set_diesel = min(maxDiesel, netload)
# save set points to database
cur.execute("update dispatch set diesel = ?, battery = ? where date = ?", (set_diesel, set_bat, test_time))
con.commit()
return set_diesel, set_bat
def dispatch_one2(test_time, load_predict, solar_predict, wind_predict):
"""
dispatch step by step version 2
"""
# net load for power dispatch
netload = load_predict - solar_predict - wind_predict
# diesel generator cost function
# df_diesel = 1.1153 + 0.1*P
maxDiesel = 5.0
# battery charging: make money
# df_bat_charge = 1.381
maxBat = 2.0
# battery discharging: cost money
# df_bat_discharge = 0.7975 + 0.2818*P
# objective: min cost
set_diesel = 0
set_bat = 0
bat_start = 180.0 # kW*min
bat_max = 270.0
bat_min = 30.0
bat_now = bat_start # instant SOC, should get it outside actually
if netload < 0: # not happen in this case
pass
elif netload == 0: # not happen in this case
pass
else: # solar + wind < load, diesel and battery supply
if bat_now > bat_min:
bat_gap = bat_now - bat_min
bat_ub = min(maxBat, bat_gap)
if netload <= bat_ub: # battery is cheaper
set_bat = netload
else: # diesel is needed, call SLSQP to optimize
def obj2(x): # objective function 2
return 2.6975 + 1.1153 * x[0] + 0.05 * x[0] * x[0] \
+ 0.1154 + 0.7975 * x[1] + 0.1409 * x[1] * x[1]
def obj2_deriv(x): # derivative of obj2
dfdx0 = 1.1153 + 0.05 * x[0] * 2
dfdx1 = 0.7975 + 0.1409 * x[1] * 2
return np.array([dfdx0, dfdx1])
# add constraints and bounds
cons = ({'type': 'eq',
'fun': lambda x: np.array([x[0] + x[1] - netload]),
'jac': lambda x: np.array([1.0, 1.0])})
bnds = ((0, maxDiesel), (0, bat_ub))
# optimize
res = minimize(obj2, [1.0, 1.0], jac=obj2_deriv, constraints=cons,
method='SLSQP', bounds=bnds)
set_diesel = res.x[0]
set_bat = res.x[1] # discharge is positive
# update battery status
bat_now = bat_now - set_bat
else: # battery at lowest energy, cannot discharge, diesel supplied solely
set_diesel = min(maxDiesel, netload)
# save set points to database
con = sq.connect('ems.sqlite')
cur = con.cursor()
cur.execute("update dispatch set diesel = ?, battery = ? where date = ?", (set_diesel, set_bat, test_time))
con.commit()
return set_diesel, set_bat
# testing code
# if __name__ == '__main__':
# train_start = "2010-01-01 00:01:00"
# train_end = "2010-01-04 00:00:00"
# test_start = "2010-01-04 12:01:00"
# test_end = "2010-01-04 13:00:00"
#
# tic = time.time()
# load_svm(train_start, train_end, test_start, test_end)
# wind_svm(train_start, train_end, test_start, test_end)
# solar_svm(train_start, train_end, test_start, test_end)
# set_diesel, set_bat, netload = dispatch(test_start, test_end)
# toc = time.time()
# print('Total running time is: %.4f s\n' % (toc-tic))
#
# showResult(test_start, test_end)
# if sys.platform == 'win32':
# import pylab as pl
# pl.plot(set_diesel, label='diesel generator', linewidth=2.0)
# pl.plot(set_bat, label='battery', linewidth=2.0)
# pl.plot(netload, label='netload', linewidth=2.0)
# pl.legend(loc='upper right')
# # pl.savefig('dispatch.png', dpi=500)
# # pl.show()
if __name__ == '__main__':
test_start = "2010-01-04 12:01:00"
test_end = "2010-01-04 13:00:00"
con = sq.connect("ems.sqlite")
cur = con.cursor()
cur.execute("select date from forecast where date >= ? and date <= ?", (test_start, test_end))
selected_dates = cur.fetchall()
print('\n')
print('---------------------- Forecasting and Economic Dispatch Results ----------------------\n')
print('%-20s%-20s%-20s%-20s%-20s%-20s' % ('Minute', 'Load', 'Solar PV', 'Wind T', 'Diesel', 'Battery'))
for i in range(0, len(selected_dates)):
test_time = ''.join(selected_dates[i])
load_f = load_svm_one(test_time)
solar_f = solar_svm_one(test_time)
wind_f = wind_svm_one(test_time)
set_diesel, set_bat = dispatch_one(test_time)
# minute = test_time.encode('ascii','ignore')[11:16]
# print('%s %.4f %.4f %.4f %.4f %.4f'
# % (minute, load_f, solar_f, wind_f, set_diesel, set_bat))
print('%2d %.4f %.4f %.4f %.4f %.4f'
% (i + 1, load_f, solar_f, wind_f, set_diesel, set_bat))
# time.sleep(0.5)