Integer programming Python

integer programming

Pure integer programming: all decision variables are restricted to integers

Mixed integer programming: only some variables are restricted to integers

0-1 integer programming: decision variables are limited to 0 or 1

1. Integer programming problems and solutions

insert image description here

import cvxpy as cp
from numpy import array
c=array([40,90])  #定义目标向量
a=array([[9,7],[-7,-20]])  #定义约束矩阵
b=array([56,-70])  #定义约束条件的右边向量
x=cp.Variable(2,integer=True)  #定义两个整数决策变量
obj=cp.Minimize(c*x)  #构造目标函数
cons=[a*x<=b, x>=0]    #构造约束条件
prob=cp.Problem(obj, cons)  #构建问题模型
prob.solve(solver='GLPK_MI',verbose =	True)  #求解问题
print("最优值为:",prob.value)
print("最优解为:\n",x.value)

2. Assignment issues

Many practical application problems can be summarized in this form: different tasks are assigned to several people to complete. Because the difficulty of the tasks and the quality of the personnel are different, there are differences in the efficiency of each person in completing different tasks. Therefore, it is necessary to consider who should be assigned to complete which tasks to maximize overall efficiency. This type of problem is often called an assignment problem.

2.1 Standard assignment model

insert image description here

insert image description here
insert image description here
A commercial company plans to open 5 new stores, and decides that 5 construction companies will undertake the construction respectively. It is known that the construction company ( ) quotes (10,000 yuan) for the construction cost of the new store ( ), see Table 6.1. In order to save costs, how should the commercial company distribute the construction tasks among the five construction companies so as to minimize the total construction cost?
insert image description here
insert image description here
insert image description here

import cvxpy as cp
import numpy as np
c=np.array([[4, 8, 7, 15, 12],
         	[7, 9, 17, 14, 10],
	        [6, 9, 12, 8, 7],
   	     	[6, 7, 14, 6, 10],
        	[6, 9, 12, 10, 6]])
x = cp.Variable((5,5),integer=True)
obj = cp.Minimize(cp.sum(cp.multiply(c,x)))
con= [0 <= x, x <= 1, cp.sum(x, axis=0, keepdims=True)==1,
             cp.sum(x, axis=1, keepdims=True)==1]
prob = cp.Problem(obj, con)
prob.solve(solver='GLPK_MI')
print("最优值为:",prob.value)
print("最优解为:\n",x.value)

insert image description here

2.2 Assignment issues with mismatched number of people or tasks

insert image description here

2.3 Assignment problem that one person can complete multiple tasks

In some assignment problems, situations may arise where a person is asked to perform several tasks. For such an assignment problem, the person can be regarded as the same several people to accept the assignment, and it is only necessary to make them all have the same efficiency in completing the same task.

2.4 An assignment problem that a certain task must not be completed by someone.

In some assignment problems, it may arise that a person cannot complete a task. For such an assignment problem, it is only necessary to take the corresponding efficiency value (cost type) to a sufficiently large number.

3. Packing problem

insert image description here
insert image description here
Solve this question B in the 1988 American College Student Mathematical Modeling Contest. All the packaging boxes in the question weigh a total of 89t, and the two flatbed trucks can only carry a total of 80t, so it is impossible to load them all. There must be evaluation standards for what types of boxes and how many are suitable for each vehicle. This standard is to abide by the constraints on weight, thickness, number of pieces, etc. stated in the question, and pack as much as possible. There are two understandings of loading as much as possible: one is to pack as much as possible in terms of volume, because the regulations are based on bread. The second is to pack as much weight as possible, that is, to make the total weight of the boxes on the two vehicles as large as possible. big. insert image description here
insert image description here
insert image description here
insert image description here
So we finally get the mathematical model
insert image description here

import cvxpy as cp
import numpy as np
L=np.array([48.7,52.0,61.3,72.0,48.7,52.0,64.0])	
w=np.array([2000,3000,1000,500,4000,2000,1000])
a=np.array([8,7,9,6,6,4,8])
x=cp.Variable((2,7), integer=True)
obj=cp.Maximize(cp.sum(x*L))
con=[cp.sum(x,axis=0,keepdims=True)<=a.reshape(1,7),
	 x*L<=1020, x*w<=40000, cp.sum(x[:,4:]*L[4:])<=302.7, x>=0]
prob = cp.Problem(obj, con)
prob.solve(solver='GLPK_MI',verbose =True)
print("最优值为:",prob.value)
print("最优解为:\n",x.value)

The optimal value is: 2039.4
The optimal solution is:
[[4. 1. 5. 3. 3. 2. 0.]
[4. 6. 4. 3. 0. 1. 0.]]

insert image description here

2. Nonlinear programming

1 Concept and theory of nonlinear programming

1.1. Nonlinear programming model

The general form of the nonlinear programming model is described as follows:
insert image description here
Written in vector representation:
insert image description here
If you encounter the situation of seeking the maximum value of the objective function or the constraint is greater than 0, it can be transformed into the general form by taking the inverse.

Definition 6.1: Strict global optimal solution and strict local optimal solution
insert image description here

It should be noted that the optimal solution of linear programming must be obtained at the boundary, especially at the vertex of the feasible region. But nonlinear programming does not have this feature. The optimal solution (if it exists) may be reached at any point in the feasible region. Generally, nonlinear programming algorithms can only provide local optimal solutions and cannot guarantee global optimal solutions.

2.2 Solution of unconstrained nonlinear programming

The unconstrained linear programming problem can be specifically expressed as:
insert image description here
this kind of problem can learn from the method of finding the extreme value of the binary function in advanced mathematics. First introduce the following theorem:
insert image description here
insert image description here
insert image description here
the common methods are: the fastest descending line method, Newton method and quasi-Newton method, which will not be introduced in detail here

2.3. Solution of constrained nonlinear programming

A more common processing idea: if possible, convert nonlinear problems into linear problems, and convert constrained problems into unconstrained problems.

(1) Solving the lagrange multiplier method for nonlinear programming with equality constraints

For the special case of nonlinear programming problems with only equality constraints,
insert image description here
insert image description here
the solution of the problem can be transformed into an unconstrained problem

(2) Penalty function method for solving constrained nonlinear programming
Penalty function method
Note: The penalty function method has poor calculation accuracy and is generally not used unless the algorithm is required to achieve real-time

2. Python solution to nonlinear programming

2.1 Solve with the minimize function of the scipy.optimize module
Solve nonlinear programming problems

insert image description here

from scipy.optimize import minimize
from numpy import ones
def obj(x):
	x1,x2,x3=x
	return (2+x1)/(1+x2)-3*x1+4*x3
LB=[0.1]*3; UB=[0.9]*3
bound=tuple(zip(LB, UB))   #生成决策向量界限的元组
res=minimize(obj,ones(3),bounds=bound) #第2个参数为初值
print(res.fun,'\n',res.success,'\n',res.x)  #输出最优值、求解状态、最优解

insert image description here

Therefore: the optimal solution is x1=x2=0.9, x3=0.1, and the optimal value of the objective function is -0.7737

Solve the following nonlinear programming problem

insert image description here

from scipy.optimize import minimize
import numpy as np
c1=np.array([1,1,3,4,2]); c2=np.array([-8,-2,-3,-1,-2])	
A=np.array([[1,1,1,1,1],[1,2,2,1,6],
	        [2,1,6,0,0],[0,0,1,1,5]])
b=np.array([400,800,200,200])
obj=lambda x: np.dot(-c1,x**2)+np.dot(-c2,x)
cons={'type':'ineq','fun':lambda x:b-A@x}
bd=[(0,99) for i in range(A.shape[1])]
res=minimize(obj,np.ones(5)*90,constraints=cons,bounds=bd)
print(res)  #输出解的信息

insert image description here

Therefore, the optimal solution is: x1=50.5,x2=99,x3=0,x4=99,x5=20.2, and the optimal value of the objective function is -51629.93

2. Use the cvxopt.solvers module to solve the quadratic programming model

If the objective function of nonlinear programming is a quadratic function of the decision vector x and the constraints are all linear, this kind of planning is called quadratic programming.

insert image description here
Firstly, the original plan is modeled into a standard form
insert image description here

#程序文件Pan6_6.py
import numpy as np
from cvxopt import matrix,solvers
n=3; P=matrix(0.,(n,n))
P[::n+1]=[3,2,1.7]; q=matrix([3,-8.2,-1.95])
A=matrix([[1.,0,1],[-1,2,0],[0,1,2]]).T
b=matrix([2.,2,3])
Aeq=matrix(1.,(1,n)); beq=matrix(3.)
s=solvers.qp(P,q,A,b,Aeq,beq)
print("最优解为:",s['x'])
print("最优值为:",s['primal objective'])

insert image description here

The optimal solution is found to be x1=0.8, x2=1.4, x3=0.8, and the optimal value of the objective function is -7.1760

Guess you like

Origin blog.csdn.net/abc1234564546/article/details/126263264