|
楼主 |
发表于 2006-8-3 19:45:43
|
显示全部楼层
if any(x0<xlb), errmsg='x0 must be in the range xlb <= x0.'; return;<br/>elseif any(xstatus==1 & (~isfinite(xlb) | xlb~=round(xlb)))<br/> errmsg='xlb(i) must be an integer if x(i) is an integer variabele.'; return;<br/>end;<br/>xlb(find(xstatus==2))=x0(find(xstatus==2));<br/>xub=ones(size(x0));<br/>xub(find(xstatus==0))=inf;<br/>if nargin>4 & ~isempty(xu)<br/> if isnumeric(xu) & isreal(xu) & all(size(xu)<=size(x0))<br/> xub(1:size(xu,1))=xu;<br/> else errmsg='xub must be a real column vector the same size as x0.'; return; end;<br/>end;<br/>if any(x0>xub), errmsg='x0 must be in the range x0 <=xub.'; return;<br/>elseif any(xstatus==1 & (~isfinite(xub) | xub~=round(xub)))<br/> errmsg='xub(i) must be an integer if x(i) is an integer variabale.'; return;<br/>end;<br/>xub(find(xstatus==2))=x0(find(xstatus==2));<br/>A=[];<br/>if nargin>5 & ~isempty(a)<br/> if isnumeric(a) & isreal(a) & size(a,2)==size(x0,1), A=a;<br/> else errmsg='Matrix A not correct.'; return; end;<br/>end;<br/>B=[];<br/>if nargin>6 & ~isempty(b)<br/> if isnumeric(b) & isreal(b) & all(size(b)==[size(A,1) 1]), B=b;<br/> else errmsg='Column vector B not correct.'; return; end;<br/>end;<br/>if isempty(B) & ~isempty(A), B=zeros(size(A,1),1); end;<br/>Aeq=[];<br/>if nargin>7 & ~isempty(aeq)<br/> if isnumeric(aeq) & isreal(aeq) & size(aeq,2)==size(x0,1), Aeq=aeq;<br/> else errmsg='Matrix Aeq not correct.'; return; end;<br/>end;<br/>Beq=[];<br/>if nargin>8 & ~isempty(beq)<br/> if isnumeric(beq) & isreal(beq) & all(size(beq)==[size(Aeq,1) 1]), Beq=beq;<br/> else errmsg='Column vector Beq not correct.'; return; end;<br/>end;<br/>if isempty(Beq) & ~isempty(Aeq), Beq=zeros(size(Aeq,1),1); end;<br/>nonlcon='';<br/>if nargin>9 & ~isempty(nonlc)<br/> if ischar(nonlc), nonlcon=nonlc; <br/> else errmsg='fun must be a string.'; return; end;<br/>end;<br/>settings = [0 0];<br/>if nargin>10 & ~isempty(setts)<br/> if isnumeric(setts) & isreal(setts) & all(size(setts)<=size(settings))<br/> settings(setts~=0)=setts(setts~=0);<br/> else errmsg='settings should be a row vector of length 1 or 2.'; return; end;<br/>end;<br/>maxSQPiter=1000;<br/>options=optimset('fmincon');<br/>if nargin>11 & ~isempty(opts)<br/> if isstruct(opts)<br/> if isfield(opts,'MaxSQPIter')<br/> if isnumeric(opts.MaxSQPIter) & isreal(opts.MaxSQPIter) & ...<br/> all(size(opts.MaxSQPIter)==1) & opts.MaxSQPIter>0 & ...<br/> round(opts.MaxSQPIter)==opts.MaxSQPIter<br/> maxSQPiter=opts.MaxSQPIter;<br/> opts=rmfield(opts,'MaxSQPIter');<br/> else errmsg='options.maxSQPiter must be an integer >0.'; return; end;<br/> end;<br/> options=optimset(options,opts);<br/> else errmsg='options must be a structure.'; return; end;<br/>end;<br/>evalreturn=0;<br/>eval(['z=',fun,'(x0,varargin{:});'],'errmsg=''fun caused error.''; evalreturn=1;');<br/>if evalreturn==1, return; end;<br/>if ~isempty(nonlcon)<br/> eval(['[C, Ceq]=',nonlcon,'(x0,varargin{:});'],'errmsg=''nonlcon caused error.''; evalreturn=1;');<br/> if evalreturn==1, return; end;<br/> if size(C,2)>1 | size(Ceq,2)>1, errmsg='C en Ceq must be column vectors.'; return; end;<br/>end;<p>% STEP 1 INITIALISATION<br/>currentwarningstate=warning;<br/>warning off;<br/>tic;<br/>lx = size(x0,1);<br/>z_incumbent=inf;<br/>x_incumbent=inf*ones(size(x0));<br/>I = ceil(sum(log2(xub(find(xstatus==1))-xlb(find(xstatus==1))+1))+size(find(xstatus==1),1)+1);<br/>stackx0=zeros(lx,I);<br/>stackx0(:,1)=x0;<br/>stackxlb=zeros(lx,I);<br/>stackxlb(:,1)=xlb;<br/>stackxub=zeros(lx,I);<br/>stackxub(:,1)=xub;<br/>stackdepth=zeros(1,I);<br/>stackdepth(1,1)=1;<br/>stacksize=1;<br/>xchoice=zeros(size(x0));<br/>if ~isempty(Aeq)<br/> j=0;<br/> for i=1:size(Aeq,1)<br/> if Beq(i)==1 & all(Aeq(i,==0 | Aeq(i,==1)<br/> J=find(Aeq(i,==1);<br/> if all(xstatus(J)~=0 & xchoice(J)==0 & xlb(J)==0 & xub(J)==1)<br/> if all(xstatus(J)~=2) | all(x0(J(find(xstatus(J)==2)))==0)<br/> j=j+1;<br/> xchoice(J)=j;<br/> if sum(x0(J))==0, errmsg='x0 not correct.'; return; end;<br/> end;<br/> end;<br/> end;<br/> end;<br/>end;<br/>errx=optimget(options,'TolX');<br/>handleupdate=[];<br/>if ishandle(settings(2))<br/> taghandlemain=get(settings(2),'Tag');<br/> if strcmp(taghandlemain,'main BNB GUI')<br/> handleupdate=guiupd;<br/> handleupdatemsg=findobj(handleupdate,'Tag','updatemessage');<br/> bnbguicb('hide main');<br/> drawnow;<br/> end;<br/>end;<br/>optionsdisplay=getfield(options,'Display');<br/>if strcmp(optionsdisplay,'iter') | strcmp(optionsdisplay,'final')<br/> show=1;<br/>else show=0; end;</p><p>% STEP 2 TERMINIATION<br/>while stacksize>0<br/> c=c+1;<br/> <br/> % STEP 3 LOADING OF CSP<br/> x0=stackx0(:,stacksize);<br/> xlb=stackxlb(:,stacksize);<br/> xub=stackxub(:,stacksize);<br/> x0(find(x0<xlb))=xlb(find(x0<xlb));<br/> x0(find(x0>xub))=xub(find(x0>xub));<br/> depth=stackdepth(1,stacksize);<br/> stacksize=stacksize-1;<br/> percdone=round(100*(1-sum(0.5.^(stackdepth(1stacksize+1))-1))));<br/> <br/> % UPDATE FOR USER<br/> if ishandle(handleupdate) & strcmp(get(handleupdate,'Tag'),'update BNB GUI')<br/> t=toc;<br/> updatemsg={ ...<br/> sprintf('searched %3d %% of three',percdone) ...<br/> sprintf('Z : %12.4e',z_incumbent) ...<br/> sprintf('t : %12.1f secs',t) ...<br/> sprintf('c : %12d cycles',c-1) ...<br/> sprintf('fail : %12d cycles',fail)};<br/> set(handleupdatemsg,'String',updatemsg);<br/> drawnow;<br/> else<br/> disp(sprintf('*** searched %3d %% of three',percdone));<br/> disp(sprintf('*** Z : %12.4e',z_incumbent));<br/> disp(sprintf('*** t : %12.1f secs',t));<br/> disp(sprintf('*** c : %12d cycles',c-1));<br/> disp(sprintf('*** fail : %12d cycles',fail));<br/> end;<br/> <br/> </p> |
|