|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "In this tutorial, we show how to run the [FAST tutorial example](https://web.stanford.edu/~lcambier/fast/tuto.php) using this package.\n", |
| 8 | + "The big difference between this example and the quickstart example is that in this example we will model serial independence.\n", |
| 9 | + "There will be 5 stages and 2 scenarios per stages except for the first stage which has only one scenario.\n", |
| 10 | + "Each pair of scenario will have the same parent.\n", |
| 11 | + "\n", |
| 12 | + "We start by setting the constants:" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "code", |
| 17 | + "execution_count": 1, |
| 18 | + "metadata": {}, |
| 19 | + "outputs": [], |
| 20 | + "source": [ |
| 21 | + "const num_stages = 5\n", |
| 22 | + "const numScen = 2\n", |
| 23 | + "const C = 5\n", |
| 24 | + "const V = 8\n", |
| 25 | + "const d = 6\n", |
| 26 | + "const r = [2, 10];" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "markdown", |
| 31 | + "metadata": {}, |
| 32 | + "source": [ |
| 33 | + "We now create a matrix to store all the variables of all the models.\n", |
| 34 | + "This allows us to use the variables of other models from a given model.\n", |
| 35 | + "We also create an array of the first model of each stage to give play the role of parent for the models of the next stage." |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": 2, |
| 41 | + "metadata": { |
| 42 | + "collapsed": true |
| 43 | + }, |
| 44 | + "outputs": [], |
| 45 | + "source": [ |
| 46 | + "using StructJuMP\n", |
| 47 | + "x = Matrix{JuMP.Variable}(num_stages, numScen)\n", |
| 48 | + "y = Matrix{JuMP.Variable}(num_stages, numScen)\n", |
| 49 | + "p = Matrix{JuMP.Variable}(num_stages, numScen)\n", |
| 50 | + "models = Vector{JuMP.Model}(num_stages);" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "markdown", |
| 55 | + "metadata": {}, |
| 56 | + "source": [ |
| 57 | + "Now, we create all the models.\n", |
| 58 | + "Note that each model declares that its parent is the first model (i.e. the model `ξ == 1`) of the previous stage.\n", |
| 59 | + "Hence if it is not the first model, it also declares that it has the same children than the first model of its stage.\n", |
| 60 | + "This is how serial independence is modeled in [StructJuMP](https://github.com/StructJuMP/StructJuMP.jl)." |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": 3, |
| 66 | + "metadata": { |
| 67 | + "collapsed": true |
| 68 | + }, |
| 69 | + "outputs": [], |
| 70 | + "source": [ |
| 71 | + "for s in 1:num_stages\n", |
| 72 | + " for ξ in 1:(s == 1 ? 1 : numScen) # for the first stage there is only 1 scenario\n", |
| 73 | + " if s == 1\n", |
| 74 | + " model = StructuredModel(num_scenarios=numScen)\n", |
| 75 | + " else\n", |
| 76 | + " model = StructuredModel(parent=models[s-1], prob=1/2, same_children_as=(ξ == 1 ? nothing : models[s]), id=ξ, num_scenarios=(s == num_stages ? 0 : numScen))\n", |
| 77 | + " end\n", |
| 78 | + " x[s, ξ] = @variable(model, lowerbound=0, upperbound=V)\n", |
| 79 | + " y[s, ξ] = @variable(model, lowerbound=0)\n", |
| 80 | + " p[s, ξ] = @variable(model, lowerbound=0)\n", |
| 81 | + " if s > 1\n", |
| 82 | + " @constraint(model, x[s, ξ] <= x[s-1, 1] + r[ξ] - y[s, ξ])\n", |
| 83 | + " else\n", |
| 84 | + " @constraint(model, x[s, ξ] <= mean(r) - y[s, ξ])\n", |
| 85 | + " end\n", |
| 86 | + " @constraint(model, p[s, ξ] + y[s, ξ] >= d)\n", |
| 87 | + " @objective(model, Min, C * p[s, ξ])\n", |
| 88 | + " # models[s] contains the first model only\n", |
| 89 | + " if ξ == 1\n", |
| 90 | + " models[s] = model\n", |
| 91 | + " end\n", |
| 92 | + " end\n", |
| 93 | + "end" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "markdown", |
| 98 | + "metadata": {}, |
| 99 | + "source": [ |
| 100 | + "We first need to pick an LP solver, see [here](http://www.juliaopt.org/) for a list of the available choices." |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": 4, |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "using GLPKMathProgInterface\n", |
| 110 | + "solver = GLPKMathProgInterface.GLPKSolverLP();" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "markdown", |
| 115 | + "metadata": {}, |
| 116 | + "source": [ |
| 117 | + "We now create the lattice, note that the master problem is `models[1]`." |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "cell_type": "code", |
| 122 | + "execution_count": 5, |
| 123 | + "metadata": {}, |
| 124 | + "outputs": [], |
| 125 | + "source": [ |
| 126 | + "using CutPruners\n", |
| 127 | + "const pruner = AvgCutPruningAlgo(-1)\n", |
| 128 | + "using StructDualDynProg\n", |
| 129 | + "sp = stochasticprogram(models[1], num_stages, solver, pruner);" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "markdown", |
| 134 | + "metadata": {}, |
| 135 | + "source": [ |
| 136 | + "The SDDP algorithm can now be run on the lattice:" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "code", |
| 141 | + "execution_count": 6, |
| 142 | + "metadata": {}, |
| 143 | + "outputs": [ |
| 144 | + { |
| 145 | + "data": { |
| 146 | + "text/plain": [ |
| 147 | + "StructDualDynProg.SDDPSolution(:Optimal, 23.75, [0.0, 6.0, 0.0], Dict{Any,Any}(Pair{Any,Any}(:stats, | Total time | Number | Average time\n", |
| 148 | + " Solving problem | 0min 1s 523ms 41μs | 66 | 0min 0s 23ms 76μs\n", |
| 149 | + " Merging paths | 0min 0s 143ms 9μs | 6 | 0min 0s 23ms 834μs\n", |
| 150 | + "Adding feasibility cuts | 0min 0s 0ms 0μs | 0 | min s ms μs\n", |
| 151 | + "Adding optimality cuts | 0min 0s 77ms 615μs | 11 | 0min 0s 7ms 55μs\n", |
| 152 | + "Setting parent solution | 0min 0s 35ms 22μs | 64 | 0min 0s 0ms 547μs\n", |
| 153 | + " | 0min 1s 635ms 680μs |),Pair{Any,Any}(:nfcuts, 0),Pair{Any,Any}(:nocuts, 11),Pair{Any,Any}(:niter, 2)))" |
| 154 | + ] |
| 155 | + }, |
| 156 | + "execution_count": 6, |
| 157 | + "metadata": {}, |
| 158 | + "output_type": "execute_result" |
| 159 | + } |
| 160 | + ], |
| 161 | + "source": [ |
| 162 | + "sol = SDDP(sp, num_stages, K = 16, stopcrit = Pereira(2., 0.5) | IterLimit(10))" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "code", |
| 167 | + "execution_count": null, |
| 168 | + "metadata": { |
| 169 | + "collapsed": true |
| 170 | + }, |
| 171 | + "outputs": [], |
| 172 | + "source": [] |
| 173 | + } |
| 174 | + ], |
| 175 | + "metadata": { |
| 176 | + "kernelspec": { |
| 177 | + "display_name": "Julia 0.6.3", |
| 178 | + "language": "julia", |
| 179 | + "name": "julia-0.6" |
| 180 | + }, |
| 181 | + "language_info": { |
| 182 | + "file_extension": ".jl", |
| 183 | + "mimetype": "application/julia", |
| 184 | + "name": "julia", |
| 185 | + "version": "0.6.3" |
| 186 | + } |
| 187 | + }, |
| 188 | + "nbformat": 4, |
| 189 | + "nbformat_minor": 2 |
| 190 | +} |
0 commit comments