function[errmsg,Z,X,t,c,fail]=BNB18(fun,x0,xstat,xl,xu,A,B,Aeq,Beq,nonlcon,setts,options1,options2,maxSQPit,varargin);%·ÇÏßÐÔÕûÊý¹æ»®Ä£ÐÍÇó½â·ÖÖ§¶¨½çµü´úËã·¨¡£ÔÚMATLAB5.3ÖÐʹÓã¬ÐèOptimizationtoolbox2.0Ö§³Ö?%MinimizeF(x)%subjectto:xlb=x=xub%A*x=B%Aeq*x=Beq%C(x)=0%Ceq(x)=0%%x(i)¿ÉΪÁ¬Ðø±äÁ¿£¬ÕûÊý£¬»ò¹Ì¶¨Öµ%ʹÓøñʽ%[errmsg,Z,X]=BNB18('fun',x0,xstat,xl,xu,A,B,Aeq,Beq,'nonlcon',setts)%fun£ºMÎļþÃû£¬±íʾ×îС»¯Ä¿±êº¯Êýf=fun(x)%x0:ÁÐÏòÁ¿£¬±íʾ±äÁ¿³õÖµ%xstat£ºÁÐÏòÁ¿£¬xstat(i)=0±íʾx(i)ΪÁ¬Ðø±äÁ¿£¬1±íʾÕûÊý£¬2±íʾ¹Ì¶¨Öµ%xl£ºÁÐÏòÁ¿£¬±íʾ±äÁ¿Ï½ç%xu:ÁÐÏòÁ¿£¬±íʾ±äÁ¿ÉϽç%A:¾ØÕó,±íʾÏßÐÔ²»µÈʽԼÊøϵÊý%B:ÁÐÏòÁ¿,±íʾÏßÐÔ²»µÈʽԼÊøÉϽç%Aeq:¾ØÕó,±íʾÏßÐÔµÈʽԼÊøϵÊý%Beg:ÁÐÏòÁ¿,±íʾÏßÐÔ²»µÈʽԼÊøÓÒ¶ËÖµ%nonlcon:MÎļþÃû£¬±íʾ·ÇÏßÐÔÔ¼Êøº¯Êý[C,Ceq]=nonlin(x),ÆäÖÐC(x)Ϊ²»µÈʽԼÊø,%Ceq(x)ΪµÈʽԼÊø%setts:Ëã·¨ÉèÖÃ%errmsq:·µ»Ø´íÎóÌáʾ%Z:·µ»ØÄ¿±êº¯Êý×îСֵ%X:·µ»Ø×îÓŽâ%%ÀýÌâ%maxx1*x2*x3%-x1+2*x2+2*x3=0%x1+2*x2+2*x3=72%10=x2=20%x1-x2=10%ÏÈдMº¯Êýdiscfun.m%functionf=discfun(x)%f=-x(1)*x(2)*x(3);%Çó½â%clear;x0=[25,15,10]';xstat=[111]';%xl=[2010-10]';xu=[302020]';%A=[1-2-2;122];B=[072]';Aeq=[1-10];Beq=10;%[err,Z,X]=BNB18('discfun',x0,xstat,xl,xu,A,B,Aeq,Beq);%XMAX=X',ZMAX=-Z%%BNB18Findstheconstrainedminimumofafunctionofseveralpossiblyintegervariables.%Usage:[errmsg,Z,X,t,c,fail]=%BNB18(fun,x0,xstatus,xlb,xub,A,B,Aeq,Beq,nonlcon,settings,options1,options2,maxSQPiter,P1,P2,...)%%BNBsolvesproblemsoftheform:%MinimizeF(x)subjectto:xlb=x0=xub%A*x=BAeq*x=Beq%C(x)=0Ceq(x)=0%x(i)iscontinuousforxstatus(i)=0%x(i)integerforxstatus(i)=1%x(i)fixedforxstatus(i)=2%%BNBuses:%OptimizationToolboxVersion2.0(R11)09-Oct-1998%Fromthistoolboxfmincon.miscalled.Formoreinfotypehelpfmincon.%%funisthefunctiontobeminimizedandshouldreturnascalar.F(x)=feval(fun,x).%x0isthestartingpointforx.x0shouldbeacolumnvector.%xstatusisacolumnvectordescribingthestatusofeveryvariablex(i).%xlbandxubarecolumnvectorswithlowerandupperboundsforx.%AandAeqarematricesforthelinearconstrains.%BandBeqarecolumnvectorsforthelinearconstrains.%nonlconisthefunctionforthenonlinearconstrains.%[C(x);Ceq(x)]=feval(nonlcon,x).BothC(x)andCeq(x)shouldbecolumnvectors.%%errmsgisastringcontaininganerrormessageifBNBfoundanerrorintheinput.%Zisthescalarresultoftheminimization,Xthevaluesoftheaccompanyingvariables.%tisthetimeelapsedwhilethealgorithmBNBhasrun,cisthenumberofBNBcyclesand%failisthenumberofunsolvedleafsub-problems.%%settingsisarowvectorwithsettingsforBNB:%settings(1)(standard0)if1:usephase1byrelaxation.Thissometimesmakesthealgorithm%faster,becausephase1meansthealgorithmfirstchecksifthereisafeasiblesolution%forasub-problembeforetryingtofindabestsolution.IfthereisnofeasiblesolutionBNB%willnottrytofindabestsolution.%settings(2)(standard0)if1:ifthesub-problemdidnotconvergedonotbranch.Ifasub-%problemdidnotconvergethismeansBNBdidnotfindasolutionforit.NormallyBNBwill%branchtheproblemsoitcantryagaintofindasolution.%Asub-problemthatisaleafofthebranch-and-bound-threecannotbebranched.Ifsuch%aproblemdoesnotconvergeitwillbeconsideredunfeasibleandtheparameterfailwillbe%raisedbyone.%settings(3)(standard0)if1:if1asub-problemthatdidnotconvergebutdidreturnafeasible%pointwillbeconsideredconvergent.Thismightbeusefuliffminconishavingahardtimewith%acertainproblembutyoudowantsomeresults.%options1andoptions2areoptionsstructuresforphase1andphase2.%Fordetailsabouttheoptionsstructuretypehelpoptimset.%maxSQPiterisaglobalvariableusedbyfmincon(ifmodifiedasdescribedinbnb18.m).%maxSQPiteris1000bydefault.%P1,P2,...areparameterstobepassedtofunandnonlcon.%F(x)=feval(fun,x,P1,P2,...).[C(x);Ceq(x)]=feval(nonlcon,x,P1,P2,...).%TypeeditBNB18formoreinfo.%E.C.Kuipers%e-mailE.C.Kuipers@cpedu.rug.nl%FI-Lab%AppliedPhysics%RijksuniversiteitGroningen%Togetridofbugsandtostopfminconfromhangingmakethefollowingchances:%%Inoptim/private/nlconst.m($Revision:1.20$$Date:1998/08/2413:46:15$):%GetEXITFLAGindependentofverbosity.%Afterthelines:disp('lessthan2*options.TolFunbutconstraintsarenotsatisfied.')%end%EXITFLAG=-1;%end%end%status=1;%addtheline:if(strncmp(howqp,'i',1)&mg0),EXITFLAG=-1;end;%%Inoptim/private/qpsub.m($Revision:1.21$$Date:1998/09/0121:37:56$):%Stopqpsubfromhanging.%Aftertheline:%AndyGrace7-9-90.MaryAnnBranch9-30-96.%addtheline:globalmaxSQPiter;%andchangedtheline:maxSQPiters=Inf;%totheline:ifexist('maxSQPiter','var'),maxSQPiters=maxSQPiter;elsemaxSQPiters=inf;end;%IguesstherewasareasontoputmaxSQPitersatinfinity,butthisworksfineforme.globalmaxSQPiter;%STEP0CHECKINGINPUTZ=[];X=[];t=0;c=0;fail=0;ifnargin2,errmsg='BNBneedsatleast2inputarguments.';return;end;ifisempty(fun),errmsg='Nofunfound.';return;end;ifisempty(x0),errmsg='Nox0found.';return;elseifsize(x0,2)1,errmsg='x0mustbeacolumnvector.';return;end;xstatus=zeros(size(x0));ifnargin2&~isempty(xstat)ifall(size(xstat)=size(x0))xstatus(1:size(xstat))=xstat;elseerrmsg='xstatusmustbeacolumnvectorthesamesizeasx0.';return;end;ifany(xstatus~=round(xstatus)|xstatus0|2xstatus)errmsg='xstatusmustconsistoftheintegers0,1en2.';return;end;end;xlb=zeros(size(x0));xlb(find(xstatus==0))=-i