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

scheduling.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-07-11 09:40:23 +0200 (Fri, 11 Jul 2008) $ by $Author: tack $
00011  *     $Revision: 7299 $
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 "test/int.hh"
00039 
00040 #include "gecode/minimodel.hh"
00041 #include "gecode/search.hh"
00042 
00043 #include <vector>
00044 #include <algorithm>
00045 #include <string>
00046 #include <sstream>
00047 
00048 namespace Test { namespace Int {
00049 
00051    namespace Scheduling {
00052    
00068      class Ass : public Gecode::Space {
00069      public:
00071        Gecode::IntVarArray x;
00073        Ass(int n, const Gecode::IntSet& d) : x(this, n, d) {
00074          using namespace Gecode;
00075          for (int i = 0; i < n; i += 4) {
00076            post(this, x[i+0] >= 0);
00077            post(this, x[i+1] >= 0);
00078            post(this, x[i+2] >= 0);
00079            post(this, x[i] + x[i+1] == x[i+2]);
00080            branch(this, x, INT_VAR_NONE, INT_VAL_MIN);
00081          }
00082        }
00084        Ass(bool share, Ass& s) : Gecode::Space(share,s) {
00085          x.update(this, share, s.x);
00086        }
00088        virtual Gecode::Space* copy(bool share) {
00089          return new Ass(share,*this);
00090        }
00091      };
00092    
00094      class CumulativeAssignment : public Assignment {
00096        Ass* cur;
00098        Ass* nxt;
00100        Gecode::DFS<Ass>* e;
00101      public:
00103        CumulativeAssignment(int n, const Gecode::IntSet& d) : Assignment(n,d) {
00104          Ass* a = new Ass(n, d);
00105          e = new Gecode::DFS<Ass>(a);
00106          delete a;
00107          nxt = cur = e->next();
00108          if (cur != NULL) 
00109            nxt = e->next();
00110        }
00112        virtual bool operator()(void) const {
00113          return nxt != NULL;
00114        }
00116        virtual void operator++(void) {
00117          delete cur;
00118          cur = nxt;
00119          if (cur != NULL) nxt = e->next();
00120        }
00122        virtual int  operator[](int i) const {
00123          assert((i>=0) && (i<n) && (cur != NULL));
00124          return cur->x[i].val();
00125        }
00127        virtual ~CumulativeAssignment(void) {
00128          delete cur; delete nxt; delete e;
00129        }
00130      };
00131    
00133      class Event {
00134      public:
00135        int p, h; 
00136        bool start; 
00137 
00138        Event(int pos, int height, bool s) : p(pos), h(height), start(s) {}
00140        bool operator<(const Event& e) const { 
00141          return p<e.p; 
00142        }
00143      };
00144    
00146      class Below {
00147      public:
00148        int limit; 
00149 
00150        Below(int l) : limit(l) {}
00152        bool operator()(int val) { 
00153          return val <= limit; 
00154        }
00155      };
00157      class Above {
00158      public:
00159        int limit; 
00160 
00161        Above(int l) : limit(l) {}
00163        bool operator()(int val) { 
00164          return val >= limit; 
00165        }
00166      };
00167    
00169      template <class C>
00170      bool valid(std::vector<Event> e, C comp) {
00171        std::sort(e.begin(), e.end());
00172        unsigned int i = 0;
00173        int p = 0;
00174        int h = 0;
00175        int n = 0;
00176        while (i < e.size()) {
00177          p = e[i].p;
00178          while (i < e.size() && e[i].p == p) {
00179            h += e[i].h;
00180            n += (e[i].start ? +1 : -1);
00181            ++i;
00182          }
00183          if (n && !comp(h))
00184            return false;
00185        }
00186        return true;
00187      }
00188    
00190      class Cumulatives : public Test {
00191      protected:
00192        int ntasks;   
00193        bool at_most; 
00194        int limit;    
00195      public:
00197        Cumulatives(const std::string& s, int nt, bool am, int l)
00198          : Test("Cumulatives::"+s,nt*4,-1,2), ntasks(nt), at_most(am), limit(l) {
00199          testsearch = false;
00200        }
00202        virtual Assignment* assignment(void) const {
00203          assert(arity == 4*ntasks);
00204          return new CumulativeAssignment(arity, dom);
00205        }
00207        virtual bool solution(const Assignment& x) const {
00208          std::vector<Event> e;
00209          for (int i = 0; i < ntasks; ++i) {
00210            int p = i*4;
00211            // Positive start, duration and end
00212            if (x[p+0] < 0 || x[p+1] < 1 || x[p+2] < 1) return false;
00213            // Start + Duration == End
00214            if (x[p+0] + x[p+1] != x[p+2]) {
00215              return false;
00216            }
00217          }
00218          for (int i = 0; i < ntasks; ++i) {
00219            int p = i*4;
00220            // Up at start, down at end.
00221            e.push_back(Event(x[p+0], +x[p+3],  true));
00222            e.push_back(Event(x[p+2], -x[p+3], false));
00223          }
00224          if (at_most) {
00225            return valid(e, Below(limit));
00226          } else {
00227            return valid(e, Above(limit));
00228          }
00229        }
00231        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00232          using namespace Gecode;
00233          IntArgs m(ntasks), l(1, limit);
00234          IntVarArgs s(ntasks), d(ntasks), e(ntasks), h(ntasks);
00235          for (int i = 0; i < ntasks; ++i) {
00236            int p = i*4;
00237            m[i] = 0;
00238            s[i] = x[p+0]; rel(home, x[p+0], Gecode::IRT_GQ, 0);
00239            d[i] = x[p+1]; rel(home, x[p+1], Gecode::IRT_GQ, 1);
00240            e[i] = x[p+2]; rel(home, x[p+2], Gecode::IRT_GQ, 1);
00241            h[i] = x[p+3];
00242          }
00243          cumulatives(home, m, s, d, e, h, l, at_most);
00244        }
00245      };
00246      
00247      Cumulatives c1t1("1t1", 1,  true, 1);
00248      Cumulatives c1f1("1f1", 1, false, 1);
00249      Cumulatives c1t2("1t2", 1,  true, 2);
00250      Cumulatives c1f2("1f2", 1, false, 2);
00251      Cumulatives c1t3("1t3", 1,  true, 3);
00252      Cumulatives c1f3("1f3", 1, false, 3);
00253      Cumulatives c2t1("2t1", 2,  true, 1);
00254      Cumulatives c2f1("2f1", 2, false, 1);
00255      Cumulatives c2t2("2t2", 2,  true, 2);
00256      Cumulatives c2f2("2f2", 2, false, 2);
00257      Cumulatives c2t3("2t3", 2,  true, 3);
00258      Cumulatives c2f3("2f3", 2, false, 3);
00259      Cumulatives c3t1("3t1", 3,  true, 1);
00260      Cumulatives c3f1("3f1", 3, false, 1);
00261      Cumulatives c3t2("3t2", 3,  true, 2);
00262      Cumulatives c3f2("3f2", 3, false, 2);
00263      Cumulatives c3t3("3t3", 3,  true, 3);
00264      Cumulatives c3f3("3f3", 3, false, 3);
00265      Cumulatives c3t_1("3t-1", 3,  true, -1);
00266      Cumulatives c3f_1("3f-1", 3, false, -1);
00267      Cumulatives c3t_2("3t-2", 3,  true, -2);
00268      Cumulatives c3f_2("3f-2", 3, false, -2);
00269      Cumulatives c3t_3("3t-3", 3,  true, -3);
00270      Cumulatives c3f_3("3f-3", 3, false, -3);
00272    
00273    }
00274 }}
00275 
00276 // STATISTICS: test-int