Ticket #7652: trac_7652-reviewer.patch
File trac_7652-reviewer.patch, 16.2 KB (added by , 11 years ago) |
---|
-
doc/en/constructions/linear_programming.rst
# HG changeset patch # User Minh Van Nguyen <nguyenminh2@gmail.com> # Date 1260580510 28800 # Node ID 63d8e7ebcef853e3ad181c252039d9cdf1a80145 # Parent 1245120e8f9755ada808b0618ad14abff5b8122f trac 7652: linear programming; reviewer patch diff -r 1245120e8f97 -r 63d8e7ebcef8 doc/en/constructions/linear_programming.rst
a b 1 1 Linear Programming 2 2 ================== 3 3 4 4 5 Basics 5 6 ------ 6 7 7 What is a Linear Program?8 """"""""""""""""""""""""" "8 What is a linear program? 9 """"""""""""""""""""""""" 9 10 10 A linear program consists of the following two pieces of information 11 A linear program consists of the following two pieces of information: 11 12 12 * A linear function, called the objective, which is 13 to be maximized or minimized (for example `2 x + y`) 14 * Linear constraints on the variables (for example, 15 `3 x + y \leq 2` and `2 x + 3 y \leq 8`) 13 * A linear function, called the objective function, which is 14 to be maximized or minimized, e.g. `2 x + y`. 16 15 17 The solver will then try to find a solution to the system of 18 constraints such that the objective function is optimized, and 19 return the values of the variables. 16 * Linear constraints on the variables, e.g. `3 x + y \leq 2` and 17 `2 x + 3 y \leq 8`. 20 18 19 A linear program solver would then try to find a solution to the 20 system of constraints such that the objective function is optimized, and 21 return specific values for the variables. 21 22 23 What is a mixed integer linear program? 24 """"""""""""""""""""""""""""""""""""""" 22 25 23 What is a Mixed Integer Linear Program ? 26 A mixed integer linear program is a linear program such that some 27 variables are forced to take integer values instead of real 28 values. This difference affects the time required to solve a 29 particular linear program. Indeed, solving a linear program can be 30 done in polynomial time while solving a general mixed integer linear 31 program is usually `NP`-complete, i.e. it can take exponential time, 32 according to a widely-held belief that `P \neq NP`. 33 34 Why is linear programming so useful? 35 """""""""""""""""""""""""""""""""""" 36 37 Linear programming is very useful in many optimization and 38 graph-theoretic problems because of its wide range of expression. 39 A linear program can be written to solve a problem whose 40 solution could be obtained within reasonable time using the wealth of 41 heuristics already contained in linear program solvers. It is often 42 difficult to theoretically determine the execution time of a linear 43 program, though it could produce very interesting results in 44 practice. 45 46 For more information, consult the Wikipedia page dedicated to 47 linear programming: http://en.wikipedia.org/wiki/Linear_programming 48 49 50 How can I solve a linear program using Sage? 51 -------------------------------------------- 52 53 Sage can solve linear programs or mixed integer linear programs 54 through the class ``MixedIntegerLinearProgram`` defined in 55 ``sage.numerical.mip``. To illustrate how it can be used, we will try 56 to solve the following problem: 57 58 .. MATH:: 59 60 \text{Maximize: } & 2 x_1 + x_2 \\ 61 \text{Such that: } & 3 x_1 + 4 x_2 \leq 2.5 \\ 62 & 0.5 \leq 1.2 x_1 + 0.5 x_2 \leq 4 63 64 First, we need to discuss ``MIPVariable`` and how to read the optimal 65 values when the solver has finished its job. 66 67 Variables in ``MixedIntegerLinearProgram`` 24 68 """""""""""""""""""""""""""""""""""""""""" 25 69 26 It is simply a Linear Program such that some variables are forced 27 to take integer values instead of real values. This difference becomes 28 very important when one learns that solving a Linear Program can be done 29 in polynomial time while solving a general Mixed Integer Linear Program 30 is `NP`-Complete (= there is no polynomial algorithm to solve it, 31 according to a widely-spread belief that `P\neq NP`) 70 A variable linked to an instance of ``MixedIntegerLinearProgram`` 71 behaves exactly as a dictionary would. It is declared as follows:: 32 72 33 Why is Linear Programming so useful ? 34 """""""""""""""""""""""""""""""""""""" 35 36 Linear Programming is very useful in many Optimization and 37 Graph-Theoretical problems because of its wide range of expression. 38 Most of the time, a natural Linear Program can be easily written 39 to solve a problem whose solution will be quickly computed thanks 40 to the wealth of heuristics already contained in Linear Program 41 Solvers. It is often hard to theoretically find out the execution 42 time of a Linear Program, though they give very interesting results 43 in practice. 44 45 For more information, you can consult the Wikipedia page dedicated to 46 Linear Programming : http://en.wikipedia.org/wiki/Linear_programming 47 48 How can I solve a linear program using Sage ? 49 --------------------------------------------- 50 51 Sage can solve Linear Programs or Mixed Integer Linear Programs through 52 the class ``MixedIntegerLinearProgram`` defined in ``sage.numerical.mip``. To illustrate how it can be 53 used, we will try to solve the following problem : 54 55 .. MATH:: 56 57 \mbox{Maximize : }&2 x_1 + x_2\\ 58 \mbox{Such that : }&3 x_1 + 4 x_2\leq 2.5\\ 59 &0.5\leq 1.2 x_1 + 0.5 x_2 \leq 4 60 61 First, we need a few informations about ``MIPVariable`` and 62 how to read the optimal values when the solver has finished its job. 63 64 Variables in ``MixedIntegerLinearProgram`` 65 """""""""""""""""""""""""""""""""""""""""""" 66 67 A variable linked to an instance of ``MixedIntegerLinearProgram`` behaves exactly as 68 a dictionary would. It is declared the following way :: 69 70 sage: p=MixedIntegerLinearProgram() 71 sage: variable=p.new_variable() 73 sage: p = MixedIntegerLinearProgram() 74 sage: variable = p.new_variable() 72 75 73 76 The variable ``variable`` can contain as many keys as you 74 would like, each of them being formally `unique`. For example 75 the following constraint (where `P` denotes the pressure, 76 and `T` the temperature) : 77 like, where each key must be unique. For example, the following 78 constraint (where `P` denotes pressure and `T` temperature) 77 79 78 80 .. MATH:: 79 2 T_{\mbox{Madrid}} + 3 T_{\mbox{London}} -80 P_{\mbox{Seattle}} + \mbox{flow}_{3,5} +81 8 \mbox{cost}_{(1,3)} + x_3 < 5\\82 81 83 ... can be expressed in Sage (quite naturally, I hope !) this way :: 82 2 T_{\text{Madrid}} + 3 T_{\text{London}} - P_{\text{Seattle}} + \text{flow}_{3, 5} + 8 \text{cost}_{(1, 3)} + x_3 < 5 84 83 85 sage: p=MixedIntegerLinearProgram() 86 sage: temperature=p.new_variable() 87 sage: pressure=p.new_variable() 88 sage: x=p.new_variable() 89 sage: cost=p.new_variable() 90 sage: flow=p.new_variable(dim=2) 91 sage: p.add_constraint(2*temperature["Madrid"]+3*temperature["London"]-pressure["Seattle"]+flow[3][5]+8*cost[(1,3)]+x[3],max=5) 84 can be written as:: 92 85 93 This example is just meant to show you the different possibilities 94 offered to you when you use the ``MixedIntegerLinearProgram`` class. You will not need 95 to declare so many variables in usual applications. 86 sage: p = MixedIntegerLinearProgram() 87 sage: temperature = p.new_variable() 88 sage: pressure = p.new_variable() 89 sage: x = p.new_variable() 90 sage: cost = p.new_variable() 91 sage: flow = p.new_variable(dim=2) 92 sage: p.add_constraint(2*temperature["Madrid"] + 3*temperature["London"] - pressure["Seattle"] + flow[3][5] + 8*cost[(1, 3)] + x[3], max=5) 96 93 97 Notice how the variable ``flow`` is defined : you can use any hashable 94 This example shows different possibilities for using the 95 ``MixedIntegerLinearProgram`` class. You would not need to declare so 96 many variables in some common applications of linear programming. 97 98 Notice how the variable ``flow`` is defined: you can use any hashable 98 99 object as a key for a ``MIPVariable``, but if you think you need 99 more than one dimension, you need to explicit ely say it when100 calling ``MixedIntegerLinearProgram.new_variable()`` 100 more than one dimension, you need to explicitly specify it when 101 calling ``MixedIntegerLinearProgram.new_variable()``. 101 102 102 For the user's convenience, however, there is a default variable103 attached to a Linear Program : indeed, the previous implementation104 means that each "variable" actually represents a property of 105 a set of objects (these objects are strings in the case of ``temperature`` 106 or a pair in the case of ``cost``). In some cases, though it is useful to 107 define an absolute variable which will not be indexed on anything. This can 108 be done through the following notation::103 For the user's convenience, there is a default variable 104 attached to a linear program. The above code listing means that each 105 ``variable`` actually represents a property of a set of objects 106 (these objects are strings in the case of ``temperature`` or a pair in 107 the case of ``cost``). In some cases, it is useful to define an 108 absolute variable which will not be indexed on anything. This can be 109 done as follows:: 109 110 110 111 sage: p = MixedIntegerLinearProgram() 111 112 sage: B = p.new_variable() 112 sage: p.set_objective( p["first unique variable"] + B[2] + p[-3])113 sage: p.set_objective(p["first unique variable"] + B[2] + p[-3]) 113 114 114 115 In this case, two of these "unique" variables are defined through 115 116 ``p["first unique variable"]`` and ``p[-3]``. 116 117 Let us solve this system !118 """"""""""""""""""""""""""119 117 120 Now that we know what are variables, 121 we are only several lines away from solving our system :: 118 Let's solve this system 119 """"""""""""""""""""""" 122 120 123 sage: # First, we define our MixedIntegerLinearProgram object, setting maximization=True 124 sage: p=MixedIntegerLinearProgram( maximization = True ) 125 sage: x=p.new_variable() 121 Now that we know what variables are, we are only several steps away 122 from solving our system:: 123 124 sage: # First, we define our MixedIntegerLinearProgram object, 125 sage: # setting maximization=True. 126 sage: p = MixedIntegerLinearProgram(maximization=True) 127 sage: x = p.new_variable() 126 128 sage: # Definition of the objective function 127 sage: p.set_objective( 2*x[1]+x[2])129 sage: p.set_objective(2*x[1] + x[2]) 128 130 sage: # Next, the two constraints 129 sage: p.add_constraint( 3*x[1]+4*x[2], max=2.5)130 sage: p.add_constraint( 1.5*x[1]+0.5*x[2], max=4,min=0.5)131 sage: p.solve() # optional - requires Glpk or COIN-OR/CBC131 sage: p.add_constraint(3*x[1] + 4*x[2], max=2.5) 132 sage: p.add_constraint(1.5*x[1]+0.5*x[2], max=4, min=0.5) 133 sage: p.solve() # optional - requires GLPK or COIN-OR/CBC 132 134 1.6666666666666667 133 sage: x_sol =p.get_values(x)134 sage: print x_sol 135 sage: x_sol = p.get_values(x) # optional - requires GLPK or COIN-OR/CBC 136 sage: print x_sol # optional - requires GLPK or COIN-OR/CBC 135 137 {1: 0.83333333333333337, 2: 0.0} 136 138 139 The value returned by ``MixedIntegerLinearProgram.solve()`` is the 140 optimal value of the objective function. To read the values taken by 141 the variables, one needs to call the method 142 ``MixedIntegerLinearProgram.get_values()`` which can return multiple 143 values at the same time if needed (type 144 ``MixedIntegerLinearProgram.get_values?`` for more information 145 on this function). 137 146 138 The value returned by ``MixedIntegerLinearProgram.solve()`` is the optimal value of139 the objective function. To read the values taken by the variables140 one needs to call the method ``MixedIntegerLinearProgram.get_values`` which can return141 multiple values at the same time if needed (type142 ``sage: MixedIntegerLinearProgram.get_values?`` for more information on this function)143 147 144 148 Some famous examples 145 -------------------- -------149 -------------------- 146 150 147 Vertex Cover in a graph148 """"""""""""""""""""""" "151 Vertex cover in a graph 152 """"""""""""""""""""""" 149 153 150 In the Vertex Cover problem, we are given a graph `G` and we want to find 151 a subset `S` of its vertices of minimal cardinality such that each edge 152 `e` is incident to at least one vertex of `S`. In order to achieve it, we 153 define a binary variable `b_v` for each vertex `v`. 154 Let `G = (V, E)` be a graph with vertex set `V` and edge set `E`. In 155 the vertex cover problem, we are given `G` and we want to find 156 a subset `S \subseteq V` of minimal cardinality such that each 157 edge `e` is incident to at least one vertex in `S`. In order to 158 achieve this, we define a binary variable `b_v` for each vertex 159 `v`. The vertex cover problem can be expressed as the following linear 160 program: 154 161 155 .. MATH:: 162 .. MATH:: 156 163 157 \ mbox{Maximize : }& \sum_{v\in G.vertices()} b_v\\158 \ mbox{Such that : }&\forall (u,v)\in G.edges(), b_u + b_v \geq 1\\159 &\forall v,b_v \mbox{ is a binary variable}164 \text{Maximize: } & \sum_{v \in V} b_v \\ 165 \text{Such that: } & \forall (u, v) \in E, b_u + b_v \geq 1 \\ 166 & \forall v, b_v \text{ is a binary variable} 160 167 161 In the linear program, the syntax is exactly the same 168 In the linear program, the syntax is exactly the same:: 162 169 163 sage: g =graphs.PetersenGraph()164 sage: p =MixedIntegerLinearProgram(maximization=False)165 sage: b =p.new_variable()166 sage: for (u,v)in g.edges(labels=None):167 ... p.add_constraint(b[u]+b[v],min=1)170 sage: g = graphs.PetersenGraph() 171 sage: p = MixedIntegerLinearProgram(maximization=False) 172 sage: b = p.new_variable() 173 sage: for u, v in g.edges(labels=None): 174 ... p.add_constraint(b[u] + b[v], min=1) 168 175 sage: p.set_binary(b) 169 170 And you but have to type ``p.solve()`` to see the result !171 176 172 Maximum matching in a Graph 173 """""""""""""""""""""""""""" 177 And you need to type ``p.solve()`` to see the result. 174 178 175 In the maximum matching problem, we are given a graph `G`, and we are 176 looking for a set of edges `M` of maximum cardinality such 177 that no two edges from `M` are adjacent : 179 Maximum matching in a graph 180 """"""""""""""""""""""""""" 178 181 179 .. MATH:: 182 In the maximum matching problem, we are given a graph `G = (V, E)` 183 and we want a set of edges `M \subseteq E` of maximum cardinality such 184 that no two edges from `M` are adjacent: 180 185 181 \mbox{Maximize : }& \sum_{e\in G.edges()} b_e\\ 182 \mbox{Such that : }&\forall v\in G.vertices(), \sum_{(v,w)\in G}b_{uv} \leq 1\\ 183 &\forall e\in G.edges(),b_e \mbox{ is a binary variable} 186 .. MATH:: 184 187 185 Here is how this is solved through Sage on a Petersen Graph :: 188 \text{Maximize: } & \sum_{e \in E} b_e \\ 189 \text{Such that: } & \forall v \in V, \sum_{(v,u) \in E} b_{vu} \leq 1 \\ 190 & \forall e \in E, b_e \text{ is a binary variable} 186 191 187 sage: g=graphs.PetersenGraph() 188 sage: p=MixedIntegerLinearProgram() 189 sage: b=p.new_variable(dim=2) 192 Here, we use Sage to solve the maximum matching problem for the case 193 of the Petersen graph:: 194 195 sage: g = graphs.PetersenGraph() 196 sage: p = MixedIntegerLinearProgram() 197 sage: b = p.new_variable(dim=2) 190 198 sage: for u in g.vertices(): 191 ... p.add_constraint(sum([b[u][v] for v in g.neighbors(u)]),max=1)192 sage: for (u,v)in g.edges(labels=None):193 ... p.add_constraint(b[u][v]+b[v][u],min=1,max=1)199 ... p.add_constraint(sum([b[u][v] for v in g.neighbors(u)]), max=1) 200 sage: for u, v in g.edges(labels=None): 201 ... p.add_constraint(b[u][v] + b[v][u], min=1, max=1) 194 202 195 And the next step is ``p.solve()`` !203 And the next step is ``p.solve()``. 196 204 197 205 198 206 Solvers 199 207 ------- 200 208 201 Sage solves linear programs by calling specific libraries. T wo are available202 fo r the moment:209 Sage solves linear programs by calling specific libraries. The 210 following libraries are currently supported as optional packages: 203 211 204 * `GLPK <http://www.gnu.org/software/glpk/>`_ : A Linear Program solver 205 from `GNU <http://www.gnu.org/>`_ 206 * `CBC <http://www.coin-or.org/projects/Cbc.xml>`_ : Mixed 207 Integer Linear Program solver from 208 `COIN-OR <http://www.coin-or.org/>`_ 212 * `GLPK <http://www.gnu.org/software/glpk/>`_: A linear program solver 213 from `GNU <http://www.gnu.org/>`_ 209 214 210 To install them if they are not available on your installation of Sage, type :: 215 * `CBC <http://www.coin-or.org/projects/Cbc.xml>`_: Mixed integer 216 linear program solver from `COIN-OR <http://www.coin-or.org/>`_ 211 217 212 sage: # To install GLPK 213 sage: install_package('glpk') # not tested 214 sage: # To install Coin-OR Branch and Cut (CBC) 215 sage: install_package('cbc') # not tested 218 Each of these packages can be installed as follows:: 219 220 sage: # To install GLPK 221 sage: install_package("glpk") # not tested 222 sage: # To install COIN-OR Branch and Cut (CBC) 223 sage: install_package("cbc") # not tested