Generated on Fri Oct 19 11:25:03 2018 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  *
00006  *  Copyright:
00007  *     Christian Schulte, 2009
00008  *
00009  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include <algorithm>
00035 
00036 namespace Gecode { namespace Int { namespace Unary {
00037 
00038   template<class OptTask, class PL>
00039   forceinline
00040   OptProp<OptTask,PL>::OptProp(Home home, TaskArray<OptTask>& t)
00041     : TaskProp<OptTask,PL>(home,t) {}
00042 
00043   template<class OptTask, class PL>
00044   forceinline
00045   OptProp<OptTask,PL>::OptProp(Space& home, OptProp<OptTask,PL>& p)
00046     : TaskProp<OptTask,PL>(home,p) {}
00047 
00048   template<class OptTask, class PL>
00049   ExecStatus
00050   OptProp<OptTask,PL>::post(Home home, TaskArray<OptTask>& t) {
00051     int m=0, o=0;
00052     for (int i=0; i<t.size(); i++) {
00053       if (t[i].mandatory())
00054         m++;
00055       else if (t[i].optional())
00056         o++;
00057     }
00058     if (m == t.size()) {
00059       TaskArray<typename TaskTraits<OptTask>::ManTask> mt(home,m);
00060       for (int i=0; i<m; i++)
00061         mt[i].init(t[i]);
00062       return ManProp<typename TaskTraits<OptTask>::ManTask,PL>::post(home,mt);
00063     }
00064     if (o+m > 1)
00065       (void) new (home) OptProp<OptTask,PL>(home,t);
00066     return ES_OK;
00067   }
00068 
00069   template<class OptTask, class PL>
00070   Actor*
00071   OptProp<OptTask,PL>::copy(Space& home) {
00072     return new (home) OptProp<OptTask,PL>(home,*this);
00073   }
00074 
00075   template<class OptTask, class PL>
00076   ExecStatus
00077   OptProp<OptTask,PL>::propagate(Space& home, const ModEventDelta& med) {
00078     // Did one of the Boolean views change?
00079     if (BoolView::me(med) == ME_BOOL_VAL)
00080       GECODE_ES_CHECK((purge<OptTask,PL>(home,*this,t)));
00081 
00082     GECODE_ES_CHECK((overload<OptTask,PL>(home,*this,t)));
00083 
00084     if (PL::basic)
00085       GECODE_ES_CHECK(timetabling(home,*this,t));
00086 
00087     if (PL::advanced) {
00088       GECODE_ES_CHECK((detectable<OptTask,PL>(home,*this,t)));
00089       GECODE_ES_CHECK((notfirstnotlast<OptTask,PL>(home,*this,t)));
00090 
00091       // Partition into mandatory and optional activities
00092       int n = t.size();
00093       int i=0, j=n-1;
00094       while (true) {
00095         while ((i < n) && t[i].mandatory()) i++;
00096         while ((j >= 0) && !t[j].mandatory()) j--;
00097         if (i >= j) break;
00098         std::swap(t[i],t[j]);
00099       }
00100 
00101       if (i > 1) {
00102         // Truncate array to only contain mandatory tasks
00103         t.size(i);
00104         GECODE_ES_CHECK(edgefinding(home,t));
00105         // Restore to also include optional tasks
00106         t.size(n);
00107       }
00108     }
00109 
00110     if (!PL::basic)
00111       GECODE_ES_CHECK(subsumed(home,*this,t));
00112 
00113     return ES_NOFIX;
00114   }
00115 
00116 }}}
00117 
00118 // STATISTICS: int-prop