当前位置:网站首页>[mathematical modeling / mathematical programming model]
[mathematical modeling / mathematical programming model]
2022-07-24 20:09:00 【Zhizi】
Statement : This article is written with reference to the online course of mathematical modeling Qingfeng .
List of articles
Mathematical programming
What is mathematical programming ?
Research on mathematical programming : Under given conditions ( constraint condition ), How to follow a certain beam index ( Objective function ) To find a plan 、 The best plan for management .
namely : The problem of finding the extreme value of the objective function under certain constraints .The general form of mathematical programming
General form :min or max f(x) s.t Inequality constraints
Objective function :
m i n z = 6 x 1 + 3 x 2 + 4 x 3 min \ z = 6x_{1} + 3x_{2} + 4x_{3} min z=6x1+3x2+4x3
constraint condition :
{ x 1 + 2 x 2 − 3 x 3 ⩽ 80 x 1 + x 2 + x 3 = 120 x 1 ⩾ 30 0 ⩽ x 2 ⩽ 50 x 3 ⩾ 20 \left\{\begin{matrix} x_{1} + 2x_{2} - 3x_{3} \leqslant 80 \\ x_{1} + x_{2} + x_{3} = 120 \\ x_{1} \geqslant 30 \\ 0 \leqslant x_{2} \leqslant 50 \\ x_{3} \geqslant 20 \end{matrix}\right. ⎩⎨⎧x1+2x2−3x3⩽80x1+x2+x3=120x1⩾300⩽x2⩽50x3⩾20Classification of mathematical programming
Mathematical programming problems mainly include linear programming , Nonlinear programming and integer programming, etc .
Linear programming
When the objective function and constraints are linear expressions , Then the mathematical programming problem at this time is the linear programming problem .1947 year , American mathematician Danzig proposed an algorithm to solve mathematical programming problems – Simplex .
Objective function :
m i n z = 6 x 1 + 3 x 2 + 4 x 3 min \ z = 6x_{1} + 3x_{2} + 4x_{3} min z=6x1+3x2+4x3
constraint condition :
{ x 1 + 2 x 2 − 3 x 3 ⩽ 80 x 1 + x 2 + x 3 = 120 x 1 ⩾ 30 0 ⩽ x 2 ⩽ 50 x 3 ⩾ 20 \left\{\begin{matrix} x_{1} + 2x_{2} - 3x_{3} \leqslant 80 \\ x_{1} + x_{2} + x_{3} = 120 \\ x_{1} \geqslant 30 \\ 0 \leqslant x_{2} \leqslant 50 \\ x_{3} \geqslant 20 \end{matrix}\right. ⎩⎨⎧x1+2x2−3x3⩽80x1+x2+x3=120x1⩾300⩽x2⩽50x3⩾20
Use matlab Solving linear programming problems
- matlab Standard form of linear programming in
m i n C T X ( C = ( c 1 c 2 c 3 c n ) , X = ( x 1 x 2 x 3 x n ) , n Is the number of decision variables ) min \ C^{T} X \ \left ( C = \begin{pmatrix} c_{1} \\ c_{2}\\ c_{3}\\ c_{n}\end{pmatrix} ,X = \begin{pmatrix} x_{1}\\ x_{2}\\ x_{3}\\ x_{n}\end{pmatrix},n Is the number of decision variables \right ) min CTX ⎝⎛C=⎝⎛c1c2c3cn⎠⎞,X=⎝⎛x1x2x3xn⎠⎞,n Is the number of decision variables ⎠⎞
S . t . { A x ≤ b ( Inequality constraints ) A e q x = b e q ( Equality constraints ) l b ≤ x ≤ u b ( Upper and lower bound constraints ) S.t.\left\{\begin{matrix} A \ x\le b \ ( Inequality constraints )\\ Aeq \ x= beq \ ( Equality constraints )\\ lb\le x\le ub \ ( Upper and lower bound constraints ) \end{matrix}\right. S.t.⎩⎨⎧A x≤b ( Inequality constraints )Aeq x=beq ( Equality constraints )lb≤x≤ub ( Upper and lower bound constraints )
matlab When solving linear programming problems , The input of solving the problem shall be specified in the format . such as :
- The objective function to be solved must be the minimum , Therefore, if we get an objective function to solve the maximum value , You need to multiply the objective function -1, At this time, the minimum value solved is multiplied -1 Is the maximum value .
m a x z * − m i n − z max \ z\Longleftrightarrow -min-z max z*−min−z - All inequalities must be of type less than or equal , At this point, if you have an inequality of type greater than or equal , You can multiply both ends of the inequality at the same time -1 To make the symbol take the opposite . For example, the following two formulas are equivalent :
4 x 1 + 6 x 2 − 7 x 3 ≥ 17 − 4 x 1 − 6 x 2 + 7 x 3 ≤ − 17 4x_{1} + 6x_{2} -7x_{3} \ge 17 \\ -4x_{1} - 6x_{2}+7x_{3} \le -17 4x1+6x2−7x3≥17−4x1−6x2+7x3≤−17
If only greater than or less than , Only add or subtract a very small value , Get a solution that meets the accuracy requirements
4 x 1 − 7 x 2 + x 3 < 4 ⇒ 4 x 1 − 7 x 2 + x 3 ≤ 4 − 0.001 4x_{1} -7x_{2}+x_{3}<4\Rightarrow 4x_{1} -7x_{2}+x_{3}\le 4-0.001 4x1−7x2+x3<4⇒4x1−7x2+x3≤4−0.001
example 1, For example, such a topic contains all special conditions :
m a x z = 2 x 1 + 3 x 2 − 5 x 3 S . t { x 1 + x 2 + x 3 = 7 2 x 1 − 5 x 2 + x 3 ≥ 10 x 1 + 3 x 2 + x 3 ≤ 12 x 1 , x 2 , x 3 ≥ 0 max \ z = 2x_{1} +3x_{2}-5x_{3} \\ S.t\left\{\begin{matrix} x_{1} + x_{2}+x_{3} = 7\\ 2x_{1} - 5x_{2} + x_{3}\ge10 \\ x_{1} + 3x_{2} + x_{3} \le 12 \\ x_{1},x_{2},x_{3}\ge 0 \end{matrix}\right. max z=2x1+3x2−5x3S.t⎩⎨⎧x1+x2+x3=72x1−5x2+x3≥10x1+3x2+x3≤12x1,x2,x3≥0
Its standard form is :
m a x z * − m i n − z max \ z\Longleftrightarrow -min-z max z*−min−z c = [ − 2 − 3 5 ] , x = [ x 1 x 2 x 3 ] , A = [ − 2 5 − 1 1 2 1 ] , b = [ − 10 12 ] , A e q = [ 1 1 1 ] , b e q = [ 7 ] , l b = [ 0 0 0 ] c = \begin{bmatrix} -2 \\ -3 \\ 5 \end{bmatrix}, x = \begin{bmatrix} x_{1} \\ x_{2} \\ x_{3} \end{bmatrix}, A = \begin{bmatrix} -2 \ 5 \ -1 \\ 1 \ 2 \ 1 \end{bmatrix}, b=\begin{bmatrix} -10\\ 12 \end{bmatrix}, Aeq = \begin{bmatrix} 1 \ 1 \ 1 \end{bmatrix}, beq = \begin{bmatrix} 7 \end{bmatrix}, lb = \begin{bmatrix} 0\\ 0\\ 0 \end{bmatrix} c=⎣⎡−2−35⎦⎤,x=⎣⎡x1x2x3⎦⎤,A=[−2 5 −11 2 1],b=[−1012],Aeq=[1 1 1],beq=[7],lb=⎣⎡000⎦⎤
Use matlab Command to solve linear programming problems
[x, fval] = linprog(c, A, b, Aeq, beq, lb, ub, X0)
- Return value x: Represents a group that obtains the optimal value x Value ;
- Return value fval: The optimal value of the objective function ;
- Parameter Division X0 Ditto outside ,X0 Represents the initial value of iterative solution , Generally, there is no need to give additional ;
- For nonexistent constraints , Use
[]placeholder ; - For variables without upper and lower bounds, you can use
+infand-infIt means no upper bound and no lower bound respectively .
for example :
m a x z = 2 x 1 + 3 x 2 − 5 x 3 S . t { x 1 + x 2 + x 3 = 7 2 x 1 − 5 x 2 + x 3 ≥ 10 x 1 + 3 x 2 + x 3 ≤ 12 x 1 , x 2 , x 3 ≥ 0 max \ z = 2x_{1} +3x_{2}-5x_{3} \\ S.t\left\{\begin{matrix} x_{1} + x_{2}+x_{3} = 7\\ 2x_{1} - 5x_{2} + x_{3}\ge10 \\ x_{1} + 3x_{2} + x_{3} \le 12 \\ x_{1},x_{2},x_{3}\ge 0 \end{matrix}\right. max z=2x1+3x2−5x3S.t⎩⎨⎧x1+x2+x3=72x1−5x2+x3≥10x1+3x2+x3≤12x1,x2,x3≥0
matlab The order is :
% Objective function coefficient
c = [-2; -3; 5];
% Inequality constraint number
A = [-2 5 -1;
1 3 1];
b = [-10; 12];
% Equality constraints
Aeq = [1 1 1];
beq = 7;
% Upper and lower bound constraints
lb = [0; 0; 0];
[x, fval] = linprog(c, A, b, Aeq, beq, lb);
fval = -fval;
The result is :
Optimal solution found.
x =
6.4286
0.5714
0
fval =
14.5714
Nonlinear regularization
When the objective function or constraint condition is a nonlinear expression , Then the mathematical programming problem at this time is a nonlinear programming problem . The solution of nonlinear programming problems is usually much more difficult , At present, there is no general algorithm . Most algorithms use the initial value of selected decision variables , Search for the optimal solution .
Objective function :
m i n z = x 1 + 3 x 2 + 4 x 3 min \ z = x_{1} + 3x_{2} + 4x_{3} min z=x1+3x2+4x3
constraint condition :
{ x 1 + x 2 − x 3 ⩽ 80 x 1 + 7 x 2 + x 3 = 120 0 ⩽ x 2 2 ⩽ 50 x 1 , x 3 ⩾ 20 \left\{\begin{matrix} x_{1} + x_{2} - x_{3} \leqslant 80 \\ x_{1} + 7x_{2} + x_{3} = 120 \\ 0 \leqslant x_{2}^{2} \leqslant 50 \\ x_{1}, x_{3} \geqslant 20 \end{matrix}\right. ⎩⎨⎧x1+x2−x3⩽80x1+7x2+x3=1200⩽x22⩽50x1,x3⩾20
Use matlab Solving nonlinear programming problems
- matlab Standard form of nonlinear programming in
m i n f ( x ) min f(x) minf(x) S . t . { A x ≤ b , A e q x = b e q ( Linear constraints ) C ( x ) ≤ 0 , C e q ( x ) = 0 ( Nonlinear constraints ) l b ≤ x ≤ u b ( Upper and lower bound constraints ) S.t.\left\{\begin{matrix} A \ x\le b, Aeq \ x= beq \ ( Linear constraints )\\ C\left ( x \right ) \le 0, Ceq(x) = 0 \ ( Nonlinear constraints )\\ lb\le x\le ub \ ( Upper and lower bound constraints ) \end{matrix}\right. S.t.⎩⎨⎧A x≤b,Aeq x=beq ( Linear constraints )C(x)≤0,Ceq(x)=0 ( Nonlinear constraints )lb≤x≤ub ( Upper and lower bound constraints )
- among C(x) and Ceq(x) They are nonlinear inequality constraints and nonlinear equality constraints .
- C(x) and Ceq(x) The right side of must be zero . When it is not zero, you need to move .
- The objective function to be solved must be the minimum , Therefore, if we get an objective function to solve the maximum value , You need to multiply the objective function -1, At this time, the minimum value solved is multiplied -1 Is the maximum value .
m a x z * − m i n − z max \ z\Longleftrightarrow -min-z max z*−min−z - All inequalities must be of type less than or equal , At this point, if you have an inequality of type greater than or equal , You can multiply both ends of the inequality at the same time -1 To make the symbol take the opposite . For example, the following two formulas are equivalent :
4 x 1 + 6 x 2 − 7 x 3 ≥ 17 − 4 x 1 − 6 x 2 + 7 x 3 ≤ − 17 4x_{1} + 6x_{2} -7x_{3} \ge 17 \\ -4x_{1} - 6x_{2}+7x_{3} \le -17 4x1+6x2−7x3≥17−4x1−6x2+7x3≤−17
If only greater than or less than , Only add or subtract a very small value , Get a solution that meets the accuracy requirements .
4 x 1 − 7 x 2 + x 3 < 4 ⇒ 4 x 1 − 7 x 2 + x 3 ≤ 4 − 0.001 4x_{1} -7x_{2}+x_{3}<4\Rightarrow 4x_{1} -7x_{2}+x_{3}\le 4-0.001 4x1−7x2+x3<4⇒4x1−7x2+x3≤4−0.001
example 2, for example :
m i n f ( x ) = x 1 2 + x 2 2 + x 3 2 + 7 S . t . { x 1 2 − x 2 + x 3 2 ≥ 0 x 1 + x 2 2 + x 3 2 ≤ 20 − x 1 − x 2 2 + 2 = 0 x 2 + 2 x 3 2 = 3 x 1 + x 2 + x 3 ≤ 5 x 1 , x 2 , x 3 ≥ 0 min \ f(x) = x_{1}^{2} + x_{2}^{2} + x_{3}^{2} + 7 \\ S.t.\left\{\begin{matrix} x_{1}^{2}-x_{2}+x_{3}^{2} \ge 0 \\ x_{1}+x_{2}^{2}+x_{3}^{2} \le20 \\ -x_{1}-x_{2}^{2}+2 = 0\\ x_{2}+2x_{3}^{2} = 3\\ x_{1} + x_{2} + x_{3}\le 5 \\ x_{1} , x_{2} , x_{3} \ge 0 \end{matrix}\right. min f(x)=x12+x22+x32+7S.t.⎩⎨⎧x12−x2+x32≥0x1+x22+x32≤20−x1−x22+2=0x2+2x32=3x1+x2+x3≤5x1,x2,x3≥0
Its standard form is :
S . t . ( A = [ 1 1 1 ] , b = 5 , A e q = [ ] , b e q = [ ] , C ( x ) = [ − x 1 2 + x 2 − x 3 2 x 1 + x 2 2 + x 3 2 − 20 ] , C e q = [ − x 1 − x 2 2 + 2 x 2 + 2 x 3 2 − 3 ] , l b = [ 0 0 0 ] ) S.t.\begin{pmatrix} A = \begin{bmatrix} 1 \ 1 \ 1 \end{bmatrix}, b = 5, Aeq = [], beq = [],\\ C(x) = \begin{bmatrix} -x_{1}^{2}+x_{2}-x_{3}^{2} \\ x_{1}+x_{2}^{2}+x_{3}^{2} - 20 \end{bmatrix}, Ceq = \begin{bmatrix} -x_{1}-x_{2}^{2}+2 \\ x_{2}+2x_{3}^{2} - 3 \end{bmatrix}, \\ lb = \begin{bmatrix} 0\\ 0\\ 0 \end{bmatrix} \end{pmatrix} S.t.⎝⎛A=[1 1 1],b=5,Aeq=[],beq=[],C(x)=[−x12+x2−x32x1+x22+x32−20],Ceq=[−x1−x22+2x2+2x32−3],lb=⎣⎡000⎦⎤⎠⎞
- Use matlab Command solving nonlinear programming
matlab Function for solving nonlinear programming[x, val] = fmincon(@fun, X0, A, b, Aeq, beq, lb, ub, @nonfun, option)
@funRepresents the objective function , You need to write a separate m File storage objective function .function f = func(x) f = x(1) ^ 2 + x(2) + ... - x(n) ^ 4 endamong ,f For the return value ,x The variable is a vector .
X0For the initial value . Different from linear programming , The selection of initial value in nonlinear programming is very important , Because the result of linear programming is only a local optimal solution . To make the result as possible optimal The following methods can be used :(1) Given different initial values , Find the optimal solution in it ;(2) First use Monte Carlo to get a solution , And use this solution as the initial value to solve .A, b, Aeq, beq, lb, ub ditto .
@nonlfunRepresents a nonlinear constraint function .function [c, ceq] = nonlfunc(x) c = [ Nonlinear inequality constraints 1, Nonlinear inequality constraints 2... Nonlinear inequality constraints n] c = [ Nonlinear equality constraints 1, Nonlinear equality constraints 2... Nonlinear equality constraints n] endUse when some constraints do not exist
[]Just occupy the space .optionChoice of solution algorithm .matlab A total of 4 Different options in , Each has its own practical conditions, advantages and disadvantages , It is advisable to use different algorithms to solve and select the optimal value in turn . The four algorithms are :interior-pointInterior point method ( If you do not actively modify the options, it is the default )、sqpSequential quadratic programming 、active-setEffective set method andtrust-region-reflectiveTrust region reflection algorithm .
Change the method of solving algorithm :option = optimoption('fmincon', 'Algorithm', ' Choose algorithm ') % The available algorithms are interior-point、sqp、active-set、trust-region-reflective
For example 2,matlab The solution code is :
func.m( Objective function )
% Objective function
function f = func(x)
f = x(1)^2 + x(2)^2 + x(3)^2 + 7;
end
nonlfunc.m( Nonlinear constraints )
% Nonlinear constraints
function [c,ceq] = nonlfunc(x)
c = [-x(1)^2 + x(2) - x(3)^2;
x(1) + x(2)^2 + x(3)^2 - 20];
ceq = [-x(1) - x(2)^2 + 2;
x(2) + 2*x(3)^2 - 3];
end
Solve the code :
% Linear inequality constraints
A = [1 1 1];
b = 5;
% Upper and lower bound constraints
lb = [0 0 0]';
% Initial value selection Note that the more reasonable the initial value is ( Compliance constraint ), The better the value obtained, the faster the solution speed
x0 = [1 1 1];
[x, fval] = fmincon(@func, x0, A, b, [], [], lb, [], @nonlfunc);
Solution result :
x =
0.5522 1.2033 0.9478
fval =
9.6511
It can be seen that the selection of the initial value for this problem is still good , But the luck of choosing the initial value is not always very good . A certain strategy needs to be taken . for example , Monte Carlo simulation :
% Number of random groups
n = 10000000;
% Further compress the range of random numbers according to the last two constraints
x1 = unifrnd(0, 5, n, 1); % Generate n Group [0, 5] Random number between
x2 = unifrnd(0, 5, n, 1);
x3 = unifrnd(0, 5, n, 1);
fmin = +inf;
for i = 1 : n
xx = [x1(i) x2(i) x3(i)];
if (xx(1)^2 - xx(2) + xx(3)^2 >=0 && xx(1) + xx(2)^2 + xx(3)^2 <=20)
result = xx(1)^2 + xx(2)^2 + xx(3)^2 + 7;
if result < fmin
fmin = result;
x0 = xx;
end
end
end
disp(x0); % A set of initial values obtained by simulation are 0.0601 0.0043 0.0380
[x, fval] = fmincon(@func, x0, A, b, [], [], lb, [], @nonlfunc);
Integer regularization
Integer programming is a kind of mathematical programming problem with integer variables . It is divided into Integer linear programming and Integer nonlinear programming . at present , The popular integer programming algorithms can only solve integer linear programming .
Use matlab Solving integer programming problems – linear
[x, fval] = intlinprog(c, intcon, A, b, Aeq, beq, lb, ub)
- intlinprog Initial value cannot be specified ;
- Joined the intcon Parameters can specify which parameters must be integers . For example, there are three decision variables [x1, x2, x3], When intcon = [1, 3] Represents a decision variable 1 And decision variables 3 It has to be an integer .
for example :
m a x z = 2 x 1 + 3 x 2 − 5 x 3 S . t { x 1 + x 2 + x 3 = 7 2 x 1 − 5 x 2 + x 3 ≥ 10 x 1 + 3 x 2 + x 3 ≤ 12 x 1 , x 2 , x 3 ∈ Z x 1 , x 2 , x 3 ≥ 0 max \ z = 2x_{1} +3x_{2}-5x_{3} \\ S.t\left\{\begin{matrix} x_{1} + x_{2}+x_{3} = 7\\ 2x_{1} - 5x_{2} + x_{3}\ge10 \\ x_{1} + 3x_{2} + x_{3} \le 12 \\ x_{1}, x_{2}, x_{3 \ \in Z} \\ x_{1},x_{2},x_{3}\ge 0 \end{matrix}\right. max z=2x1+3x2−5x3S.t⎩⎨⎧x1+x2+x3=72x1−5x2+x3≥10x1+3x2+x3≤12x1,x2,x3 ∈Zx1,x2,x3≥0
matlab Program :
% Objective function coefficient
c = [-2; -3; 5];
% Inequality constraint number
A = [-2 5 -1;
1 3 1];
b = [-10; 12];
% Equality constraints
Aeq = [1 1 1];
beq = 7;
% Upper and lower bound constraints
lb = [0; 0; 0];
[x, fval] = intlinprog(c, [1, 2, 3], A, b, Aeq, beq, lb);
fval = -fval;
Solution result :
x =
7.0000
0
0
fval =
14.0000
0-1 Regularization
0-1 Programming is a special case of integer programming ,0-1 The value of planning variables can only be 0 or 1.
Use matlab solve 01 Programming problem – linear
[x, fval] = intlinprog(c, intcon, A, b, Aeq, beq, lb, ub)
It is the same as solving linear integer programming , Just change the upper and lower bounds to :
l b = [ 0 0 0 0 ] , u b = [ 1 1 1 1 ] lb=\begin{bmatrix} 0\\ 0\\ 0\\ 0 \end{bmatrix}, ub=\begin{bmatrix} 1\\ 1\\ 1\\ 1 \end{bmatrix} lb=⎣⎡0000⎦⎤,ub=⎣⎡1111⎦⎤
Use integer linearity 01 Planning to solve 01 knapsack problem ( The capacity of the backpack is 30):
| goods | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| weight | 6 | 3 | 4 | 5 | 1 | 2 | 3 | 5 | 4 | 2 |
| value | 540 | 200 | 180 | 350 | 60 | 150 | 280 | 450 | 320 | 120 |
matlab Solver :
% Objective function coefficient
c = [-540; -200; -180; -350; -60; -150; -280; -450; -320; -120];
% Inequality constraint number
A = [6 3 4 5 1 2 3 5 4 2];
b = 30;
intcon = 1:10;
% Upper and lower bound constraints
lb = zeros(10, 1);
ub = ones(10, 1);
[x, fval] = intlinprog(c, intcon, A, b, [], [], lb, ub);
fval = -fval;
Solution result :
x =
1
1
0
1
0
1
1
1
1
1
fval =
2410
边栏推荐
- What is IDE (integrated development environment)
- Sword finger offer 46. translate numbers into strings
- Excuse me: is Flink 1.14.5 compatible with MySQL CDC 2.1.0
- MySQL advanced
- Sword finger offer 49. ugly number
- Choose the appropriate container runtime for kubernetes
- Google's display of Chrome browser icons was abandoned, and it was intended to be a small rocket
- Leetcode 146: LRU cache
- Getting started with COM programming 1- creating projects and writing interfaces
- Valdo2021 - vascular space segmentation in vascular disease detection challenge (I)
猜你喜欢

Lights of thousands of families in the year of xinchou

Look at the interface control devaxpress WinForms - how to customize auxiliary function properties (Part 2)

Talk about your transformation test development process

clip:learning transferable visual models from natural language supervision

Valdo2021 - vascular space segmentation in vascular disease detection challenge (I)

Browser local storage webstroage

【德味】安全:如何为行人提供更多保护

Create a life cycle aware MVP architecture

Home Assistant中接入博联WiFi智能遥控

BGP - border gateway protocol
随机推荐
How to use the interface control telerik UI for WinForms development step progress bar?
Data transmission of different fragments in the same activity
微服务架构 | 服务监控与隔离 - [Sentinel] TBC...
How to select the shelling tool?
[leetcode] 1184. Distance between bus stops
Getting started with COM programming 1- creating projects and writing interfaces
Sword finger offer 42. maximum sum of continuous subarrays
Thymeleaf application notes
The beginning of winter in the year of bitterness and ugliness
147 set whether to cache by using the routing meta information - use of include and exclude - use of activated and deactivated
Original reverse compensation and size end
01 | opening words: teach you to build a blog website hand in hand
Home Assistant中接入博联WiFi智能遥控
SSL Error: Unable to verify the first certificate
870. Approximate number
Ask a question: is there an error msg = ora-04036: instance usage when using CDC to monitor oracle
6.0 holes stepped by fragment request permission in the system
Login Huawei device in SSH mode
Redis common configuration description
Hook 32-bit function using the method modified to JMP instruction