MATLAB 自定义优化模型

简介

MATLAB R2017b 中推出的优化工具箱提供了一系列的优化算法,可以用来求解优化问题。

它包括以下方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
% 创建优化问题
prob = optimproblem(Name,Value);

% 创建优化变量
x = optimvar(name,[n1,n2,...,nk],Name,Value);

% 设置优化目标
prob.Objective = f(x);

% 添加约束
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;
prob.Constraints.cons3 = cons3;

% 检查优化问题
show(prob)

% 求解优化问题
[sol,fval] = solve(prob,x0,Name,Value);

通过自定义优化模型,可以解决多种类型的优化问题,并且具有更好的可读性


线性规划

maxZ=2x1+3x2+4x3\begin{equation} \max \quad Z=2 x_{1}+3 x_{2} + 4 x_{3} \end{equation}

 s.t. {2x1+x2+3x336x1+x28x1+x310x1+x2x3=4x1,x2,x30\begin{equation} \text { s.t. } \left\{ \begin{array}{c} 2 x_{1}+x_{2}+3x_{3} \leq 36 \\ x_{1}+x_{2} \geq 8 \\ x_{1}+x_{3} \geq 10 \\ x_{1}+x_{2}-x_{3} = 4 \\ x_{1}, x_{2}, x_{3} \geq 0 \end{array} \right. \end{equation}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
% 创建优化问题
prob = optimproblem('ObjectiveSense','max');

% 创建优化变量
x = optimvar('x',3,'LowerBound',0);

% 设置优化目标
prob.Objective = 2*x(1)+3*x(2)+4*x(3);

% 添加约束
prob.Constraints.cons1 = 2*x(1)+x(2)+3*x(3)<=36;
prob.Constraints.cons2 = x(1)+x(2)>=8;
prob.Constraints.cons3 = x(1)+x(3)>=10;
prob.Constraints.cons4 = x(1)+x(2)-x(3)==4;

% 检查优化问题
show(prob)

% 求解优化问题
[sol,fval] = solve(prob);

结果

1
2
3
4
5
6
7
sol.x =
2.6667
8.6667
7.3333

fval =
60.6667

非线性规划

minf(x)=x12+x22+x32+8\begin{equation} \min \quad f(x)=x_{1}^2+x_{2}^2+x_{3}^2+8 \end{equation}

 s.t. {x12x2+x320x1+x22+x3320x1x22+2=0x2+2x32=3x1,x2,x30\begin{equation} \text { s.t. } \left\{ \begin{array}{c} x_{1}^2-x_{2}+x_{3}^2 \geq 0 \\ x_{1}+x_{2}^2+x_{3}^3 \leq 20 \\ -x_{1}-x_{2}^2+2 = 0 \\ x_{2}+2x_{3}^2 = 3 \\ x_{1}, x_{2}, x_{3} \geq 0 \end{array} \right. \end{equation}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
% 创建优化问题
prob = optimproblem('ObjectiveSense','min');

% 创建优化变量
x = optimvar('x',3,'LowerBound',0);

% 设置优化目标
prob.Objective = x(1)^2+x(2)^2+x(3)^2+8;

% 添加约束
prob.Constraints.cons1 = x(1)^2-x(2)+x(3)^2>=0;
prob.Constraints.cons2 = x(1)+x(2)^2+x(3)^3<=20;
prob.Constraints.cons3 = -x(1)-x(2)^2+2==0;
prob.Constraints.cons4 = x(2)+2*x(3)^2==3;

% 检查优化问题
show(prob)

% 求解优化问题
x0.x = [0;0;0];
[sol,fval] = solve(prob,x0);

结果

1
2
3
4
5
6
7
sol.x =
0.5522
1.2033
0.9478

fval =
10.6511

线性整数规划

maxZ=4x1+3y1+5y2\begin{equation} \max \quad Z=4 x_{1}+3y_{1}+5y_{2} \end{equation}

 s.t. {y1,y2 are integers2x1+y1+3y236x1+y18x1+y210x1+y1y2=4x1,y1,y20\begin{equation} \text { s.t. } \left\{ \begin{array}{c} y_{1},y_{2} \text{ are integers} \\ 2 x_{1}+y_{1}+3y_{2} \leq 36 \\ x_{1}+y_{1} \geq 8 \\ x_{1}+y_{2} \geq 10 \\ x_{1}+y_{1}-y_{2} = 4 \\ x_{1}, y_{1}, y_{2} \geq 0 \end{array} \right. \end{equation}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
% 创建优化问题
prob = optimproblem('ObjectiveSense','max');

% 创建优化变量
x = optimvar('x',1,'LowerBound',0);
y = optimvar('y',2,'LowerBound',0,'Type','integer');

% 设置优化目标
prob.Objective = 4*x+3*y(1)+5*y(2);

% 添加约束
prob.Constraints.cons1 = 2*x+y(1)+3*y(2)<=36;
prob.Constraints.cons2 = x+y(1)>=8;
prob.Constraints.cons3 = x+y(2)>=10;
prob.Constraints.cons4 = x+y(1)-y(2)==4;

% 检查优化问题
show(prob)

% 求解优化问题
[sol,fval] = solve(prob);

结果

1
2
3
4
5
6
7
8
9
sol.x =
4.0000

sol.y =
7.0000
7.0000

fval =
72.0000

指派问题


已知指派成本矩阵为:

[215134104145914161378119]\begin{bmatrix} 2 & 15 & 13 & 4 \\ 10 & 4 & 14 & 5 \\ 9 & 14 & 16 & 13 \\ 7 & 8 & 11 & 9 \end{bmatrix}

求最小化总成本的指派方案。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
% 指派矩阵
cost = [2 15 13 4;10 4 14 5;9 14 16 13;7 8 11 9];

% 创建优化问题
prob = optimproblem('ObjectiveSense','min');

% 创建优化变量
x = optimvar('x',4,4,'Type','integer','LowerBound',0,'UpperBound',1);

% 设置优化目标
prob.Objective = sum(sum(x.*cost));

% 添加约束
prob.Constraints.cons1 = sum(x,2)==1;
prob.Constraints.cons2 = sum(x,1)==1;

% 检查优化问题
show(prob)

% 求解优化问题
[sol,fval] = solve(prob);

结果

1
2
3
4
5
6
7
8
sol.x =
0 0 0 1
0 1 0 0
1 0 0 0
0 0 1 0

fval =
28