-
Notifications
You must be signed in to change notification settings - Fork 3
/
sim_demand_learning.jl
347 lines (285 loc) · 15.2 KB
/
sim_demand_learning.jl
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
begin
dir = @__DIR__
include("$dir/src/system_structs.jl")
include("$dir/src/network_dynamics.jl")
end
begin
using JLD2, FileIO, GraphIO, CSV, DataFrames
using Distributed
using Interpolations
using DifferentialEquations
using Distributions
using LightGraphs
using LinearAlgebra
using Random
using DSP
using ToeplitzMatrices
Random.seed!(42)
end
begin
N = 4
num_days = 10
freq_threshold = 0.2
end
begin
phase_filter = 1:N
freq_filter = N+1:2N
control_filter = 2N+1:3N
energy_filter = 3N+1:4N
energy_abs_filter = 4N+1:5N
end
############################################
begin
l_day = 3600*24 # DemCurve.l_day
l_hour = 3600 # DemCurve.l_hour
l_minute = 60 # DemCurve.l_minute
#low_layer_control = system_structs.LeakyIntegratorPars(M_inv=0.2,kP=52,T_inv=1/0.05,kI=10)
# low_layer_control = system_structs.LeakyIntegratorPars(M_inv=0.2,kP=525,T_inv=1/0.05,kI=0.005)
low_layer_control = system_structs.LeakyIntegratorPars(M_inv=repeat([0.2], inner=N),kP=repeat([525], inner=N),T_inv=repeat([1/0.05], inner=N),kI=repeat([0.005], inner=N)) # different for each node, change array
# low_layer_control = system_structs.LeakyIntegratorPars(M_inv=[1/5.; 1/4.8; 1/4.1; 1/4.8],kP= [400.; 110.; 100.; 200.],T_inv=[1/0.04; 1/0.045; 1/0.047; 1/0.043],kI=[0.05; 0.004; 0.05; 0.001]) # different for each node, change array
#low_layer_control = system_structs.LeakyIntegratorPars(M_inv=repeat([0.2], inner=N),kP=[0.1; 10; 100; 1000],T_inv=repeat([1/0.05], inner=N),kI=repeat([0.005], inner=N)) # different for each node, change array
#low_layer_control = system_structs.LeakyIntegratorPars(M_inv=repeat([0.2], inner=N),kP=repeat([525], inner=N),T_inv=[1/0.05; 1/0.5; 1/5; 1/50],kI=repeat([0.005], inner=N)) # different for each node, change array
#low_layer_control = system_structs.LeakyIntegratorPars(M_inv=repeat([0.2], inner=N),kP=repeat([525], inner=N),T_inv=repeat([1/0.05], inner = N),kI=[0.005; 0.5; 5; 500]) # different for each node, change array
#low_layer_control = system_structs.LeakyIntegratorPars(M_inv=[0.002; 0.2; 2; 20],kP=repeat([525], inner=N),T_inv=repeat([1/0.05], inner = N),kI=repeat([0.005], inner=N)) # different for each node, change array
kappa = 1.0 / l_hour
lambda = 1.
end
############################################
# NETWORK - this should only run on one process
############################################
# # Full graph for N=4 and degree 3 graph otherwise, change last 3 to 1 for N=2
begin
graph = random_regular_graph(iseven(3N) ? N : (N-1), 3)
end
# change last "3" to 1 for N=2
# N = 1
#graph = SimpleGraph(1)
# # Square - needs to be changed only here
# graph = SimpleGraph(4)
# add_edge!(_graph_lst, 1,2)
# add_edge!(_graph_lst, 2,3)
# add_edge!(_graph_lst, 3,4)
# add_edge!(_graph_lst, 4,1)
# using GraphPlot
# gplot(graph)
# # Line - needs to be changed only here
# graph = SimpleGraph(4)
# add_edge!(_graph_lst, 1,2)
# add_edge!(_graph_lst, 2,3)
# add_edge!(_graph_lst, 3,4)
# using GraphPlot
# gplot(graph)
############################################
# demand
############################################
struct demand_amp_var
demand
end
function (dav::demand_amp_var)(t)
index = Int(floor(t / (24*3600)))
dav.demand[index + 1,:]
end
# fixed amp over the days
# demand_amp = rand(N) .* 250.
# # slowly increasing amplitude - only working for 10 days now
# demand_ampp = demand_amp_var(repeat([10 20 30 40 50 60 70 80 90 100 110], outer=Int(N/2))') # random positive amp over days by 10%
# demand_ampn = demand_amp_var(repeat([-10 -20 -30 -40 -50 -60 -70 -80 -90 -100 -110], outer=Int(N/2))') # random positive amp over days by 10%
# demand_amp = t->vcat(demand_ampp(t), demand_ampn(t))
# # slowly decreasing amplitude - only working for 10 days now
# demand_ampp = demand_amp_var(repeat([110 100 90 80 70 60 50 40 30 20 10], outer=Int(N/2))') # random positive amp over days by 10%
# demand_ampn = demand_amp_var(repeat([-110 -100 -90 -80 -70 -60 -50 -40 -30 -20 -10], outer=Int(N/2))') # random positive amp over days by 10%
# demand_amp = t->vcat(demand_ampp(t), demand_ampn(t))
# # slowly decreasing and increasing amplitude - only working for 10 days now
# demand_ampp = demand_amp_var(repeat([120 120 120 120 120 170 200 120 120 120 120 120 170 200 120], outer=Int(N/2))') # random positive amp over days by 10%
# demand_ampn = demand_amp_var(repeat([120 120 120 120 120 170 200 120 120 120 120 120 170 200 120], outer=Int(N/2))') # random positive amp over days by 10%
# demand_amp = t->vcat(demand_ampp(t), demand_ampn(t))
# slowly increasing and decreasing amplitude - only working for <= 10 days and N = 4 now
demand_amp1 = demand_amp_var(repeat([80 80 80 10 10 10 40 40 40 40 40], outer=Int(N/4))') # random positive amp over days by 10%
demand_amp2 = demand_amp_var(repeat([10 10 10 80 80 80 40 40 40 40 40], outer=Int(N/4))') # random positive amp over days by 10%
demand_amp3 = demand_amp_var(repeat([60 60 60 60 10 10 10 40 40 40 40], outer=Int(N/4))') # random positive amp over days by 10%
demand_amp4 = demand_amp_var(repeat([30 30 30 30 10 10 10 80 80 80 80], outer=Int(N/4))') # random positive amp over days by 10%
demand_amp = t->vcat(demand_amp1(t), demand_amp2(t), demand_amp3(t), demand_amp4(t))
# demand_amp1 = demand_amp_var(60 .+ rand(num_days+1,Int(N/4)).* 40.)
# demand_amp2 = demand_amp_var(70 .+ rand(num_days+1,Int(N/4)).* 30.)
# demand_amp3 = demand_amp_var(80 .+ rand(num_days+1,Int(N/4)).* 20.)
# demand_amp4 = demand_amp_var(90 .+ rand(num_days+1,Int(N/4)).* 10.)
# demand_amp = t->vcat(demand_amp1(t), demand_amp2(t),demand_amp3(t),demand_amp4(t))
#
# # random positive amp over days by 30%
# demand_ampp = demand_amp_var(70 .+ rand(num_days+1,Int(N/2)).* 30.)
# demand_ampn = demand_amp_var(70 .+ rand(num_days+1,Int(N/2)).* 30.) # random negative amp over days by 10%
# demand_amp = t->vcat(demand_ampp(t), demand_ampn(t))
periodic_demand = t-> demand_amp(t)./100 .* sin(t*pi/(24*3600))^2
samples = 24*4
inter = interpolate([.2 * randn(N) for i in 1:(num_days * samples + 1)], BSpline(Linear()))
residual_demand = t -> inter(1. + t / (24*3600) * samples) # 1. + is needed to avoid trying to access out of range
using Plots
plotlyjs()
using LaTeXStrings
dd = t->((periodic_demand(t) .+ residual_demand(t)))
plot(0:num_days*l_day, t -> (dd(t)[1] .+ dd(t)[2] .+ dd(t)[3] .+ dd(t)[4]), alpha=0.6, label = latexstring("\$P^d_j\$"),linewidth=3, linestyle=:dot)
#########################################
# SIM #
#########################################
vc1 = 1:N # ilc_nodes (here: without communication)
cover1 = Dict([v => [] for v in vc1])# ilc_cover
u = [zeros(1000,1);1;zeros(1000,1)];
fc = 1/6;
a = digitalfilter(Lowpass(fc),Butterworth(2));
Q1 = filtfilt(a,u);# Markov Parameter
Q = Toeplitz(Q1[1001:1001+24-1],Q1[1001:1001+24-1]);
compound_pars = system_structs.compound_pars(N, low_layer_control, kappa, vc1, cover1, Q, lambda)
compound_pars.hl.daily_background_power .= 0
compound_pars.hl.current_background_power .= 0
compound_pars.hl.mismatch_yesterday .= 0.
compound_pars.periodic_demand = periodic_demand # t -> zeros(N) #periodic_demand
compound_pars.residual_demand = residual_demand #t -> zeros(N) #residual_demand
compound_pars.graph = graph
coupfact= 6.
compound_pars.coupling = coupfact .* diagm(0=>ones(ne(graph)))
begin
factor = 0#0.01*rand(compound_pars.D * compound_pars.N)#0.001#0.00001
ic = factor .* ones(compound_pars.D * compound_pars.N)
tspan = (0., num_days * l_day)
ode_tl1 = ODEProblem(network_dynamics.ACtoymodel!, ic, tspan, compound_pars,
callback=CallbackSet(PeriodicCallback(network_dynamics.HourlyUpdate(), l_hour),
PeriodicCallback(network_dynamics.DailyUpdate_X, l_day)))
end
sol1 = solve(ode_tl1, Rodas4())
#######################################################################
# PLOTTING #
######################################################################
using Plots
# phi = [p[1:N] for p in sol1.u]
# phi_lin = [p[1:N] for p in sol.u]
# L = laplacian_matrix(graph)
# deltaphi = [L * phi[i] for i in 1:length(sol1.t)]
# deltaphi_lin = [L * phi_lin[i] for i in 1:length(sol.t)]
# maxdphi = [maximum(L * phi[i]) for i in 1:length(sol1.t)]
# maxdphi_lin = [maximum(L * phi_lin[i]) for i in 1:length(sol.t)]
#
# plot(sol1.t, [p[1] for p in phi])
# plot(sol1.t, maxdphi)
# plot!(sol1.t, sin.(maxdphi))
#
# plot(sol.t, maxdphi_lin)
# plot!(sol.t, sin.(maxdphi_lin))
#
# phi_dev = zeros(Int(sol1.t[end]))
# for i=1:Int(sol1.t[end])
# phi_dev[i] = maximum((sol(i)[phase_filter] .- sol1(i)[phase_filter]) ./ sol1(i)[phase_filter])
# end
# plot(phi_dev)
#
hourly_energy = zeros(24*num_days+1,N)
for i=1:24*num_days+1
for j = 1:N
hourly_energy[i,j] = sol1((i-1)*3600)[energy_filter[j]]
end
end
plot(hourly_energy)
ILC_power = zeros(num_days+2,24,N)
for j = 1:N
ILC_power[2,:,j] = Q*(zeros(24,1) + kappa*hourly_energy[1:24,j])
end
norm_energy_d = zeros(num_days,N)
for j = 1:N
norm_energy_d[1,j] = norm(hourly_energy[1:24,j])
end
for i=2:num_days
for j = 1:N
ILC_power[i+1,:,j] = Q*(ILC_power[i,:,j] + kappa*hourly_energy[(i-1)*24+1:i*24,j])
norm_energy_d[i,j] = norm(hourly_energy[(i-1)*24+1:i*24,j])
end
end
#ILC_power_agg = maximum(mean(ILC_power.^2,dims=3),dims=2)
ILC_power_agg = [norm(mean(ILC_power,dims=3)[d,:]) for d in 1:num_days+2]
ILC_power_hourly_mean = vcat(mean(ILC_power,dims=3)[:,:,1]'...)
ILC_power_hourly_mean_node1 = vcat(ILC_power[:,:,1]'...)
ILC_power_hourly = [norm(reshape(ILC_power,(num_days+2)*24,N)[h,:]) for h in 1:24*(num_days+2)]
ILC_power_hourly_node1 = [norm(reshape(ILC_power,(num_days+2)*24,N)[h,1]) for h in 1:24*(num_days+2)]
dd = t->((periodic_demand(t) .+ residual_demand(t)))
load_amp = [first(maximum(dd(t))) for t in 1:3600*24:3600*24*num_days]
norm_hourly_energy = [norm(hourly_energy[h,:]) for h in 1:24*num_days]
using LaTeXStrings
# NODE WISE second-wisenode = 1
node = 1
p1 = plot()
ILC_power_hourly_mean_node = vcat(ILC_power[:,:,node]'...)
dd = t->((periodic_demand(t) .+ residual_demand(t)))
plot!(0:num_days*l_day, t -> dd(t)[1], alpha=0.6, label = latexstring("P^d_$node"),linewidth=3, linestyle=:dot)
plot!(1:3600:24*num_days*3600,hourly_energy[1:num_days*24,node]./3600, label=latexstring("y_$node^{c,h}"),linewidth=3) #, linestyle=:dash)
plot!(1:3600:num_days*24*3600, ILC_power_hourly_mean_node[1:num_days*24], label=latexstring("\$u_$node^{ILC}\$"), xticks = (0:3600*24:num_days*24*3600, string.(0:num_days)), ytickfontsize=14,
xtickfontsize=14,
legendfontsize=10, linewidth=3, yaxis=("normed power",font(14)),legend=false, lc =:black, margin=5Plots.mm)
ylims!(-0.7,1.5)
title!(L"j = 1")
savefig("$dir/plots/demand_seconds_Y$(coupfact)_node_$(node)_hetero.png")
node = 2
p2 = plot()
ILC_power_hourly_mean_node = vcat(ILC_power[:,:,node]'...)
dd = t->((periodic_demand(t) .+ residual_demand(t)))
plot!(0:num_days*l_day, t -> dd(t)[2], alpha=0.6, label = latexstring("P^d_$node"),linewidth=3, linestyle=:dot)
plot!(1:3600:24*num_days*3600,hourly_energy[1:num_days*24,node]./3600, label=latexstring("y_$node^{c,h}"),linewidth=3)#, linestyle=:dash)
plot!(1:3600:num_days*24*3600, ILC_power_hourly_mean_node[1:num_days*24], label=latexstring("\$u_$node^{ILC}\$"), xticks = (0:3600*24:num_days*24*3600, string.(0:num_days)), ytickfontsize=14,
xtickfontsize=14, yticks=false, #xaxis=("days [c]",font(14)), yaxis=("normed power",font(14))
legendfontsize=10, linewidth=3,legend=false, lc =:black, margin=5Plots.mm)
ylims!(-0.7,1.5)
title!(L"j = 2")
savefig("$dir/plots/demand_seconds_Y$(coupfact)_node_$(node)_hetero.png")
node = 3
p3 = plot()
ILC_power_hourly_mean_node = vcat(ILC_power[:,:,node]'...)
dd = t->((periodic_demand(t) .+ residual_demand(t)))
plot!(0:num_days*l_day, t -> dd(t)[3], alpha=0.6, label = latexstring("P^d_$node"),linewidth=3, linestyle=:dot)
plot!(1:3600:24*num_days*3600,hourly_energy[1:num_days*24,node]./3600, label=latexstring("y_$node^{c,h}"),linewidth=3)#, linestyle=:dash)
plot!(1:3600:num_days*24*3600, ILC_power_hourly_mean_node[1:num_days*24], label=latexstring("\$u_$node^{ILC}\$"), xticks = (0:3600*24:num_days*24*3600, string.(0:num_days)), ytickfontsize=14,
xtickfontsize=18,
legendfontsize=10, linewidth=3,xaxis=("days [c]",font(14)),yaxis=("normed power",font(14)),legend=false, lc =:black, margin=5Plots.mm)
ylims!(-0.7,1.5)
title!(L"j = 3")
savefig("$dir/plots/demand_seconds_Y$(coupfact)_node_$(node)_hetero.png")
node = 4
p4 = plot()
ILC_power_hourly_mean_node = vcat(ILC_power[:,:,node]'...)
dd = t->((periodic_demand(t) .+ residual_demand(t)))
plot!(0:num_days*l_day, t -> dd(t)[4], alpha=0.6, label = latexstring("P^d_$node"),linewidth=3, linestyle=:dot)
plot!(1:3600:24*num_days*3600,hourly_energy[1:num_days*24,node]./3600, label=latexstring("y_$node^{c,h}"),linewidth=3)#, linestyle=:dash)
plot!(1:3600:num_days*24*3600, ILC_power_hourly_mean_node[1:num_days*24], label=latexstring("\$u_$node^{ILC}\$"), xticks = (0:3600*24:num_days*24*3600, string.(0:num_days)), ytickfontsize=14,
xtickfontsize=18,
legendfontsize=10, yticks=false, linewidth=3,xaxis=("days [c]",font(14)),legend=false, lc =:black, margin=5Plots.mm)
ylims!(-0.7,1.5)
title!(L"j = 4")
savefig("$dir/plots/demand_seconds_Y$(coupfact)_node_$(node)_hetero.png")
l = @layout [a b; c d]
plot_demand = plot(p1,p2,p3,p4,layout = l)
savefig(plot_demand, "$dir/plots/demand_seconds_Y$(coupfact)_all_nodes_hetero.png")
l2 = @layout [a b]
plot_demand2 = plot(p2,p4,layout = l2)
savefig(plot_demand2, "$dir/plots/demand_seconds_Y$(coupfact)_nodes2+4_hetero.png")
psum = plot()
ILC_power_hourly_mean_sum = vcat(ILC_power[:,:,1]'...) .+ vcat(ILC_power[:,:,2]'...) .+ vcat(ILC_power[:,:,3]'...) .+ vcat(ILC_power[:,:,4]'...)
dd = t->((periodic_demand(t) .+ residual_demand(t)))
plot!(0:num_days*l_day, t -> (dd(t)[1] .+ dd(t)[2] .+ dd(t)[3] .+ dd(t)[4]), alpha=0.6, label = latexstring("\$P^d_j\$"),linewidth=3, linestyle=:dot)
plot!(1:3600:24*num_days*3600,(hourly_energy[1:num_days*24,1] + hourly_energy[1:num_days*24,2] + hourly_energy[1:num_days*24,3] + hourly_energy[1:num_days*24,4])./3600, label=latexstring("y_j^{c,h}"),linewidth=3, linestyle=:dash)
plot!(1:3600:num_days*24*3600, ILC_power_hourly_mean_sum[1:num_days*24], label=latexstring("\$u_j^{ILC}\$"), xticks = (0:3600*24:num_days*24*3600, string.(0:num_days)), ytickfontsize=14,
xtickfontsize=18,legend=false,
legendfontsize=10, linewidth=3,xaxis=("days [c]",font(14)), yaxis=("normed power",font(14)),lc =:black, margin=5Plots.mm)
#ylims!(-0.7,1.5)
#title!("Initial convergence")
savefig(psum,"$dir/plots/demand_seconds_Y$(coupfact)_sum_hetero.png")
# hourly plotting
using LaTeXStrings
plot(0:(num_days)*24-1, ILC_power_hourly[1:num_days*24], label=L"$\max_h \Vert P_{ILC, k}\Vert$", xticks = (1:24:24*num_days, string.(1:num_days)))
plot!(0:24*num_days-1,norm_hourly_energy./3600, label=L"y_h")
plot!(0:24:24*num_days-1, load_amp, label = "demand amplitude")
xlabel!("hour h [h]")
ylabel!("normed quantities [a.u.]")
savefig("$dir/plots/yh_demand_ILC_new_hourly_hetero.png")
# daily plotting
plot(1:num_days, ILC_power_agg[1:num_days,1,1] ./ maximum(ILC_power_agg), label=L"$\max_h \Vert P_{ILC, k}\Vert$")
plot!(1:num_days, mean(norm_energy_d,dims=2) ./ maximum(norm_energy_d), label=L"norm(y_h)")
plot!(1:num_days, load_amp ./ maximum(load_amp), label = "demand amplitude")
xlabel!("day d [d]")
ylabel!("normed quantities [a.u.]")
savefig("$dir/plots/demand_daily_hetero.png")