Generated on Thu Apr 11 13:59:12 2019 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  *  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/int/sequence.hh>
00039 
00040 #include <algorithm>
00041 
00042 namespace Gecode {
00043 
00044   using namespace Int;
00045 
00046   void
00047   sequence(Home home, const IntVarArgs& x, const IntSet &s,
00048            int q, int l, int u, IntPropLevel) {
00049     Limits::check(s.min(),"Int::sequence");
00050     Limits::check(s.max(),"Int::sequence");
00051 
00052     if (x.size() == 0)
00053       throw TooFewArguments("Int::sequence");
00054 
00055     Limits::check(q,"Int::sequence");
00056     Limits::check(l,"Int::sequence");
00057     Limits::check(u,"Int::sequence");
00058 
00059     if (same(x))
00060       throw ArgumentSame("Int::sequence");
00061 
00062     if ((q < 1) || (q > x.size()))
00063       throw OutOfLimits("Int::sequence");
00064 
00065     GECODE_POST;
00066 
00067     // Normalize l and u
00068     l=std::max(0,l); u=std::min(q,u);
00069 
00070     // Lower bound of values taken can never exceed upper bound
00071     if (u < l) {
00072       home.fail(); return;
00073     }
00074 
00075     // Already subsumed as any number of values taken is okay
00076     if ((0 == l) && (q == u))
00077       return;
00078 
00079     // All variables must take a value in s
00080     if (l == q) {
00081       for (int i=0; i<x.size(); i++) {
00082         IntView xv(x[i]);
00083         IntSetRanges ris(s);
00084         GECODE_ME_FAIL(xv.inter_r(home,ris,false));
00085       }
00086       return;
00087     }
00088 
00089     // No variable can take a value in s
00090     if (0 == u) {
00091       for (int i=0; i<x.size(); i++) {
00092         IntView xv(x[i]);
00093         IntSetRanges ris(s);
00094         GECODE_ME_FAIL(xv.minus_r(home,ris,false));
00095       }
00096       return;
00097     }
00098 
00099     ViewArray<IntView> xv(home,x);
00100     if (s.size() == 1) {
00101       GECODE_ES_FAIL(
00102                      (Sequence::Sequence<IntView,int>::post
00103                       (home,xv,s.min(),q,l,u)));
00104     } else {
00105       GECODE_ES_FAIL(
00106                      (Sequence::Sequence<IntView,IntSet>::post
00107                       (home,xv,s,q,l,u)));
00108     }
00109   }
00110 
00111   void
00112   sequence(Home home, const BoolVarArgs& x, const IntSet& s,
00113            int q, int l, int u, IntPropLevel) {
00114     if ((s.min() < 0) || (s.max() > 1))
00115       throw NotZeroOne("Int::sequence");
00116 
00117     if (x.size() == 0)
00118       throw TooFewArguments("Int::sequence");
00119 
00120     Limits::check(q,"Int::sequence");
00121     Limits::check(l,"Int::sequence");
00122     Limits::check(u,"Int::sequence");
00123 
00124     if (same(x))
00125       throw ArgumentSame("Int::sequence");
00126 
00127     if ((q < 1) || (q > x.size()))
00128       throw OutOfLimits("Int::sequence");
00129 
00130     GECODE_POST;
00131 
00132     // Normalize l and u
00133     l=std::max(0,l); u=std::min(q,u);
00134 
00135     // Lower bound of values taken can never exceed upper bound
00136     if (u < l) {
00137       home.fail(); return;
00138     }
00139 
00140     // Already subsumed as any number of values taken is okay
00141     if ((0 == l) && (q == u))
00142       return;
00143 
00144     // Check whether the set is {0,1}, then the number of values taken is q
00145     if ((s.min() == 0) && (s.max() == 1)) {
00146       if ((l > 0) || (u < q))
00147         home.fail();
00148       return;
00149     }
00150     assert(s.min() == s.max());
00151 
00152     // All variables must take a value in s
00153     if (l == q) {
00154       if (s.min() == 0) {
00155         for (int i=0; i<x.size(); i++) {
00156           BoolView xv(x[i]); GECODE_ME_FAIL(xv.zero(home));
00157         }
00158       } else {
00159         assert(s.min() == 1);
00160         for (int i=0; i<x.size(); i++) {
00161           BoolView xv(x[i]); GECODE_ME_FAIL(xv.one(home));
00162         }
00163       }
00164       return;
00165     }
00166 
00167     // No variable can take a value in s
00168     if (0 == u) {
00169       if (s.min() == 0) {
00170         for (int i=0; i<x.size(); i++) {
00171           BoolView xv(x[i]); GECODE_ME_FAIL(xv.one(home));
00172         }
00173       } else {
00174         assert(s.min() == 1);
00175         for (int i=0; i<x.size(); i++) {
00176           BoolView xv(x[i]); GECODE_ME_FAIL(xv.zero(home));
00177         }
00178       }
00179       return;
00180     }
00181 
00182     ViewArray<BoolView> xv(home,x);
00183 
00184     GECODE_ES_FAIL(
00185                    (Sequence::Sequence<BoolView,int>::post
00186                     (home,xv,s.min(),q,l,u)));
00187   }
00188 
00189 }
00190 
00191 // STATISTICS: int-post