Generated on Tue Apr 18 10:21:51 2017 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: 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 #include <algorithm>
00041 
00042 namespace Gecode { namespace Int { namespace Cumulative {
00043 
00044   template<class OptTask, class Cap, class PL>
00045   forceinline
00046   OptProp<OptTask,Cap,PL>::OptProp(Home home, Cap c0, TaskArray<OptTask>& t)
00047     : TaskProp<OptTask,PL>(home,t), c(c0) {
00048     c.subscribe(home,*this,PC_INT_BND);
00049   }
00050 
00051   template<class OptTask, class Cap, class PL>
00052   forceinline
00053   OptProp<OptTask,Cap,PL>::OptProp(Space& home, bool shared,
00054                                    OptProp<OptTask,Cap,PL>& p)
00055     : TaskProp<OptTask,PL>(home,shared,p) {
00056     c.update(home,shared,p.c);
00057   }
00058 
00059   template<class OptTask, class Cap, class PL>
00060   ExecStatus
00061   OptProp<OptTask,Cap,PL>::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,PL>
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,PL>
00099         ::post(home,c,mt);
00100     }
00101     (void) new (home) OptProp<OptTask,Cap,PL>(home,c,t);
00102     return ES_OK;
00103   }
00104 
00105   template<class OptTask, class Cap, class PL>
00106   Actor*
00107   OptProp<OptTask,Cap,PL>::copy(Space& home, bool share) {
00108     return new (home) OptProp<OptTask,Cap,PL>(home,share,*this);
00109   }
00110 
00111   template<class OptTask, class Cap, class PL>
00112   forceinline size_t
00113   OptProp<OptTask,Cap,PL>::dispose(Space& home) {
00114     (void) TaskProp<OptTask,PL>::dispose(home);
00115     c.cancel(home,*this,PC_INT_BND);
00116     return sizeof(*this);
00117   }
00118 
00119   template<class OptTask, class Cap, class PL>
00120   ExecStatus
00121   OptProp<OptTask,Cap,PL>::propagate(Space& home, const ModEventDelta& med) {
00122     // Did one of the Boolean views change?
00123     if (BoolView::me(med) == ME_BOOL_VAL)
00124       GECODE_ES_CHECK((purge<OptTask,PL>(home,*this,t,c)));
00125 
00126     // Only bounds changes?
00127     if (IntView::me(med) != ME_INT_DOM)
00128       GECODE_ES_CHECK(overload(home,c.max(),t));
00129 
00130     if (PL::basic)
00131       GECODE_ES_CHECK(timetabling(home,*this,c,t));
00132 
00133     if (PL::advanced) {
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 
00153     if (Cap::varderived() && c.assigned() && c.val()==1) {
00154       // Check that tasks do not overload resource
00155       for (int i=t.size(); i--; )
00156         if (t[i].c() > 1)
00157           GECODE_ME_CHECK(t[i].excluded(home));
00158       // Rewrite to unary resource constraint
00159       TaskArray<typename TaskTraits<OptTask>::UnaryTask> ut(home,t.size());
00160       for (int i=t.size(); i--;)
00161         ut[i]=t[i];
00162       GECODE_REWRITE(*this,
00163                      (Unary::OptProp<typename TaskTraits<OptTask>::UnaryTask,PL>
00164                       ::post(home(*this),ut)));
00165     }
00166 
00167     if (!PL::basic && c.assigned())
00168       GECODE_ES_CHECK(subsumed(home,*this,c.val(),t));
00169 
00170     return ES_NOFIX;
00171   }
00172 
00173 }}}
00174 
00175 // STATISTICS: int-prop