Generated on Tue May 22 09:40:12 2018 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  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include <gecode/search/nogoods.hh>
00035 
00036 namespace Gecode { namespace Search {
00037 
00039   forceinline NGL*
00040   disposenext(NGL* ngl, Space& home, Propagator& p, bool c) {
00041     NGL* n = ngl->next();
00042     if (c)
00043       ngl->cancel(home,p);
00044     home.rfree(ngl,ngl->dispose(home));
00045     return n;
00046   }
00047 
00048   void
00049   NoNGL::subscribe(Space&, Propagator&) {
00050     GECODE_NEVER;
00051   }
00052   void
00053   NoNGL::cancel(Space&, Propagator&) {
00054     GECODE_NEVER;
00055   }
00056   void
00057   NoNGL::reschedule(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&) {
00072     GECODE_NEVER;
00073     return NULL;
00074   }
00075 
00076   Actor*
00077   NoGoodsProp::copy(Space& home) {
00078     return new (home) NoGoodsProp(home,*this);
00079   }
00080 
00081   PropCost
00082   NoGoodsProp::cost(const Space&, const ModEventDelta&) const {
00083     return PropCost::linear(PropCost::LO,n);
00084   }
00085 
00086   void
00087   NoGoodsProp::reschedule(Space& home) {
00088     root->reschedule(home,*this);
00089     NGL* l = root->next();
00090     while ((l != NULL) && l->leaf()) {
00091       l->reschedule(home,*this);
00092       l = l->next();
00093     }
00094     if (l != NULL)
00095       l->reschedule(home,*this);
00096   }
00097 
00098 
00099   ExecStatus
00100   NoGoodsProp::propagate(Space& home, const ModEventDelta&) {
00101   restart:
00102     // Start with checking the first literal
00103     switch (root->status(home)) {
00104     case NGL::FAILED:
00105       // All no-goods are dead, let dispose() clean up
00106       return home.ES_SUBSUMED(*this);
00107     case NGL::SUBSUMED:
00108       {
00109         NGL* l = disposenext(root,home,*this,true); n--;
00110         // Prune leaf-literals
00111         while ((l != NULL) && l->leaf()) {
00112           l->cancel(home,*this); n--;
00113           GECODE_ES_CHECK(l->prune(home));
00114           l = disposenext(l,home,*this,false);
00115         }
00116         root = l;
00117         // Is there anything left?
00118         if (l == NULL)
00119           return home.ES_SUBSUMED(*this);
00120         // Skip literal that already has a subscription
00121         l = l->next();
00122         // Create subscriptions for leafs
00123         while ((l != NULL) && l->leaf()) {
00124           l->subscribe(home,*this); n++;
00125           l = l->next();
00126         }
00127         // Create subscription for possible non-leaf literal
00128         if (l != NULL) {
00129           l->subscribe(home,*this); n++;
00130         }
00131         goto restart;
00132       }
00133     case NGL::NONE:
00134       break;
00135     default: GECODE_NEVER;
00136     }
00137 
00138     {
00139       NGL* p = root;
00140       NGL* l = p->next();
00141 
00142       // Check the leaves
00143       while ((l != NULL) && l->leaf()) {
00144         switch (l->status(home)) {
00145         case NGL::SUBSUMED:
00146           l = disposenext(l,home,*this,true); n--;
00147           p->next(l);
00148           GECODE_ES_CHECK(root->prune(home));
00149           if (root->status(home) == NGL::FAILED)
00150             return home.ES_SUBSUMED(*this);
00151           break;
00152         case NGL::FAILED:
00153           l = disposenext(l,home,*this,true); n--;
00154           p->next(l);
00155           break;
00156         case NGL::NONE:
00157           p = l; l = l->next();
00158           break;
00159         default: GECODE_NEVER;
00160         }
00161       }
00162 
00163       // Check the next subtree
00164       if (l != NULL) {
00165         switch (l->status(home)) {
00166         case NGL::FAILED:
00167           (void) disposenext(l,home,*this,true); n--;
00168           // Prune entire subtree
00169           p->next(NULL);
00170           break;
00171         case NGL::SUBSUMED:
00172           {
00173             // Unlink node
00174             l = disposenext(l,home,*this,true); n--;
00175             p->next(l);
00176             // Create subscriptions
00177             while ((l != NULL) && l->leaf()) {
00178               l->subscribe(home,*this); n++;
00179               l = l->next();
00180             }
00181             if (l != NULL) {
00182               l->subscribe(home,*this); n++;
00183             }
00184           }
00185           break;
00186         case NGL::NONE:
00187           break;
00188         default: GECODE_NEVER;
00189         }
00190       }
00191     }
00192     return ES_NOFIX;
00193   }
00194 
00195   size_t
00196   NoGoodsProp::dispose(Space& home) {
00197     if (home.failed()) {
00198       // This will be executed when one ngl returned true for notice()
00199       NGL* l = root;
00200       while (l != NULL) {
00201         NGL* t = l->next();
00202         (void) l->dispose(home);
00203         l = t;
00204       }
00205     } else if (root != NULL) {
00206       // This will be executed for subsumption
00207       NGL* l = disposenext(root,home,*this,true);
00208       while ((l != NULL) && l->leaf())
00209         l = disposenext(l,home,*this,true);
00210       if (l != NULL)
00211         l = disposenext(l,home,*this,true);
00212       while (l != NULL)
00213         l = disposenext(l,home,*this,false);
00214     }
00215     home.ignore(*this,AP_DISPOSE,true);
00216     (void) Propagator::dispose(home);
00217     return sizeof(*this);
00218   }
00219 
00220 }}
00221 
00222 // STATISTICS: search-other