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

atmostOne.cc

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Guido Tack <tack@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Guido Tack, 2004
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-01-31 18:29:16 +0100 (Thu, 31 Jan 2008) $ by $Author: tack $
00011  *     $Revision: 6017 $
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 #include "gecode/set/distinct.hh"
00039 
00040 /*
00041  * These propagators implement the scheme discussed in
00042  *
00043  * Andrew Sadler and Carment Gervet: Global Reasoning on Sets.
00044  * FORMUL'01 workshop in conjunction with CP 2001.
00045  *
00046  * Todo: make the propagators incremental.
00047  */
00048 
00049 namespace Gecode { namespace Set { namespace Distinct {
00050 
00051   /*
00052    * "AtMostOneIntersection" propagator
00053    *
00054    */
00055 
00056   Actor*
00057   AtmostOne::copy(Space* home, bool share) {
00058     return new (home) AtmostOne(home,share,*this);
00059   }
00060 
00061   Support::Symbol
00062   AtmostOne::ati(void) {
00063     return Support::Symbol("Gecode::Set::Distinct::AtmostOne");
00064   }
00065 
00066   Reflection::ActorSpec
00067   AtmostOne::spec(const Space* home, Reflection::VarMap& m) const {
00068     return NaryPropagator<SetView, PC_SET_ANY>::spec(home, m, ati())
00069       << c;
00070   }
00071 
00072   ExecStatus
00073   AtmostOne::propagate(Space* home, ModEventDelta) {
00074 
00075     GECODE_AUTOARRAY(LubRanges<SetView>, lubs, x.size());
00076     for (int i = x.size(); i--; ) {
00077       lubs[i].init(x[i]);
00078     }
00079     Iter::Ranges::NaryUnion<LubRanges<SetView> > bigT(lubs, x.size());
00080     Iter::Ranges::Cache<Iter::Ranges::NaryUnion<LubRanges<SetView> > >
00081       bigTC(bigT);
00082 
00083     Iter::Ranges::ToValues<Iter::Ranges::Cache<Iter::Ranges::NaryUnion<LubRanges<SetView> > > >
00084       as(bigTC);
00085 
00086     while (as()) {
00087       int a = as.val(); ++as;
00088 
00089       // cardSa is the number of sets that contain a in the glb
00090       int cardSa = 0;
00091       for (int i=x.size(); i--;)
00092         if (x[i].contains(a))
00093           cardSa++;
00094 
00095       // bigTa is the union of all lubs that contain a
00096       GLBndSet bigTa(home);
00097       for (int i=x.size(); i--;) {
00098         if (!x[i].notContains(a)) {
00099           LubRanges<SetView> xilub(x[i]);
00100           bigTa.includeI(home, xilub);
00101         }
00102       }
00103 
00104       // maxa is the maximum number of sets that can contain a
00105       int maxa = (bigTa.size() - 1) / (c - 1);
00106       bigTa.dispose(home);
00107 
00108       // Conditional Rule A:
00109       // If more sets already contain a than allowed, fail.
00110       if (maxa < cardSa)
00111         return ES_FAILED;
00112 
00113       if (maxa == cardSa) {
00114         // Conditional Rule B:
00115         // All a used up. All other sets (those that don't have a in their
00116         // glb already) cannot contain a.
00117         for (int i=x.size(); i--;) {
00118           if (!x[i].contains(a)) {
00119             GECODE_ME_CHECK(x[i].exclude(home, a));
00120           }
00121         }
00122       } else {
00123         GECODE_AUTOARRAY(LubRanges<SetView>, lubs2, x.size());
00124         for (int i = x.size(); i--; ) {
00125           lubs2[i].init(x[i]);
00126         }
00127         Iter::Ranges::NaryUnion<LubRanges<SetView> > bigT2(lubs2, x.size());
00128         
00129         GECODE_AUTOARRAY(GlbRanges<SetView>, glbs, cardSa);
00130         int count = 0;
00131         for (int i=x.size(); i--; ) {
00132           if (x[i].contains(a)) {
00133             glbs[count].init(x[i]);
00134             count++;
00135           }
00136         }
00137         Iter::Ranges::NaryUnion<GlbRanges<SetView> > glbsa(glbs, cardSa);
00138         Iter::Ranges::Diff<Iter::Ranges::NaryUnion<LubRanges<SetView> >,
00139           Iter::Ranges::NaryUnion<GlbRanges<SetView> > > deltaA(bigT2, glbsa);
00140         Iter::Ranges::Cache<
00141         Iter::Ranges::Diff<Iter::Ranges::NaryUnion<LubRanges<SetView> >,
00142           Iter::Ranges::NaryUnion<GlbRanges<SetView> > > > deltaAC(deltaA);
00143         // deltaAC contains all elements that are not yet known to be
00144         // in a set together with a.
00145         // Formally: \cup_i lub(x_i) - \cup_i {glb(s_i) | a\in glb(s_i)}
00146 
00147 
00148         if (Iter::Ranges::size(deltaAC) == c - 1) {
00149           // Conditional Rule C:
00150           // If deltaA has exactly c-1 elements, all sets that are not yet
00151           // known to contain a cannot contain a if it is impossible that
00152           // they contain all of deltaA. Or the other way around:
00153           // If a is added to the glb of a set s, deltaA must be a subset of
00154           // s, because otherwise s would share at least one more element
00155           // with another set that has a in its lower bound.
00156           // Weird, eh?
00157           for (int i=x.size(); i--; ) {
00158             if (!x[i].contains(a) && !x[i].notContains(a)) {
00159               deltaAC.reset();
00160               LubRanges<SetView> xilub(x[i]);
00161               if (!Iter::Ranges::subset(deltaAC, xilub)) {
00162                 GECODE_ME_CHECK(x[i].exclude(home, a));
00163               }
00164             }
00165           }
00166         }
00167 
00168       }
00169 
00170     }
00171 
00172     return ES_NOFIX;
00173   }
00174 
00175   void
00176   AtmostOne::post(Space* home, Reflection::VarMap& vars,
00177                   const Reflection::ActorSpec& spec) {
00178     spec.checkArity(2);
00179     ViewArray<SetView> s(home, vars, spec[0]);
00180     int c = spec[1]->toInt();
00181     (void) new (home) AtmostOne(home,s,c);
00182   }
00183 
00184 }}
00185 
00186 namespace {
00187   GECODE_REGISTER1(Set::Distinct::AtmostOne);
00188 }
00189 
00190 }
00191 
00192 // STATISTICS: set-prop