Python integer programming - 0-1 type programming

        Integer programming of type 0 −1 is a special case of integer programming whose variables x_{j}only take the value 0 or 1. at this time x_{j}called

0-1 variable, or binary variable. x_{j}The condition that only takes the value 0 or 1 is subject to the following constraints:

0\leq x_{j}\leq 1

        The replacement of integers is consistent with the constraint form of general integer programming. In practical problems, if 0 −1 variables are introduced, the linear programming problems that need to be discussed separately in various situations can be unified in one problem. We first introduce the practical problem of introducing 0 −1 variables.


1. Mathematical modeling process

Selection of Investment Sites - Mutually Exclusive Programs

Example 4 A company intends to establish sales offices in the east, west and south districts of the city. There are 7 positions (points) A_{i}(i=1,2,...,7)to choose from in the proposal. Regulation:

in the Eastside. Choose at most two of the  A_{1},A_{2},A_{3} three points;

in the West End. At least one of the A_{4},A_{5} two points is selected;

In the South Zone, A_{6},A_{7} at least one of the two points is selected.

If the A_{i}point is , the equipment investment is estimated to be b_{i}RMB, and the annual profit is estimated to be c_{i}RMB , but the total investment cannot exceed BRMB. ask

Which points should be chosen to maximize annual profit?

When solving the problem, first introduce the 0 −1 variable x_{i}(i=1,2,...,7) so that

So the problem can be written as:

\begin{aligned} &\operatorname{Max} \quad z=\sum_{i=1}^{7} c_{i} x_{i} \\ &\left\{\begin{array}{l} \sum_{i=1}^{7} b_{i} x_{i} \leq B \\ x_{1}+x_{2}+x_{3} \leq 2 \\ x_{4}+x_{5} \geq 1 \\ x_{6}+x_{7} \geq 1, \quad x_{i}=0 \text { or } 1 \end{array}\right. \end{aligned}

2. Mutually exclusive constraints

There are two mutually exclusive constraints

5 x_{1}+4 x_{2} \leq 24 \text { or } 7 x_{1}+3 x_{2} \leq 45

Introducing a 0 −1 variable y , the above constraints can be rewritten as:

\left\{\begin{array}{l} 5 x_{1}+4 x_{2} \leq 24+y M \\ 7 x_{1}+3 x_{2} \leq 45+(1-y) M \\ y=0 \text { or } 1 \end{array}\right.

where M is a sufficiently large number. Restrictions

x_{1}=0 \text { or } 500 \leq x_{1} \leq 800

can be rewritten as

\left\{\begin{array}{l} 500 y \leq x_{1} \leq 800 y \\ y=0 \text { or } 1 \end{array}\right.

If there are m mutually exclusive constraints:

a_{i 1} x_{1}+\cdots+a_{i n} x_{n} \leq b_{i} \quad i=1,2, \cdots, m

In order to ensure mthat only one of the constraints works, we introduce m0 −1 variables y_{i}(i=1,2,...,m)and a sufficiently large constant M, and the following set m+1of constraints

\begin{aligned} &a_{i 1} x_{1}+\cdots+a_{i n} x_{n} \leq b_{i}+y_{i} M \quad i=1,2, \cdots, m \\ &y_{1}+\cdots+y_{m}=m-1 \end{aligned}

 meet the above requirements. This is because, due to (2), y_{i} only one of m can take a value of 0. If , is y_{i}^{*}=0substituted into (1), only i=i^{*}the constraints will work, and other expressions are redundant.

3. Fixed Cost Problem

        Fixed cost (fixed cost) problems cannot be described by general linear programming, but can be solved by changing to mixed integer programming, see the example below.

        Example 5 In order to produce a certain product, a factory has several different production methods to choose from. For example, the selected production method has high investment (purchasing equipment with a high degree of automation), and due to the large output, it is allocated to each product. Variable costs are reduced; conversely, if the investment in the selected production method is low, the variable costs allocated to each product may increase in the future. So it must be considered comprehensively. There are three ways to choose from, so:

  • x_{j}Indicates the output when the jth method is adopted;
  • c_{j}represents the variable cost of each product when using the jth way;
  • k_{j}Indicates the fixed cost when using the jth way.

In order to illustrate the characteristics of cost, other constraints are not considered for the time being. The total cost of using the various production methods is:

P_{j}=\left\{\begin{array}{ll} k_{j}+c_{j} x_{j}, & \text { } x_{j}>0 \\ 0, & \text { } x_{j}=0 \end{array} \quad j=1,2,3 .\right.

When composing the objective function, in order to discuss it in one problem uniformly, 0 −1 variables are now introduced y_{j}, let

 So the objective function

 \min z=\left(k_{1} y_{1}+c_{1} x_{1}\right)+\left(k_{2} y_{2}+c_{2} x_{2}\right)+\left(k_{3} y_{3}+c_{3} x_{3}\right)

(3) This requirement can be expressed as the following three linear constraints:

y_{j} \varepsilon \leq x_{j} \leq y_{j} M, \quad j=1,2,3

 where \varepsilonis a sufficiently small positive number and Mis a sufficiently large positive number. Mustx_{j} be 1 when > 0; only meaningful when = 0 is 0.y_{j}x_{j}y_{j}

Take a chestnut:

 \begin{aligned} &\operatorname{Max} \quad z=3 x_{1}-2 x_{2}+5 x_{3} \\ &\left\{\begin{array}{l} x_{1}+2 x_{2}-x_{3} \leq 2 \\ x_{1}+4 x_{2}+x_{3} \leq 4 \\ x_{1}+x_{2} \leq 3 \\ 4 x_{2}+x_{3} \leq 6 \\ x_{1}, x_{2}, x_{3}=0 \text { or } 1 \end{array}\right. \end{aligned}

4. Programming implementation

Implemented in Python

import pulp
InvestLP = pulp.LpProblem("0 −1型整数规划问题", sense=pulp.LpMaximize)  # 定义问题,求最大值
x1= pulp.LpVariable('x1', cat='Binary')  # 定义 x1
x2= pulp.LpVariable('x2', cat='Binary')  # 定义 x2
x3= pulp.LpVariable('x3', cat='Binary')  # 定义 x3
InvestLP += (3*x1 - 2*x2 + 5*x3 )  # 设置目标函数 f(x)
InvestLP += (x1 + 2*x2 - x3  <= 2)  # 不等式约束
InvestLP += (x1 + 4*x2 + x3 <= 4)
InvestLP += (x1 +x2 <= 3)
InvestLP += (4*x2 +x3 <= 6)
InvestLP.solve()
print(InvestLP.name)  # 输出求解状态
print("求解状态:", pulp.LpStatus[InvestLP.status])  # 输出求解状态
for v in InvestLP.variables():
    print(v.name, "=", v.varValue)  # 输出每个变量的最优值
print("目标函数值 =", pulp.value(InvestLP.objective))  # 输出最优解的目标函数值

Output result:

0 −1型整数规划问题
求解状态: Optimal
x1 = 1.0
x2 = 0.0
x3 = 1.0
目标函数值 = 8.0

Guess you like

Origin blog.csdn.net/qq_21402983/article/details/126443083