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