Generated on Fri May 13 2011 22:41:21 for Gecode by doxygen 1.7.1

opt-prop.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, 2009
00009  *     Guido Tack, 2010
00010  *
00011  *  Last modified:
00012  *     $Date: 2011-01-18 23:37:08 +0100 (Tue, 18 Jan 2011) $ by $Author: tack $
00013  *     $Revision: 11551 $
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 #include <algorithm>
00041 
00042 namespace Gecode { namespace Scheduling { namespace Cumulative {
00043   
00044   template<class OptTask>
00045   forceinline
00046   OptProp<OptTask>::OptProp(Home home, int c0, TaskArray<OptTask>& t)
00047     : TaskProp<OptTask,Int::PC_INT_DOM>(home,t), c(c0) {}
00048 
00049   template<class OptTask>
00050   forceinline
00051   OptProp<OptTask>::OptProp(Space& home, bool shared, OptProp<OptTask>& p) 
00052     : TaskProp<OptTask,Int::PC_INT_DOM>(home,shared,p), c(p.c) {}
00053 
00054   template<class OptTask>
00055   forceinline ExecStatus 
00056   OptProp<OptTask>::post(Home home, int c, TaskArray<OptTask>& t) {
00057     // Check for overload by single task and remove excluded tasks
00058     int n=t.size(), m=0;
00059     for (int i=n; i--; ) {
00060       if (t[i].c() > c) 
00061         GECODE_ME_CHECK(t[i].excluded(home));
00062       if (t[i].excluded())
00063         t[i]=t[--n];
00064       else if (t[i].mandatory())
00065         m++;
00066     }
00067     t.size(n);
00068     if (t.size() < 2)
00069       return ES_OK;
00070     if (m == t.size()) {
00071       TaskArray<typename TaskTraits<OptTask>::ManTask> mt(home,m);
00072       for (int i=m; i--; )
00073         mt[i].init(t[i]);
00074       return ManProp<typename TaskTraits<OptTask>::ManTask>::post(home,c,mt);
00075     }
00076     (void) new (home) OptProp<OptTask>(home,c,t);
00077     return ES_OK;
00078   }
00079 
00080   template<class OptTask>
00081   Actor* 
00082   OptProp<OptTask>::copy(Space& home, bool share) {
00083     return new (home) OptProp<OptTask>(home,share,*this);
00084   }
00085 
00086   template<class OptTask>  
00087   forceinline size_t 
00088   OptProp<OptTask>::dispose(Space& home) {
00089     (void) TaskProp<OptTask,Int::PC_INT_DOM>::dispose(home);
00090     return sizeof(*this);
00091   }
00092 
00093   template<class OptTask>
00094   ExecStatus 
00095   OptProp<OptTask>::propagate(Space& home, const ModEventDelta& med) {
00096     // Did one of the Boolean views change?
00097     if (Int::BoolView::me(med) == Int::ME_BOOL_VAL)
00098       GECODE_ES_CHECK((purge<OptTask,Int::PC_INT_DOM>(home,*this,t)));
00099     // Only bounds changes?
00100     if (Int::IntView::me(med) != Int::ME_INT_DOM)
00101       GECODE_ES_CHECK(overload(home,c,t));
00102 
00103     GECODE_ES_CHECK(basic(home,*this,c,t));
00104 
00105     // Partition into mandatory and optional activities
00106     int n = t.size();
00107     int i=0, j=n-1;
00108     while (true) {
00109       while ((i < n) && t[i].mandatory()) i++;
00110       while ((j >= 0) && !t[j].mandatory()) j--;
00111       if (i >= j) break;
00112       std::swap(t[i],t[j]);
00113     }
00114 
00115     if (i > 1) {
00116       // Truncate array to only contain mandatory tasks
00117       t.size(i);
00118       GECODE_ES_CHECK(edgefinding(home,c,t));
00119       // Restore to also include optional tasks
00120       t.size(n);
00121     }
00122 
00123     return ES_NOFIX;
00124   }
00125 
00126 }}}
00127 
00128 // STATISTICS: scheduling-prop