|
| 1 | +# Copyright 2015, Vincent Leclere, Francois Pacaud and Henri Gerard |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | +############################################################################# |
| 6 | +# Test impact of risk solving a stock problem : |
| 7 | +# Min F [\sum_{t=1}^TF c_t u_t] |
| 8 | +# s.t. s_{t+1} = s_t + u_t - xi_t, s_0 given |
| 9 | +# 0 <= s_t <= 1 |
| 10 | +# u_min <= u_t <= u_max |
| 11 | +# u_t choosen knowing xi_1 .. xi_t |
| 12 | +############################################################################# |
| 13 | + |
| 14 | +using StochDynamicProgramming, Clp |
| 15 | +println("library loaded") |
| 16 | + |
| 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 |
| 22 | + |
| 23 | +######## Optimization parameters ######## |
| 24 | +# choose the LP solver used. |
| 25 | +SOLVER = ClpSolver() # require "using Clp" |
| 26 | +#const SOLVER = CplexSolver(CPX_PARAM_SIMDISPLAY=0) # require "using CPLEX" |
| 27 | + |
| 28 | +# convergence test |
| 29 | +MAX_ITER = 10 # number of iterations of SDDP |
| 30 | + |
| 31 | +######## Stochastic Model Parameters ######## |
| 32 | +N_STAGES = 6 # number of stages of the SP problem |
| 33 | +COSTS = [sin(3*t)-1 for t in 1:N_STAGES-1] |
| 34 | +#const COSTS = rand(N_STAGES) # randomly generating deterministic costs |
| 35 | + |
| 36 | +CONTROL_MAX = 0.5 # bounds on the control |
| 37 | +CONTROL_MIN = 0 |
| 38 | + |
| 39 | +XI_MAX = 0.3 # bounds on the noise |
| 40 | +XI_MIN = 0 |
| 41 | +N_XI = 10 # discretization of the noise |
| 42 | + |
| 43 | +S0 = 0.5 # initial stock |
| 44 | + |
| 45 | +# create law of noises |
| 46 | +proba = 1/N_XI*ones(N_XI) # uniform probabilities |
| 47 | +xi_support = collect(linspace(XI_MIN,XI_MAX,N_XI)) |
| 48 | +xi_law = NoiseLaw(xi_support, proba) |
| 49 | +xi_laws = NoiseLaw[xi_law for t in 1:N_STAGES-1] |
| 50 | + |
| 51 | +# Define dynamic of the stock: |
| 52 | +function dynamic(t, x, u, xi) |
| 53 | + return [x[1] + u[1] - xi[1]] |
| 54 | +end |
| 55 | + |
| 56 | +# Define cost corresponding to each timestep: |
| 57 | +function cost_t(t, x, u, w) |
| 58 | + return COSTS[t] * u[1] |
| 59 | +end |
| 60 | + |
| 61 | +######## Setting up the SPmodel |
| 62 | +s_bounds = [(0, 1)] # bounds on the state |
| 63 | +u_bounds = [(CONTROL_MIN, CONTROL_MAX)] # bounds on controls |
| 64 | + |
| 65 | +println("Initializing functions to compare execution time") |
| 66 | +spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, riskMeasure = Expectation()) |
| 67 | +set_state_bounds(spmodel, s_bounds) # adding the bounds to the model |
| 68 | +# 10 forward pass, stop at MAX_ITER |
| 69 | +paramSDDP = SDDPparameters(SOLVER, |
| 70 | + passnumber=10, |
| 71 | + max_iterations=MAX_ITER) |
| 72 | +sddp = solve_SDDP(spmodel, paramSDDP, 0) # display information every 2 iterations |
| 73 | +lb_sddp = StochDynamicProgramming.get_lower_bound(spmodel, paramSDDP, sddp.bellmanfunctions) |
| 74 | + |
| 75 | +######### Solving the problem via SDDP with Expectation |
| 76 | +if run_Expectation |
| 77 | + tic() |
| 78 | + spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, riskMeasure = Expectation()) |
| 79 | + set_state_bounds(spmodel, s_bounds) # adding the bounds to the model |
| 80 | + println("Expectation's model set up") |
| 81 | + println("Starting resolution with Expectation") |
| 82 | + # 10 forward pass, stop at MAX_ITER |
| 83 | + paramSDDP = SDDPparameters(SOLVER, |
| 84 | + passnumber=10, |
| 85 | + max_iterations=MAX_ITER) |
| 86 | + sddp = solve_SDDP(spmodel, paramSDDP, 2) # display information every 2 iterations |
| 87 | + lb_sddp = StochDynamicProgramming.get_lower_bound(spmodel, paramSDDP, sddp.bellmanfunctions) |
| 88 | + println("Lower bound obtained by SDDP: "*string(round(lb_sddp,4))) |
| 89 | + toc(); println(); |
| 90 | +end |
| 91 | + |
| 92 | +######### Solving the problem via SDDP with AVaR |
| 93 | +if run_AVaR |
| 94 | + tic() |
| 95 | + spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, riskMeasure = AVaR((N_XI-1)/N_XI)) |
| 96 | + set_state_bounds(spmodel, s_bounds) # adding the bounds to the model |
| 97 | + println("AVaR's model set up") |
| 98 | + println("Starting resolution with AVaR") |
| 99 | + # 10 forward pass, stop at MAX_ITER |
| 100 | + paramSDDP = SDDPparameters(SOLVER, |
| 101 | + passnumber=10, |
| 102 | + max_iterations=MAX_ITER) |
| 103 | + sddp = solve_SDDP(spmodel, paramSDDP, 2) # display information every 2 iterations |
| 104 | + lb_sddp = StochDynamicProgramming.get_lower_bound(spmodel, paramSDDP, sddp.bellmanfunctions) |
| 105 | + println("Lower bound obtained by SDDP: "*string(round(lb_sddp,4))) |
| 106 | + toc(); println(); |
| 107 | +end |
| 108 | + |
| 109 | +######### Solving the problem via SDDP with Worst Case |
| 110 | +if run_WorstCase |
| 111 | + tic() |
| 112 | + spmodel = LinearSPModel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws, riskMeasure = WorstCase()) |
| 113 | + set_state_bounds(spmodel, s_bounds) # adding the bounds to the model |
| 114 | + println("Worst Case's model set up") |
| 115 | + println("Starting resolution with Worst Case") |
| 116 | + # 10 forward pass, stop at MAX_ITER |
| 117 | + paramSDDP = SDDPparameters(SOLVER, |
| 118 | + passnumber=10, |
| 119 | + max_iterations=MAX_ITER) |
| 120 | + sddp = solve_SDDP(spmodel, paramSDDP, 2) # display information every 2 iterations |
| 121 | + lb_sddp = StochDynamicProgramming.get_lower_bound(spmodel, paramSDDP, sddp.bellmanfunctions) |
| 122 | + println("Lower bound obtained by SDDP: "*string(round(lb_sddp,4))) |
| 123 | + toc(); println(); |
| 124 | +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 |
0 commit comments