|
| 1 | +.. _quickstart: |
| 2 | + |
| 3 | +==================== |
| 4 | +Step-by-step example |
| 5 | +==================== |
| 6 | + |
| 7 | +This page gives a short introduction to the interface of this package. It explains the resolution with SDDP of a classical example: the management of a dam over one year with random inflow. |
| 8 | + |
| 9 | +Use case |
| 10 | +======== |
| 11 | +In the following, :math:`x_t` will denote the state and :math:`u_t` the control at time :math:`t`. |
| 12 | +We will consider a dam, whose dynamic is: |
| 13 | + |
| 14 | +.. math:: |
| 15 | + x_{t+1} = x_t - u_t + w_t |
| 16 | +
|
| 17 | +At time :math:`t`, we have a random inflow :math:`w_t` and we choose to turbine a quantity :math:`u_t` of water. |
| 18 | + |
| 19 | +The turbined water is used to produce electricity, which is being sold at a price :math:`c_t`. At time :math:`t` we gain: |
| 20 | + |
| 21 | +.. math:: |
| 22 | + C(x_t, u_t, w_t) = c_t \times u_t |
| 23 | +
|
| 24 | +We want to minimize the following criterion: |
| 25 | + |
| 26 | +.. math:: |
| 27 | + J = \underset{x, u}{\min} \sum_{t=0}^{T-1} C(x_t, u_t, w_t) |
| 28 | +
|
| 29 | +We will assume that both states and controls are bounded: |
| 30 | + |
| 31 | +.. math:: |
| 32 | + x_t \in [0, 100], \qquad u_t \in [0, 7] |
| 33 | +
|
| 34 | +
|
| 35 | +Problem definition in Julia |
| 36 | +=========================== |
| 37 | + |
| 38 | +We will consider 52 time steps as we want to find optimal value functions for one year:: |
| 39 | + |
| 40 | + N_STAGES = 52 |
| 41 | + |
| 42 | + |
| 43 | +and we consider the following initial position:: |
| 44 | + |
| 45 | + X0 = [50] |
| 46 | + |
| 47 | +Note that X0 is a vector. |
| 48 | + |
| 49 | +Dynamic |
| 50 | +^^^^^^^ |
| 51 | + |
| 52 | +We write the dynamic (which return a vector):: |
| 53 | + |
| 54 | + function dynamic(t, x, u, xi) |
| 55 | + return [x[1] + u[1] - xi[1]] |
| 56 | + end |
| 57 | + |
| 58 | + |
| 59 | +Cost |
| 60 | +^^^^ |
| 61 | + |
| 62 | +we store evolution of costs :math:`c_t` in an array `COSTS`, and we define the cost function (which return a float):: |
| 63 | + |
| 64 | + function cost_t(t, x, u, w) |
| 65 | + return COSTS[t] * u[1] |
| 66 | + end |
| 67 | + |
| 68 | +Noises |
| 69 | +^^^^^^ |
| 70 | + |
| 71 | +Noises are defined in an array of Noiselaw. This type defines a discrete probability. |
| 72 | + |
| 73 | + |
| 74 | +For instance, if we want to define a uniform probability with size :math:`N= 10`, such that: |
| 75 | + |
| 76 | +.. math:: |
| 77 | + \mathbb{P} \left(X_i = i \right) = \dfrac{1}{N} \qquad \forall i \in 1 .. N |
| 78 | +
|
| 79 | +we write:: |
| 80 | + |
| 81 | + N = 10 |
| 82 | + proba = 1/N*ones(N) # uniform probabilities |
| 83 | + xi_support = collect(linspace(1,N,N)) |
| 84 | + xi_law = NoiseLaw(xi_support, proba) |
| 85 | + |
| 86 | + |
| 87 | +Thus, we could define a different probability laws for each time :math:`t`. Here, we suppose that the probability is constant over time, so we could build the following vector:: |
| 88 | + |
| 89 | + xi_laws = NoiseLaw[xi_law for t in 1:N_STAGES-1] |
| 90 | + |
| 91 | + |
| 92 | +Bounds |
| 93 | +^^^^^^ |
| 94 | + |
| 95 | +We add bounds over the state and the control:: |
| 96 | + |
| 97 | + s_bounds = [(0, 100)] |
| 98 | + u_bounds = [(0, 7)] |
| 99 | + |
| 100 | + |
| 101 | +Problem definition |
| 102 | +^^^^^^^^^^^^^^^^^^ |
| 103 | + |
| 104 | +As our problem is purely linear, we instantiate:: |
| 105 | + |
| 106 | + spmodel = LinearDynamicLinearCostSPmodel(N_STAGES,u_bounds,X0,cost_t,dynamic,xi_laws) |
| 107 | + |
| 108 | + |
| 109 | +Solver |
| 110 | +^^^^^^ |
| 111 | +We define a SDDP solver for our problem. |
| 112 | + |
| 113 | +First, we need to use a LP solver:: |
| 114 | + |
| 115 | + using Clp |
| 116 | + SOLVER = ClpSolver() |
| 117 | + |
| 118 | +Clp is automatically installed during package installation. To install different solvers on your machine, refer to the JuMP_ documentation. |
| 119 | + |
| 120 | +Once the solver installed, we define SDDP algorithm parameters:: |
| 121 | + |
| 122 | + forwardpassnumber = 2 # number of forward pass |
| 123 | + sensibility = 0. # admissible gap between upper and lower bound |
| 124 | + max_iter = 20 # maximum number of iterations |
| 125 | + |
| 126 | + paramSDDP = SDDPparameters(SOLVER, forwardpassnumber, sensibility, max_iter) |
| 127 | + |
| 128 | + |
| 129 | +Now, we solve the problem by computing Bellman values:: |
| 130 | + |
| 131 | + V, pbs = solve_SDDP(spmodel, paramSDDP, 10) # display information every 10 iterations |
| 132 | + |
| 133 | +:code:`V` is an array storing the value functions, and :code:`pbs` a vector of JuMP.Model storing each value functions as a linear problem. |
| 134 | + |
| 135 | +We have an exact lower bound given by :code:`V` with the function:: |
| 136 | + |
| 137 | + lb_sddp = StochDynamicProgramming.get_lower_bound(spmodel, paramSDDP, V) |
| 138 | + |
| 139 | + |
| 140 | +Find optimal controls |
| 141 | +===================== |
| 142 | + |
| 143 | +Once Bellman functions are computed, we can control our system over assessments scenarios, without assuming knowledge of the future. |
| 144 | + |
| 145 | +We build 1000 scenarios according to the laws stored in :code:`xi_laws`:: |
| 146 | + |
| 147 | + scenarios = StochDynamicProgramming.simulate_scenarios(xi_laws,1000) |
| 148 | + |
| 149 | +We compute 1000 simulations of the system over these scenarios:: |
| 150 | + |
| 151 | + costsddp, stocks = forward_simulations(spmodel, paramSDDP, V, pbs, scenarios) |
| 152 | + |
| 153 | +:code:`costsddp` returns the costs for each scenario, and :code:`stocks` the evolution of each stock along time, for each scenario. |
| 154 | + |
| 155 | +.. _JuMP: http://jump.readthedocs.io/en/latest/installation.html#coin-or-clp-and-cbc |
| 156 | + |
0 commit comments