Generated on Fri Mar 20 15:56:19 2015 for Gecode by doxygen 1.6.3

nogoods.hh

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2013
00008  *
00009  *  Last modified:
00010  *     $Date: 2015-03-20 15:37:34 +0100 (Fri, 20 Mar 2015) $ by $Author: schulte $
00011  *     $Revision: 14471 $
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 #ifndef __GECODE_SEARCH_META_NO_GOODS_HH__
00039 #define __GECODE_SEARCH_META_NO_GOODS_HH__
00040 
00041 #include <gecode/search.hh>
00042 
00043 namespace Gecode { namespace Search { namespace Meta {
00044 
00046   class GECODE_VTABLE_EXPORT NoNGL : public NGL {
00047   public:
00049     NoNGL(void);
00051     NoNGL(Space& home);
00053     NoNGL(Space& home, bool share, NoNGL& ngl);
00055     virtual void subscribe(Space& home, Propagator& p);
00057     virtual void cancel(Space& home, Propagator& p);
00059     virtual NGL::Status status(const Space& home) const;
00061     virtual ExecStatus prune(Space& home);
00063     virtual NGL* copy(Space& home, bool share);
00064   };
00065 
00067   class GECODE_SEARCH_EXPORT NoGoodsProp : public Propagator {
00068   protected:
00070     NGL* root;
00072     unsigned int n;
00074     NoGoodsProp(Home home, NGL* root);
00076     NoGoodsProp(Space& home, bool shared, NoGoodsProp& p);
00077   public:
00079     virtual Actor* copy(Space& home, bool share);
00081     virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
00083     virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
00085     template<class Path>
00086     static ExecStatus post(Space& home, const Path& p);
00088     virtual size_t dispose(Space& home);
00089   };
00090 
00091   forceinline
00092   NoNGL::NoNGL(void) {} 
00093 
00094   forceinline
00095   NoNGL::NoNGL(Space& home) 
00096     : NGL(home) {}
00097 
00098   forceinline
00099   NoNGL::NoNGL(Space& home, bool share, NoNGL& ngl) 
00100     : NGL(home,share,ngl) {}
00101 
00102 
00103 
00104   forceinline
00105   NoGoodsProp::NoGoodsProp(Home home, NGL* root0)
00106     : Propagator(home), root(root0), n(0U) {
00107     // Create subscriptions
00108     root->subscribe(home,*this); n++;
00109     bool notice = root->notice();
00110     NGL* l = root->next();
00111     while ((l != NULL) && l->leaf()) {
00112       l->subscribe(home,*this); n++;
00113       notice = notice || l->notice();
00114       l = l->next();
00115     }
00116     if (l != NULL) {
00117       l->subscribe(home,*this); n++;
00118     }
00119     while (!notice && (l != NULL)) {
00120       notice = notice || l->notice();
00121       l = l->next();
00122     }
00123     if (notice)
00124       home.notice(*this,AP_DISPOSE);
00125   }
00126 
00127   forceinline
00128   NoGoodsProp::NoGoodsProp(Space& home, bool shared, NoGoodsProp& p) 
00129     : Propagator(home,shared,p), n(p.n) {
00130     assert(p.root != NULL);
00131     NoNGL s;
00132     NGL* c = &s;
00133     for (NGL* pc = p.root; pc != NULL; pc = pc->next()) {
00134       NGL* n = pc->copy(home,shared);
00135       n->leaf(pc->leaf());
00136       c->next(n); c=n;
00137     }
00138     root = s.next();
00139   }
00140   
00141 
00142 
00143   template<class Path>
00144   forceinline ExecStatus 
00145   NoGoodsProp::post(Space& home, const Path& p) {
00146     int s = 0;
00147     int n = std::min(p.ds.entries(),static_cast<int>(p.ngdl()));
00148 
00149     unsigned long int n_nogood = 0;
00150 
00151     // Eliminate the alternatives which are not no-goods at the end
00152     while ((n > s) && (p.ds[n-1].truealt() == 0U))
00153       n--;
00154 
00155     // A sentinel element
00156     NoNGL nn;
00157     // Current no-good literal
00158     NGL* c = &nn;
00159 
00160     // Commit no-goods at the beginning
00161     while ((s < n) && (p.ds[s].truealt() > 0U))
00162       // Try whether this is a rightmost alternative
00163       if (p.ds[s].rightmost()) {
00164         // No literal needed, directly try to commit
00165         home.trycommit(*p.ds[s].choice(),p.ds[s].truealt());
00166         s++;
00167       } else {
00168         // Prune using no-good literals
00169         for (unsigned int a=0U; a<p.ds[s].truealt(); a++) {
00170           NGL* l = home.ngl(*p.ds[s].choice(),a);
00171           // Does the brancher support no-good literals?
00172           if (l == NULL)
00173             return ES_OK;
00174           GECODE_ES_CHECK(l->prune(home));
00175         }
00176         // Add literal as root if needed and stop
00177         if (NGL* l = home.ngl(*p.ds[s].choice(),p.ds[s].truealt())) {
00178           c = c->add(l,false);
00179           s++; break;
00180         }
00181       }
00182 
00183     // There are no literals
00184     if (home.failed()) 
00185       return ES_FAILED;
00186     if (s >= n)
00187       return ES_OK;
00188 
00189     // There must be at least two literals
00190     assert((n-s > 1) || 
00191            ((n-s == 1) && (c != &nn)));
00192 
00193     // Remember the last leaf
00194     NGL* ll = NULL;
00195 
00196     // Create literals
00197     for (int i=s; i<n; i++) {
00198       // Add leaves
00199       for (unsigned int a=0U; a<p.ds[i].truealt(); a++) {
00200         NGL* l = home.ngl(*p.ds[i].choice(),a);
00201         if (l == NULL) {
00202           // The brancher does not support no-goods
00203           if (ll == NULL)
00204             return ES_OK;
00205           ll->next(NULL);
00206           break;
00207         }
00208         c = c->add(l,true); ll = c;
00209         n_nogood++;
00210       }
00211       // Check whether to add an additional subtree
00212       if (NGL* l = home.ngl(*p.ds[i].choice(),p.ds[i].truealt())) {
00213         c = c->add(l,false);
00214       } else if (!p.ds[i].rightmost()) {
00215         // The brancher does not support no-goods
00216         if (ll == NULL)
00217           return ES_OK;
00218         ll->next(NULL);
00219         break;
00220       }
00221     }
00222 
00223     const_cast<Path&>(p).ng(n_nogood);
00224 
00225     (void) new (home) NoGoodsProp(home,nn.next());
00226     return ES_OK;
00227   }
00228 
00229 }}}
00230 
00231 #endif
00232 
00233 // STATISTICS: search-meta