Generated on Tue May 22 09:40:14 2018 for Gecode by doxygen 1.6.3

atmostOne.cpp

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  *  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 #include <gecode/set/distinct.hh>
00035 
00036 /*
00037  * These propagators implement the scheme discussed in
00038  *
00039  * Andrew Sadler and Carment Gervet: Global Reasoning on Sets.
00040  * FORMUL'01 workshop in conjunction with CP 2001.
00041  *
00042  * Todo: make the propagators incremental.
00043  */
00044 
00045 namespace Gecode { namespace Set { namespace Distinct {
00046 
00047   /*
00048    * "AtMostOneIntersection" propagator
00049    *
00050    */
00051 
00052   Actor*
00053   AtmostOne::copy(Space& home) {
00054     return new (home) AtmostOne(home,*this);
00055   }
00056 
00057   ExecStatus
00058   AtmostOne::propagate(Space& home, const ModEventDelta&) {
00059     Region r;
00060     LubRanges<SetView>* lubs = r.alloc<LubRanges<SetView> >(x.size());
00061     for (int i = x.size(); i--; ) {
00062       lubs[i].init(x[i]);
00063     }
00064     Iter::Ranges::NaryUnion bigT(r, lubs, x.size());
00065 
00066     Iter::Ranges::ToValues<Iter::Ranges::NaryUnion>
00067       as(bigT);
00068 
00069     while (as()) {
00070       int a = as.val(); ++as;
00071 
00072       // cardSa is the number of sets that contain a in the glb
00073       int cardSa = 0;
00074       for (int i=x.size(); i--;)
00075         if (x[i].contains(a))
00076           cardSa++;
00077 
00078       // bigTa is the union of all lubs that contain a
00079       GLBndSet bigTa(home);
00080       for (int i=x.size(); i--;) {
00081         if (!x[i].notContains(a)) {
00082           LubRanges<SetView> xilub(x[i]);
00083           bigTa.includeI(home, xilub);
00084         }
00085       }
00086 
00087       // maxa is the maximum number of sets that can contain a
00088       int maxa = static_cast<int>((bigTa.size() - 1) / (c - 1));
00089       bigTa.dispose(home);
00090 
00091       // Conditional Rule A:
00092       // If more sets already contain a than allowed, fail.
00093       if (maxa < cardSa)
00094         return ES_FAILED;
00095 
00096       if (maxa == cardSa) {
00097         // Conditional Rule B:
00098         // All a used up. All other sets (those that don't have a in their
00099         // glb already) cannot contain a.
00100         for (int i=x.size(); i--;) {
00101           if (!x[i].contains(a)) {
00102             GECODE_ME_CHECK(x[i].exclude(home, a));
00103           }
00104         }
00105       } else {
00106         LubRanges<SetView>* lubs2 = r.alloc<LubRanges<SetView> >(x.size());
00107         for (int i = x.size(); i--; ) {
00108           lubs2[i].init(x[i]);
00109         }
00110         Iter::Ranges::NaryUnion bigT2(r, lubs2, x.size());
00111 
00112         GlbRanges<SetView>* glbs = r.alloc<GlbRanges<SetView> >(cardSa);
00113         int count = 0;
00114         for (int i=x.size(); i--; ) {
00115           if (x[i].contains(a)) {
00116             glbs[count].init(x[i]);
00117             count++;
00118           }
00119         }
00120         Iter::Ranges::NaryUnion glbsa(r, glbs, cardSa);
00121         Iter::Ranges::Diff<Iter::Ranges::NaryUnion,
00122           Iter::Ranges::NaryUnion> deltaA(bigT2, glbsa);
00123         Iter::Ranges::Cache deltaAC(r,deltaA);
00124         // deltaAC contains all elements that are not yet known to be
00125         // in a set together with a.
00126         // Formally: \cup_i lub(x_i) - \cup_i {glb(s_i) | a\in glb(s_i)}
00127 
00128 
00129         if (Iter::Ranges::size(deltaAC) == c - 1) {
00130           // Conditional Rule C:
00131           // If deltaA has exactly c-1 elements, all sets that are not yet
00132           // known to contain a cannot contain a if it is impossible that
00133           // they contain all of deltaA. Or the other way around:
00134           // If a is added to the glb of a set s, deltaA must be a subset of
00135           // s, because otherwise s would share at least one more element
00136           // with another set that has a in its lower bound.
00137           // Weird, eh?
00138           for (int i=x.size(); i--; ) {
00139             if (!x[i].contains(a) && !x[i].notContains(a)) {
00140               deltaAC.reset();
00141               LubRanges<SetView> xilub(x[i]);
00142               if (!Iter::Ranges::subset(deltaAC, xilub)) {
00143                 GECODE_ME_CHECK(x[i].exclude(home, a));
00144               }
00145             }
00146           }
00147         }
00148 
00149       }
00150 
00151     }
00152 
00153     return ES_NOFIX;
00154   }
00155 
00156 }}}
00157 
00158 // STATISTICS: set-prop