Skip to content

Commit d06b0d8

Browse files
author
trigaut
committed
[UPD] Remake battery example parallel
1 parent 778bb6c commit d06b0d8

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

examples/battery_storage_parallel.jl

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,26 @@
77
# dynamic programming :
88
# We manage a network connecting an electrical demand,
99
# a renewable energy production unit, a battery and the global network.
10+
# We want to minimize the cost of consumed electricity until time T: \sum_{t=0}^T c_t * G_{t+1}.
1011
# We assume electrical demand d_t as well as cost of electricity c_t deterministic
1112
# We decide which quantity to store before knowing renewable energy production
12-
# If more energy comes, we store the excess up to the state of charge upper bound
13-
# The remaining excess is wasted
14-
# If not enough energy comes, we lower accordingly what was decided to discharge
15-
# to ensure state of charge lower bound constraint
16-
# We have to ensure supply/demand balance: the energy provided by the network
17-
# equals the demand minus the renewable production, plus the battery demand
18-
# or minus the battery production: G_t = max(d_t - xi_t, 0) + F_t(u_t)
19-
# We forbid electricity sale to the network
20-
# Min E [\sum_{t=1}^TF c_t G_t]
21-
# s.t. s_{t+1} = s_t - u_t + max(0,xi_t-d_t), if 0 <= s_t - u_t + max(0,xi_t-d_t) <= s_{max}
22-
# s_{t+1} = s_{max}, if s_t - u_t + max(0,xi_t-d_t) >= s_{min}
23-
# s_{t+1} = 0, if s_t - u_t + max(0,xi_t-d_t) < 0
24-
# F_t(u_t) = max(0,s_{max} - s_{t} - max(0,xi_t-d_t)), if s_{t+1} = s_{max}
25-
# F_t(u_t) = s_{min} - s_{t} - max(0,xi_t-d_t), if s_{t+1} = s_{min}
26-
# F_t(u_t) = u_t, otherwise
13+
# but we don't waste the eventual excess.
14+
# If more energy comes, we store the excess up to the state of charge upper bound.
15+
# The remaining excess is provided directly to the demand or wasted if still too important.
16+
# We have to ensure supply/demand balance: the energy provided by the network G_{t+1}
17+
# equals the demand d_t plus the battery effective demand or minus the battery effective
18+
# production U_{t+1} then minus the used renewable production xi_{t+1} - R_{t+1].
19+
# R_{t+1] is the renewable energy wasted/curtailed
20+
# U_{t+1} is a function of the decision variable: the amount of energy decided
21+
# to store or discharge u_t before the uncertainty realization.
22+
# We forbid electricity sale to the network: G_t+1 >=0 .
23+
# This inequality constraint can be translated on an equality constraint on R_{t+1}
24+
# Min E [\sum_{t=1}^T c_t G_{t+1}]
25+
# s.t. s_{t+1} = s_t + U_{t+1}
26+
# G_{t+1} = d_t + U_{t+1} - (W_{t+1} - R_{t+1})
27+
# U_{t+1} = | rho_c * min(S_{max} - S_t, min( u_t ,W_{t+1})), if u_t >=0
28+
# | (1/rho_dc) * max(u_t, -max( d_t - W_{t+1}, 0)), otherwise
29+
# R_{t+1} = max(W_{t+1} - d_t - U_{t+1}, 0)
2730
# s_0 given
2831
# s_{min} <= s_t <= s_{max}
2932
# u_min <= u_t <= u_max
@@ -55,6 +58,10 @@ println("library loaded")
5558
# initial stock
5659
const S0 = 0.5
5760

61+
# charge and discharge efficiency parameters
62+
const rho_c = 0.98
63+
const rho_dc = 0.97
64+
5865
# create law of noises
5966
proba = 1/N_XI*ones(N_XI) # uniform probabilities
6067
xi_support = collect(linspace(XI_MIN,XI_MAX,N_XI))
@@ -63,33 +70,43 @@ println("library loaded")
6370

6471
# Define dynamic of the stock:
6572
function dynamic(t, x, u, xi)
66-
return [min(STATE_MAX, max(STATE_MIN,x[1] + u[1] + max(0,xi[1], DEMAND[t])))]
73+
if u[1]>=0
74+
return [ x[1] + rho_c * min(max(u[1], xi[1]),STATE_MAX-x[1])]
75+
else
76+
return [ x[1] + 1/rho_dc * max(u[1],-max(0,DEMAND[t])) ]
77+
end
6778
end
6879

6980
# Define cost corresponding to each timestep:
7081
function cost_t(t, x, u, xi)
71-
x1 = dynamic(t, x, u, xi)[1]
72-
c = max(0, DEMAND[t] - xi[1])
73-
if x1 == STATE_MAX
74-
c += max(0, STATE_MAX - x[1] - max(0,xi[1], DEMAND[t]))
75-
elseif x1 == STATE_MIN
76-
c += STATE_MIN - x[1] - max(0,xi[1], DEMAND[t])
82+
U = 0
83+
if u[1]>=0
84+
U = rho_c * min(max(u[1], xi[1]),STATE_MAX-x[1])
7785
else
78-
c += u[1]
86+
U = 1/rho_dc * max(u[1],-max(0,DEMAND[t]))
7987
end
80-
return COSTS[t] * c
88+
return COSTS[t] * max(0, DEMAND[t] + U - xi[1])
89+
end
90+
91+
function constraint(t, x, u, xi)
92+
return( (x[1] <= s_bounds[1][2] )&(x[1] >= s_bounds[1][1]))
93+
end
94+
95+
function finalCostFunction(x)
96+
return(0)
8197
end
8298

8399
######## Setting up the SPmodel
84100
s_bounds = [(STATE_MIN, STATE_MAX)]
85101
u_bounds = [(CONTROL_MIN, CONTROL_MAX)]
86-
spmodel = StochDynamicProgramming.LinearDynamicLinearCostSPmodel(N_STAGES,
102+
spmodel = StochDynamicProgramming.StochDynProgModel(N_STAGES, s_bounds,
87103
u_bounds,
88104
[S0],
89105
cost_t,
106+
finalCostFunction,
90107
dynamic,
108+
constraint,
91109
xi_laws)
92-
StochDynamicProgramming.set_state_bounds(spmodel, s_bounds)
93110

94111
scenarios = StochDynamicProgramming.simulate_scenarios(xi_laws,1000)
95112

@@ -101,11 +118,7 @@ println("library loaded")
101118
controlSteps, infoStruct)
102119
end
103120

104-
Vs = []
105-
106-
@time for i in 1:1
107121
Vs = StochDynamicProgramming.solve_DP(spmodel,paramSDP, 1)
108-
end
109122

110123
lb_sdp = StochDynamicProgramming.get_bellman_value(spmodel,paramSDP,Vs)
111124
println("Value obtained by SDP: "*string(lb_sdp))

0 commit comments

Comments
 (0)