Generated on Wed Nov 1 15:04:43 2006 for Gecode by doxygen 1.4.5

atmostOne.cc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Guido Tack <tack@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Guido Tack, 2004
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-04-11 15:58:37 +0200 (Tue, 11 Apr 2006) $ by $Author: tack $
00010  *     $Revision: 3188 $
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
00015  *
00016  *  See the file "LICENSE" for information on usage and
00017  *  redistribution of this file, and for a
00018  *     DISCLAIMER OF ALL WARRANTIES.
00019  *
00020  */
00021 
00022 #include "gecode/set.hh"
00023 #include "gecode/set/distinct.hh"
00024 
00025 /*
00026  * These propagators implement the scheme discussed in
00027  *
00028  * Andrew Sadler and Carment Gervet: Global Reasoning on Sets.
00029  * FORMUL'01 workshop in conjunction with CP 2001.
00030  *
00031  * Todo: make the propagators incremental.
00032  */
00033 
00034 namespace Gecode { namespace Set { namespace Distinct {
00035 
00036   /*
00037    * "AtMostOneIntersection" propagator
00038    *
00039    */
00040 
00041   Actor*
00042   AtmostOne::copy(Space* home, bool share) {
00043     return new (home) AtmostOne(home,share,*this);
00044   }
00045 
00046   ExecStatus
00047   AtmostOne::propagate(Space* home) {
00048 
00049     GECODE_AUTOARRAY(LubRanges<SetView>, lubs, x.size());
00050     for (int i = x.size(); i--; ) {
00051       lubs[i].init(x[i]);
00052     }
00053     Iter::Ranges::NaryUnion<LubRanges<SetView> > bigT(lubs, x.size());
00054     Iter::Ranges::Cache<Iter::Ranges::NaryUnion<LubRanges<SetView> > >
00055       bigTC(bigT);
00056 
00057     Iter::Ranges::ToValues<Iter::Ranges::Cache<Iter::Ranges::NaryUnion<LubRanges<SetView> > > >
00058       as(bigTC);
00059 
00060     while (as()) {
00061       int a = as.val(); ++as;
00062 
00063       // cardSa is the number of sets that contain a in the glb
00064       int cardSa = 0;
00065       for (int i=x.size(); i--;)
00066         if (x[i].contains(a))
00067           cardSa++;
00068 
00069       // bigTa is the union of all lubs that contain a
00070       GLBndSet bigTa(home);
00071       for (int i=x.size(); i--;) {
00072         if (!x[i].notContains(a)) {
00073           LubRanges<SetView> xilub(x[i]);
00074           bigTa.includeI(home, xilub);
00075         }
00076       }
00077 
00078       // maxa is the maximum number of sets that can contain a
00079       int maxa = (bigTa.size() - 1) / (c - 1);
00080       bigTa.dispose(home);
00081 
00082       // Conditional Rule A:
00083       // If more sets already contain a than allowed, fail.
00084       if (maxa < cardSa)
00085         return ES_FAILED;
00086 
00087       if (maxa == cardSa) {
00088         // Conditional Rule B:
00089         // All a used up. All other sets (those that don't have a in their
00090         // glb already) cannot contain a.
00091         for (int i=x.size(); i--;) {
00092           if (!x[i].contains(a)) {
00093             GECODE_ME_CHECK(x[i].exclude(home, a));
00094           }
00095         }
00096       } else {
00097         GECODE_AUTOARRAY(LubRanges<SetView>, lubs2, x.size());
00098         for (int i = x.size(); i--; ) {
00099           lubs2[i].init(x[i]);
00100         }
00101         Iter::Ranges::NaryUnion<LubRanges<SetView> > bigT2(lubs2, x.size());
00102         
00103         GECODE_AUTOARRAY(GlbRanges<SetView>, glbs, cardSa);
00104         int count = 0;
00105         for (int i=x.size(); i--; ) {
00106           if (x[i].contains(a)) {
00107             glbs[count].init(x[i]);
00108             count++;
00109           }
00110         }
00111         Iter::Ranges::NaryUnion<GlbRanges<SetView> > glbsa(glbs, cardSa);
00112         Iter::Ranges::Diff<Iter::Ranges::NaryUnion<LubRanges<SetView> >,
00113           Iter::Ranges::NaryUnion<GlbRanges<SetView> > > deltaA(bigT2, glbsa);
00114         Iter::Ranges::Cache<
00115         Iter::Ranges::Diff<Iter::Ranges::NaryUnion<LubRanges<SetView> >,
00116           Iter::Ranges::NaryUnion<GlbRanges<SetView> > > > deltaAC(deltaA);
00117         // deltaAC contains all elements that are not yet known to be
00118         // in a set together with a.
00119         // Formally: \cup_i lub(x_i) - \cup_i {glb(s_i) | a\in glb(s_i)}
00120 
00121 
00122         if (Iter::Ranges::size(deltaAC) == c - 1) {
00123           // Conditional Rule C:
00124           // If deltaA has exactly c-1 elements, all sets that are not yet
00125           // known to contain a cannot contain a if it is impossible that
00126           // they contain all of deltaA. Or the other way around:
00127           // If a is added to the glb of a set s, deltaA must be a subset of
00128           // s, because otherwise s would share at least one more element
00129           // with another set that has a in its lower bound.
00130           // Weird, eh?
00131           for (int i=x.size(); i--; ) {
00132             if (!x[i].contains(a) && !x[i].notContains(a)) {
00133               deltaAC.reset();
00134               LubRanges<SetView> xilub(x[i]);
00135               if (!Iter::Ranges::subset(deltaAC, xilub)) {
00136                 GECODE_ME_CHECK(x[i].exclude(home, a));
00137               }
00138             }
00139           }
00140         }
00141 
00142       }
00143 
00144     }
00145 
00146     return ES_NOFIX;
00147   }
00148 
00149 }}}
00150 
00151 // STATISTICS: set-prop