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

time-tabling.hpp

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  *     Guido Tack <tack@gecode.org>
00006  *
00007  *  Copyright:
00008  *     Christian Schulte, 2010
00009  *     Guido Tack, 2010
00010  *
00011  *  This file is part of Gecode, the generic constraint
00012  *  development environment:
00013  *     http://www.gecode.org
00014  *
00015  *  Permission is hereby granted, free of charge, to any person obtaining
00016  *  a copy of this software and associated documentation files (the
00017  *  "Software"), to deal in the Software without restriction, including
00018  *  without limitation the rights to use, copy, modify, merge, publish,
00019  *  distribute, sublicense, and/or sell copies of the Software, and to
00020  *  permit persons to whom the Software is furnished to do so, subject to
00021  *  the following conditions:
00022  *
00023  *  The above copyright notice and this permission notice shall be
00024  *  included in all copies or substantial portions of the Software.
00025  *
00026  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00027  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00028  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00029  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00030  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00031  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00032  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00033  *
00034  */
00035 
00036 namespace Gecode { namespace Int { namespace Cumulative {
00037 
00039   template<class Task>
00040   class TaskByDecCap {
00041   public:
00043     bool operator ()(const Task& t1, const Task& t2) const;
00044   };
00045 
00046   template<class Task>
00047   forceinline bool
00048   TaskByDecCap<Task>::operator ()(const Task& t1, const Task& t2) const {
00049     return t1.c() > t2.c();
00050   }
00051 
00052   // Basic propagation (timetabling)
00053   template<class Task, class Cap>
00054   forceinline ExecStatus
00055   timetabling(Space& home, Propagator& p, Cap c, TaskArray<Task>& t) {
00056     int ccur = c.max();
00057     int cmax = ccur;
00058     int cmin = ccur;
00059 
00060     // Sort tasks by decreasing capacity
00061     TaskByDecCap<Task> tbdc;
00062     Support::quicksort(&t[0], t.size(), tbdc);
00063 
00064     Region r;
00065 
00066     bool assigned;
00067     if (Event* e = Event::events(r,t,assigned)) {
00068       // Set of current but not required tasks
00069       Support::BitSet<Region> tasks(r,static_cast<unsigned int>(t.size()));
00070 
00071       // Process events, use ccur as the capacity that is still free
00072       do {
00073         // Current time
00074         int time = e->time();
00075 
00076         // Process events for completion of required part
00077         for ( ; (e->type() == Event::LRT) && (e->time() == time); e++)
00078           if (t[e->idx()].mandatory()) {
00079             tasks.set(static_cast<unsigned int>(e->idx()));
00080             ccur += t[e->idx()].c();
00081           }
00082         // Process events for completion of task
00083         for ( ; (e->type() == Event::LCT) && (e->time() == time); e++)
00084           tasks.clear(static_cast<unsigned int>(e->idx()));
00085         // Process events for start of task
00086         for ( ; (e->type() == Event::EST) && (e->time() == time); e++)
00087           tasks.set(static_cast<unsigned int>(e->idx()));
00088         // Process events for zero-length task
00089         for ( ; (e->type() == Event::ZRO) && (e->time() == time); e++) {
00090           ccur -= t[e->idx()].c();
00091           if (ccur < cmin) cmin=ccur;
00092           if (ccur < 0)
00093             return ES_FAILED;
00094           ccur += t[e->idx()].c();
00095         }
00096 
00097         // norun start time
00098         int nrstime = time;
00099         // Process events for start of required part
00100         for ( ; (e->type() == Event::ERT) && (e->time() == time); e++)
00101           if (t[e->idx()].mandatory()) {
00102             tasks.clear(static_cast<unsigned int>(e->idx()));
00103             ccur -= t[e->idx()].c();
00104             if (ccur < cmin) cmin=ccur;
00105             nrstime = time+1;
00106             if (ccur < 0)
00107               return ES_FAILED;
00108           } else if (t[e->idx()].optional() && (t[e->idx()].c() > ccur)) {
00109             GECODE_ME_CHECK(t[e->idx()].excluded(home));
00110           }
00111 
00112         // Exploit that tasks are sorted according to capacity
00113         for (Iter::Values::BitSet<Support::BitSet<Region> > j(tasks);
00114              j() && (t[j.val()].c() > ccur); ++j)
00115           // Task j cannot run from zltime to next time - 1
00116           if (t[j.val()].mandatory())
00117             GECODE_ME_CHECK(t[j.val()].norun(home, nrstime, e->time() - 1));
00118       } while (e->type() != Event::END);
00119 
00120       GECODE_ME_CHECK(c.gq(home,cmax-cmin));
00121     }
00122 
00123     if (assigned)
00124       return home.ES_SUBSUMED(p);
00125     return ES_NOFIX;
00126   }
00127 
00128 }}}
00129 
00130 // STATISTICS: int-prop