Skip to content

Commit 48cdb3d

Browse files
committed
[DEV] Take comments of pull request into account
1 parent 69e6b6e commit 48cdb3d

File tree

12 files changed

+115
-68
lines changed

12 files changed

+115
-68
lines changed

examples/risk-example.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ s_bounds = [(0, 1)] # bounds on the state
6161
u_bounds = [(CONTROL_MIN, CONTROL_MAX)] # bounds on controls
6262

6363
println("Initialzing functions to compare execution time")
64-
spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, Expectation())
64+
spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, riskMeasure = Expectation())
6565
set_state_bounds(spmodel, s_bounds) # adding the bounds to the model
6666
# 10 forward pass, stop at MAX_ITER
6767
paramSDDP = SDDPparameters(SOLVER,
@@ -73,7 +73,7 @@ lb_sddp = StochDynamicProgramming.get_lower_bound(spmodel, paramSDDP, sddp.bellm
7373
######### Solving the problem via SDDP with Expectation
7474
if run_expectation
7575
tic()
76-
spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, Expectation())
76+
spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, riskMeasure = Expectation())
7777
set_state_bounds(spmodel, s_bounds) # adding the bounds to the model
7878
println("Expectation's model set up")
7979
println("Starting resolution with Expectation")
@@ -107,7 +107,7 @@ end
107107
######### Solving the problem via SDDP with Worst Case
108108
if run_WorstCase
109109
tic()
110-
spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, WorstCase())
110+
spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, riskMeasure = WorstCase())
111111
set_state_bounds(spmodel, s_bounds) # adding the bounds to the model
112112
println("Worst Case's model set up")
113113
println("Starting resolution with Worst Case")

examples/stock-example.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ end
6161
######## Setting up the SPmodel
6262
s_bounds = [(0, 1)] # bounds on the state
6363
u_bounds = [(CONTROL_MIN, CONTROL_MAX)] # bounds on controls
64-
spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, Expectation())
64+
spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws)
6565
set_state_bounds(spmodel, s_bounds) # adding the bounds to the model
6666
println("Model set up")
6767

src/SDDPoptimize.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function solve_SDDP(model::SPModel, param::SDDPparameters, verbosity=0::Int64, v
4040
stopcrit::AbstractStoppingCriterion=IterLimit(param.max_iterations),
4141
prunalgo::AbstractCutPruningAlgo=CutPruners.AvgCutPruningAlgo(-1),
4242
regularization=nothing)
43+
4344
sddp = SDDPInterface(model, param,
4445
stopcrit,
4546
prunalgo,

src/StochDynamicProgramming.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ export solve_SDDP,
2525
StochDynProgModel, SDPparameters, solve_dp,
2626
sampling, get_control, get_bellman_value,
2727
benchmark_parameters, SDDPInterface,
28-
change_proba_risk,
29-
RiskMeasure, CVaR, Expectation, WorstCase, ConvexCombi
28+
argsup_proba_risk,
29+
RiskMeasure, AVaR, Expectation, WorstCase, ConvexCombi
3030

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

src/forwardBackwardIterations.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ function compute_cuts_hd!(model::SPModel, param::SDDPparameters,
345345
proba /= sum(proba)
346346

347347
#Modify the probability vector to compute the value of the risk measure
348-
proba = change_proba_risk(proba,model.riskMeasure,sortperm(costs,rev = true))
348+
proba = argsup_proba_risk(proba,model.riskMeasure,costs)
349349

350350
# Compute expectation of subgradient λ:
351351
subgradient = vec(sum(proba' .* subgradient_array, 2))

src/objects.jl

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ type Expectation <: RiskMeasure
1616
end
1717
end
1818

19-
type CVaR <: RiskMeasure
20-
# level of risk aversity (0 = Expectation, 1 = Worst Case):
19+
type AVaR <: RiskMeasure
20+
# If the random variable is a cost and beta = 0.05,
21+
# it returns the average of the five worst costs solving the problem
22+
# minimize alpha + 1/beta * E[max(X - alpha; 0)]
23+
# beta = 1 ==> AVaR = Expectation
24+
# beta = 0 ==> AVaR = WorstCase
2125
beta::Float64
22-
function CVaR(beta)
26+
function AVaR(beta)
2327
return new(beta)
2428
end
2529
end
@@ -31,15 +35,28 @@ type WorstCase <: RiskMeasure
3135
end
3236

3337
type ConvexCombi <: RiskMeasure
34-
# level of risk aversity (0 = Expectation, 1 = Worst Case):
38+
# Define a convex combination between Expectation and AVaR_{beta}
39+
# with form lambda*E + (1-lambda)*AVaR
40+
# lambda = 1 ==> Expectation
41+
# lambda = 0 ==> AVaR
3542
beta::Float64
36-
#Convex coefficient
3743
lambda::Float64
3844
function ConvexCombi(beta,lambda)
3945
return new(beta,lambda)
4046
end
4147
end
4248

49+
type PolyhedralRisk <: RiskMeasure
50+
# Define a convex polyhedral set P of probability distributions
51+
# by its extreme points p1, ..., pn
52+
# In the case of costs X, the problem solved is
53+
# maximize E_{pi}[X]
54+
# p1, ..., pn
55+
polyset::Array{Float64,2}
56+
function PolyhedralRisk(polyset)
57+
return new(polyset)
58+
end
59+
end
4360

4461
@compat abstract type SPModel end
4562

@@ -100,12 +117,12 @@ type LinearSPModel <: SPModel
100117
cost, # cost function
101118
dynamic, # dynamic
102119
aleas, # modelling of noises
103-
riskMeasure;
104120
Vfinal=nothing, # final cost
105121
eqconstr=nothing, # equality constraints
106122
ineqconstr=nothing, # inequality constraints
107123
info=:HD, # information structure
108124
control_cat=nothing, # category of controls
125+
riskMeasure = Expectation();
109126
)
110127

111128
# infer the problem's dimension

src/risk.jl

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,44 @@
77
#############################################################################
88

99
"""
10-
Update probability distribution to consider only worst case scenarios
10+
Risk measures consider here can be written (in case of a minimization problem)
11+
sup E_{p}[x]
12+
p in P
13+
where P is a convex set of probability distribution.
14+
The function returns the argsup allowing to compute the
15+
risk measure as a expectation
1116
1217
$(SIGNATURES)
1318
1419
# Description
15-
Keep only the tail of the probability distribution given an order
16-
and renormalized it
20+
Return the probability among the set of possible probabilities
21+
allowing to compute the risk
1722
1823
# Arguments
19-
* `prob::probabilitydistribution`:
20-
SDDP interface object
21-
* `beta::Float64`:
22-
real number
23-
* `perm::Array{float,1}`:
24-
array of permutations
24+
* `prob::Array{float,1}`:
25+
probability distribution
26+
* `riskMeasure::RiskMeasure`:
27+
risk Measure
28+
* `costs`:
29+
array of costs
2530
2631
# Returns
27-
* `aux::Array{float,1}`:
32+
* `proba::Array{float,1}`:
2833
a new probability distribution taking risk into account.
2934
3035
"""
31-
function change_proba_risk(prob, riskMeasure::RiskMeasure,perm)
32-
erro("'change_proba_risk' not defined for $(typedof(s))")
36+
function argsup_proba_risk(prob, riskMeasure::RiskMeasure,costs)
37+
error("'argsup_proba_risk' not defined for $(typedof(s))")
3338
end
3439

3540
"""
3641
$(TYPEDEF)
3742
38-
Update the probability distribution to calculate a Conditional Value at Risk of level beta.
43+
Return the probability distribution to compute a Average Value at Risk of level beta.
3944
"""
40-
function change_proba_risk(prob,riskMeasure::CVaR,perm)
41-
beta = 1-riskMeasure.beta
45+
function argsup_proba_risk(prob,riskMeasure::AVaR,costs)
46+
perm = sortperm(costs,rev = true)
47+
beta = riskMeasure.beta
4248
proba = zeros(length(prob))
4349
for i in 1:length(perm)
4450
proba[i] = prob[perm[i]]
@@ -61,31 +67,32 @@ end
6167
"""
6268
$(TYPEDEF)
6369
64-
Leave the probability distribution unchanged to calculate the expectation.
70+
Leave the probability distribution unchanged to compute the expectation.
6571
"""
66-
function change_proba_risk(prob,riskMeasure::Expectation,perm)
72+
function argsup_proba_risk(prob,riskMeasure::Expectation,costs)
6773
return prob
6874
end
6975

7076
"""
7177
$(TYPEDEF)
72-
78+
perm
7379
Return a dirac on the worst cost as a probability distribution.
7480
"""
75-
function change_proba_risk(prob,riskMeasure::WorstCase,perm)
81+
function argsup_proba_risk(prob,riskMeasure::WorstCase,costs)
7682
proba = zeros(length(prob))
77-
proba[perm[1]] = 1
83+
proba[indmax(prob)] = 1
7884
return proba
7985
end
8086

8187
"""
8288
$(TYPEDEF)
8389
84-
Update the probability distribution to calculate a convex combination
85-
between expactation and a Condiational Value at Risk of level beta.
90+
Return the probability distribution to compute a convex combination
91+
between expactation and an Average Value at Risk of level beta.
8692
"""
87-
function change_proba_risk(prob,riskMeasure::ConvexCombi,perm)
88-
beta = 1-riskMeasure.beta
93+
function argsup_proba_risk(prob,riskMeasure::ConvexCombi,costs)
94+
perm = sortperm(costs,rev = true)
95+
beta = riskMeasure.beta
8996
lambda = riskMeasure.lambda
9097
proba = zeros(length(prob))
9198
for i in 1:length(perm)
@@ -105,3 +112,15 @@ function change_proba_risk(prob,riskMeasure::ConvexCombi,perm)
105112
end
106113
return lambda*prob + (1-lambda)*aux
107114
end
115+
116+
"""
117+
$(TYPEDEF)
118+
119+
Return the worst extreme probability distribution
120+
defining the convex set P
121+
"""
122+
function argsup_proba_risk(prob,riskMeasure::PolyhedralRisk,costs)
123+
P = riskMeasure.polyset
124+
valuesup = P*costs
125+
return P[indmax(valuesup),:]
126+
end

src/stopcrit.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ Stops if the lower bound is stabilized
137137
total time of execution is greater than the time limit specified.
138138
For instance, `TimeLimit(100)` stops after 100s.
139139
"""
140-
type Stabilization <: AbstractStoppingCriterion
140+
type LBStabilization <: AbstractStoppingCriterion
141141
epsilon::Float64
142142
n_back::Int
143143
end
144144

145-
function stop(s::Stabilization, stats::AbstractSDDPStats, totalstats::AbstractSDDPStats)
145+
function stop(s::LBStabilization, stats::AbstractSDDPStats, totalstats::AbstractSDDPStats)
146146
totalstats.niterations > s.n_back && ((stats.lowerbound[end] - stats.lowerbound[end-s.n_back]) < epsilon)
147147
end

test/changeprob.jl

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,64 @@ using Base.Test
33

44
EPSILON = 0.0001
55

6+
# Check that there is no problem of definition
67
@testset "Risk measure definition" begin
78
@test isa(Expectation(), RiskMeasure)
89
@test isa(WorstCase(), RiskMeasure)
9-
for i in 0:10
10-
@test isa(CVaR(i/10), RiskMeasure)
11-
for j in 0:10
12-
@test isa(ConvexCombi(i/10, j/10), RiskMeasure)
13-
end
14-
end
10+
@test isa(AVaR(0), RiskMeasure)
11+
@test isa(AVaR(0.5), RiskMeasure)
12+
@test isa(AVaR(1), RiskMeasure)
13+
@test isa(ConvexCombi(0,0), RiskMeasure)
14+
@test isa(ConvexCombi(0,1), RiskMeasure)
15+
@test isa(ConvexCombi(1,0), RiskMeasure)
16+
@test isa(ConvexCombi(1,1), RiskMeasure)
17+
@test isa(ConvexCombi(0.5,0.5), RiskMeasure)
1518
end
1619

17-
20+
# Test limit cases
21+
# An AVaR with beta = 0 is a WorstCase
22+
# An AVaR with beta = 1 is an Expectation
23+
# A ConvexCombi with lambda = 0 is an AVaR
24+
# A ConvexCombi with lambda = 1 is an Expectation
1825
@testset "Equality formulations" begin
19-
@testset "Equality WorstCase CVaR(1)" begin
26+
@testset "Equality WorstCase AVaR(0)" begin
2027
n = 100
2128
prob = 1/n*ones(n)
22-
@test sum(abs.(change_proba_risk(prob,CVaR(1),1:n)-change_proba_risk(prob,WorstCase(),1:n)) .<= EPSILON*ones(n)) == n
29+
@test sum(abs.(argsup_proba_risk(prob,AVaR(0),1:n)-argsup_proba_risk(prob,WorstCase(),1:n)) .<= EPSILON*ones(n)) == n
2330
end
2431

25-
@testset "Equality Expectation CVaR(0)" begin
32+
@testset "Equality Expectation AVaR(1)" begin
2633
n = 100
2734
prob = 1/n*ones(n)
28-
@test sum(abs.(change_proba_risk(prob,CVaR(0),1:n)-change_proba_risk(prob,Expectation(),1:n)) .<= EPSILON*ones(n)) == n
35+
@test sum(abs.(argsup_proba_risk(prob,AVaR(0),1:n)-argsup_proba_risk(prob,Expectation(),1:n)) .<= EPSILON*ones(n)) == n
2936
end
3037

3138
@testset "Equality Expectation ConvexCombi(beta,1)" begin
3239
n = 100
3340
prob = 1/n*ones(n)
34-
@test sum(abs.(change_proba_risk(prob,Expectation(),1:n)-change_proba_risk(prob,ConvexCombi(rand(),1),1:n)) .<= EPSILON*ones(n)) == n
41+
@test sum(abs.(argsup_proba_risk(prob,Expectation(),1:n)-argsup_proba_risk(prob,ConvexCombi(rand(),1),1:n)) .<= EPSILON*ones(n)) == n
3542
end
3643

37-
@testset "Equality CVaR(beta) ConvexCombi(beta,0)" begin
44+
@testset "Equality AVaR(beta) ConvexCombi(beta,0)" begin
3845
n = 100
3946
beta = rand()
4047
prob = 1/n*ones(n)
41-
@test sum(abs.(change_proba_risk(prob,CVaR(beta),1:n)-change_proba_risk(prob,ConvexCombi(beta,0),1:n)) .<= EPSILON*ones(n)) == n
48+
@test sum(abs.(argsup_proba_risk(prob,AVaR(beta),1:n)-argsup_proba_risk(prob,ConvexCombi(beta,0),1:n)) .<= EPSILON*ones(n)) == n
4249
end
4350

44-
@testset "Right sense of CVaR" begin
45-
betamin = rand()/2
46-
betamax = rand()/2+0.5
51+
# Check that in the case of minimization, AVaR find the worst costs
52+
@testset "Right sense of AVaR" begin
53+
betamax = rand()/2
54+
betamin = rand()/2+0.5
4755
n = 100
4856
prob = 1/n*ones(n)
49-
@test change_proba_risk(prob, CVaR(betamax), 1:n)'*(n:-1:1) - change_proba_risk(prob, CVaR(betamin), 1:n)'*(n:-1:1) >= 0
57+
@test argsup_proba_risk(prob, AVaR(betamax), 1:n)'*(n:-1:1) - argsup_proba_risk(prob, AVaR(betamin), 1:n)'*(n:-1:1) >= 0
5058
end
5159
end
5260

53-
@testset "Equality CVaR linear program" begin
61+
# AVaR can be computed as a linear program
62+
# We check equality between the formulation of Rockafellar and Urysev
63+
@testset "Equality AVaR linear program" begin
5464
n = 100
5565
X = rand(-100:100,100)
5666
beta = rand()
@@ -63,11 +73,11 @@ end
6373

6474
@constraint(m, theta[1:n] .>= X[1:n] - alpha)
6575

66-
@objective(m, Min, alpha + 1/(1-beta)*sum(prob[i]*theta[i] for i in 1:n))
76+
@objective(m, Min, alpha + 1/(beta)*sum(prob[i]*theta[i] for i in 1:n))
6777

6878
status = solve(m)
6979

70-
probaCVaR = change_proba_risk(prob, CVaR(beta), sortperm(X, rev = true))
80+
probaAVaR = argsup_proba_risk(prob, AVaR(beta), sortperm(X, rev = true))
7181

72-
@test abs(probaCVaR'*X - getobjectivevalue(m)) <= EPSILON
82+
@test abs(probaAVaR'*X - getobjectivevalue(m)) <= EPSILON
7383
end

test/extensive_formulation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ using StochDynamicProgramming, Base.Test, Clp
3939
u_bounds = [(0., 7.), (0., Inf)]
4040

4141
model_ef = StochDynamicProgramming.LinearSPModel(n_stages, u_bounds,
42-
x0, cost, dynamic, laws, Expectation())
42+
x0, cost, dynamic, laws)
4343
x_bounds_ef = [(0, 100)]
4444
set_state_bounds(model_ef, x_bounds_ef)
4545

0 commit comments

Comments
 (0)