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

nogoods.cpp

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-19 14:02:56 +0100 (Thu, 19 Mar 2015) $ by $Author: schulte $
00011  *     $Revision: 14468 $
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 #include <gecode/search/meta/nogoods.hh>
00039 
00040 namespace Gecode { namespace Search { namespace Meta {
00041 
00043   forceinline NGL*
00044   disposenext(NGL* ngl, Space& home, Propagator& p, bool c) {
00045     NGL* n = ngl->next();
00046     if (c)
00047       ngl->cancel(home,p);
00048     home.rfree(ngl,ngl->dispose(home));
00049     return n;
00050   }
00051     
00052   void
00053   NoNGL::subscribe(Space&, Propagator&) {
00054     GECODE_NEVER;
00055   }
00056   void
00057   NoNGL::cancel(Space&, Propagator&) {
00058     GECODE_NEVER;
00059   }
00060   NGL::Status
00061   NoNGL::status(const Space&) const {
00062     GECODE_NEVER;
00063     return NGL::NONE;
00064   }
00065   ExecStatus
00066   NoNGL::prune(Space&) {
00067     GECODE_NEVER;
00068     return ES_OK;
00069   }
00070   NGL*
00071   NoNGL::copy(Space&, bool) {
00072     GECODE_NEVER;
00073     return NULL;
00074   }
00075 
00076   Actor* 
00077   NoGoodsProp::copy(Space& home, bool share) {
00078     return new (home) NoGoodsProp(home,share,*this);
00079   }
00080 
00081   PropCost 
00082   NoGoodsProp::cost(const Space&, const ModEventDelta&) const {
00083     return PropCost::linear(PropCost::LO,n);
00084   }
00085 
00086   ExecStatus 
00087   NoGoodsProp::propagate(Space& home, const ModEventDelta&) {
00088   restart:
00089     // Start with checking the first literal
00090     switch (root->status(home)) {
00091     case NGL::FAILED:
00092       // All no-goods are dead, let dispose() clean up
00093       return home.ES_SUBSUMED(*this);
00094     case NGL::SUBSUMED:
00095       {
00096         NGL* l = disposenext(root,home,*this,true); n--;
00097         // Prune leaf-literals
00098         while ((l != NULL) && l->leaf()) {
00099           l->cancel(home,*this); n--;
00100           GECODE_ES_CHECK(l->prune(home));
00101           l = disposenext(l,home,*this,false);
00102         }
00103         root = l;
00104         // Is there anything left?
00105         if (l == NULL)
00106           return home.ES_SUBSUMED(*this);
00107         // Skip literal that already has a subscription
00108         l = l->next();
00109         // Create subscriptions for leafs
00110         while ((l != NULL) && l->leaf()) {
00111           l->subscribe(home,*this); n++;
00112           l = l->next();
00113         }
00114         // Create subscription for possible non-leaf literal
00115         if (l != NULL) {
00116           l->subscribe(home,*this); n++;
00117         }
00118         goto restart;
00119       }
00120     case NGL::NONE:
00121       break;
00122     default: GECODE_NEVER;
00123     }
00124 
00125     {
00126       NGL* p = root;
00127       NGL* l = p->next();
00128 
00129       // Check the leaves
00130       while ((l != NULL) && l->leaf()) {
00131         switch (l->status(home)) {
00132         case NGL::SUBSUMED:
00133           l = disposenext(l,home,*this,true); n--;
00134           p->next(l);
00135           GECODE_ES_CHECK(root->prune(home));
00136           if (root->status(home) == NGL::FAILED)
00137             return home.ES_SUBSUMED(*this);
00138           break;
00139         case NGL::FAILED:
00140           l = disposenext(l,home,*this,true); n--;
00141           p->next(l);
00142           break;
00143         case NGL::NONE:
00144           p = l; l = l->next();
00145           break;
00146         default: GECODE_NEVER;
00147         }
00148       }
00149 
00150       // Check the next subtree
00151       if (l != NULL) {
00152         switch (l->status(home)) {
00153         case NGL::FAILED:
00154           (void) disposenext(l,home,*this,true); n--;
00155           // Prune entire subtree
00156           p->next(NULL);
00157           break;
00158         case NGL::SUBSUMED:
00159           {
00160             // Unlink node
00161             l = disposenext(l,home,*this,true); n--;
00162             p->next(l);
00163             // Create subscriptions
00164             while ((l != NULL) && l->leaf()) {
00165               l->subscribe(home,*this); n++;
00166               l = l->next();
00167             }
00168             if (l != NULL) {
00169               l->subscribe(home,*this); n++;
00170             }
00171           }
00172           break;
00173         case NGL::NONE:
00174           break;
00175         default: GECODE_NEVER;
00176         }
00177       }
00178     }
00179     return ES_NOFIX;
00180   }
00181 
00182   size_t 
00183   NoGoodsProp::dispose(Space& home) {
00184     if (home.failed()) {
00185       // This will be executed when one ngl returned true for notice()
00186       NGL* l = root;
00187       while (l != NULL) {
00188         NGL* t = l->next();
00189         (void) l->dispose(home);
00190         l = t;
00191       }
00192     } else if (root != NULL) {
00193       // This will be executed for subsumption
00194       NGL* l = disposenext(root,home,*this,true);
00195       while ((l != NULL) && l->leaf())
00196         l = disposenext(l,home,*this,true);
00197       if (l != NULL)
00198         l = disposenext(l,home,*this,true);
00199       while (l != NULL)
00200         l = disposenext(l,home,*this,false);
00201     }
00202     home.ignore(*this,AP_DISPOSE,true);
00203     (void) Propagator::dispose(home);
00204     return sizeof(*this);
00205   }
00206 
00207 }}}
00208 
00209 // STATISTICS: search-meta