Generated on Fri Oct 19 11:25:08 2018 for Gecode by doxygen 1.6.3

no-overlap.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2011
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/int/no-overlap.hh>
00035 
00036 namespace Gecode {
00037 
00038   namespace Int { namespace NoOverlap {
00039 
00040     bool
00041     optional(const BoolVarArgs& m) {
00042       for (int i=0; i<m.size(); i++)
00043         if (m[i].none())
00044           return true;
00045       return false;
00046     }
00047 
00048   }}
00049 
00050   void
00051   nooverlap(Home home,
00052             const IntVarArgs& x, const IntArgs& w,
00053             const IntVarArgs& y, const IntArgs& h,
00054             IntPropLevel) {
00055     using namespace Int;
00056     using namespace NoOverlap;
00057     if ((x.size() != w.size()) || (x.size() != y.size()) ||
00058         (x.size() != h.size()))
00059       throw ArgumentSizeMismatch("Int::nooverlap");
00060     for (int i=0; i<x.size(); i++) {
00061       Limits::nonnegative(w[i],"Int::nooverlap");
00062       Limits::nonnegative(h[i],"Int::nooverlap");
00063       Limits::check(static_cast<long long int>(x[i].max()) + w[i],
00064                     "Int::nooverlap");
00065       Limits::check(static_cast<long long int>(y[i].max()) + h[i],
00066                     "Int::nooverlap");
00067     }
00068     GECODE_POST;
00069 
00070     ManBox<FixDim,2>* b
00071       = static_cast<Space&>(home).alloc<ManBox<FixDim,2> >(x.size());
00072     for (int i=0; i<x.size(); i++) {
00073       b[i][0] = FixDim(x[i],w[i]);
00074       b[i][1] = FixDim(y[i],h[i]);
00075     }
00076 
00077     GECODE_ES_FAIL((
00078       NoOverlap::ManProp<ManBox<FixDim,2> >::post(home,b,x.size())));
00079   }
00080 
00081   void
00082   nooverlap(Home home,
00083             const IntVarArgs& x, const IntArgs& w,
00084             const IntVarArgs& y, const IntArgs& h,
00085             const BoolVarArgs& m,
00086             IntPropLevel) {
00087     using namespace Int;
00088     using namespace NoOverlap;
00089     if ((x.size() != w.size()) || (x.size() != y.size()) ||
00090         (x.size() != h.size()) || (x.size() != m.size()))
00091       throw ArgumentSizeMismatch("Int::nooverlap");
00092     for (int i=0; i<x.size(); i++) {
00093       Limits::nonnegative(w[i],"Int::nooverlap");
00094       Limits::nonnegative(h[i],"Int::nooverlap");
00095       Limits::check(static_cast<long long int>(x[i].max()) + w[i],
00096                     "Int::nooverlap");
00097       Limits::check(static_cast<long long int>(y[i].max()) + h[i],
00098                     "Int::nooverlap");
00099     }
00100     GECODE_POST;
00101 
00102     if (optional(m)) {
00103       OptBox<FixDim,2>* b
00104         = static_cast<Space&>(home).alloc<OptBox<FixDim,2> >(x.size());
00105       for (int i=0; i<x.size(); i++) {
00106         b[i][0] = FixDim(x[i],w[i]);
00107         b[i][1] = FixDim(y[i],h[i]);
00108         b[i].optional(m[i]);
00109       }
00110       GECODE_ES_FAIL((
00111         NoOverlap::OptProp<OptBox<FixDim,2> >::post(home,b,x.size())));
00112     } else {
00113       ManBox<FixDim,2>* b
00114         = static_cast<Space&>(home).alloc<ManBox<FixDim,2> >(x.size());
00115       int n = 0;
00116       for (int i=0; i<x.size(); i++)
00117         if (m[i].one()) {
00118           b[n][0] = FixDim(x[i],w[i]);
00119           b[n][1] = FixDim(y[i],h[i]);
00120           n++;
00121         }
00122       GECODE_ES_FAIL((NoOverlap::ManProp<ManBox<FixDim,2> >::post(home,b,n)));
00123     }
00124   }
00125 
00126   void
00127   nooverlap(Home home,
00128             const IntVarArgs& x0, const IntVarArgs& w, const IntVarArgs& x1,
00129             const IntVarArgs& y0, const IntVarArgs& h, const IntVarArgs& y1,
00130             IntPropLevel) {
00131     using namespace Int;
00132     using namespace NoOverlap;
00133     if ((x0.size() != w.size())  || (x0.size() != x1.size()) ||
00134         (x0.size() != y0.size()) || (x0.size() != h.size()) ||
00135         (x0.size() != y1.size()))
00136       throw ArgumentSizeMismatch("Int::nooverlap");
00137     GECODE_POST;
00138 
00139     for (int i=0; i<x0.size(); i++) {
00140       GECODE_ME_FAIL(IntView(w[i]).gq(home,0));
00141       GECODE_ME_FAIL(IntView(h[i]).gq(home,0));
00142     }
00143 
00144     if (w.assigned() && h.assigned()) {
00145       IntArgs wc(x0.size()), hc(x0.size());
00146       for (int i=0; i<x0.size(); i++) {
00147         wc[i] = w[i].val();
00148         hc[i] = h[i].val();
00149       }
00150       nooverlap(home, x0, wc, y0, hc);
00151     } else {
00152       ManBox<FlexDim,2>* b
00153         = static_cast<Space&>(home).alloc<ManBox<FlexDim,2> >(x0.size());
00154       for (int i=0; i<x0.size(); i++) {
00155         b[i][0] = FlexDim(x0[i],w[i],x1[i]);
00156         b[i][1] = FlexDim(y0[i],h[i],y1[i]);
00157       }
00158       GECODE_ES_FAIL((
00159         NoOverlap::ManProp<ManBox<FlexDim,2> >::post(home,b,x0.size())));
00160     }
00161   }
00162 
00163   void
00164   nooverlap(Home home,
00165             const IntVarArgs& x0, const IntVarArgs& w, const IntVarArgs& x1,
00166             const IntVarArgs& y0, const IntVarArgs& h, const IntVarArgs& y1,
00167             const BoolVarArgs& m,
00168             IntPropLevel) {
00169     using namespace Int;
00170     using namespace NoOverlap;
00171     if ((x0.size() != w.size())  || (x0.size() != x1.size()) ||
00172         (x0.size() != y0.size()) || (x0.size() != h.size()) ||
00173         (x0.size() != y1.size()) || (x0.size() != m.size()))
00174       throw ArgumentSizeMismatch("Int::nooverlap");
00175     GECODE_POST;
00176 
00177     for (int i=0; i<x0.size(); i++) {
00178       GECODE_ME_FAIL(IntView(w[i]).gq(home,0));
00179       GECODE_ME_FAIL(IntView(h[i]).gq(home,0));
00180     }
00181 
00182     if (w.assigned() && h.assigned()) {
00183       IntArgs wc(x0.size()), hc(x0.size());
00184       for (int i=0; i<x0.size(); i++) {
00185         wc[i] = w[i].val();
00186         hc[i] = h[i].val();
00187       }
00188       nooverlap(home, x0, wc, y0, hc, m);
00189     } else if (optional(m)) {
00190       OptBox<FlexDim,2>* b
00191         = static_cast<Space&>(home).alloc<OptBox<FlexDim,2> >(x0.size());
00192       for (int i=0; i<x0.size(); i++) {
00193         b[i][0] = FlexDim(x0[i],w[i],x1[i]);
00194         b[i][1] = FlexDim(y0[i],h[i],y1[i]);
00195         b[i].optional(m[i]);
00196       }
00197       GECODE_ES_FAIL((
00198         NoOverlap::OptProp<OptBox<FlexDim,2> >::post(home,b,x0.size())));
00199     } else {
00200       ManBox<FlexDim,2>* b
00201         = static_cast<Space&>(home).alloc<ManBox<FlexDim,2> >(x0.size());
00202       int n = 0;
00203       for (int i=0; i<x0.size(); i++)
00204         if (m[i].one()) {
00205           b[n][0] = FlexDim(x0[i],w[i],x1[i]);
00206           b[n][1] = FlexDim(y0[i],h[i],y1[i]);
00207           n++;
00208         }
00209       GECODE_ES_FAIL((NoOverlap::ManProp<ManBox<FlexDim,2> >::post(home,b,n)));
00210     }
00211   }
00212 
00213 }
00214 
00215 // STATISTICS: int-post