Matlab求下列整数:
%目标函数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
.m调用代码
%非线性整数规划----枚举法--随机取样计算法--有误差
%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