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