Matlab\Lingo:数模10-非线性规划与01规划模型

非线形规划例题

在这里插入图片描述

非线形规划例题(lingo解法)

在这里插入图片描述
在这里插入图片描述

非线形规划例题(Matlab解法)

在这里插入图片描述
首先建立脚本:

function f = fun1(x)
f = -98*x(1) - 277*x(2) + x(1)^2 + 0.3*x(1)*x(2) + 2*x(2)^2;
end

注意由于题目是max,需要改成标准形式。所有的符号都要变号。(而“数模9”文章给的代码自动转换了,所以max可以直接输入,这个要自己创建函数文件,所以需要自己修改为标准型)

保存为fun1.m。

再建立脚本:

function [g,h] = fun2(x)
g(1) = x(1) + x(2) - 100;
g(2) = x(1) - 2*x(2);
h = 0;
end

其中g是不等式约束,h是等式约束。

保存为fun2.m。

最后建立一个脚本:

disp('max(0) or min(1)?');
Judge=input('Judge=');
options = optimset;
[x, y] = fmincon('fun1', rand(2,1), [], [], [], [], zeros(2,1), [], 'fun2', options);
if Judge==0
    x
    fval=-y
else
    x
    fval=y
end

在写fun2时,可以把线性和非线性约束的等式和不等式约束都按照这种格式写到这个函数里面,这样的话fun2就包含了所有约束条件,在后面运行fmincon()时不需要再写A,B,Aeq,Beq,直接用[]略过。

‘fun1’代表目标函数,rand(2,1)随机给了x初值,zeros(2,1)代表下限为0,即x1,x2>=0, 'fun2’即刚才写的约束条件。

运行后得到:
在这里插入图片描述
与lingo给的结果基本一致。

01规划模型定义及例题

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

01规划模型例题(lingo解法)

在这里插入图片描述
在这里插入图片描述

01规划模型例题(Matlab解法)

原本可用以解决的bintprog在2014年被废除了,面对这样大的数据我尝试了几个都报错,如果有人可以把这道题用Matlab解答请私信我。

(尝试1:)

disp('max(0) or min(1)?');
Judge=input('Judge=');
f=[8,13,18,23,10,14,16,27,2,10,21,26,14,22,26,28];
A=[];
b=[];
Aeq=[1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0;
    0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0;
    0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0;
    0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1;
    1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0;
    0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0;
    0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0;
    0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1];
beq=[1,1,1,1,1,1,1,1];
lb=[0;0;0;0;0;0;0;0];
ub=[inf;inf;inf;inf;inf;inf;inf;inf];
if Judge==0
    [x,fval]=linprog(-f,A,b,Aeq,beq,lb,ub);
    x
    fval=-fval
else
    [x,fval]=linprog(f,A,b,Aeq,beq);
    x
    fval
end

(尝试2:)

function f = fun1(x)
f = 8*x(11)+13*x(12)+18*x(13)+23*x(14)+10*x(21)+14*x(22)+16*x(23)+27*x(24)+2*x(31)+10*x(32)+21*x(33)+26*x(34)+14*x(41)+22*x(42)+26*x(43)+28*x(44);
end
function [g,h] = fun2(x)
g = 0;
h(1) = x(11)+x(12)+x(13)+x(14)-1;
h(2) = x(21)+x(22)+x(23)+x(24)-1;
h(3) = x(31)+x(32)+x(33)+x(34)-1;
h(4) = x(41)+x(42)+x(43)+x(44)-1;
h(5) = x(11)+x(21)+x(31)+x(41)-1;
h(6) = x(11)+x(21)+x(31)+x(41)-1;
h(7) = x(13)+x(23)+x(33)+x(43)-1;
h(8) = x(14)+x(24)+x(34)+x(44)-1;
end
disp('max(0) or min(1)?');
Judge=input('Judge=');
options = optimset;
[x, y] = fmincon('fun1', rand(16,1), [], [], [], [], zeros(16,1), [], 'fun2', options);
if Judge==0
    x
    fval=-y
else
    x
    fval=y
end
  • 6
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个利用LingoMATLAB求解非线性规划问题模型的案例: 假设有一个工厂需要生产两种产品A和B,生产A和B需要不同的原材料和工人数量,且有一些限制条件。假设每天工厂有8个小时的生产时间,每个工人每天最多工作6个小时,原材料的供应量也有限制。现在需要确定每天生产A和B的数量,以最大化收益。 根据以上问题,可以得到如下的非线性规划模型: 最大化收益:f(x) = 20x1 + 30x2 约束条件: - 原材料限制:2x1 + 3x2 <= 120 - 工人数量限制:4x1 + 3x2 <= 96 - 生产时间限制:x1 + x2 <= 8 - 非负限制:x1 >= 0, x2 >= 0 其中,x1表示生产A的数量,x2表示生产B的数量。 接下来,我们可以使用LingoMATLAB求解以上非线性规划模型。 首先,我们使用Lingo语言编写以上模型,得到以下的Lingo模型: ``` max = 20x1 + 30x2 c1: 2x1 + 3x2 <= 120 c2: 4x1 + 3x2 <= 96 c3: x1 + x2 <= 8 x1 >= 0 x2 >= 0 ``` 然后,我们可以在MATLAB中编写调用程序,调用LINGO软件求解以上模型代码如下: ``` % Lingo and MATLAB nonlinear programming example % Define the Lingo model lingo_model = [ 'max = 20x1 + 30x2' 'c1: 2x1 + 3x2 <= 120' 'c2: 4x1 + 3x2 <= 96' 'c3: x1 + x2 <= 8' 'x1 >= 0' 'x2 >= 0' ]; % Write the Lingo model to a file lingo_file = 'nonlinear.lp'; fid = fopen(lingo_file, 'w'); fprintf(fid, '%s', lingo_model); fclose(fid); % Call Lingo to solve the nonlinear programming problem [status, result] = system(['lingo -s "', lingo_file, '"']); % Parse the Lingo solution if status == 0 pattern = '20X1 = %f, 30X2 = %f'; [tokens, matches] = regexp(result, pattern, 'tokens', 'match'); x1 = tokens{1}(1); x2 = tokens{1}(2); fprintf('Optimal solution: x1 = %f, x2 = %f\n', x1, x2); else fprintf('Error: Lingo solver failed\n'); end ``` 运行以上MATLAB程序,即可得到最优解为x1=24,x2=16,最优值为960。 以上就是一个利用LingoMATLAB求解非线性规划问题模型的案例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值