Generated on Thu Apr 11 13:59:07 2019 for Gecode by doxygen 1.6.3

tree.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   /*
00039    * Omega tree
00040    */
00041 
00042   forceinline void
00043   OmegaNode::init(const OmegaNode&, const OmegaNode&) {
00044     p = 0; ect = -Limits::infinity;
00045   }
00046 
00047   forceinline void
00048   OmegaNode::update(const OmegaNode& l, const OmegaNode& r) {
00049     p = l.p + r.p;
00050     ect = std::max(plus(l.ect,r.p), r.ect);
00051   }
00052 
00053   template<class TaskView>
00054   forceinline
00055   OmegaTree<TaskView>::OmegaTree(Region& r, const TaskViewArray<TaskView>& t)
00056     : TaskTree<TaskView,OmegaNode>(r,t) {
00057     for (int i=0; i<tasks.size(); i++) {
00058       leaf(i).p = 0; leaf(i).ect = -Limits::infinity;
00059     }
00060     init();
00061   }
00062 
00063   template<class TaskView>
00064   forceinline void
00065   OmegaTree<TaskView>::insert(int i) {
00066     leaf(i).p = tasks[i].pmin();
00067     leaf(i).ect = tasks[i].est()+tasks[i].pmin();
00068     update(i);
00069   }
00070 
00071   template<class TaskView>
00072   forceinline void
00073   OmegaTree<TaskView>::remove(int i) {
00074     leaf(i).p = 0; leaf(i).ect = -Limits::infinity;
00075     update(i);
00076   }
00077 
00078   template<class TaskView>
00079   forceinline int
00080   OmegaTree<TaskView>::ect(void) const {
00081     return root().ect;
00082   }
00083 
00084   template<class TaskView>
00085   forceinline int
00086   OmegaTree<TaskView>::ect(int i) const {
00087     // Check whether task i is in?
00088     OmegaTree<TaskView>& o = const_cast<OmegaTree<TaskView>&>(*this);
00089     if (o.leaf(i).ect != -Limits::infinity) {
00090       o.remove(i);
00091       int ect = o.root().ect;
00092       o.insert(i);
00093       return ect;
00094     } else {
00095       return root().ect;
00096     }
00097   }
00098 
00099 
00100 
00101   /*
00102    * Omega lambda tree
00103    */
00104 
00105   forceinline void
00106   OmegaLambdaNode::init(const OmegaLambdaNode& l, const OmegaLambdaNode& r) {
00107     OmegaNode::init(l,r);
00108     lp = p; lect = ect; resEct = undef; resLp = undef;
00109   }
00110 
00111   forceinline void
00112   OmegaLambdaNode::update(const OmegaLambdaNode& l, const OmegaLambdaNode& r) {
00113     OmegaNode::update(l,r);
00114     if (l.lp + r.p > l.p + r.lp) {
00115       resLp = l.resLp;
00116       lp = l.lp + r.p;
00117     } else {
00118       resLp = r.resLp;
00119       lp = l.p + r.lp;
00120     }
00121     if ((r.lect >= plus(l.ect,r.lp)) && (r.lect >= plus(l.lect,r.p))) {
00122       lect = r.lect; resEct = r.resEct;
00123     } else if (plus(l.ect,r.lp) >= plus(l.lect,r.p)) {
00124       assert(plus(l.ect,r.lp) > r.lect);
00125       lect = plus(l.ect,r.lp); resEct = r.resLp;
00126     } else {
00127       assert((plus(l.lect,r.p) > r.lect) &&
00128              (plus(l.lect,r.p) > plus(l.ect,r.lp)));
00129       lect = plus(l.lect,r.p); resEct = l.resEct;
00130     }
00131   }
00132 
00133 
00134   template<class TaskView>
00135   forceinline
00136   OmegaLambdaTree<TaskView>::OmegaLambdaTree(Region& r,
00137                                              const TaskViewArray<TaskView>& t,
00138                                              bool inc)
00139     : TaskTree<TaskView,OmegaLambdaNode>(r,t) {
00140     if (inc) {
00141       // Enter all tasks into tree (omega = all tasks, lambda = empty)
00142       for (int i=0; i<tasks.size(); i++) {
00143         leaf(i).p = leaf(i).lp = tasks[i].pmin();
00144         leaf(i).ect = leaf(i).lect = tasks[i].est()+tasks[i].pmin();
00145         leaf(i).resEct = OmegaLambdaNode::undef;
00146         leaf(i).resLp = OmegaLambdaNode::undef;
00147       }
00148       update();
00149     } else {
00150       // Enter no tasks into tree (omega = empty, lambda = empty)
00151       for (int i=0; i<tasks.size(); i++) {
00152         leaf(i).p = leaf(i).lp = 0;
00153         leaf(i).ect = leaf(i).lect = -Limits::infinity;
00154         leaf(i).resEct = OmegaLambdaNode::undef;
00155         leaf(i).resLp = OmegaLambdaNode::undef;
00156       }
00157       init();
00158      }
00159   }
00160 
00161   template<class TaskView>
00162   forceinline void
00163   OmegaLambdaTree<TaskView>::shift(int i) {
00164     // That means that i is in omega
00165     assert(leaf(i).ect > -Limits::infinity);
00166     leaf(i).p = 0;
00167     leaf(i).ect = -Limits::infinity;
00168     leaf(i).resEct = i;
00169     leaf(i).resLp = i;
00170     update(i);
00171   }
00172 
00173   template<class TaskView>
00174   forceinline void
00175   OmegaLambdaTree<TaskView>::oinsert(int i) {
00176     leaf(i).p = tasks[i].pmin();
00177     leaf(i).ect = tasks[i].est()+tasks[i].pmin();
00178     update(i);
00179   }
00180 
00181   template<class TaskView>
00182   forceinline void
00183   OmegaLambdaTree<TaskView>::linsert(int i) {
00184     leaf(i).lp = tasks[i].pmin();
00185     leaf(i).lect = tasks[i].est()+tasks[i].pmin();
00186     leaf(i).resEct = i;
00187     leaf(i).resLp = i;
00188     update(i);
00189   }
00190 
00191   template<class TaskView>
00192   forceinline void
00193   OmegaLambdaTree<TaskView>::lremove(int i) {
00194     leaf(i).lp = 0;
00195     leaf(i).lect = -Limits::infinity;
00196     leaf(i).resEct = OmegaLambdaNode::undef;
00197     leaf(i).resLp = OmegaLambdaNode::undef;
00198     update(i);
00199   }
00200 
00201   template<class TaskView>
00202   forceinline bool
00203   OmegaLambdaTree<TaskView>::lempty(void) const {
00204     return root().resEct < 0;
00205   }
00206 
00207   template<class TaskView>
00208   forceinline int
00209   OmegaLambdaTree<TaskView>::responsible(void) const {
00210     return root().resEct;
00211   }
00212 
00213   template<class TaskView>
00214   forceinline int
00215   OmegaLambdaTree<TaskView>::ect(void) const {
00216     return root().ect;
00217   }
00218 
00219   template<class TaskView>
00220   forceinline int
00221   OmegaLambdaTree<TaskView>::lect(void) const {
00222     return root().lect;
00223   }
00224 
00225 }}}
00226 
00227 // STATISTICS: int-prop