⎨⎧2x1+x2+3x3≤36x1+x2≥8x1+x3≥10x1+x2−x3=4x1,x2,x3≥0解
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
s.t. ⎩⎨⎧x12−x2+x32≥0x1+x22+x33≤20−x1−x22+2=0x2+2x32=3x1,x2,x3≥0
解
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
s.t. ⎩⎨⎧y1,y2 are integers2x1+y1+3y2≤36x1+y1≥8x1+y2≥10x1+y1−y2=4x1,y1,y2≥0
解
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
|
指派问题
例
已知指派成本矩阵为:
210971541481314161145139
求最小化总成本的指派方案。
解
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
|
小嗷犬
分享技术,记录生活
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 小嗷犬!