|
| 1 | +.. _quickstart_sdp: |
| 2 | + |
| 3 | +==================== |
| 4 | +SDP: Step-by-step example |
| 5 | +==================== |
| 6 | + |
| 7 | +This page gives a short introduction to the interface of this package. It explains the resolution with Stochastic Dynamic Programming 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 | +We have two options to contruct a model that can be solved by the SDP algorithm. |
| 105 | +We can instantiate a model that can be solved by SDDP as well:: |
| 106 | + |
| 107 | + spmodel = LinearDynamicLinearCostSPmodel(N_STAGES,u_bounds,X0,cost_t,dynamic,xi_laws) |
| 108 | + |
| 109 | + set_state_bounds(spmodel, s_bounds) |
| 110 | + |
| 111 | +Or we can instantiate a StochDynProgModel that can be solved only by SDP but we |
| 112 | +need to define the constraint function and the final cost function:: |
| 113 | + |
| 114 | + function constraints(t, x, u, xi) # return true when there is no constraints ecept state and control bounds |
| 115 | + return true |
| 116 | + end |
| 117 | + |
| 118 | + function final_cost_function(x) |
| 119 | + return 0 |
| 120 | + end |
| 121 | + |
| 122 | + spmodel = StochDynProgModel(N_STAGES, s_bounds, u_bounds, X0, cost_t, |
| 123 | + final_cost_function, dynamic, constraints, |
| 124 | + xi_laws) |
| 125 | + |
| 126 | + |
| 127 | +Solver |
| 128 | +^^^^^^ |
| 129 | + |
| 130 | +It remains to define SDP algorithm parameters:: |
| 131 | + |
| 132 | + stateSteps = [1] # discretization steps of the state space |
| 133 | + controlSteps = [0.1] # discretization steps of the control space |
| 134 | + infoStruct = "HD" # noise at time t is known before taking the decision at time t |
| 135 | + paramSDP = SDPparameters(spmodel, stateSteps, controlSteps, infoStruct) |
| 136 | + |
| 137 | + |
| 138 | +Now, we solve the problem by computing Bellman values:: |
| 139 | + |
| 140 | + Vs = solve_DP(spmodel,paramSDP, 1) |
| 141 | + |
| 142 | +:code:`V` is an array storing the value functions |
| 143 | + |
| 144 | +We have an exact lower bound given by :code:`V` with the function:: |
| 145 | + |
| 146 | + value_sdp = StochDynamicProgramming.get_bellman_value(spmodel,paramSDP,Vs) |
| 147 | + |
| 148 | + |
| 149 | +Find optimal controls |
| 150 | +===================== |
| 151 | + |
| 152 | +Once Bellman functions are computed, we can control our system over assessments scenarios, without assuming knowledge of the future. |
| 153 | + |
| 154 | +We build 1000 scenarios according to the laws stored in :code:`xi_laws`:: |
| 155 | + |
| 156 | + scenarios = StochDynamicProgramming.simulate_scenarios(xi_laws,1000) |
| 157 | + |
| 158 | +We compute 1000 simulations of the system over these scenarios:: |
| 159 | + |
| 160 | + costsdp, states, controls =sdp_forward_simulation(spmodel,paramSDP,scenarios,Vs) |
| 161 | + |
| 162 | +:code:`costsdp` returns the costs for each scenario, :code:`states` the simulation of each state variable along time, for each scenario, and |
| 163 | +:code:`controls` returns the optimal controls for each scenario |
| 164 | + |
0 commit comments