Generated on Tue Apr 18 10:21:48 2017 for Gecode by doxygen 1.6.3

base.hpp

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, 2007
00008  *
00009  *  Last modified:
00010  *     $Date: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
00011  *     $Revision: 14967 $
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 { namespace Int { namespace Circuit {
00039 
00040   template<class View, class Offset>
00041   forceinline
00042   Base<View,Offset>::Base(Home home, ViewArray<View>& x, Offset& o0)
00043     : NaryPropagator<View,Int::PC_INT_DOM>(home,x),
00044       start(0), y(home,x), o(o0) {
00045     home.notice(*this,AP_WEAKLY);
00046   }
00047 
00048   template<class View, class Offset>
00049   forceinline
00050   Base<View,Offset>::Base(Space& home, bool share, Base<View,Offset>& p)
00051     : NaryPropagator<View,Int::PC_INT_DOM>(home,share,p), start(p.start) {
00052     o.update(p.o);
00053     y.update(home,share,p.y);
00054   }
00055 
00057   template<class View>
00058   class NodeInfo {
00059   public:
00060     int min, low, pre;
00061     Int::ViewValues<View> v;
00062   };
00063 
00065   template<class View>
00066   class TellInfo {
00067   public:
00068     View x; int n;
00069   };
00070 
00071   template<class View, class Offset>
00072   ExecStatus
00073   Base<View,Offset>::connected(Space& home) {
00074     int n = x.size();
00075 
00077     {
00078       int v = start;
00080       int m = n;
00081       while (x[v].assigned()) {
00082         m--;
00083         v = o(x[v]).val();
00084         // Reached start node again, check whether all nodes have been visited
00085         if (start == v)
00086           return (m == 0) ? home.ES_SUBSUMED(*this) : ES_FAILED;
00087       }
00088       start = v;
00089     }
00090 
00092     Region r(home);
00093     typedef typename Offset::ViewType OView;
00094     NodeInfo<OView>* si = r.alloc<NodeInfo<OView> >(n);
00095     unsigned int n_edges = 0;
00096     for (int i=n; i--; ) {
00097       n_edges += x[i].size();
00098       si[i].pre=-1;
00099     }
00100 
00101     // Stack to remember which nodes have not been processed completely
00102     Support::StaticStack<int,Region> next(r,n);
00103 
00104     // Array to remember which mandatory tells need to be done
00105     TellInfo<OView>* eq = r.alloc<TellInfo<OView> >(n);
00106     int n_eq = 0;
00107 
00108     // Array to remember which edges need to be pruned
00109     TellInfo<OView>* nq = r.alloc<TellInfo<OView> >(n_edges);
00110     int n_nq = 0;
00111 
00112     /*
00113      * Check whether there is a single strongly connected component.
00114      * This is a downstripped version of Tarjan's algorithm as
00115      * the computation of sccs proper is not needed. In addition, it
00116      * checks a mandatory condition for a graph to be Hamiltonian
00117      * (due to Mats Carlsson).
00118      *
00119      * To quote Mats: Suppose you do a depth-first search of the graph.
00120      * In that search, the root node will have a number of child subtrees
00121      * T1, ..., Tn. By construction, if i<j then there is no edge from
00122      * Ti to Tj. The necessary condition for Hamiltonianicity is that
00123      * there be an edge from Ti+1 to Ti, for 0 < i < n.
00124      *
00125      * In addition, we do the following: if there is only a single edge
00126      * from Ti+1 to Ti, then it must be mandatory and the variable must
00127      * be assigned to that value.
00128      *
00129      * The same holds true for a back edge from T0 to the root node.
00130      *
00131      * Then, all edges that reach from Ti+k+1 to Ti can be pruned.
00132      *
00133      */
00134 
00135     {
00136       // Start always at node start
00137       int i = start;
00138       // Counter for scc
00139       int cnt = 0;
00140       // Smallest preorder number of last subtree (initially, the root node)
00141       int subtree_min = 0;
00142       // Largest preorder number of last subtree (initially, the root node)
00143       int subtree_max = 0;
00144       // Number of back edges into last subtree or root
00145       int back = 0;
00146     start:
00147       si[i].min = si[i].pre = si[i].low = cnt++;
00148       si[i].v.init(o(x[i]));
00149       do {
00150         if (si[si[i].v.val()].pre < 0) {
00151           next.push(i);
00152           i=si[i].v.val();
00153           goto start;
00154         } else if ((subtree_min <= si[si[i].v.val()].pre) &&
00155                  (si[si[i].v.val()].pre <= subtree_max)) {
00156           back++;
00157           eq[n_eq].x = o(x[i]);
00158           eq[n_eq].n = si[i].v.val();
00159         } else if (si[si[i].v.val()].pre < subtree_min) {
00160           nq[n_nq].x = o(x[i]);
00161           nq[n_nq].n = si[i].v.val();
00162           n_nq++;
00163         }
00164       cont:
00165         if (si[si[i].v.val()].low < si[i].min)
00166           si[i].min = si[si[i].v.val()].low;
00167         ++si[i].v;
00168       } while (si[i].v());
00169       if (si[i].min < si[i].low) {
00170         si[i].low = si[i].min;
00171       } else if (i != start) {
00172         // If it is not the first node visited, there is more than one SCC
00173         return ES_FAILED;
00174       }
00175       if (!next.empty()) {
00176         i=next.pop();
00177         if (i == start) {
00178           // No back edge
00179           if (back == 0)
00180             return ES_FAILED;
00181           // Exactly one back edge, make it mandatory (keep topmost entry)
00182           if (back == 1)
00183             n_eq++;
00184           back        = 0;
00185           subtree_min = subtree_max+1;
00186           subtree_max = cnt-1;
00187         }
00188         goto cont;
00189       }
00190 
00191       // Whether all nodes have been visited
00192       if (cnt != n)
00193         return ES_FAILED;
00194 
00195       /*
00196        * Whether there is more than one subtree
00197        *
00198        * This propagation rule is taken from: Kathryn Glenn Francis,
00199        * Peter Stuckey, Explaining Circuit Propagation,
00200        * Constraints (2014) 19:1-29.
00201        *
00202        */
00203       if (subtree_min > 1) {
00204         for (Int::ViewValues<OView> v(o(x[start])); v(); ++v)
00205           if (si[v.val()].pre < subtree_min) {
00206             nq[n_nq].x = o(x[v.val()]);
00207             nq[n_nq].n = v.val();
00208             n_nq++;
00209           }
00210       }
00211 
00212       ExecStatus es = ES_FIX;
00213       // Assign all mandatory edges
00214       while (n_eq-- > 0) {
00215         ModEvent me = eq[n_eq].x.eq(home,eq[n_eq].n);
00216         if (me_failed(me))
00217           return ES_FAILED;
00218         if (me_modified(me))
00219           es = ES_NOFIX;
00220       }
00221 
00222       // Remove all edges that would require a non-simple cycle
00223       while (n_nq-- > 0) {
00224         ModEvent me = nq[n_nq].x.nq(home,nq[n_nq].n);
00225         if (me_failed(me))
00226           return ES_FAILED;
00227         if (me_modified(me))
00228           es = ES_NOFIX;
00229       }
00230 
00231       // Move start to different node for next run
00232       start = o(x[start]).min();
00233 
00234       return es;
00235     }
00236   }
00237 
00238   template<class View, class Offset>
00239   ExecStatus
00240   Base<View,Offset>::path(Space& home) {
00241     // Prunes that partial assigned paths are not completed to cycles
00242 
00243     int n=x.size();
00244 
00245     Region r(home);
00246 
00247     // The path starting at assigned x[i] ends at x[end[j]] which is
00248     // not assigned.
00249     int* end = r.alloc<int>(n);
00250     for (int i=n; i--; )
00251       end[i]=-1;
00252 
00253     // A stack that records all indices i such that end[i] != -1
00254     Support::StaticStack<int,Region> tell(r,n);
00255 
00256     typedef typename Offset::ViewType OView;
00257     for (int i=y.size(); i--; ) {
00258       assert(!y[i].assigned());
00259       // Non-assigned views serve as starting points for assigned paths
00260       Int::ViewValues<OView> v(o(y[i]));
00261       // Try all connected values
00262       do {
00263         int j0=v.val();
00264         // Starting point for not yet followed assigned path found
00265         if (x[j0].assigned() && (end[j0] < 0)) {
00266           // Follow assigned path until non-assigned view:
00267           // all assigned view on the paths can be skipped, as
00268           // if x[i] is assigned to j, then x[j] will only have
00269           // x[i] as predecessor due to propagating distinct.
00270           int j = j0;
00271           do {
00272             j=o(x[j]).val();
00273           } while (x[j].assigned());
00274           // Now there cannot be a cycle from x[j] to x[v.val()]!
00275           // However, the tell cannot be done here as j might be
00276           // equal to i and might hence kill the iterator v!
00277           end[j0]=j; tell.push(j0);
00278         }
00279         ++v;
00280       } while (v());
00281     }
00282 
00283     // Now do the tells based on the end information
00284     while (!tell.empty()) {
00285       int i = tell.pop();
00286       assert(end[i] >= 0);
00287       GECODE_ME_CHECK(o(x[end[i]]).nq(home,i));
00288     }
00289     return ES_NOFIX;
00290   }
00291 
00292   template<class View, class Offset>
00293   forceinline size_t
00294   Base<View,Offset>::dispose(Space& home) {
00295     home.ignore(*this,AP_WEAKLY);
00296     (void) NaryPropagator<View,Int::PC_INT_DOM>::dispose(home);
00297     return sizeof(*this);
00298   }
00299 
00300 }}}
00301 
00302 // STATISTICS: int-prop
00303