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