Generated on Mon Aug 25 11:35:35 2008 for Gecode by doxygen 1.5.6

base.icc

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: 2008-07-11 10:26:26 +0200 (Fri, 11 Jul 2008) $ by $Author: tack $
00011  *     $Revision: 7330 $
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>
00041   forceinline
00042   Base<View>::Base(Space* home, ViewArray<View>& x)
00043     : NaryPropagator<View,PC_INT_DOM>(home,x), y(home,x) {}
00044 
00045   template <class View>
00046   forceinline
00047   Base<View>::Base(Space* home, bool share, Base<View>& p)
00048     : NaryPropagator<View,PC_INT_DOM>(home,share,p) {
00049     y.update(home,share,p.y);
00050   }
00051 
00053   template <class View>
00054   class SsccInfo {
00055   public:
00056     int min, low, pre;
00057     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>
00068   ExecStatus
00069   Base<View>::connected(Space* home) {
00070     int n = x.size();
00071 
00073     int start = 0;
00074     while (x[start].assigned()) {
00075       start = x[start].val();
00076       if (start == 0) break;
00077     }
00078 
00080     GECODE_AUTOARRAY(SsccInfo<View>,si,n);
00081     unsigned int n_edges = 0;
00082     for (int i=n; i--; ) {
00083       n_edges += x[i].size();
00084       si[i].pre=-1;
00085     }
00086 
00087     // Stack to remember which nodes have not been processed completely
00088     GECODE_AUTOSTACK(int,-1,next,n);
00089 
00090     // Array to remember which mandatory tells need to be done
00091     GECODE_AUTOARRAY(TellInfo<View>,eq,n);
00092     int n_eq = 0;
00093 
00094     // Array to remember which edges need to be pruned
00095     GECODE_AUTOARRAY(TellInfo<View>,nq,n_edges);
00096     int n_nq = 0;
00097 
00098     /* 
00099      * Check whether there is a single strongly connected component.
00100      * This is a downstripped version of Tarjan's algorithm as
00101      * the computation of sccs proper is not needed. In addition, it
00102      * checks a mandatory condition for a graph to be Hamiltonian
00103      * (due to Mats Carlsson).
00104      *
00105      * To quote Mats: Suppose you do a depth-first search of the graph. 
00106      * In that search, the root node will have a number of child subtrees 
00107      * T1, ..., Tn. By construction, if i<j then there is no edge from 
00108      * Ti to Tj. The necessary condition for Hamiltonianicity is that 
00109      * there be an edge from Ti+1 to Ti, for 0 < i < n.
00110      *
00111      * In addition, we do the following: if there is only a single edge
00112      * from Ti+1 to Ti, then it must be mandatory and the variable must
00113      * be assigned to that value.
00114      *
00115      * The same holds true for a back edge from T0 to the root node.
00116      *
00117      * Then, all edges that reach from Ti+k+1 to Ti can be pruned.
00118      *
00119      */
00120 
00121     // Start always at node start
00122     int i = start;
00123     // Counter for scc
00124     int cnt = 0;
00125     // Smallest preorder number of last subtree (initially, the root node)
00126     int subtree_min = 0;
00127     // Largest preorder number of last subtree (initially, the root node)
00128     int subtree_max = 0;
00129     // Number of back edges into last subtree or root
00130     int back = 0;
00131   start:
00132     si[i].min = si[i].pre = si[i].low = cnt++;
00133     si[i].v.init(x[i]);
00134     do {
00135       if (si[si[i].v.val()].pre < 0) {
00136         next.push(i);
00137         i=si[i].v.val();
00138         goto start;
00139       } else if ((subtree_min <= si[si[i].v.val()].pre) &&
00140                  (si[si[i].v.val()].pre <= subtree_max)) {
00141         back++;
00142         eq[n_eq].x = x[i];
00143         eq[n_eq].n = si[i].v.val();
00144       } else if (si[si[i].v.val()].pre < subtree_min) {
00145         nq[n_nq].x = x[i];
00146         nq[n_nq].n = si[i].v.val();
00147         n_nq++;
00148       }
00149     cont:
00150       if (si[si[i].v.val()].low < si[i].min)
00151         si[i].min = si[si[i].v.val()].low;
00152       ++si[i].v;
00153     } while (si[i].v());
00154     if (si[i].min < si[i].low) {
00155       si[i].low = si[i].min;
00156     } else if (i != start) {
00157       // If it is not the first node visited, there is more than one SCC
00158       return ES_FAILED;
00159     }
00160     if (!next.empty()) {
00161       i=next.pop(); 
00162       if (i == start) {
00163         // No back edge
00164         if (back == 0)
00165           return ES_FAILED;
00166         // Exactly one back edge, make it mandatory (keep topmost entry on ti)
00167         if (back == 1)
00168           n_eq++;
00169         back        = 0;
00170         subtree_min = subtree_max+1;
00171         subtree_max = cnt-1;
00172       }
00173       goto cont;
00174     }
00175     // Whether all nodes have been visited
00176     if (cnt != n)
00177       return ES_FAILED;
00178     ExecStatus es = ES_FIX;
00179     // Assign all mandatory edges
00180     while (n_eq-- > 0) {
00181       ModEvent me = eq[n_eq].x.eq(home,eq[n_eq].n);
00182       if (me_failed(me))
00183         return ES_FAILED;
00184       if (me_modified(me))
00185         es = ES_NOFIX;
00186     }      
00187     // Remove all edges that would require a non-simple cycle
00188     while (n_nq-- > 0) {
00189       ModEvent me = nq[n_nq].x.nq(home,nq[n_nq].n);
00190       if (me_failed(me))
00191         return ES_FAILED;
00192       if (me_modified(me))
00193         es = ES_NOFIX;
00194     }      
00195     return es;
00196   }
00197 
00198   template <class View>
00199   ExecStatus
00200   Base<View>::path(Space* home) {
00201     // Prunes that partial assigned paths are not completed to cycles
00202 
00203     int n=x.size();
00204 
00205     // The path starting at assigned x[i] ends at x[end[j]] which is
00206     // not assigned.
00207     GECODE_AUTOARRAY(int,end,n);
00208     for (int i=n; i--; )
00209       end[i]=-1;
00210 
00211     // A stack that records all indices i such that end[i] != -1
00212     GECODE_AUTOSTACK(int,-1,tell,n);
00213 
00214     for (int i=y.size(); i--; ) {
00215       assert(!y[i].assigned());
00216       // Non-assigned views serve as starting points for assigned paths
00217       ViewValues<View> v(y[i]);
00218       // Try all connected values
00219       do {
00220         int j0=v.val();
00221         // Starting point for not yet followed assigned path found
00222         if (x[j0].assigned() && (end[j0] < 0)) {
00223           // Follow assigned path until non-assigned view:
00224           // all assigned view on the paths can be skipped, as
00225           // if x[i] is assigned to j, then x[j] will only have
00226           // x[i] as predecessor due to propagating distinct.
00227           int j = j0;
00228           do {
00229             j=x[j].val();
00230           } while (x[j].assigned());
00231           // Now there cannot be a cycle from x[j] to x[v.val()]!
00232           // However, the tell cannot be done here as j might be
00233           // equal to i and might hence kill the iterator v!
00234           end[j0]=j; tell.push(j0);
00235         }
00236         ++v;
00237       } while (v());
00238     }
00239 
00240     // Now do the tells based on the end information
00241     while (!tell.empty()) {
00242       int i = tell.pop();
00243       assert(end[i] >= 0);
00244       GECODE_ME_CHECK(x[end[i]].nq(home,i));
00245     }
00246     return ES_NOFIX;
00247   }
00248 
00249   template <class View>
00250   forceinline size_t
00251   Base<View>::dispose(Space* home) {
00252     (void) NaryPropagator<View,PC_INT_DOM>::dispose(home);
00253     return sizeof(*this);
00254   }
00255 
00256   template <class View>
00257   Reflection::ActorSpec
00258   Base<View>::spec(const Space* home, Reflection::VarMap& m,
00259                    const Support::Symbol& name) const {
00260     return NaryPropagator<View,PC_INT_DOM>::spec(home, m, name);                   
00261   }
00262 
00263 }}}
00264 
00265 // STATISTICS: int-prop
00266