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