|
| 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 | +# Compare different ways of solving a stock problem : |
| 7 | +# Min E [\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 | +push!(LOAD_PATH, "../src") |
| 15 | +using StochDynamicProgramming, JuMP, Clp, Distributions |
| 16 | + |
| 17 | +######## Optimization parameters ######## |
| 18 | +# choose the LP solver used. |
| 19 | +const SOLVER = ClpSolver() |
| 20 | +# const SOLVER = CplexSolver(CPX_PARAM_SIMDISPLAY=0) |
| 21 | +# const SOLVER = GurobiSolver() |
| 22 | + |
| 23 | +# convergence test |
| 24 | +const EPSILON = 1e-3 # |
| 25 | +const MAX_ITER = 100 # maximum iteration of SDDP |
| 26 | + |
| 27 | +######## Stochastic Model Parameters ######## |
| 28 | +const N_STAGES = 5 |
| 29 | +const COSTS = rand(N_STAGES) |
| 30 | + |
| 31 | +const CONTROL_MAX = 0.5 |
| 32 | +const CONTROL_MIN = 0 |
| 33 | + |
| 34 | +const XI_MAX = 0.3 |
| 35 | +const XI_MIN = 0 |
| 36 | +const N_XI = 10 |
| 37 | + |
| 38 | +const S0 = 0.5 |
| 39 | + |
| 40 | +# create law of noises |
| 41 | +proba = 1/N_XI*ones(N_XI) # uniform probabilities |
| 42 | +xi_support = collect(linspace(XI_MIN,XI_MAX,N_XI)) |
| 43 | +xi_law = NoiseLaw(xi_support, proba) |
| 44 | +xi_laws = Vector{NoiseLaw}(N_STAGES) |
| 45 | +for t=1:N_STAGES-1 |
| 46 | + xi_laws[t] = xi_law |
| 47 | +end |
| 48 | + |
| 49 | +# Define dynamic of the stock: |
| 50 | +function dynamic(t, x, u, xi) |
| 51 | + return [x[1] + u[1] - xi[1]] |
| 52 | +end |
| 53 | + |
| 54 | +# Define cost corresponding to each timestep: |
| 55 | +function cost_t(t, x, u, w) |
| 56 | + return COSTS[t] * u[1] |
| 57 | +end |
| 58 | + |
| 59 | + |
| 60 | +######## Setting up the SPmodel |
| 61 | +s_bounds = [(0, 1)] |
| 62 | +u_bounds = [(CONTROL_MIN, CONTROL_MAX)] |
| 63 | +spmodel = LinearDynamicLinearCostSPmodel(N_STAGES, |
| 64 | + u_bounds, |
| 65 | + [S0], |
| 66 | + cost_t, |
| 67 | + dynamic, |
| 68 | + xi_laws) |
| 69 | + |
| 70 | +set_state_bounds(spmodel, s_bounds) |
| 71 | +paramSDDP = SDDPparameters(SOLVER, 10, 0, MAX_ITER) # 10 forward path, stop at MAX_ITER |
| 72 | + |
| 73 | +######### Solving the problem via SDDP |
| 74 | +#V, pbs = solve_SDDP(spmodel, paramSDDP, 10) # display information every 10 iterations |
| 75 | +#lb = StochDynamicProgramming.get_lower_bound(spmodel, params, V) |
| 76 | +#println("Lower bound obtained by SDDP: "*string(lb)) |
| 77 | + |
| 78 | +######### Solving the problem via Dynamic Programming |
| 79 | + |
| 80 | +stateSteps = [0.1] |
| 81 | +controlSteps = [0.1] |
| 82 | +infoStruct = "HD" # noise at time t is known before taking the decision at time t |
| 83 | +monteCarloSize = 1000 |
| 84 | +paramSDP = SDPparameters(spmodel, stateSteps, controlSteps, monteCarloSize, infoStruct) |
| 85 | +V = sdp_optimize(spmodel,paramSDP) |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +######### Comparing the solution |
| 90 | +#scenarios = generate_scenarios(xi_laws,1000) |
| 91 | +#costs, stocks = forward_simulations(spmodel, params, V, pbs, scenarios) |
| 92 | + |
0 commit comments