Python Mathematical Modeling PuLP Library Linear Programming Introduction Example Detailed Explanation
- 2021-12-09 09:33:53
- OfStack
1. What is linear programming
Linear programming (Linear programming), which solves the extremum problem of linear objective function under linear equality or inequality constraints, is often used to solve resource allocation, production scheduling and mixed problems. For example:
max fx = 2*x1 + 3*x2 - 5*x3
s.t. x1 + 3*x2 + x3 <= 12
2*x1 - 5*x2 + x3 >= 10
x1 + x2 + x3 = 7
x1, x2, x3 >=0
Modeling and solving linear programming problems usually follow the following steps:
(1) Defining the problem, determining the decision variables, objective functions and constraints;
(2) Model construction, which establishes mathematical equations from problem description and transforms them into standard mathematical models;
(3) Solving the model, using the optimization algorithm of the standard model to solve the model and get the optimization result;
2. Solving linear programming by PuLP library
PuLP is an open source third-party toolkit, which can solve linear programming, integer programming and mixed integer programming problems.
The following is an example to explain the steps of PuLP in solving linear programming problems:
-(0) Import PuLP library functions
import pulp
-(1) Defining a programming problem
MyProbLP = pulp.LpProblem("LPProbDemo1", sense=pulp.LpMaximize)
pulp. LpProblem is the constructor that defines the problem.
"LPProbDemo1" is the user-defined question name (used to output information).
Parameter sense is used to specify the minimum/maximum problem. Optional parameter values: LpMinimize, LpMaximize.
-(2) Defining decision variables
x1 = pulp.LpVariable('x1', lowBound=0, upBound=7, cat='Continuous')
x2 = pulp.LpVariable('x2', lowBound=0, upBound=7, cat='Continuous')
x3 = pulp.LpVariable('x3', lowBound=0, upBound=7, cat='Continuous')
pulp. LpVariable is a function that defines decision variables.
'x1' is a user-defined variable name.
Parameters lowBound and upBound are used to set the lower bound and upper bound of decision variables. You can not define a lower bound/upper bound, and the default lower bound/upper bound is negative infinity/positive infinity. In this example, the value intervals of x1, x2 and x3 are [0, 7].
Parameter cat is used to set the variable type, and optional parameter values: 'Continuous' represents continuous variable (default value), 'Integer' represents discrete variable (for integer programming problem), and 'Binary' represents 0/1 variable (for 0/1 programming problem).
-(3) Add objective function
MyProbLP += 2*x1 + 3*x2 - 5*x3 # Set the target function
Add the target function in the format of "Problem Name += Target Function Formula".
-(4) Adding constraints
MyProbLP += (2*x1 - 5*x2 + x3 >= 10) # Inequality constraint
MyProbLP += (x1 + 3*x2 + x3 <= 12) # Inequality constraint
MyProbLP += (x1 + x2 + x3 == 7) # Equality constraint
Add constraints in the format of "Problem Name += Constraint Expression".
Constraints can be equality constraints or inequality constraints, and inequality constraints can be less than or equal to or greater than or equal to, using the keywords "
>
= ","
<
= "and" = = ".
-(5) Solution
MyProbLP.solve()
print("Status:", pulp.LpStatus[MyProbLP.status]) # Output solution status
for v in MyProbLP.variables():
print(v.name, "=", v.varValue) # Output the optimal value of each variable
print("F(x) = ", pulp.value(MyProbLP.objective)) # Output the objective function value of the optimal solution
solve () is the solution function. PuLP uses the CBC solver to solve the optimization problem by default, and other optimizers can be called to solve it, such as GLPK, COIN, CLP/CBC, CPLEX, and GUROBI, but they need to be installed separately.
3. Python program and running results
The complete program code is as follows:
import pulp
MyProbLP = pulp.LpProblem("LPProbDemo1", sense=pulp.LpMaximize)
x1 = pulp.LpVariable('x1', lowBound=0, upBound=7, cat='Continuous')
x2 = pulp.LpVariable('x2', lowBound=0, upBound=7, cat='Continuous')
x3 = pulp.LpVariable('x3', lowBound=0, upBound=7, cat='Continuous')
MyProbLP += 2*x1 + 3*x2 - 5*x3 # Set the target function
MyProbLP += (2*x1 - 5*x2 + x3 >= 10) # Inequality constraint
MyProbLP += (x1 + 3*x2 + x3 <= 12) # Inequality constraint
MyProbLP += (x1 + x2 + x3 == 7) # Equality constraint
MyProbLP.solve()
print("Status:", pulp.LpStatus[MyProbLP.status]) # Output solution status
for v in MyProbLP.variables():
print(v.name, "=", v.varValue) # Output the optimal value of each variable
print("F(x) = ", pulp.value(MyProbLP.objective)) # Output the objective function value of the optimal solution
#= Attention Youcans Share the original series https://blog.csdn.net/youcans =
The program runs as follows:
Welcome to the CBC MILP Solver
Version: 2.9.0
Build Date: Feb 12 2015
Status: Optimal
x1 = 6.4285714
x2 = 0.57142857
x3 = 0.0
F(x) = 14.57142851
The above is the Python mathematical modeling PuLP library Linear Programming Getting Started example details, more about mathematical modeling PuLP library Linear Programming Getting Started information please pay attention to other related articles on this site!