源代码如下:fun ctio n [x,y]=ILp(f,G,h,Geq,heq,lb,ub,x,id,optio ns)%整数线性规划分支定界法,可求解纯整数规划和混合整数规划。
%y=minf ' *x s.t. G*x<=h Geq*x=heq x为全整数或混合整数列向量%用法%[x,y]=ILp(f,G,h,Geq,heq,lb,ub,x,id,optio ns)%参数说明%lb:解的下界列向量(Default:」nt )%ub:解的上界列向量(Default:int)%x:迭代初值列向量%id :整数变量指标列向量,1-整数,0-实数(Default:1 )global upper opt c x0 A b Aeq beq ID options;if nargin<10,options=optimset({});options.Display='off;opti on rgeScale='off;e ndif nargin<9,id=ones(size(f));endif nargin<8,x=[];endif nargin<7 |isempty(ub),ub=inf*ones(size(f));endif nargin<6 |isempty(lb),lb=zeros(size(f));endif narginv5,heq=[];endif narginv4,Geq=[];endupper=i nf;c=f;xO=x;A=G;b=h;Aeq=Geq;beq=heq;ID=id; ftemp=ILP(lb(:),ub(:));x=opt;y=upper;%下面是子函数fun cti on ftemp=ILP(vlb,vub)global upper opt c x0 A b Aeq beq ID options;[x,ftemp,how]=li nprog(c,A,b,Aeq,beq,vlb,vub,xO,opti on s);if how <=0return;en d;if ftemp-upper>0.00005 %in order to avoid errorreturn;en d;if max(abs(x.*ID-round(x.*ID)))<0.00005if upper-ftemp>0.00005 %in order to avoid error opt=x';upper=ftemp;return;elseopt=[opt;x'];return;en d;en d;notintx=find(abs(x-round(x))>=0.00005); %in order to avoid errorin tx=fix(x);tempvlb=vlb;tempvub=vub;if vub(notintx(1,1),1)>=intx(notintx(1,1),1)+1;tempvlb( no ti ntx(1,1),1)=i ntx(n oti ntx(1,1),1)+1;ftemp=I ntLP(tempvlb,vub);en d;if vlb(notintx(1,1),1)<=intx(notintx(1,1),1)tempvub( not in tx(1,1),1)=i ntx(notin tx(1,1),1);ftemp=I ntLP(vlb,tempvub);en d;在命令行运行>>c=[1,1,-4];a=[1,1,2;1,1,-1;-1,1,1];b=[9;2;4];>> [x,f]=ILp(c,a,b,[],[],[0;0;0],[inf;inf;inf])从你的程序中的如下这条语句可以看出变量ID是全局变量:global upper opt c x0 A b Aeq beq ID opti ons;该全局变量(ID)是在你的函数之外被别的程序语句进行过赋值,而且也很有可能被赋成了向量/矩阵、并且它的维数大小跟向量x的维数大小并不相同,这就造成了进行运算x.*ID的时候维数不匹配的问题第二个:题目:min (4*x1+4*x2); 约束条件:2*x1+5*x2<=15,2*x1-2*x2<=5,x1,x2>=0, 且都为整数.解这个还是很容易,算出来x1,x2都为0点几,因为题目要求是整数,所以主要是这个分支定界的问题,急求一个分支定界的MATLAB算法,通用算法也可以,或者只能解这道题也可以,只要能进行计算就行,最后解出来x1,x2都为0.希望大家帮帮忙,先谢谢大家了.解线性方程很容易,但主要是整数解,需要用分支定界法•希望算法尽量简单易懂,最好有注释,本人是初学者,谢谢了把以下程序存为ILP.m ,function [x,y]=ILp(f,G,h,Geq,heq,lb,ub,x,id,options)%整数线性规划分支定界法,可求解纯整数规划和混合整数规划。
%y=minf ' *x s.t. G*x<=h Geq*x=heq x 为全整数或混合整数列向量%用法%[x,y]=ILp(f,G,h,Geq,heq,lb,ub,x,id,optio ns)%参数说明%lb:解的下界列向量 (Default:」nt )%ub:解的上界列向量(Default:int)%x:迭代初值列向量%id :整数变量指标列向量,1-整数,0-实数(Default:1 )global upper opt c x0 A b Aeq beq ID opti ons;if nargin <10,opti on s=optimset({});opti on s.Display='off;opti on rgeScale='off;e ndif nargin< 9,id=on es(size(f));e ndif nargin< 8,x=[];e ndif nargin<7 |isempty(ub),ub=inf*on es(size(f));e ndif nargin<6 |isempty(lb),lb=zeros(size(f));e ndif narginv 5,heq=[];e ndif narginv 4,Geq=[];e ndupper=i nf;c=f;xO=x;A=G;b=h;Aeq=Geq;beq=heq;ID=id;ftemp=ILP(lb(:),ub(:));x=opt;y=upper;%下面是子函数fun cti on ftemp=ILP(vlb,vub)global upper opt c x0 A b Aeq beq ID opti ons;[x,ftemp,how]=li nprog(c,A,b,Aeq,beq,vlb,vub,xO,opti on s);if how <=0return;en d;if ftemp-upper>0.00005 %in order to avoid errorreturn;en d;if max(abs(x.*ID-rou nd(x.*ID)))<0.00005if upper-ftemp>0.00005 %in order to avoid erroropt=x';upper=ftemp;return;elseopt=[opt;x'];x =return;en d;en d;no ti ntx=fi nd(abs(x-r oun d(x))>=0.00005); %in order to avoid error in tx=fix(x);tempvlb=vlb;tempvub=vub;if vub( not in tx(1,1),1)>=i ntx(no ti ntx(1,1),1)+1;tempvlb( no ti ntx(1,1),1)=i ntx(n oti ntx(1,1),1)+1;ftemp=I ntLP(tempvlb,vub);en d;if vlb( noti ntx(1,1),1)<=i ntx(n oti ntx(1,1),1)tempvub( not in tx(1,1),1)=i ntx(notin tx(1,1),1);ftemp=I ntLP(vlb,tempvub);en d;%==================================== 然后:clc;clearf=[4 4]A=[2 5;2 -2]b=[15;5]Aeq=[];beq=[];LB=[0 0];UB=[];[x n,yn ]=ILp(f,A,b,Aeq,beq,LB,UB,[1 1],1,[]) [x,fval,exitflag]=li nprog(f,A,b,Aeq,be q,L B,UB)结果:xn =0 0yn =Optimization terminated.1.0e-013 *0.2990040786747590.503948216933779fval =3.211809182434153e-013exitflag =1matlab的整数规划功能不行,还不如EXCEL的solver。
提问者评价貌似还是不能正确得出整数。