title: 每日一题【20200727】
excerpt: 第六天建模打卡
tags: [数学建模, 线性规划, intlinprog, 0-1规划]
categories:
- [学习, 数学建模]
index_img: https://picture-store-repository.oss-cn-hangzhou.aliyuncs.com/PicGo/20201129201333.jpg
banner_img: https://picture-store-repository.oss-cn-hangzhou.aliyuncs.com/PicGo/20201129201333.jpg
date: 2020-07-27 11:11:11
comment: true
math: true
非线性规划
matlab随机取样计算法解决非线性整数规划
matlab代码:
(1)编辑mengte.m文件
%目标函数f,约束条件g
function [f,g] = mengte(x)
f = x(1)^2 + x(2)^2 + 3*x(3)^2 + 4*x(4)^2 + 2*x(5)^2 - 8*x(1) - 2*x(2) - 3*x(3) - x(4) - 2*x(5);
g(1) = sum(x) - 400;
g(2) = x(1) + 2*x(2) + 2*x(3) + x(4) + 6*x(5) - 800;
g(3) = 2*x(1) + x(2) + 6*x(3) - 200;
g(4) = x(3) + x(4) + 5*x(5) - 200;
end
(2)matlab执行代码
%非线性整数规划----枚举法--随机取样计算法--有误差
%tic和toc用来记录matlab命令执行的时间。
%整数问题最好用Lingo-----可每次将 p0 改为最大值
tic
p0 = 0;
for i = 1 : 10^5
x = 99 * rand(5,1); %0 <= x <=99
x1 = floor(x); x2 = ceil(x); %取整
[f,g] = mengte(x1);
if sum(g <= 0) == 4
if f >= p0
x0 = x1;
p0 = f;
end
end
[f,g] = mengte(x2);
if sum(g <= 0) == 4
if f >= p0;
x0 = x2;
p0 = f;
end
end
end
x0,p0
toc
答案是:
x0 =
27
98
5
99
1
p0 =
49086
历时 0.770322 秒。