Generated on Thu Mar 22 10:39:36 2012 for Gecode by doxygen 1.6.3

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-07-13 22:44:50 +0200 (Wed, 13 Jul 2011) $ by $Author: tack $
00013  *     $Revision: 12202 $
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 Int { namespace Cumulative {
00043   
00044   template<class OptTask, class Cap>
00045   forceinline
00046   OptProp<OptTask,Cap>::OptProp(Home home, Cap c0, TaskArray<OptTask>& t)
00047     : TaskProp<OptTask,Int::PC_INT_DOM>(home,t), c(c0) {
00048     c.subscribe(home,*this,PC_INT_BND);
00049   }
00050 
00051   template<class OptTask, class Cap>
00052   forceinline
00053   OptProp<OptTask,Cap>::OptProp(Space& home, bool shared,
00054                                 OptProp<OptTask,Cap>& p) 
00055     : TaskProp<OptTask,Int::PC_INT_DOM>(home,shared,p) {
00056     c.update(home,shared,p.c);
00057   }
00058 
00059   template<class OptTask, class Cap>
00060   forceinline ExecStatus 
00061   OptProp<OptTask,Cap>::post(Home home, Cap c, TaskArray<OptTask>& t) {
00062     // Capacity must be nonnegative
00063     GECODE_ME_CHECK(c.gq(home, 0));
00064     // Check for overload by single task and remove excluded tasks
00065     int n=t.size(), m=0;
00066     for (int i=n; i--; ) {
00067       if (t[i].c() > c.max()) 
00068         GECODE_ME_CHECK(t[i].excluded(home));
00069       if (t[i].excluded())
00070         t[i]=t[--n];
00071       else if (t[i].mandatory())
00072         m++;
00073     }
00074     t.size(n);
00075     if (t.size() < 2) {
00076       if (t.size() == 1) {
00077         if (t[0].mandatory()) {
00078           GECODE_ME_CHECK(c.gq(home, t[0].c()));
00079           return ES_OK;
00080         } else if (c.min() >= t[0].c()) {
00081           return ES_OK;
00082         }
00083       } else {
00084         return ES_OK;
00085       }
00086     }
00087     if (c.assigned() && c.val()==1) {
00088       TaskArray<typename TaskTraits<OptTask>::UnaryTask> mt(home,t.size());
00089       for (int i=t.size(); i--; )
00090         mt[i]=t[i];
00091       return Unary::OptProp<typename TaskTraits<OptTask>::UnaryTask>
00092         ::post(home,mt);
00093     }
00094     if (m == t.size()) {
00095       TaskArray<typename TaskTraits<OptTask>::ManTask> mt(home,m);
00096       for (int i=m; i--; )
00097         mt[i].init(t[i]);
00098       return ManProp<typename TaskTraits<OptTask>::ManTask,Cap>
00099         ::post(home,c,mt);
00100     }
00101     (void) new (home) OptProp<OptTask,Cap>(home,c,t);
00102     return ES_OK;
00103   }
00104 
00105   template<class OptTask, class Cap>
00106   Actor* 
00107   OptProp<OptTask,Cap>::copy(Space& home, bool share) {
00108     return new (home) OptProp<OptTask,Cap>(home,share,*this);
00109   }
00110 
00111   template<class OptTask, class Cap>  
00112   forceinline size_t 
00113   OptProp<OptTask,Cap>::dispose(Space& home) {
00114     (void) TaskProp<OptTask,Int::PC_INT_DOM>::dispose(home);
00115     c.cancel(home,*this,PC_INT_BND);
00116     return sizeof(*this);
00117   }
00118 
00119   template<class OptTask, class Cap>
00120   ExecStatus 
00121   OptProp<OptTask,Cap>::propagate(Space& home, const ModEventDelta& med) {
00122     // Did one of the Boolean views change?
00123     if (Int::BoolView::me(med) == Int::ME_BOOL_VAL)
00124       GECODE_ES_CHECK((purge<OptTask,Int::PC_INT_DOM>(home,*this,t,c)));
00125     // Only bounds changes?
00126     if (Int::IntView::me(med) != Int::ME_INT_DOM)
00127       GECODE_ES_CHECK(overload(home,c.max(),t));
00128 
00129     bool subsumed;
00130     GECODE_ES_CHECK(basic(home,subsumed,c,t));
00131     if (subsumed)
00132       return home.ES_SUBSUMED(*this);
00133 
00134     // Partition into mandatory and optional activities
00135     int n = t.size();
00136     int i=0, j=n-1;
00137     while (true) {
00138       while ((i < n) && t[i].mandatory()) i++;
00139       while ((j >= 0) && !t[j].mandatory()) j--;
00140       if (i >= j) break;
00141       std::swap(t[i],t[j]);
00142     }
00143 
00144     if (i > 1) {
00145       // Truncate array to only contain mandatory tasks
00146       t.size(i);
00147       GECODE_ES_CHECK(edgefinding(home,c.max(),t));
00148       // Restore to also include optional tasks
00149       t.size(n);
00150     }
00151 
00152     if (Cap::varderived() && c.assigned() && c.val()==1) {
00153       // Check that tasks do not overload resource
00154       for (int i=t.size(); i--; )
00155         if (t[i].c() > 1)
00156           GECODE_ME_CHECK(t[i].excluded(home));
00157       // Rewrite to unary resource constraint
00158       TaskArray<typename TaskTraits<OptTask>::UnaryTask> ut(home,t.size());
00159       for (int i=t.size(); i--;)
00160         ut[i]=t[i];
00161       GECODE_REWRITE(*this,
00162         (Unary::OptProp<typename TaskTraits<OptTask>::UnaryTask>
00163           ::post(home(*this),ut)));
00164     } else {
00165       return ES_NOFIX;
00166     }
00167   }
00168 
00169 }}}
00170 
00171 // STATISTICS: int-prop