Skip to content

Commit c9bae21

Browse files
committed
[DEV] Add polyhedral risk measure
1 parent d5e04a9 commit c9bae21

File tree

4 files changed

+77
-24
lines changed

4 files changed

+77
-24
lines changed

examples/risk-example.jl

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
using StochDynamicProgramming, Clp
1515
println("library loaded")
1616

17-
run_expectation = true # false if you don't want to test expectation
18-
run_AVaR = true # false if you don't want to test AVaR
19-
run_WorstCase = true # false if you don't want to test WorstCase
17+
run_Expectation = true # false if you don't want to test Expectation
18+
run_AVaR = true # false if you don't want to test AVaR
19+
run_WorstCase = true # false if you don't want to test WorstCase
20+
run_ConvexCombi = true # false if you don't want to test Convex Combination
21+
run_Polyhedral = true # false if you don't want to test Polyhedral Risk Measure
2022

2123
######## Optimization parameters ########
2224
# choose the LP solver used.
@@ -71,7 +73,7 @@ sddp = solve_SDDP(spmodel, paramSDDP, 0) # display information every 2 iteration
7173
lb_sddp = StochDynamicProgramming.get_lower_bound(spmodel, paramSDDP, sddp.bellmanfunctions)
7274

7375
######### Solving the problem via SDDP with Expectation
74-
if run_expectation
76+
if run_Expectation
7577
tic()
7678
spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, riskMeasure = Expectation())
7779
set_state_bounds(spmodel, s_bounds) # adding the bounds to the model
@@ -90,7 +92,7 @@ end
9092
######### Solving the problem via SDDP with AVaR
9193
if run_AVaR
9294
tic()
93-
spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, riskMeasure = AVaR(0.5))
95+
spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, riskMeasure = AVaR((N_XI-1)/N_XI))
9496
set_state_bounds(spmodel, s_bounds) # adding the bounds to the model
9597
println("AVaR's model set up")
9698
println("Starting resolution with AVaR")
@@ -120,3 +122,41 @@ if run_WorstCase
120122
println("Lower bound obtained by SDDP: "*string(round(lb_sddp,4)))
121123
toc(); println();
122124
end
125+
126+
if run_ConvexCombi
127+
tic()
128+
spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, riskMeasure = ConvexCombi((N_XI-1)/N_XI,0.5))
129+
set_state_bounds(spmodel, s_bounds) # adding the bounds to the model
130+
println("Convex Combination's model set up")
131+
println("Starting resolution with Convex Combination")
132+
# 10 forward pass, stop at MAX_ITER
133+
paramSDDP = SDDPparameters(SOLVER,
134+
passnumber=10,
135+
max_iterations=MAX_ITER)
136+
sddp = solve_SDDP(spmodel, paramSDDP, 2) # display information every 2 iterations
137+
lb_sddp = StochDynamicProgramming.get_lower_bound(spmodel, paramSDDP, sddp.bellmanfunctions)
138+
println("Lower bound obtained by SDDP: "*string(round(lb_sddp,4)))
139+
toc(); println();
140+
end
141+
142+
if run_Polyhedral
143+
beta = (N_XI-1)/N_XI
144+
prob = 1/N_XI*ones(N_XI)
145+
polyset = repmat(1/beta*prob',N_XI)
146+
for i = 1:N_XI
147+
polyset[i,i] = (beta*N_XI-N_XI+1)/(N_XI*beta)
148+
end
149+
tic()
150+
spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, riskMeasure = PolyhedralRisk(polyset))
151+
set_state_bounds(spmodel, s_bounds) # adding the bounds to the model
152+
println("Polyhedral's model set up")
153+
println("Starting resolution with Polyhedral")
154+
# 10 forward pass, stop at MAX_ITER
155+
paramSDDP = SDDPparameters(SOLVER,
156+
passnumber=10,
157+
max_iterations=MAX_ITER)
158+
sddp = solve_SDDP(spmodel, paramSDDP, 2) # display information every 2 iterations
159+
lb_sddp = StochDynamicProgramming.get_lower_bound(spmodel, paramSDDP, sddp.bellmanfunctions)
160+
println("Lower bound obtained by SDDP: "*string(round(lb_sddp,4)))
161+
toc(); println();
162+
end

src/StochDynamicProgramming.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export solve_SDDP,
2626
sampling, get_control, get_bellman_value,
2727
benchmark_parameters, SDDPInterface,
2828
argsup_proba_risk,
29-
RiskMeasure, AVaR, Expectation, WorstCase, ConvexCombi
29+
RiskMeasure, AVaR, Expectation, WorstCase, ConvexCombi, PolyhedralRisk
3030

3131
include("noises.jl")
3232
include("objects.jl")

test/changeprob.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ EPSILON = 0.0001
1515
@test isa(ConvexCombi(1,0), RiskMeasure)
1616
@test isa(ConvexCombi(1,1), RiskMeasure)
1717
@test isa(ConvexCombi(0.5,0.5), RiskMeasure)
18+
@test isa(PolyhedralRisk(1/100*ones(1,100)), RiskMeasure)
1819
end
1920

2021
# Test limit cases
@@ -81,3 +82,23 @@ end
8182

8283
@test abs(probaAVaR'*X - getobjectivevalue(m)) <= EPSILON
8384
end
85+
86+
# The convex set of probabilty distributions of AVaR_{beta} is defined by
87+
# {Q | dQ/dP <= 1/beta}
88+
# We check equality between polyhedral formulation and AVaR formulation
89+
@testset "Equality AVaR Polyhedral" begin
90+
n = 10
91+
X = rand(-10:10,10)
92+
beta = (n-1)/n+rand()*(1-(n-1)/n)
93+
prob = 1/n*ones(n)
94+
95+
polyset = repmat(1/beta*prob',n)
96+
for i = 1:n
97+
polyset[i,i] = (beta*n-n+1)/(n*beta)
98+
end
99+
100+
probaAVaR = argsup_proba_risk(prob, AVaR(beta), X)
101+
probaPolyhedral = argsup_proba_risk(prob, PolyhedralRisk(polyset), X)
102+
103+
@test sum(abs.(probaAVaR-probaPolyhedral) .<= EPSILON*ones(n)) == n
104+
end

test/sddprisk.jl

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,26 @@ using StochDynamicProgramming, JuMP, Clp, CutPruners
5151

5252
V = nothing
5353
model = StochDynamicProgramming.LinearSPModel(n_stages, u_bounds,
54-
x0, cost, dynamic, laws)
55-
model.beta = 0.0
56-
println(model.beta)
54+
x0, cost, dynamic, laws, riskMeasure = WorstCase())
55+
56+
5757
set_state_bounds(model, x_bounds)
5858
# Test error if bounds are not well specified:
59-
59+
6060

6161
# Generate scenarios for forward simulations:
6262
noise_scenarios = simulate_scenarios(model.noises,param.forwardPassNumber)
6363

6464
sddp_costs = 0
6565

66-
66+
6767
# Compute bellman functions with SDDP:
6868
sddp = solve_SDDP(model, param, 0)
69-
69+
7070

7171
V = sddp.bellmanfunctions
7272
# Test if the first subgradient has the same dimension as state:
73-
73+
7474

7575
# Test upper bounds estimation with Monte-Carlo:
7676
n_simulations = 100
@@ -79,29 +79,21 @@ using StochDynamicProgramming, JuMP, Clp, CutPruners
7979
V,
8080
sddp.solverinterface,
8181
n_simulations)[1]
82-
82+
8383

8484
pbs = sddp.solverinterface
8585
sddp_costs, stocks = forward_simulations(model, param, pbs,
8686
noise_scenarios)
8787
# Test error if scenarios are not given in the right shape:
88-
88+
8989

9090
# Test computation of optimal control:
9191
aleas = noise_scenarios[1, 1, :]
9292
opt = StochDynamicProgramming.get_control(model, param,
9393
sddp.solverinterface,
9494
1, model.initialState, aleas)
95-
95+
9696

9797
# Test display:
9898
param.compute_ub = 0
9999
sddp = solve_SDDP(model, param, V, 2)
100-
101-
102-
103-
104-
105-
106-
107-

0 commit comments

Comments
 (0)