这几天在搞一个关于整数线性规划求解的程序,在实际例子测试时,总是出错,搞得现在头都:'( 晕沉沉的。希望高手们来帮忙看看,点拨一下:Q 。
M文件:
function [x,fval,status,output,bound]=fen(f,A,b,lb,ub,x0,options)
%先求解松弛问题的解
[x,fval,status,outout,bound]=linprog(f,A,b,[],[],lb,ub,x0,options);
%【如果没有可行解,则返回】
if status<=0
return;
end
%如果有可行解,则判断是否已经为整数解
p=rem(x,1);
integer=find(p~=0);%找出非整数解得位置
if isempty(integer) %如果没有非整数解,即已经全是整数解,则调用结束
return
end
%如果还有非整数解,则分支
n=integer(1); %第一个非整数解的位置
low=floor(x(n)); %朝负无穷舍入
high=-ceil(x(n)); %朝正无穷舍入
c=length(f);
%分支一
temp1=zeros(1,c);
temp1(n)=1;
A1=[A;temp1];
b1=b;
b1(end+1)=low;
%递归调用,求解分支一
[x1,fval1,status1,output1,bound1]=fen(f,A1,b1,lb,ub,x0,options);
%分支二
temp2=zeros(1,c);
temp2(n)=-1;
A2=[A;temp2];
b2=b;
b2(end+1)=high;
%递归调用,求解分支二
[x2,fval2,status2,output2,bound2]=fen(f,A2,b2,lb,ub,x0,options);
if isempty(integer)&((status1>0&status2<=0&fval<=fval1)|(status2>0&status1<=0&fval<=fval2)|(status1>0&status2>0&fval<=fval1&fval<=fval2))
disp('error!');
return
end
%下面根据status的返回结果确定最优值
if status1>0&status2<=0 %左分支有最优解,右分支无最优解
x=x1;
fval=fval1;
status=status1;
output=output1;
bound=bound1;
elseif status2>0&status1<=0%左分支无最优解,右分支有最优解
x=x2;
fval=fval2;
status=status2;
output=outpit2;
bound=bound2;
elseif status1>0&status2>0 %左右分支都有各自的最优解,则比较那个更优
if fval1
x=x1;
fval=fval1;
status=status1;
output=output1;
bound=bound1;
else %右分支更优
x=x2;
fval=fval2;
status=status2;
output=outpit2;
bound=bound2;
end
end
我输入以下例子:min z=-4x1-9x2
9x1+7x2<=47
7x1+20x2<=63
xj>=0
例子答案:x1=3, x2=2, min=-30
我测试时老是出错,自己又难以发现哪里出错了,所谓当局者迷,旁观者清,请各位帮忙看看:
??? One or more output arguments not assigned during call to 'D:\MATLAB7\work\fen.m (fen)'.
Error in ==> fen at 26
[x1,fval1,status1,output1,bound1]=fen(f,A1,b1,lb,ub,x0,options);
各路英雄豪侠,以上问题求帮助:handshake !