opt-prop.hpp algorithm /usr/include/gecode/int/cumulative.hh Gecode Gecode::Int Gecode::Int::Cumulative /*-*-mode:C++;c-basic-offset:2;indent-tabs-mode:nil-*-*/ /* *Mainauthors: *ChristianSchulte<schulte@gecode.org> *GuidoTack<tack@gecode.org> * *Copyright: *ChristianSchulte,2009 *GuidoTack,2010 * *Lastmodified: *$Date:2011-07-1322:44:50+0200(Wed,13Jul2011)$by$Author:tack$ *$Revision:12202$ * *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. * */ #include<algorithm> namespaceGecode{namespaceInt{namespaceCumulative{ template<classOptTask,classCap> forceinline OptProp<OptTask,Cap>::OptProp(Homehome,Capc0,TaskArray<OptTask>&t) :TaskProp<OptTask,Int::PC_INT_DOM>(home,t),c(c0){ c.subscribe(home,*this,PC_INT_BND); } template<classOptTask,classCap> forceinline OptProp<OptTask,Cap>::OptProp(Space&home,boolshared, OptProp<OptTask,Cap>&p) :TaskProp<OptTask,Int::PC_INT_DOM>(home,shared,p){ c.update(home,shared,p.c); } template<classOptTask,classCap> forceinlineExecStatus OptProp<OptTask,Cap>::post(Homehome,Capc,TaskArray<OptTask>&t){ //Capacitymustbenonnegative GECODE_ME_CHECK(c.gq(home,0)); //Checkforoverloadbysingletaskandremoveexcludedtasks intn=t.size(),m=0; for(inti=n;i--;){ if(t[i].c()>c.max()) GECODE_ME_CHECK(t[i].excluded(home)); if(t[i].excluded()) t[i]=t[--n]; elseif(t[i].mandatory()) m++; } t.size(n); if(t.size()<2){ if(t.size()==1){ if(t[0].mandatory()){ GECODE_ME_CHECK(c.gq(home,t[0].c())); returnES_OK; }elseif(c.min()>=t[0].c()){ returnES_OK; } }else{ returnES_OK; } } if(c.assigned()&&c.val()==1){ TaskArray<typename TaskTraits<OptTask>::UnaryTask>mt(home,t.size()); for(inti=t.size();i--;) mt[i]=t[i]; returnUnary::OptProp<typename TaskTraits<OptTask>::UnaryTask> ::post(home,mt); } if(m==t.size()){ TaskArray<typename TaskTraits<OptTask>::ManTask>mt(home,m); for(inti=m;i--;) mt[i].init(t[i]); returnManProp<typename TaskTraits<OptTask>::ManTask,Cap> ::post(home,c,mt); } (void)new(home)OptProp<OptTask,Cap>(home,c,t); returnES_OK; } template<classOptTask,classCap> Actor* OptProp<OptTask,Cap>::copy(Space&home,boolshare){ returnnew(home)OptProp<OptTask,Cap>(home,share,*this); } template<classOptTask,classCap> forceinlinesize_t OptProp<OptTask,Cap>::dispose(Space&home){ (void)TaskProp<OptTask,Int::PC_INT_DOM>::dispose(home); c.cancel(home,*this,PC_INT_BND); returnsizeof(*this); } template<classOptTask,classCap> ExecStatus OptProp<OptTask,Cap>::propagate(Space&home,constModEventDelta&med){ //DidoneoftheBooleanviewschange? if(Int::BoolView::me(med)==Int::ME_BOOL_VAL) GECODE_ES_CHECK((purge<OptTask,Int::PC_INT_DOM>(home,*this,t,c))); //Onlyboundschanges? if(Int::IntView::me(med)!=Int::ME_INT_DOM) GECODE_ES_CHECK(overload(home,c.max(),t)); boolsubsumed; GECODE_ES_CHECK(basic(home,subsumed,c,t)); if(subsumed) returnhome.ES_SUBSUMED(*this); //Partitionintomandatoryandoptionalactivities intn=t.size(); inti=0,j=n-1; while(true){ while((i<n)&&t[i].mandatory())i++; while((j>=0)&&!t[j].mandatory())j--; if(i>=j)break; std::swap(t[i],t[j]); } if(i>1){ //Truncatearraytoonlycontainmandatorytasks t.size(i); GECODE_ES_CHECK(edgefinding(home,c.max(),t)); //Restoretoalsoincludeoptionaltasks t.size(n); } if(Cap::varderived()&&c.assigned()&&c.val()==1){ //Checkthattasksdonotoverloadresource for(inti=t.size();i--;) if(t[i].c()>1) GECODE_ME_CHECK(t[i].excluded(home)); //Rewritetounaryresourceconstraint TaskArray<typename TaskTraits<OptTask>::UnaryTask>ut(home,t.size()); for(inti=t.size();i--;) ut[i]=t[i]; GECODE_REWRITE(*this, (Unary::OptProp<typenameTaskTraits<OptTask>::UnaryTask> ::post(home(*this),ut))); }else{ returnES_NOFIX; } } }}} //STATISTICS:int-prop