Generated on Wed Nov 1 15:04:33 2006 for Gecode by doxygen 1.4.5

cumulatives.cc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Mikael Lagerkvist, 2005
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-08-04 16:03:26 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $
00010  *     $Revision: 3512 $
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
00015  *
00016  *  See the file "LICENSE" for information on usage and
00017  *  redistribution of this file, and for a
00018  *     DISCLAIMER OF ALL WARRANTIES.
00019  *
00020  */
00021 
00022 #include "gecode/int/cumulatives.hh"
00023 
00024 #include "gecode/int/linear.hh"
00025 
00026 namespace Gecode {
00027 
00028   using namespace Int;
00029 
00030   namespace {
00031     ViewArray<IntView>
00032     make_view_array(Space *home, const IntVarArgs& in)
00033     {
00034       return ViewArray<IntView>(home, in);
00035     }
00036 
00037     ViewArray<ConstIntView>
00038     make_view_array(Space *home, const IntArgs& in)
00039     {
00040       ViewArray<ConstIntView> res(home, in.size());
00041       for (int i = in.size(); i--; ) {
00042         if(in[i] < Limits::Int::int_min ||
00043            in[i] > Limits::Int::int_max)
00044           throw new NumericalOverflow("Int::cumulatives");
00045         res[i] = ConstIntView(in[i]);
00046       }
00047 
00048       return res;
00049     }
00050 
00051     template<class In> class ViewType;
00052 
00053     template<>
00054     class ViewType<IntArgs> {
00055     public:
00056       typedef ConstIntView Result;
00057     };
00058 
00059     template<>
00060     class ViewType<IntVarArgs> {
00061     public:
00062       typedef IntView Result;
00063     };
00064 
00065     void
00066     sum(Space *home, const IntVar& s, const IntVar& d, const IntVar& e)
00067     {
00068       GECODE_AUTOARRAY(Linear::Term, t, 3);
00069       t[0].a= 1; t[0].x=s;
00070       t[1].a= 1; t[1].x=d;
00071       t[2].a=-1; t[2].x=e;
00072       Linear::post(home, t, 3, IRT_EQ, 0);
00073     }
00074 
00075     void
00076     sum(Space *home, const IntVar& s, int d, const IntVar& e)
00077     {
00078       GECODE_AUTOARRAY(Linear::Term, t, 2);
00079       t[0].a= 1; t[0].x=s;
00080       t[1].a=-1; t[1].x=e;
00081       Linear::post(home, t, 2, IRT_EQ, -d);
00082     }
00083 
00084     template <class Machine, class Duration, class Height>
00085     void
00086     post_cumulatives(Space* home, const Machine& machine,
00087                      const IntVarArgs& start, const Duration& duration,
00088                      const IntVarArgs& end, const Height& height,
00089                      const IntArgs& limit, bool at_most,
00090                      IntConLevel cl) {
00091       if (home->failed()) return;
00092 
00093       if(machine.size() != start.size()  ||
00094          start.size() != duration.size() ||
00095          duration.size() != end.size()   ||
00096          end.size() != height.size())
00097         throw new ArgumentSizeMismatch("Int::cumulatives");
00098 
00099       ViewArray<typename ViewType<Machine>::Result>
00100         vmachine  = make_view_array(home,  machine);
00101       ViewArray<typename ViewType<Duration>::Result>
00102         vduration = make_view_array(home, duration);
00103       ViewArray<typename ViewType<Height>::Result>
00104         vheight   = make_view_array(home,   height);
00105       ViewArray<IntView>
00106         vstart    = make_view_array(home,    start),
00107         vend      = make_view_array(home,      end);
00108 
00109       // There is only the value-consistent propagator for this constraint
00110       GECODE_ES_FAIL(home,(Cumulatives::Val<
00111                            typename ViewType<Machine>::Result,
00112                            typename ViewType<Duration>::Result,
00113                            typename ViewType<Height>::Result,
00114                            IntView>::post(home, vmachine,vstart, vduration,
00115                                           vend, vheight,limit, at_most)));
00116 
00117       // By definition, s+d=e should hold.
00118       // We enforce this using basic linear constraints, since the
00119       // sweep-algorithm does not check for it.
00120       for (int i = start.size(); i--; ) {
00121         sum(home, start[i], duration[i], end[i]);
00122       }
00123     }
00124   }
00125 
00126 
00127   void
00128   cumulatives(Space* home, const IntVarArgs& machine,
00129               const IntVarArgs& start, const IntVarArgs& duration,
00130               const IntVarArgs& end, const IntVarArgs& height,
00131               const IntArgs& limit, bool at_most,
00132               IntConLevel cl) {
00133     post_cumulatives(home, machine, start, duration, end,
00134                      height, limit, at_most, cl);
00135   }
00136 
00137   void
00138   cumulatives(Space* home, const IntArgs& machine,
00139               const IntVarArgs& start, const IntVarArgs& duration,
00140               const IntVarArgs& end, const IntVarArgs& height,
00141               const IntArgs& limit, bool at_most,
00142               IntConLevel cl) {
00143     post_cumulatives(home, machine, start, duration, end,
00144                      height, limit, at_most, cl);
00145   }
00146 
00147   void
00148   cumulatives(Space* home, const IntVarArgs& machine,
00149               const IntVarArgs& start, const IntArgs& duration,
00150               const IntVarArgs& end, const IntVarArgs& height,
00151               const IntArgs& limit, bool at_most,
00152               IntConLevel cl) {
00153     post_cumulatives(home, machine, start, duration, end,
00154                      height, limit, at_most, cl);
00155   }
00156 
00157   void
00158   cumulatives(Space* home, const IntArgs& machine,
00159               const IntVarArgs& start, const IntArgs& duration,
00160               const IntVarArgs& end, const IntVarArgs& height,
00161               const IntArgs& limit, bool at_most,
00162               IntConLevel cl) {
00163     post_cumulatives(home, machine, start, duration, end,
00164                      height, limit, at_most, cl);
00165   }
00166 
00167   void
00168   cumulatives(Space* home, const IntVarArgs& machine,
00169               const IntVarArgs& start, const IntVarArgs& duration,
00170               const IntVarArgs& end, const IntArgs& height,
00171               const IntArgs& limit, bool at_most,
00172               IntConLevel cl) {
00173     post_cumulatives(home, machine, start, duration, end,
00174                      height, limit, at_most, cl);
00175   }
00176 
00177   void
00178   cumulatives(Space* home, const IntArgs& machine,
00179               const IntVarArgs& start, const IntVarArgs& duration,
00180               const IntVarArgs& end, const IntArgs& height,
00181               const IntArgs& limit, bool at_most,
00182               IntConLevel cl) {
00183     post_cumulatives(home, machine, start, duration, end,
00184                      height, limit, at_most, cl);
00185   }
00186 
00187   void
00188   cumulatives(Space* home, const IntVarArgs& machine,
00189               const IntVarArgs& start, const IntArgs& duration,
00190               const IntVarArgs& end, const IntArgs& height,
00191               const IntArgs& limit, bool at_most,
00192               IntConLevel cl) {
00193     post_cumulatives(home, machine, start, duration, end,
00194                      height, limit, at_most, cl);
00195   }
00196 
00197   void
00198   cumulatives(Space* home, const IntArgs& machine,
00199               const IntVarArgs& start, const IntArgs& duration,
00200               const IntVarArgs& end, const IntArgs& height,
00201               const IntArgs& limit, bool at_most,
00202               IntConLevel cl) {
00203     post_cumulatives(home, machine, start, duration, end,
00204                      height, limit, at_most, cl);
00205   }
00206 
00207 }
00208 
00209 // STATISTICS: int-post
00210