Generated on Tue Apr 18 10:22:05 2017 for Gecode by doxygen 1.6.3

brancher-view-sel.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main author:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2012
00008  *
00009  *  Last modified:
00010  *     $Date: 2017-04-01 20:27:10 +0200 (Sat, 01 Apr 2017) $ by $Author: schulte $
00011  *     $Revision: 15623 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 namespace Gecode {
00039 
00046 
00047   template<class _View>
00048   class ViewSel {
00049   public:
00051     typedef _View View;
00053     typedef typename View::VarType Var;
00055 
00056 
00057     ViewSel(Space& home, const VarBranch<Var>& vb);
00059     ViewSel(Space& home, bool shared, ViewSel<View>& vs);
00061 
00062 
00063 
00064     virtual int select(Space& home, ViewArray<View>& x, int s) = 0;
00066     virtual int select(Space& home, ViewArray<View>& x, int s,
00067                        BrancherFilter<View>& f) = 0;
00069     virtual int select(Space& home, ViewArray<View>& x, int s,
00070                        BrancherNoFilter<View>& f);
00072     virtual void ties(Space& home, ViewArray<View>& x, int s,
00073                       int* ties, int& n) = 0;
00075     virtual void ties(Space& home, ViewArray<View>& x, int s,
00076                       int* ties, int& n,
00077                       BrancherFilter<View>& f) = 0;
00079     virtual void ties(Space& home, ViewArray<View>& x, int s,
00080                       int* ties, int& n,
00081                       BrancherNoFilter<View>& f);
00083     virtual void brk(Space& home, ViewArray<View>& x,
00084                      int* ties, int& n) = 0;
00086     virtual int select(Space& home, ViewArray<View>& x,
00087                        int* ties, int n) = 0;
00089 
00090 
00091 
00092     virtual ViewSel<View>* copy(Space& home, bool shared) = 0;
00094     virtual bool notice(void) const;
00096     virtual void dispose(Space& home);
00098     virtual ~ViewSel(void);
00100 
00101 
00102 
00103     static void* operator new(size_t s, Space& home);
00105     static void operator delete(void* p, Space& home);
00107     static void operator delete(void* p);
00109   };
00110 
00112   template<class View>
00113   class ViewSelNone : public ViewSel<View> {
00114   protected:
00115     typedef typename ViewSel<View>::Var Var;
00116   public:
00118 
00119 
00120     ViewSelNone(Space& home, const VarBranch<Var>& vb);
00122     ViewSelNone(Space& home, bool shared, ViewSelNone<View>& vs);
00124 
00125 
00126 
00127     virtual int select(Space& home, ViewArray<View>& x, int s);
00129     virtual int select(Space& home, ViewArray<View>& x, int s,
00130                        BrancherFilter<View>& f);
00132     virtual void ties(Space& home, ViewArray<View>& x, int s,
00133                       int* ties, int& n);
00135     virtual void ties(Space& home, ViewArray<View>& x, int s,
00136                       int* ties, int& n,
00137                       BrancherFilter<View>& f);
00139     virtual void brk(Space& home, ViewArray<View>& x,
00140                      int* ties, int& n);
00142     virtual int select(Space& home, ViewArray<View>& x, int* ties, int n);
00144 
00145 
00146 
00147     virtual ViewSel<View>* copy(Space& home, bool shared);
00149   };
00150 
00152   template<class View>
00153   class ViewSelRnd : public ViewSel<View> {
00154   protected:
00155     typedef typename ViewSel<View>::Var Var;
00157     Rnd r;
00158   public:
00160 
00161 
00162     ViewSelRnd(Space& home, const VarBranch<Var>& vb);
00164     ViewSelRnd(Space& home, bool shared, ViewSelRnd<View>& vs);
00166 
00167 
00168 
00169     virtual int select(Space& home, ViewArray<View>& x, int s);
00171     virtual int select(Space& home, ViewArray<View>& x, int s,
00172                        BrancherFilter<View>& f);
00174     virtual void ties(Space& home, ViewArray<View>& x, int s,
00175                       int* ties, int& n);
00177     virtual void ties(Space& home, ViewArray<View>& x, int s,
00178                       int* ties, int& n,
00179                       BrancherFilter<View>& f);
00181     virtual void brk(Space& home, ViewArray<View>& x, int* ties, int& n);
00183     virtual int select(Space& home, ViewArray<View>& x, int* ties, int n);
00185 
00186 
00187 
00188     virtual ViewSel<View>* copy(Space& home, bool shared);
00190   };
00191 
00193   class ChooseMin {
00194   public:
00196     template<class Val>
00197     bool operator ()(Val a, Val b) const;
00198   };
00199 
00201   class ChooseMax {
00202   public:
00204     template<class Val>
00205     bool operator ()(Val a, Val b) const;
00206   };
00207 
00209   template<class Choose, class Merit>
00210   class ViewSelChoose : public ViewSel<typename Merit::View> {
00211   protected:
00212     typedef typename ViewSel<typename Merit::View>::Var Var;
00213     typedef typename ViewSel<typename Merit::View>::View View;
00215     typedef typename Merit::Val Val;
00217     Choose c;
00219     Merit m;
00220   public:
00222 
00223 
00224     ViewSelChoose(Space& home, const VarBranch<Var>& vb);
00226     ViewSelChoose(Space& home, bool shared, ViewSelChoose<Choose,Merit>& vs);
00228 
00229 
00230 
00231     virtual int select(Space& home, ViewArray<View>& x, int s);
00233     virtual int select(Space& home, ViewArray<View>& x, int s,
00234                        BrancherFilter<View>& f);
00236     virtual void ties(Space& home, ViewArray<View>& x, int s,
00237                       int* ties, int& n);
00239     virtual void ties(Space& home, ViewArray<View>& x, int s,
00240                       int* ties, int& n,
00241                       BrancherFilter<View>& f);
00243     virtual void brk(Space& home, ViewArray<View>& x, int* ties, int& n);
00245     virtual int select(Space& home, ViewArray<View>& x, int* ties, int n);
00247 
00248 
00249 
00250     virtual bool notice(void) const;
00252     virtual void dispose(Space& home);
00254   };
00255 
00256 
00258   template<class Choose, class Merit>
00259   class ViewSelChooseTbl : public ViewSelChoose<Choose,Merit> {
00260   protected:
00261     typedef typename ViewSelChoose<Choose,Merit>::Val Val;
00262     typedef typename ViewSelChoose<Choose,Merit>::View View;
00263     typedef typename ViewSelChoose<Choose,Merit>::Var Var;
00264     using ViewSelChoose<Choose,Merit>::c;
00265     using ViewSelChoose<Choose,Merit>::m;
00267     SharedData<BranchTbl> tbl;
00268   public:
00270 
00271 
00272     ViewSelChooseTbl(Space& home, const VarBranch<Var>& vb);
00274     ViewSelChooseTbl(Space& home, bool shared,
00275                      ViewSelChooseTbl<Choose,Merit>& vs);
00277 
00278 
00279 
00280     virtual void ties(Space& home, ViewArray<View>& x, int s,
00281                       int* ties, int& n);
00283     virtual void ties(Space& home, ViewArray<View>& x, int s,
00284                       int* ties, int& n,
00285                       BrancherFilter<View>& f);
00287     virtual void brk(Space& home, ViewArray<View>& x, int* ties, int& n);
00289 
00290 
00291 
00292     virtual bool notice(void) const;
00294     virtual void dispose(Space& home);
00296   };
00297 
00299   template<class Merit>
00300   class ViewSelMin : public ViewSelChoose<ChooseMin,Merit> {
00301     typedef typename ViewSelChoose<ChooseMin,Merit>::View View;
00302     typedef typename ViewSelChoose<ChooseMin,Merit>::Var Var;
00303   public:
00305 
00306 
00307     ViewSelMin(Space& home, const VarBranch<Var>& vb);
00309     ViewSelMin(Space& home, bool shared, ViewSelMin<Merit>& vs);
00311 
00312 
00313 
00314     virtual ViewSel<View>* copy(Space& home, bool shared);
00316   };
00317 
00319   template<class Merit>
00320   class ViewSelMinTbl : public ViewSelChooseTbl<ChooseMin,Merit> {
00321     typedef typename ViewSelChooseTbl<ChooseMin,Merit>::View View;
00322     typedef typename ViewSelChooseTbl<ChooseMin,Merit>::Var Var;
00323   public:
00325 
00326 
00327     ViewSelMinTbl(Space& home, const VarBranch<Var>& vb);
00329     ViewSelMinTbl(Space& home, bool shared, ViewSelMinTbl<Merit>& vs);
00331 
00332 
00333 
00334     virtual ViewSel<View>* copy(Space& home, bool shared);
00336   };
00337 
00339   template<class Merit>
00340   class ViewSelMax : public ViewSelChoose<ChooseMax,Merit> {
00341     typedef typename ViewSelChoose<ChooseMax,Merit>::View View;
00342     typedef typename ViewSelChoose<ChooseMax,Merit>::Var Var;
00343   public:
00345 
00346 
00347     ViewSelMax(Space& home, const VarBranch<Var>& vb);
00349     ViewSelMax(Space& home, bool shared, ViewSelMax<Merit>& vs);
00351 
00352 
00353 
00354     virtual ViewSel<View>* copy(Space& home, bool shared);
00356   };
00357 
00359   template<class Merit>
00360   class ViewSelMaxTbl : public ViewSelChooseTbl<ChooseMax,Merit> {
00361     typedef typename ViewSelChooseTbl<ChooseMax,Merit>::View View;
00362     typedef typename ViewSelChooseTbl<ChooseMax,Merit>::Var Var;
00363   public:
00365 
00366 
00367     ViewSelMaxTbl(Space& home, const VarBranch<Var>& vb);
00369     ViewSelMaxTbl(Space& home, bool shared, ViewSelMaxTbl<Merit>& vs);
00371 
00372 
00373 
00374     virtual ViewSel<View>* copy(Space& home, bool shared);
00376   };
00378 
00379 
00380   template<class View>
00381   forceinline
00382   ViewSel<View>::ViewSel(Space&, const VarBranch<Var>&) {}
00383   template<class View>
00384   forceinline
00385   ViewSel<View>::ViewSel(Space&, bool, ViewSel<View>&) {}
00386   template<class View>
00387   int
00388   ViewSel<View>::select(Space&, ViewArray<View>&, int,
00389                         BrancherNoFilter<View>&) {
00390     GECODE_NEVER;
00391     return 0;
00392   }
00393   template<class View>
00394   void
00395   ViewSel<View>::ties(Space&, ViewArray<View>&, int,
00396                       int*, int&,
00397                       BrancherNoFilter<View>&) {
00398     GECODE_NEVER;
00399   }
00400   template<class View>
00401   bool
00402   ViewSel<View>::notice(void) const {
00403     return false;
00404   }
00405   template<class View>
00406   void
00407   ViewSel<View>::dispose(Space&) {}
00408   template<class View>
00409   ViewSel<View>::~ViewSel(void) {}
00410   template<class View>
00411   forceinline void
00412   ViewSel<View>::operator delete(void*) {}
00413   template<class View>
00414   forceinline void
00415   ViewSel<View>::operator delete(void*, Space&) {}
00416   template<class View>
00417   forceinline void*
00418   ViewSel<View>::operator new(size_t s, Space& home) {
00419     return home.ralloc(s);
00420   }
00421 
00422 
00423 
00424   template<class View>
00425   forceinline
00426   ViewSelNone<View>::ViewSelNone(Space& home, const VarBranch<Var>& vb)
00427       : ViewSel<View>(home,vb) {}
00428   template<class View>
00429   forceinline
00430   ViewSelNone<View>::ViewSelNone(Space& home, bool shared,
00431                                  ViewSelNone<View>& vs)
00432     : ViewSel<View>(home,shared,vs) {}
00433   template<class View>
00434   int
00435   ViewSelNone<View>::select(Space&, ViewArray<View>&, int s) {
00436     return s;
00437   }
00438   template<class View>
00439   int
00440   ViewSelNone<View>::select(Space&, ViewArray<View>&, int s,
00441                             BrancherFilter<View>&) {
00442     return s;
00443   }
00444   template<class View>
00445   void
00446   ViewSelNone<View>::ties(Space&, ViewArray<View>& x, int s,
00447                           int* ties, int& n) {
00448     int j=0; ties[j++]=s;
00449     for (int i=s+1; i<x.size(); i++)
00450       if (!x[i].assigned())
00451         ties[j++]=i;
00452     n=j;
00453     assert(n > 0);
00454   }
00455   template<class View>
00456   void
00457   ViewSelNone<View>::ties(Space& home, ViewArray<View>& x, int s,
00458                           int* ties, int& n,
00459                           BrancherFilter<View>& f) {
00460     int j=0; ties[j++]=s;
00461     for (int i=s+1; i<x.size(); i++)
00462       if (!x[i].assigned() && f(home,x[i],i))
00463         ties[j++]=i;
00464     n=j;
00465     assert(n > 0);
00466   }
00467   template<class View>
00468   void
00469   ViewSelNone<View>::brk(Space&, ViewArray<View>&, int*, int&) {
00470     // Nothing needs to be done
00471   }
00472   template<class View>
00473   int
00474   ViewSelNone<View>::select(Space&, ViewArray<View>&, int* ties, int) {
00475     return ties[0];
00476   }
00477   template<class View>
00478   ViewSel<View>*
00479   ViewSelNone<View>::copy(Space& home, bool shared) {
00480     return new (home) ViewSelNone<View>(home,shared,*this);
00481   }
00482 
00483 
00484   template<class View>
00485   forceinline
00486   ViewSelRnd<View>::ViewSelRnd(Space& home, const VarBranch<Var>& vb)
00487       : ViewSel<View>(home,vb), r(vb.rnd()) {}
00488   template<class View>
00489   forceinline
00490   ViewSelRnd<View>::ViewSelRnd(Space& home, bool shared, ViewSelRnd<View>& vs)
00491       : ViewSel<View>(home,shared,vs), r(vs.r) {}
00492   template<class View>
00493   int
00494   ViewSelRnd<View>::select(Space&, ViewArray<View>& x, int s) {
00495     unsigned int n=1;
00496     int j=s;
00497     for (int i=s+1; i<x.size(); i++)
00498       if (!x[i].assigned()) {
00499         n++;
00500         if (r(n) == 0U)
00501           j=i;
00502       }
00503     return j;
00504   }
00505   template<class View>
00506   int
00507   ViewSelRnd<View>::select(Space& home, ViewArray<View>& x, int s,
00508                            BrancherFilter<View>& f) {
00509     unsigned int n=1;
00510     int j=s;
00511     for (int i=s+1; i<x.size(); i++)
00512       if (!x[i].assigned() && f(home,x[i],i)) {
00513         n++;
00514         if (r(n) == 0U)
00515           j=i;
00516       }
00517     return j;
00518   }
00519   template<class View>
00520   void
00521   ViewSelRnd<View>::ties(Space& home, ViewArray<View>& x, int s,
00522                          int* ties, int& n) {
00523     n=1; ties[0] = select(home,x,s);
00524   }
00525   template<class View>
00526   void
00527   ViewSelRnd<View>::ties(Space& home, ViewArray<View>& x, int s,
00528                          int* ties, int& n,
00529                          BrancherFilter<View>&) {
00530     n=1; ties[0] = select(home,x,s);
00531   }
00532   template<class View>
00533   void
00534   ViewSelRnd<View>::brk(Space&, ViewArray<View>&, int* ties, int& n) {
00535     ties[0] = ties[static_cast<int>(r(static_cast<unsigned int>(n)))];
00536     n=1;
00537   }
00538   template<class View>
00539   int
00540   ViewSelRnd<View>::select(Space&, ViewArray<View>&, int* ties, int n) {
00541     return ties[static_cast<int>(r(static_cast<unsigned int>(n)))];
00542   }
00543   template<class View>
00544   ViewSel<View>*
00545   ViewSelRnd<View>::copy(Space& home, bool shared) {
00546     return new (home) ViewSelRnd<View>(home,shared,*this);
00547   }
00548 
00549 
00550   template<class Val>
00551   forceinline bool
00552   ChooseMin::operator ()(Val a, Val b) const {
00553     return a < b;
00554   }
00555   template<class Val>
00556   forceinline bool
00557   ChooseMax::operator ()(Val a, Val b) const {
00558     return a > b;
00559   }
00560 
00561 
00562   template<class Choose, class Merit>
00563   forceinline
00564   ViewSelChoose<Choose,Merit>::ViewSelChoose(Space& home, const VarBranch<Var>& vb)
00565     : ViewSel<View>(home,vb), m(home,vb) {}
00566 
00567   template<class Choose, class Merit>
00568   forceinline
00569   ViewSelChoose<Choose,Merit>::ViewSelChoose(Space& home, bool shared,
00570                                              ViewSelChoose<Choose,Merit>& vs)
00571     : ViewSel<View>(home,shared,vs), m(home,shared,vs.m) {}
00572 
00573   template<class Choose, class Merit>
00574   int
00575   ViewSelChoose<Choose,Merit>::select(Space& home, ViewArray<View>& x, int s) {
00576     // Consider x[s] as the so-far best view
00577     int b_i = s;
00578     Val b_m = m(home,x[s],s);
00579     // Scan all non-assigned views from s+1 onwards
00580     for (int i=s+1; i<x.size(); i++)
00581       if (!x[i].assigned()) {
00582         Val mxi = m(home,x[i],i);
00583         if (c(mxi,b_m)) {
00584           b_i = i; b_m = mxi;
00585         }
00586       }
00587     return b_i;
00588   }
00589 
00590   template<class Choose, class Merit>
00591   int
00592   ViewSelChoose<Choose,Merit>::select(Space& home, ViewArray<View>& x, int s,
00593                                       BrancherFilter<View>& f) {
00594     // Consider x[s] as the so-far best view
00595     int b_i = s;
00596     Val b_m = m(home,x[s],s);
00597     // Scan all non-assigned views from s+1 onwards
00598     for (int i=s+1; i<x.size(); i++)
00599       if (!x[i].assigned() && f(home,x[i],i)) {
00600         Val mxi = m(home,x[i],i);
00601         if (c(mxi,b_m)) {
00602           b_i = i; b_m = mxi;
00603         }
00604       }
00605     return b_i;
00606   }
00607 
00608   template<class Choose, class Merit>
00609   void
00610   ViewSelChoose<Choose,Merit>::ties(Space& home, ViewArray<View>& x, int s,
00611                                     int* ties, int& n) {
00612     // Consider x[s] as the so-far best view and record as tie
00613     Val b = m(home,x[s],s);
00614     int j=0; ties[j++]=s;
00615     for (int i=s+1; i<x.size(); i++)
00616       if (!x[i].assigned()) {
00617         Val mxi = m(home,x[i],i);
00618         if (c(mxi,b)) {
00619           // Found a better one, reset all ties and record
00620           j=0; ties[j++]=i; b=mxi;
00621         } else if (mxi == b) {
00622           // Found a tie, record
00623           ties[j++]=i;
00624         }
00625       }
00626     n=j;
00627     // There must be at least one tie, of course!
00628     assert(n > 0);
00629   }
00630 
00631   template<class Choose, class Merit>
00632   void
00633   ViewSelChoose<Choose,Merit>::ties(Space& home, ViewArray<View>& x, int s,
00634                                     int* ties, int& n,
00635                                     BrancherFilter<View>& f) {
00636     // Consider x[s] as the so-far best view and record as tie
00637     Val b = m(home,x[s],s);
00638     int j=0; ties[j++]=s;
00639     for (int i=s+1; i<x.size(); i++)
00640       if (!x[i].assigned() && f(home,x[i],i)) {
00641         Val mxi = m(home,x[i],i);
00642         if (c(mxi,b)) {
00643           // Found a better one, reset all ties and record
00644           j=0; ties[j++]=i; b=mxi;
00645         } else if (mxi == b) {
00646           // Found a tie, record
00647           ties[j++]=i;
00648         }
00649       }
00650     n=j;
00651     // There must be at least one tie, of course!
00652     assert(n > 0);
00653   }
00654 
00655   template<class Choose, class Merit>
00656   void
00657   ViewSelChoose<Choose,Merit>::brk(Space& home, ViewArray<View>& x,
00658                                    int* ties, int& n) {
00659     // Keep first tie in place
00660     Val b = m(home,x[ties[0]],ties[0]);
00661     int j=1;
00662     // Scan remaining ties
00663     for (int i=1; i<n; i++) {
00664       Val mxi = m(home,x[ties[i]],ties[i]);
00665       if (c(mxi,b)) {
00666         // Found a better one, reset all ties
00667         b=mxi; j=0; ties[j++]=ties[i];
00668       } else if (mxi == b) {
00669         // Found a tie and record it
00670         ties[j++]=ties[i];
00671       }
00672     }
00673     n=j;
00674     // There must be at least one tie, of course!
00675     assert(n > 0);
00676   }
00677 
00678   template<class Choose, class Merit>
00679   int
00680   ViewSelChoose<Choose,Merit>::select(Space& home, ViewArray<View>& x,
00681                                       int* ties, int n) {
00682     int b_i = ties[0];
00683     Val b_m = m(home,x[ties[0]],ties[0]);
00684     for (int i=1; i<n; i++) {
00685       Val mxi = m(home,x[ties[i]],ties[i]);
00686       if (c(mxi,b_m)) {
00687         b_i = ties[i]; b_m = mxi;
00688       }
00689     }
00690     return b_i;
00691   }
00692 
00693   template<class Choose, class Merit>
00694   bool
00695   ViewSelChoose<Choose,Merit>::notice(void) const {
00696     return m.notice();
00697   }
00698 
00699   template<class Choose, class Merit>
00700   void
00701   ViewSelChoose<Choose,Merit>::dispose(Space& home) {
00702     m.dispose(home);
00703   }
00704 
00705 
00706   template<class Choose, class Merit>
00707   forceinline
00708   ViewSelChooseTbl<Choose,Merit>::ViewSelChooseTbl(Space& home,
00709                                                    const VarBranch<Var>& vb)
00710     : ViewSelChoose<Choose,Merit>(home,vb), tbl(vb.tbl()) {
00711     if (!tbl())
00712       throw InvalidFunction("ViewSelChooseTbl::ViewSelChooseTbl");
00713   }
00714 
00715   template<class Choose, class Merit>
00716   forceinline
00717   ViewSelChooseTbl<Choose,Merit>::ViewSelChooseTbl
00718   (Space& home, bool shared,
00719    ViewSelChooseTbl<Choose,Merit>& vs)
00720     : ViewSelChoose<Choose,Merit>(home,shared,vs) {
00721     tbl.update(home,shared,vs.tbl);
00722   }
00723 
00724   template<class Choose, class Merit>
00725   void
00726   ViewSelChooseTbl<Choose,Merit>::ties(Space& home, ViewArray<View>& x, int s,
00727                                        int* ties, int& n) {
00728     // Find the worst and best merit value
00729     Val w = m(home,x[s],s);
00730     Val b = w;
00731     for (int i=s+1; i<x.size(); i++)
00732       if (!x[i].assigned()) {
00733         Val mxi = m(home,x[i],i);
00734         if (c(mxi,b))
00735           b=mxi;
00736         else if (c(w,mxi))
00737           w=mxi;
00738       }
00739     // Compute tie-break limit
00740     GECODE_VALID_FUNCTION(tbl());
00741     double l = tbl()(home,static_cast<double>(w),static_cast<double>(b));
00742     // If the limit is not better than the worst merit, everything is a tie
00743     if (!c(l,static_cast<double>(w))) {
00744       int j=0;
00745       for (int i=s; i<x.size(); i++)
00746         if (!x[i].assigned())
00747           ties[j++]=i;
00748       n=j;
00749     } else {
00750       // The limit is not allowed to better than the best merit value
00751       if (c(l,static_cast<double>(b)))
00752         l = static_cast<double>(b);
00753       // Record all ties that are not worse than the limit merit value
00754       int j=0;
00755       for (int i=s; i<x.size(); i++)
00756         if (!x[i].assigned() && !c(l,static_cast<double>(m(home,x[i],i))))
00757           ties[j++]=i;
00758       n=j;
00759     }
00760     // There will be at least one tie (the best will qualify, of course)
00761     assert(n > 0);
00762   }
00763 
00764   template<class Choose, class Merit>
00765   void
00766   ViewSelChooseTbl<Choose,Merit>::ties(Space& home, ViewArray<View>& x, int s,
00767                                        int* ties, int& n,
00768                                        BrancherFilter<View>& f) {
00769     // Find the worst and best merit value
00770     assert(f(home,x[s],s));
00771     Val w = m(home,x[s],s);
00772     Val b = w;
00773     for (int i=s+1; i<x.size(); i++)
00774       if (!x[i].assigned() && f(home,x[i],i)) {
00775         Val mxi = m(home,x[i],i);
00776         if (c(mxi,b))
00777           b=mxi;
00778         else if (c(w,mxi))
00779           w=mxi;
00780       }
00781     // Compute tie-break limit
00782     GECODE_VALID_FUNCTION(tbl());
00783     double l = tbl()(home,static_cast<double>(w),static_cast<double>(b));
00784     // If the limit is not better than the worst merit, everything is a tie
00785     if (!c(l,static_cast<double>(w))) {
00786       int j=0;
00787       for (int i=s; i<x.size(); i++)
00788         if (!x[i].assigned() && f(home,x[i],i))
00789           ties[j++]=i;
00790       n=j;
00791     } else {
00792       // The limit is not allowed to better than the best merit value
00793       if (c(l,static_cast<double>(b)))
00794         l = static_cast<double>(b);
00795       // Record all ties that are not worse than the limit merit value
00796       int j=0;
00797       for (int i=s; i<x.size(); i++)
00798         if (!x[i].assigned() && f(home,x[i],i) &&
00799             !c(l,static_cast<double>(m(home,x[i],i))))
00800           ties[j++]=i;
00801       n=j;
00802     }
00803     // There will be at least one tie (the best will qualify, of course)
00804     assert(n > 0);
00805   }
00806 
00807   template<class Choose, class Merit>
00808   void
00809   ViewSelChooseTbl<Choose,Merit>::brk(Space& home, ViewArray<View>& x,
00810                                       int* ties, int& n) {
00811     // Find the worst and best merit value
00812     Val w = m(home,x[ties[0]],ties[0]);
00813     Val b = w;
00814     for (int i=1; i<n; i++) {
00815       Val mxi = m(home,x[ties[i]],ties[i]);
00816       if (c(mxi,b))
00817         b=mxi;
00818       else if (c(w,mxi))
00819         w=mxi;
00820     }
00821     // Compute tie-break limit
00822     GECODE_VALID_FUNCTION(tbl());
00823     double l = tbl()(home,static_cast<double>(w),static_cast<double>(b));
00824     // If the limit is not better than the worst merit, everything is a tie
00825     // and no breaking is required
00826     if (c(l,static_cast<double>(w))) {
00827       // The limit is not allowed to better than the best merit value
00828       if (c(l,static_cast<double>(b)))
00829         l = static_cast<double>(b);
00830       // Keep all ties that are not worse than the limit merit value
00831       int j=0;
00832       for (int i=0; i<n; i++)
00833         if (!c(l,static_cast<double>(m(home,x[ties[i]],ties[i]))))
00834           ties[j++]=ties[i];
00835       n=j;
00836     }
00837     // There will be at least one tie (the best will qualify)
00838     assert(n > 0);
00839   }
00840   template<class Choose, class Merit>
00841   bool
00842   ViewSelChooseTbl<Choose,Merit>::notice(void) const {
00843     return true;
00844   }
00845   template<class Choose, class Merit>
00846   void
00847   ViewSelChooseTbl<Choose,Merit>::dispose(Space&) {
00848     tbl.~SharedData<BranchTbl>();
00849   }
00850 
00851 
00852 
00853   template<class Merit>
00854   forceinline
00855   ViewSelMin<Merit>::ViewSelMin(Space& home, const VarBranch<Var>& vb)
00856     : ViewSelChoose<ChooseMin,Merit>(home,vb) {}
00857 
00858   template<class Merit>
00859   forceinline
00860   ViewSelMin<Merit>::ViewSelMin(Space& home, bool shared,
00861                                 ViewSelMin<Merit>& vs)
00862     : ViewSelChoose<ChooseMin,Merit>(home,shared,vs) {}
00863 
00864   template<class Merit>
00865   ViewSel<typename ViewSelMin<Merit>::View>*
00866   ViewSelMin<Merit>::copy(Space& home, bool shared) {
00867     return new (home) ViewSelMin<Merit>(home,shared,*this);
00868   }
00869 
00870 
00871   template<class Merit>
00872   forceinline
00873   ViewSelMinTbl<Merit>::ViewSelMinTbl(Space& home, const VarBranch<Var>& vb)
00874     : ViewSelChooseTbl<ChooseMin,Merit>(home,vb) {}
00875 
00876   template<class Merit>
00877   forceinline
00878   ViewSelMinTbl<Merit>::ViewSelMinTbl(Space& home, bool shared,
00879                                       ViewSelMinTbl<Merit>& vs)
00880     : ViewSelChooseTbl<ChooseMin,Merit>(home,shared,vs) {}
00881 
00882   template<class Merit>
00883   ViewSel<typename ViewSelMinTbl<Merit>::View>*
00884   ViewSelMinTbl<Merit>::copy(Space& home, bool shared) {
00885     return new (home) ViewSelMinTbl<Merit>(home,shared,*this);
00886   }
00887 
00888 
00889 
00890   template<class Merit>
00891   forceinline
00892   ViewSelMax<Merit>::ViewSelMax(Space& home, const VarBranch<Var>& vb)
00893     : ViewSelChoose<ChooseMax,Merit>(home,vb) {}
00894 
00895   template<class Merit>
00896   forceinline
00897   ViewSelMax<Merit>::ViewSelMax(Space& home, bool shared,
00898                                 ViewSelMax<Merit>& vs)
00899     : ViewSelChoose<ChooseMax,Merit>(home,shared,vs) {}
00900 
00901   template<class Merit>
00902   ViewSel<typename ViewSelMax<Merit>::View>*
00903   ViewSelMax<Merit>::copy(Space& home, bool shared) {
00904     return new (home) ViewSelMax<Merit>(home,shared,*this);
00905   }
00906 
00907 
00908 
00909   template<class Merit>
00910   forceinline
00911   ViewSelMaxTbl<Merit>::ViewSelMaxTbl(Space& home, const VarBranch<Var>& vb)
00912     : ViewSelChooseTbl<ChooseMax,Merit>(home,vb) {}
00913 
00914   template<class Merit>
00915   forceinline
00916   ViewSelMaxTbl<Merit>::ViewSelMaxTbl(Space& home, bool shared,
00917                                       ViewSelMaxTbl<Merit>& vs)
00918     : ViewSelChooseTbl<ChooseMax,Merit>(home,shared,vs) {}
00919 
00920   template<class Merit>
00921   ViewSel<typename ViewSelMaxTbl<Merit>::View>*
00922   ViewSelMaxTbl<Merit>::copy(Space& home, bool shared) {
00923     return new (home) ViewSelMaxTbl<Merit>(home,shared,*this);
00924   }
00925 
00926 
00927 
00928 }
00929 
00930 // STATISTICS: kernel-branch