Generated on Tue Apr 18 10:21:59 2017 for Gecode by doxygen 1.6.3

sequence.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     David Rijsman <David.Rijsman@quintiq.com>
00005  *
00006  *  Contributing authors:
00007  *     Christian Schulte <schulte@gecode.org>
00008  *
00009  *  Copyright:
00010  *     David Rijsman, 2009
00011  *     Christian Schulte, 2009
00012  *
00013  *  Last modified:
00014  *     $Date: 2016-05-23 22:18:23 +0200 (Mon, 23 May 2016) $
00015  *     $Revision: 15073 $
00016  *
00017  *  This file is part of Gecode, the generic constraint
00018  *  development environment:
00019  *     http://www.gecode.org
00020  *
00021  *  Permission is hereby granted, free of charge, to any person obtaining
00022  *  a copy of this software and associated documentation files (the
00023  *  "Software"), to deal in the Software without restriction, including
00024  *  without limitation the rights to use, copy, modify, merge, publish,
00025  *  distribute, sublicense, and/or sell copies of the Software, and to
00026  *  permit persons to whom the Software is furnished to do so, subject to
00027  *  the following conditions:
00028  *
00029  *  The above copyright notice and this permission notice shall be
00030  *  included in all copies or substantial portions of the Software.
00031  *
00032  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00033  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00034  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00035  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00036  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00037  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00038  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00039  *
00040  */
00041 
00042 #include <gecode/int/sequence.hh>
00043 
00044 #include <algorithm>
00045 
00046 namespace Gecode {
00047 
00048   using namespace Int;
00049 
00050   void
00051   sequence(Home home, const IntVarArgs& x, const IntSet &s,
00052            int q, int l, int u, IntPropLevel) {
00053     Limits::check(s.min(),"Int::sequence");
00054     Limits::check(s.max(),"Int::sequence");
00055 
00056     if (x.size() == 0)
00057       throw TooFewArguments("Int::sequence");
00058 
00059     Limits::check(q,"Int::sequence");
00060     Limits::check(l,"Int::sequence");
00061     Limits::check(u,"Int::sequence");
00062 
00063     if (x.same(home))
00064       throw ArgumentSame("Int::sequence");
00065 
00066     if ((q < 1) || (q > x.size()))
00067       throw OutOfLimits("Int::sequence");
00068 
00069     GECODE_POST;
00070 
00071     // Normalize l and u
00072     l=std::max(0,l); u=std::min(q,u);
00073 
00074     // Lower bound of values taken can never exceed upper bound
00075     if (u < l) {
00076       home.fail(); return;
00077     }
00078 
00079     // Already subsumed as any number of values taken is okay
00080     if ((0 == l) && (q == u))
00081       return;
00082 
00083     // All variables must take a value in s
00084     if (l == q) {
00085       for (int i=x.size(); i--; ) {
00086         IntView xv(x[i]);
00087         IntSetRanges ris(s);
00088         GECODE_ME_FAIL(xv.inter_r(home,ris,false));
00089       }
00090       return;
00091     }
00092 
00093     // No variable can take a value in s
00094     if (0 == u) {
00095       for (int i=x.size(); i--; ) {
00096         IntView xv(x[i]);
00097         IntSetRanges ris(s);
00098         GECODE_ME_FAIL(xv.minus_r(home,ris,false));
00099       }
00100       return;
00101     }
00102 
00103     ViewArray<IntView> xv(home,x);
00104     if (s.size() == 1) {
00105       GECODE_ES_FAIL(
00106                      (Sequence::Sequence<IntView,int>::post
00107                       (home,xv,s.min(),q,l,u)));
00108     } else {
00109       GECODE_ES_FAIL(
00110                      (Sequence::Sequence<IntView,IntSet>::post
00111                       (home,xv,s,q,l,u)));
00112     }
00113   }
00114 
00115   void
00116   sequence(Home home, const BoolVarArgs& x, const IntSet& s,
00117            int q, int l, int u, IntPropLevel) {
00118     if ((s.min() < 0) || (s.max() > 1))
00119       throw NotZeroOne("Int::sequence");
00120 
00121     if (x.size() == 0)
00122       throw TooFewArguments("Int::sequence");
00123 
00124     Limits::check(q,"Int::sequence");
00125     Limits::check(l,"Int::sequence");
00126     Limits::check(u,"Int::sequence");
00127 
00128     if (x.same(home))
00129       throw ArgumentSame("Int::sequence");
00130 
00131     if ((q < 1) || (q > x.size()))
00132       throw OutOfLimits("Int::sequence");
00133 
00134     GECODE_POST;
00135 
00136     // Normalize l and u
00137     l=std::max(0,l); u=std::min(q,u);
00138 
00139     // Lower bound of values taken can never exceed upper bound
00140     if (u < l) {
00141       home.fail(); return;
00142     }
00143 
00144     // Already subsumed as any number of values taken is okay
00145     if ((0 == l) && (q == u))
00146       return;
00147 
00148     // Check whether the set is {0,1}, then the number of values taken is q
00149     if ((s.min() == 0) && (s.max() == 1)) {
00150       if ((l > 0) || (u < q))
00151         home.fail();
00152       return;
00153     }
00154     assert(s.min() == s.max());
00155 
00156     // All variables must take a value in s
00157     if (l == q) {
00158       if (s.min() == 0) {
00159         for (int i=x.size(); i--; ) {
00160           BoolView xv(x[i]); GECODE_ME_FAIL(xv.zero(home));
00161         }
00162       } else {
00163         assert(s.min() == 1);
00164         for (int i=x.size(); i--; ) {
00165           BoolView xv(x[i]); GECODE_ME_FAIL(xv.one(home));
00166         }
00167       }
00168       return;
00169     }
00170 
00171     // No variable can take a value in s
00172     if (0 == u) {
00173       if (s.min() == 0) {
00174         for (int i=x.size(); i--; ) {
00175           BoolView xv(x[i]); GECODE_ME_FAIL(xv.one(home));
00176         }
00177       } else {
00178         assert(s.min() == 1);
00179         for (int i=x.size(); i--; ) {
00180           BoolView xv(x[i]); GECODE_ME_FAIL(xv.zero(home));
00181         }
00182       }
00183       return;
00184     }
00185 
00186     ViewArray<BoolView> xv(home,x);
00187 
00188     GECODE_ES_FAIL(
00189                    (Sequence::Sequence<BoolView,int>::post
00190                     (home,xv,s.min(),q,l,u)));
00191   }
00192 
00193 }
00194 
00195 // STATISTICS: int-post