brancher-view-sel.hpp /usr/include/gecode/kernel.hh Gecode::ViewSel Gecode::ViewSelNone Gecode::ViewSelRnd Gecode::ChooseMin Gecode::ChooseMax Gecode::ViewSelChoose Gecode::ViewSelChooseTbl Gecode::ViewSelMin Gecode::ViewSelMinTbl Gecode::ViewSelMax Gecode::ViewSelMaxTbl Gecode /*-*-mode:C++;c-basic-offset:2;indent-tabs-mode:nil-*-*/ /* *Mainauthor: *ChristianSchulte<schulte@gecode.org> * *Copyright: *ChristianSchulte,2012 * *Lastmodified: *$Date:2012-10-3007:46:12+0100(Tue,30Oct2012)$by$Author:tack$ *$Revision:13166$ * *ThisfileispartofGecode,thegenericconstraint *developmentenvironment: *http://www.gecode.org * *Permissionisherebygranted,freeofcharge,toanypersonobtaining *acopyofthissoftwareandassociateddocumentationfiles(the *"Software"),todealintheSoftwarewithoutrestriction,including *withoutlimitationtherightstouse,copy,modify,merge,publish, *distribute,sublicense,and/orsellcopiesoftheSoftware,andto *permitpersonstowhomtheSoftwareisfurnishedtodoso,subjectto *thefollowingconditions: * *Theabovecopyrightnoticeandthispermissionnoticeshallbe *includedinallcopiesorsubstantialportionsoftheSoftware. * *THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND, *EXPRESSORIMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOF *MERCHANTABILITY,FITNESSFORAPARTICULARPURPOSEAND *NONINFRINGEMENT.INNOEVENTSHALLTHEAUTHORSORCOPYRIGHTHOLDERSBE *LIABLEFORANYCLAIM,DAMAGESOROTHERLIABILITY,WHETHERINANACTION *OFCONTRACT,TORTOROTHERWISE,ARISINGFROM,OUTOFORINCONNECTION *WITHTHESOFTWAREORTHEUSEOROTHERDEALINGSINTHESOFTWARE. * */ namespaceGecode{ template<class_View> classViewSel{ public: typedef_ViewView; typedeftypenameBranchTraits<typename View::VarType>::FilterBranchFilter; ViewSel(Space&home,constVarBranch&vb); ViewSel(Space&home,boolshared,ViewSel<View>&vs); virtualintselect(Space&home,ViewArray<View>&x,ints)=0; virtualintselect(Space&home,ViewArray<View>&x,ints, BranchFilterbf)=0; virtualvoidties(Space&home,ViewArray<View>&x,ints, int*ties,int&n)=0; virtualvoidties(Space&home,ViewArray<View>&x,ints, int*ties,int&n,BranchFilterbf)=0; virtualvoidbrk(Space&home,ViewArray<View>&x, int*ties,int&n)=0; virtualintselect(Space&home,ViewArray<View>&x, int*ties,intn)=0; virtualViewSel<View>*copy(Space&home,boolshared)=0; virtualboolnotice(void)const; virtualvoiddispose(Space&home); staticvoid*operatornew(size_ts,Space&home); staticvoidoperatordelete(void*p,Space&home); staticvoidoperatordelete(void*p); }; template<classView> classViewSelNone:publicViewSel<View>{ typedeftypenameViewSel<View>::BranchFilterBranchFilter; public: ViewSelNone(Space&home,constVarBranch&vb); ViewSelNone(Space&home,boolshared,ViewSelNone<View>&vs); virtualintselect(Space&home,ViewArray<View>&x,ints); virtualintselect(Space&home,ViewArray<View>&x,ints, BranchFilterbf); virtualvoidties(Space&home,ViewArray<View>&x,ints, int*ties,int&n); virtualvoidties(Space&home,ViewArray<View>&x,ints, int*ties,int&n, BranchFilterbf); virtualvoidbrk(Space&home,ViewArray<View>&x, int*ties,int&n); virtualintselect(Space&home,ViewArray<View>&x,int*ties,intn); virtualViewSel<View>*copy(Space&home,boolshared); }; template<classView> classViewSelRnd:publicViewSel<View>{ typedeftypenameViewSel<View>::BranchFilterBranchFilter; protected: Rndr; public: ViewSelRnd(Space&home,constVarBranch&vb); ViewSelRnd(Space&home,boolshared,ViewSelRnd<View>&vs); virtualintselect(Space&home,ViewArray<View>&x,ints); virtualintselect(Space&home,ViewArray<View>&x,ints, BranchFilterbf); virtualvoidties(Space&home,ViewArray<View>&x,ints, int*ties,int&n); virtualvoidties(Space&home,ViewArray<View>&x,ints, int*ties,int&n,BranchFilterbf); virtualvoidbrk(Space&home,ViewArray<View>&x,int*ties,int&n); virtualintselect(Space&home,ViewArray<View>&x,int*ties,intn); virtualViewSel<View>*copy(Space&home,boolshared); }; classChooseMin{ public: template<classVal> booloperator ()(Vala,Valb)const; }; classChooseMax{ public: template<classVal> booloperator ()(Vala,Valb)const; }; template<classChoose,classMerit> classViewSelChoose:publicViewSel<typenameMerit::View>{ protected: typedeftypenameViewSel<typename Merit::View>::ViewView; typedeftypenameViewSel<typename Merit::View>::BranchFilterBranchFilter; typedeftypenameMerit::ValVal; Choosec; Meritm; public: ViewSelChoose(Space&home,constVarBranch&vb); ViewSelChoose(Space&home,boolshared,ViewSelChoose<Choose,Merit>&vs); virtualintselect(Space&home,ViewArray<View>&x,ints); virtualintselect(Space&home,ViewArray<View>&x,ints, BranchFilterbf); virtualvoidties(Space&home,ViewArray<View>&x,ints, int*ties,int&n); virtualvoidties(Space&home,ViewArray<View>&x,ints, int*ties,int&n,BranchFilterbf); virtualvoidbrk(Space&home,ViewArray<View>&x,int*ties,int&n); virtualintselect(Space&home,ViewArray<View>&x,int*ties,intn); virtualboolnotice(void)const; virtualvoiddispose(Space&home); }; template<classChoose,classMerit> classViewSelChooseTbl:publicViewSelChoose<Choose,Merit>{ protected: typedeftypenameViewSelChoose<Choose,Merit>::ValVal; typedeftypenameViewSelChoose<Choose,Merit>::ViewView; typedeftypenameViewSelChoose<Choose,Merit>::BranchFilterBranchFilter; usingViewSelChoose<Choose,Merit>::c; usingViewSelChoose<Choose,Merit>::m; BranchTbltbl; public: ViewSelChooseTbl(Space&home,constVarBranch&vb); ViewSelChooseTbl(Space&home,boolshared, ViewSelChooseTbl<Choose,Merit>&vs); virtualvoidties(Space&home,ViewArray<View>&x,ints, int*ties,int&n); virtualvoidties(Space&home,ViewArray<View>&x,ints, int*ties,int&n,BranchFilterbf); virtualvoidbrk(Space&home,ViewArray<View>&x,int*ties,int&n); }; template<classMerit> classViewSelMin:publicViewSelChoose<ChooseMin,Merit>{ typedeftypenameViewSelChoose<ChooseMin,Merit>::ViewView; typedeftypenameViewSelChoose<ChooseMin,Merit>::BranchFilterBranchFilter; public: ViewSelMin(Space&home,constVarBranch&vb); ViewSelMin(Space&home,boolshared,ViewSelMin<Merit>&vs); virtualViewSel<View>*copy(Space&home,boolshared); }; template<classMerit> classViewSelMinTbl:publicViewSelChooseTbl<ChooseMin,Merit>{ typedeftypenameViewSelChooseTbl<ChooseMin,Merit>::ViewView; typedeftypenameViewSelChooseTbl<ChooseMin,Merit>::BranchFilterBranchFilter; public: ViewSelMinTbl(Space&home,constVarBranch&vb); ViewSelMinTbl(Space&home,boolshared,ViewSelMinTbl<Merit>&vs); virtualViewSel<View>*copy(Space&home,boolshared); }; template<classMerit> classViewSelMax:publicViewSelChoose<ChooseMax,Merit>{ typedeftypenameViewSelChoose<ChooseMax,Merit>::ViewView; typedeftypenameViewSelChoose<ChooseMax,Merit>::BranchFilterBranchFilter; public: ViewSelMax(Space&home,constVarBranch&vb); ViewSelMax(Space&home,boolshared,ViewSelMax<Merit>&vs); virtualViewSel<View>*copy(Space&home,boolshared); }; template<classMerit> classViewSelMaxTbl:publicViewSelChooseTbl<ChooseMax,Merit>{ typedeftypenameViewSelChooseTbl<ChooseMax,Merit>::ViewView; typedeftypenameViewSelChooseTbl<ChooseMax,Merit>::BranchFilterBranchFilter; public: ViewSelMaxTbl(Space&home,constVarBranch&vb); ViewSelMaxTbl(Space&home,boolshared,ViewSelMaxTbl<Merit>&vs); virtualViewSel<View>*copy(Space&home,boolshared); }; template<classView> forceinline ViewSel<View>::ViewSel(Space&,constVarBranch&){} template<classView> forceinline ViewSel<View>::ViewSel(Space&,bool,ViewSel<View>&){} template<classView> bool ViewSel<View>::notice(void)const{ returnfalse; } template<classView> void ViewSel<View>::dispose(Space&){} template<classView> forceinlinevoid ViewSel<View>::operatordelete(void*){} template<classView> forceinlinevoid ViewSel<View>::operatordelete(void*,Space&){} template<classView> forceinlinevoid* ViewSel<View>::operatornew(size_ts,Space&home){ returnhome.ralloc(s); } template<classView> forceinline ViewSelNone<View>::ViewSelNone(Space&home,constVarBranch&vb) :ViewSel<View>(home,vb){} template<classView> forceinline ViewSelNone<View>::ViewSelNone(Space&home,boolshared, ViewSelNone<View>&vs) :ViewSel<View>(home,shared,vs){} template<classView> int ViewSelNone<View>::select(Space&,ViewArray<View>&,ints){ returns; } template<classView> int ViewSelNone<View>::select(Space&,ViewArray<View>&,ints,BranchFilter){ returns; } template<classView> void ViewSelNone<View>::ties(Space&,ViewArray<View>&x,ints, int*ties,int&n){ intj=0;ties[j++]=s; for(inti=s+1;i<x.size();i++) if(!x[i].assigned()) ties[j++]=i; n=j; assert(n>0); } template<classView> void ViewSelNone<View>::ties(Space&home,ViewArray<View>&x,ints, int*ties,int&n,BranchFilterbf){ intj=0;ties[j++]=s; for(inti=s+1;i<x.size();i++){ typenameView::VarTypey(x[i].varimp()); if(!x[i].assigned()&&bf(home,y,i)) ties[j++]=i; } n=j; assert(n>0); } template<classView> void ViewSelNone<View>::brk(Space&,ViewArray<View>&,int*,int&){ //Nothingneedstobedone } template<classView> int ViewSelNone<View>::select(Space&,ViewArray<View>&,int*ties,int){ returnties[0]; } template<classView> ViewSel<View>* ViewSelNone<View>::copy(Space&home,boolshared){ returnnew(home)ViewSelNone<View>(home,shared,*this); } template<classView> forceinline ViewSelRnd<View>::ViewSelRnd(Space&home,constVarBranch&vb) :ViewSel<View>(home,vb),r(vb.rnd()){} template<classView> forceinline ViewSelRnd<View>::ViewSelRnd(Space&home,boolshared,ViewSelRnd<View>&vs) :ViewSel<View>(home,shared,vs),r(vs.r){} template<classView> int ViewSelRnd<View>::select(Space&,ViewArray<View>&x,ints){ unsignedintn=1; intj=s; for(inti=s+1;i<x.size();i++) if(!x[i].assigned()){ n++; if(r(n)==0U) j=i; } returnj; } template<classView> intViewSelRnd<View>::select(Space&home,ViewArray<View>&x,ints, BranchFilterbf){ unsignedintn=1; intj=s; for(inti=s+1;i<x.size();i++){ typenameView::VarTypey(x[i].varimp()); if(!x[i].assigned()&&bf(home,y,i)){ n++; if(r(n)==0U) j=i; } } returnj; } template<classView> void ViewSelRnd<View>::ties(Space&home,ViewArray<View>&x,ints, int*ties,int&n){ n=1;ties[0]=select(home,x,s); } template<classView> void ViewSelRnd<View>::ties(Space&home,ViewArray<View>&x,ints, int*ties,int&n,BranchFilterbf){ n=1;ties[0]=select(home,x,s,bf); } template<classView> void ViewSelRnd<View>::brk(Space&,ViewArray<View>&,int*ties,int&n){ ties[0]=ties[static_cast<int>(r(static_cast<unsignedint>(n)))]; n=1; } template<classView> int ViewSelRnd<View>::select(Space&,ViewArray<View>&,int*ties,intn){ returnties[static_cast<int>(r(static_cast<unsignedint>(n)))]; } template<classView> ViewSel<View>* ViewSelRnd<View>::copy(Space&home,boolshared){ returnnew(home)ViewSelRnd<View>(home,shared,*this); } template<classVal> forceinlinebool ChooseMin::operator ()(Vala,Valb)const{ returna<b; } template<classVal> forceinlinebool ChooseMax::operator ()(Vala,Valb)const{ returna>b; } template<classChoose,classMerit> forceinline ViewSelChoose<Choose,Merit>::ViewSelChoose(Space&home,constVarBranch&vb) :ViewSel<View>(home,vb),m(home,vb){} template<classChoose,classMerit> forceinline ViewSelChoose<Choose,Merit>::ViewSelChoose(Space&home,boolshared, ViewSelChoose<Choose,Merit>&vs) :ViewSel<View>(home,shared,vs),m(home,shared,vs.m){} template<classChoose,classMerit> int ViewSelChoose<Choose,Merit>::select(Space&home,ViewArray<View>&x,ints){ //Considerx[s]astheso-farbestview intb_i=s; Valb_m=m(home,x[s],s); //Scanallnon-assignedviewsfroms+1onwards for(inti=s+1;i<x.size();i++) if(!x[i].assigned()){ Valmxi=m(home,x[i],i); if(c(mxi,b_m)){ b_i=i;b_m=mxi; } } returnb_i; } template<classChoose,classMerit> int ViewSelChoose<Choose,Merit>::select(Space&home,ViewArray<View>&x,ints, BranchFilterbf){ //Considerx[s]astheso-farbestview intb_i=s; Valb_m=m(home,x[s],s); //Scanallassignedviewsfroms+1onwards for(inti=s+1;i<x.size();i++){ typenameView::VarTypey(x[i].varimp()); if(!x[i].assigned()&&bf(home,y,i)){ Valmxi=m(home,x[i],i); if(c(mxi,b_m)){ b_i=i;b_m=mxi; } } } returnb_i; } template<classChoose,classMerit> void ViewSelChoose<Choose,Merit>::ties(Space&home,ViewArray<View>&x,ints, int*ties,int&n){ //Considerx[s]astheso-farbestviewandrecordastie Valb=m(home,x[s],s); intj=0;ties[j++]=s; for(inti=s+1;i<x.size();i++) if(!x[i].assigned()){ Valmxi=m(home,x[i],i); if(c(mxi,b)){ //Foundabetterone,resetalltiesandrecord j=0;ties[j++]=i;b=mxi; }elseif(mxi==b){ //Foundatie,record ties[j++]=i; } } n=j; //Theremustbeatleastonetie,ofcourse! assert(n>0); } template<classChoose,classMerit> void ViewSelChoose<Choose,Merit>::ties(Space&home,ViewArray<View>&x,ints, int*ties,int&n,BranchFilterbf){ //Considerx[s]astheso-farbestviewandrecordastie Valb=m(home,x[s],s); intj=0;ties[j++]=s; for(inti=s+1;i<x.size();i++){ typenameView::VarTypey(x[i].varimp()); if(!x[i].assigned()&&bf(home,y,i)){ Valmxi=m(home,x[i],i); if(c(mxi,b)){ //Foundabetterone,resetalltiesandrecord j=0;ties[j++]=i;b=mxi; }elseif(mxi==b){ //Foundatie,record ties[j++]=i; } } } n=j; //Theremustbeatleastonetie,ofcourse! assert(n>0); } template<classChoose,classMerit> void ViewSelChoose<Choose,Merit>::brk(Space&home,ViewArray<View>&x, int*ties,int&n){ //Keepfirsttieinplace Valb=m(home,x[ties[0]],ties[0]); intj=1; //Scanremainingties for(inti=1;i<n;i++){ Valmxi=m(home,x[ties[i]],ties[i]); if(c(mxi,b)){ //Foundabetterone,resetallties b=mxi;j=0;ties[j++]=ties[i]; }elseif(mxi==b){ //Foundatieandrecordit ties[j++]=ties[i]; } } n=j; //Theremustbeatleastonetie,ofcourse! assert(n>0); } template<classChoose,classMerit> int ViewSelChoose<Choose,Merit>::select(Space&home,ViewArray<View>&x, int*ties,intn){ intb_i=ties[0]; Valb_m=m(home,x[ties[0]],ties[0]); for(inti=1;i<n;i++){ Valmxi=m(home,x[ties[i]],ties[i]); if(c(mxi,b_m)){ b_i=ties[i];b_m=mxi; } } returnb_i; } template<classChoose,classMerit> bool ViewSelChoose<Choose,Merit>::notice(void)const{ returnm.notice(); } template<classChoose,classMerit> void ViewSelChoose<Choose,Merit>::dispose(Space&home){ m.dispose(home); } template<classChoose,classMerit> forceinline ViewSelChooseTbl<Choose,Merit>::ViewSelChooseTbl(Space&home, constVarBranch&vb) :ViewSelChoose<Choose,Merit>(home,vb),tbl(vb.tbl()){} template<classChoose,classMerit> forceinline ViewSelChooseTbl<Choose,Merit>::ViewSelChooseTbl (Space&home,boolshared, ViewSelChooseTbl<Choose,Merit>&vs) :ViewSelChoose<Choose,Merit>(home,shared,vs),tbl(vs.tbl){} template<classChoose,classMerit> void ViewSelChooseTbl<Choose,Merit>::ties(Space&home,ViewArray<View>&x,ints, int*ties,int&n){ //Findtheworstandbestmeritvalue Valw=m(home,x[s],s); Valb=w; for(inti=s+1;i<x.size();i++) if(!x[i].assigned()){ Valmxi=m(home,x[i],i); if(c(mxi,b)) b=mxi; elseif(c(w,mxi)) w=mxi; } //Computetie-breaklimit doublel=tbl(home,static_cast<double>(w),static_cast<double>(b)); //Ifthelimitisnotbetterthantheworstmerit,everythingisatie if(!c(l,static_cast<double>(w))){ intj=0; for(inti=s;i<x.size();i++) if(!x[i].assigned()) ties[j++]=i; n=j; }else{ //Thelimitisnotallowedtobetterthanthebestmeritvalue if(c(l,static_cast<double>(b))) l=static_cast<double>(b); //Recordalltiesthatarenotworsethanthelimitmeritvalue intj=0; for(inti=s;i<x.size();i++) if(!x[i].assigned()&&!c(l,static_cast<double>(m(home,x[i],i)))) ties[j++]=i; n=j; } //Therewillbeatleastonetie(thebestwillqualify,ofcourse) assert(n>0); } template<classChoose,classMerit> void ViewSelChooseTbl<Choose,Merit>::ties(Space&home,ViewArray<View>&x,ints, int*ties,int&n,BranchFilterbf){ //Findtheworstandbestmeritvalue Valw=m(home,x[s],s); Valb=w; for(inti=s+1;i<x.size();i++){ typenameView::VarTypey(x[i].varimp()); if(!x[i].assigned()&&bf(home,y,i)){ Valmxi=m(home,x[i],i); if(c(mxi,b)) b=mxi; elseif(c(w,mxi)) w=mxi; } } //Computetie-breaklimit doublel=tbl(home,static_cast<double>(w),static_cast<double>(b)); //Ifthelimitisnotbetterthantheworstmerit,everythingisatie if(!c(l,static_cast<double>(w))){ intj=0; for(inti=s;i<x.size();i++){ typenameView::VarTypey(x[i].varimp()); if(!x[i].assigned()&&bf(home,y,i)) ties[j++]=i; } n=j; }else{ //Thelimitisnotallowedtobetterthanthebestmeritvalue if(c(l,static_cast<double>(b))) l=static_cast<double>(b); //Recordalltiesthatarenotworsethanthelimitmeritvalue intj=0; for(inti=s;i<x.size();i++){ typenameView::VarTypey(x[i].varimp()); if(!x[i].assigned()&&bf(home,y,i)&& !c(l,static_cast<double>(m(home,x[i],i)))) ties[j++]=i; } n=j; } //Therewillbeatleastonetie(thebestwillqualify,ofcourse) assert(n>0); } template<classChoose,classMerit> void ViewSelChooseTbl<Choose,Merit>::brk(Space&home,ViewArray<View>&x, int*ties,int&n){ //Findtheworstandbestmeritvalue Valw=m(home,x[ties[0]],ties[0]); Valb=w; for(inti=1;i<n;i++){ Valmxi=m(home,x[ties[i]],ties[i]); if(c(mxi,b)) b=mxi; elseif(c(w,mxi)) w=mxi; } //Computetie-breaklimit doublel=tbl(home,static_cast<double>(w),static_cast<double>(b)); //Ifthelimitisnotbetterthantheworstmerit,everythingisatie //andnobreakingisrequired if(c(l,static_cast<double>(w))){ //Thelimitisnotallowedtobetterthanthebestmeritvalue if(c(l,static_cast<double>(b))) l=static_cast<double>(b); //Keepalltiesthatarenotworsethanthelimitmeritvalue intj=0; for(inti=0;i<n;i++) if(!c(l,static_cast<double>(m(home,x[ties[i]],ties[i])))) ties[j++]=ties[i]; n=j; } //Therewillbeatleastonetie(thebestwillqualify) assert(n>0); } template<classMerit> forceinline ViewSelMin<Merit>::ViewSelMin(Space&home,constVarBranch&vb) :ViewSelChoose<ChooseMin,Merit>(home,vb){} template<classMerit> forceinline ViewSelMin<Merit>::ViewSelMin(Space&home,boolshared, ViewSelMin<Merit>&vs) :ViewSelChoose<ChooseMin,Merit>(home,shared,vs){} template<classMerit> ViewSel<typename ViewSelMin<Merit>::View>* ViewSelMin<Merit>::copy(Space&home,boolshared){ returnnew(home)ViewSelMin<Merit>(home,shared,*this); } template<classMerit> forceinline ViewSelMinTbl<Merit>::ViewSelMinTbl(Space&home,constVarBranch&vb) :ViewSelChooseTbl<ChooseMin,Merit>(home,vb){} template<classMerit> forceinline ViewSelMinTbl<Merit>::ViewSelMinTbl(Space&home,boolshared, ViewSelMinTbl<Merit>&vs) :ViewSelChooseTbl<ChooseMin,Merit>(home,shared,vs){} template<classMerit> ViewSel<typename ViewSelMinTbl<Merit>::View>* ViewSelMinTbl<Merit>::copy(Space&home,boolshared){ returnnew(home)ViewSelMinTbl<Merit>(home,shared,*this); } template<classMerit> forceinline ViewSelMax<Merit>::ViewSelMax(Space&home,constVarBranch&vb) :ViewSelChoose<ChooseMax,Merit>(home,vb){} template<classMerit> forceinline ViewSelMax<Merit>::ViewSelMax(Space&home,boolshared, ViewSelMax<Merit>&vs) :ViewSelChoose<ChooseMax,Merit>(home,shared,vs){} template<classMerit> ViewSel<typename ViewSelMax<Merit>::View>* ViewSelMax<Merit>::copy(Space&home,boolshared){ returnnew(home)ViewSelMax<Merit>(home,shared,*this); } template<classMerit> forceinline ViewSelMaxTbl<Merit>::ViewSelMaxTbl(Space&home,constVarBranch&vb) :ViewSelChooseTbl<ChooseMax,Merit>(home,vb){} template<classMerit> forceinline ViewSelMaxTbl<Merit>::ViewSelMaxTbl(Space&home,boolshared, ViewSelMaxTbl<Merit>&vs) :ViewSelChooseTbl<ChooseMax,Merit>(home,shared,vs){} template<classMerit> ViewSel<typename ViewSelMaxTbl<Merit>::View>* ViewSelMaxTbl<Merit>::copy(Space&home,boolshared){ returnnew(home)ViewSelMaxTbl<Merit>(home,shared,*this); } } //STATISTICS:kernel-branch