Generated on Mon Aug 25 11:35:36 2008 for Gecode by doxygen 1.5.6

cumulatives.cc

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Mikael Lagerkvist, 2005
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-02-06 20:23:59 +0100 (Wed, 06 Feb 2008) $ by $Author: schulte $
00011  *     $Revision: 6103 $
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/cumulatives.hh"
00039 #include "gecode/int/linear.hh"
00040 
00041 namespace Gecode {
00042 
00043   using namespace Int;
00044 
00045   namespace {
00046     ViewArray<IntView>
00047     make_view_array(Space* home, const IntVarArgs& in) {
00048       return ViewArray<IntView>(home, in);
00049     }
00050 
00051     ViewArray<ConstIntView>
00052     make_view_array(Space* home, const IntArgs& in) {
00053       ViewArray<ConstIntView> res(home, in.size());
00054       for (int i = in.size(); i--; ) {
00055         Limits::check(in[i],"Int::cumulatives");
00056         res[i] = ConstIntView(in[i]);
00057       }
00058 
00059       return res;
00060     }
00061 
00062     template<class In> class ViewType;
00063 
00064     template<>
00065     class ViewType<IntArgs> {
00066     public:
00067       typedef ConstIntView Result;
00068     };
00069 
00070     template<>
00071     class ViewType<IntVarArgs> {
00072     public:
00073       typedef IntView Result;
00074     };
00075 
00076     void
00077     sum(Space* home, IntVar s, IntVar d, IntVar e) {
00078       GECODE_AUTOARRAY(Linear::Term<IntView>, t, 3);
00079       t[0].a= 1; t[0].x=s;
00080       t[1].a= 1; t[1].x=d;
00081       t[2].a=-1; t[2].x=e;
00082       Linear::post(home, t, 3, IRT_EQ, 0);
00083     }
00084 
00085     void
00086     sum(Space* home, IntVar s, int d, IntVar e) {
00087       GECODE_AUTOARRAY(Linear::Term<IntView>, t, 2);
00088       t[0].a= 1; t[0].x=s;
00089       t[1].a=-1; t[1].x=e;
00090       Linear::post(home, t, 2, IRT_EQ, -d);
00091     }
00092 
00093     template <class Machine, class Duration, class Height>
00094     void
00095     post_cumulatives(Space* home, const Machine& machine,
00096                      const IntVarArgs& start, const Duration& duration,
00097                      const IntVarArgs& end, const Height& height,
00098                      const IntArgs& limit, bool at_most,
00099                      IntConLevel) {
00100       if (machine.size() != start.size()  ||
00101           start.size() != duration.size() ||
00102           duration.size() != end.size()   ||
00103           end.size() != height.size())
00104         throw ArgumentSizeMismatch("Int::cumulatives");
00105       if (home->failed()) return;
00106 
00107       ViewArray<typename ViewType<Machine>::Result>
00108         vmachine  = make_view_array(home,  machine);
00109       ViewArray<typename ViewType<Duration>::Result>
00110         vduration = make_view_array(home, duration);
00111       ViewArray<typename ViewType<Height>::Result>
00112         vheight   = make_view_array(home,   height);
00113       ViewArray<IntView>
00114         vstart    = make_view_array(home,    start),
00115         vend      = make_view_array(home,      end);
00116 
00117       // There is only the value-consistent propagator for this constraint
00118       GECODE_ES_FAIL(home,(Cumulatives::Val<
00119                            typename ViewType<Machine>::Result,
00120                            typename ViewType<Duration>::Result,
00121                            typename ViewType<Height>::Result,
00122                            IntView>::post(home, vmachine,vstart, vduration,
00123                                           vend, vheight,limit, at_most)));
00124 
00125       // By definition, s+d=e should hold.
00126       // We enforce this using basic linear constraints, since the
00127       // sweep-algorithm does not check for it.
00128       for (int i = start.size(); i--; )
00129         sum(home, start[i], duration[i], end[i]);
00130     }
00131   }
00132 
00133 
00134   void
00135   cumulatives(Space* home, const IntVarArgs& machine,
00136               const IntVarArgs& start, const IntVarArgs& duration,
00137               const IntVarArgs& end, const IntVarArgs& height,
00138               const IntArgs& limit, bool at_most,
00139               IntConLevel cl, PropKind) {
00140     post_cumulatives(home, machine, start, duration, end,
00141                      height, limit, at_most, cl);
00142   }
00143 
00144   void
00145   cumulatives(Space* home, const IntArgs& machine,
00146               const IntVarArgs& start, const IntVarArgs& duration,
00147               const IntVarArgs& end, const IntVarArgs& height,
00148               const IntArgs& limit, bool at_most,
00149               IntConLevel cl, PropKind) {
00150     post_cumulatives(home, machine, start, duration, end,
00151                      height, limit, at_most, cl);
00152   }
00153 
00154   void
00155   cumulatives(Space* home, const IntVarArgs& machine,
00156               const IntVarArgs& start, const IntArgs& duration,
00157               const IntVarArgs& end, const IntVarArgs& height,
00158               const IntArgs& limit, bool at_most,
00159               IntConLevel cl, PropKind) {
00160     post_cumulatives(home, machine, start, duration, end,
00161                      height, limit, at_most, cl);
00162   }
00163 
00164   void
00165   cumulatives(Space* home, const IntArgs& machine,
00166               const IntVarArgs& start, const IntArgs& duration,
00167               const IntVarArgs& end, const IntVarArgs& height,
00168               const IntArgs& limit, bool at_most,
00169               IntConLevel cl, PropKind) {
00170     post_cumulatives(home, machine, start, duration, end,
00171                      height, limit, at_most, cl);
00172   }
00173 
00174   void
00175   cumulatives(Space* home, const IntVarArgs& machine,
00176               const IntVarArgs& start, const IntVarArgs& duration,
00177               const IntVarArgs& end, const IntArgs& height,
00178               const IntArgs& limit, bool at_most,
00179               IntConLevel cl, PropKind) {
00180     post_cumulatives(home, machine, start, duration, end,
00181                      height, limit, at_most, cl);
00182   }
00183 
00184   void
00185   cumulatives(Space* home, const IntArgs& machine,
00186               const IntVarArgs& start, const IntVarArgs& duration,
00187               const IntVarArgs& end, const IntArgs& height,
00188               const IntArgs& limit, bool at_most,
00189               IntConLevel cl, PropKind) {
00190     post_cumulatives(home, machine, start, duration, end,
00191                      height, limit, at_most, cl);
00192   }
00193 
00194   void
00195   cumulatives(Space* home, const IntVarArgs& machine,
00196               const IntVarArgs& start, const IntArgs& duration,
00197               const IntVarArgs& end, const IntArgs& height,
00198               const IntArgs& limit, bool at_most,
00199               IntConLevel cl, PropKind) {
00200     post_cumulatives(home, machine, start, duration, end,
00201                      height, limit, at_most, cl);
00202   }
00203 
00204   void
00205   cumulatives(Space* home, const IntArgs& machine,
00206               const IntVarArgs& start, const IntArgs& duration,
00207               const IntVarArgs& end, const IntArgs& height,
00208               const IntArgs& limit, bool at_most,
00209               IntConLevel cl, PropKind) {
00210     post_cumulatives(home, machine, start, duration, end,
00211                      height, limit, at_most, cl);
00212   }
00213 
00214   GECODE_REGISTER4(Int::Cumulatives::Val<Int::ConstIntView, Int::IntView, Int::ConstIntView, Int::IntView>);
00215   GECODE_REGISTER4(Int::Cumulatives::Val<Int::ConstIntView, Int::IntView, Int::IntView, Int::IntView>);
00216   GECODE_REGISTER4(Int::Cumulatives::Val<Int::ConstIntView, Int::ConstIntView, Int::IntView, Int::IntView>);
00217   GECODE_REGISTER4(Int::Cumulatives::Val<Int::ConstIntView, Int::ConstIntView, Int::ConstIntView, Int::IntView>);
00218   GECODE_REGISTER4(Int::Cumulatives::Val<Int::IntView, Int::ConstIntView, Int::IntView, Int::IntView>);
00219   GECODE_REGISTER4(Int::Cumulatives::Val<Int::IntView, Int::ConstIntView, Int::ConstIntView, Int::IntView>);
00220   GECODE_REGISTER4(Int::Cumulatives::Val<Int::IntView, Int::IntView, Int::ConstIntView, Int::IntView>);
00221   GECODE_REGISTER4(Int::Cumulatives::Val<Int::IntView, Int::IntView, Int::IntView, Int::IntView>);
00222 
00223 }
00224 
00225 // STATISTICS: int-post
00226