tree.hpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2009
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <algorithm>
namespace Gecode { namespace Int { namespace Unary {
/*
* Omega tree
*/
forceinline void
OmegaNode::init(const OmegaNode&, const OmegaNode&) {
p = 0; ect = -Limits::infinity;
}
forceinline void
OmegaNode::update(const OmegaNode& l, const OmegaNode& r) {
p = l.p + r.p;
ect = std::max(plus(l.ect,r.p), r.ect);
}
template<class TaskView>
forceinline
OmegaTree<TaskView>::OmegaTree(Region& r, const TaskViewArray<TaskView>& t)
: TaskTree<TaskView,OmegaNode>(r,t) {
for (int i=0; i<tasks.size(); i++) {
leaf(i).p = 0; leaf(i).ect = -Limits::infinity;
}
init();
}
template<class TaskView>
forceinline void
OmegaTree<TaskView>::insert(int i) {
leaf(i).p = tasks[i].pmin();
leaf(i).ect = tasks[i].est()+tasks[i].pmin();
update(i);
}
template<class TaskView>
forceinline void
OmegaTree<TaskView>::remove(int i) {
leaf(i).p = 0; leaf(i).ect = -Limits::infinity;
update(i);
}
template<class TaskView>
forceinline int
OmegaTree<TaskView>::ect(void) const {
return root().ect;
}
template<class TaskView>
forceinline int
OmegaTree<TaskView>::ect(int i) const {
// Check whether task i is in?
OmegaTree<TaskView>& o = const_cast<OmegaTree<TaskView>&>(*this);
if (o.leaf(i).ect != -Limits::infinity) {
o.remove(i);
int ect = o.root().ect;
o.insert(i);
return ect;
} else {
return root().ect;
}
}
/*
* Omega lambda tree
*/
forceinline void
OmegaLambdaNode::init(const OmegaLambdaNode& l, const OmegaLambdaNode& r) {
OmegaNode::init(l,r);
lp = p; lect = ect; resEct = undef; resLp = undef;
}
forceinline void
OmegaLambdaNode::update(const OmegaLambdaNode& l, const OmegaLambdaNode& r) {
OmegaNode::update(l,r);
if (l.lp + r.p > l.p + r.lp) {
resLp = l.resLp;
lp = l.lp + r.p;
} else {
resLp = r.resLp;
lp = l.p + r.lp;
}
if ((r.lect >= plus(l.ect,r.lp)) && (r.lect >= plus(l.lect,r.p))) {
lect = r.lect; resEct = r.resEct;
} else if (plus(l.ect,r.lp) >= plus(l.lect,r.p)) {
assert(plus(l.ect,r.lp) > r.lect);
lect = plus(l.ect,r.lp); resEct = r.resLp;
} else {
assert((plus(l.lect,r.p) > r.lect) &&
(plus(l.lect,r.p) > plus(l.ect,r.lp)));
lect = plus(l.lect,r.p); resEct = l.resEct;
}
}
template<class TaskView>
forceinline
OmegaLambdaTree<TaskView>::OmegaLambdaTree(Region& r,
const TaskViewArray<TaskView>& t,
bool inc)
: TaskTree<TaskView,OmegaLambdaNode>(r,t) {
if (inc) {
// Enter all tasks into tree (omega = all tasks, lambda = empty)
for (int i=0; i<tasks.size(); i++) {
leaf(i).p = leaf(i).lp = tasks[i].pmin();
leaf(i).ect = leaf(i).lect = tasks[i].est()+tasks[i].pmin();
leaf(i).resEct = OmegaLambdaNode::undef;
leaf(i).resLp = OmegaLambdaNode::undef;
}
update();
} else {
// Enter no tasks into tree (omega = empty, lambda = empty)
for (int i=0; i<tasks.size(); i++) {
leaf(i).p = leaf(i).lp = 0;
leaf(i).ect = leaf(i).lect = -Limits::infinity;
leaf(i).resEct = OmegaLambdaNode::undef;
leaf(i).resLp = OmegaLambdaNode::undef;
}
init();
}
}
template<class TaskView>
forceinline void
OmegaLambdaTree<TaskView>::shift(int i) {
// That means that i is in omega
assert(leaf(i).ect > -Limits::infinity);
leaf(i).p = 0;
leaf(i).ect = -Limits::infinity;
leaf(i).resEct = i;
leaf(i).resLp = i;
update(i);
}
template<class TaskView>
forceinline void
OmegaLambdaTree<TaskView>::oinsert(int i) {
leaf(i).p = tasks[i].pmin();
leaf(i).ect = tasks[i].est()+tasks[i].pmin();
update(i);
}
template<class TaskView>
forceinline void
OmegaLambdaTree<TaskView>::linsert(int i) {
leaf(i).lp = tasks[i].pmin();
leaf(i).lect = tasks[i].est()+tasks[i].pmin();
leaf(i).resEct = i;
leaf(i).resLp = i;
update(i);
}
template<class TaskView>
forceinline void
OmegaLambdaTree<TaskView>::lremove(int i) {
leaf(i).lp = 0;
leaf(i).lect = -Limits::infinity;
leaf(i).resEct = OmegaLambdaNode::undef;
leaf(i).resLp = OmegaLambdaNode::undef;
update(i);
}
template<class TaskView>
forceinline bool
OmegaLambdaTree<TaskView>::lempty(void) const {
return root().resEct < 0;
}
template<class TaskView>
forceinline int
OmegaLambdaTree<TaskView>::responsible(void) const {
return root().resEct;
}
template<class TaskView>
forceinline int
OmegaLambdaTree<TaskView>::ect(void) const {
return root().ect;
}
template<class TaskView>
forceinline int
OmegaLambdaTree<TaskView>::lect(void) const {
return root().lect;
}
}}}
// STATISTICS: int-prop
Gecode::Int::TaskTree< TaskView, OmegaNode >::tasks
const TaskViewArray< TaskView > & tasks
Definition task.hh:369
Gecode::Int::TaskTree< TaskView, OmegaNode >::TaskTree
TaskTree(Region &r, const TaskViewArray< TaskView > &t)
Definition tree.hpp:141
Gecode::Int::TaskTree< TaskView, OmegaNode >::init
void init(void)
Definition tree.hpp:115
Gecode::Int::TaskTree< TaskView, OmegaLambdaNode >::update
void update(int i, bool l=true)
Definition tree.hpp:129
Gecode::Int::TaskTree< TaskView, OmegaNode >::root
const OmegaNode & root(void) const
Definition tree.hpp:109
Gecode::Int::TaskTree< TaskView, OmegaNode >::leaf
OmegaNode & leaf(int i)
Definition tree.hpp:103
Gecode::Int::TaskViewArray
Task view array.
Definition task.hh:233
Gecode::Int::Unary::OmegaLambdaNode
Node for an omega lambda tree.
Definition unary.hh:695
Gecode::Int::Unary::OmegaLambdaNode::update
void update(const OmegaLambdaNode &l, const OmegaLambdaNode &r)
Update node from left child l and right child r.
Definition tree.hpp:112
Gecode::Int::Unary::OmegaLambdaNode::init
void init(const OmegaLambdaNode &l, const OmegaLambdaNode &r)
Initialize node from left child l and right child r.
Definition tree.hpp:106
Gecode::Int::Unary::OmegaLambdaNode::lp
int lp
Processing times for subtree.
Definition unary.hh:700
Gecode::Int::Unary::OmegaLambdaNode::undef
static const int undef
Undefined task.
Definition unary.hh:698
Gecode::Int::Unary::OmegaLambdaNode::resEct
int resEct
Node which is responsible for lect.
Definition unary.hh:704
Gecode::Int::Unary::OmegaLambdaNode::resLp
int resLp
Node which is responsible for lp.
Definition unary.hh:706
Gecode::Int::Unary::OmegaLambdaNode::lect
int lect
Earliest completion times for subtree.
Definition unary.hh:702
Gecode::Int::Unary::OmegaLambdaTree::responsible
int responsible(void) const
Return responsible task.
Definition tree.hpp:209
Gecode::Int::Unary::OmegaLambdaTree::OmegaLambdaTree
OmegaLambdaTree(Region &r, const TaskViewArray< TaskView > &t, bool inc=true)
Initialize tree for tasks t with all tasks included, if inc is true.
Definition tree.hpp:136
Gecode::Int::Unary::OmegaLambdaTree::shift
void shift(int i)
Shift task with index i from omega to lambda.
Definition tree.hpp:163
Gecode::Int::Unary::OmegaLambdaTree::lremove
void lremove(int i)
Remove task with index i from lambda.
Definition tree.hpp:193
Gecode::Int::Unary::OmegaLambdaTree::lempty
bool lempty(void) const
Whether has responsible task.
Definition tree.hpp:203
Gecode::Int::Unary::OmegaLambdaTree::oinsert
void oinsert(int i)
Insert task with index i to omega.
Definition tree.hpp:175
Gecode::Int::Unary::OmegaLambdaTree::lect
int lect(void) const
Return earliest completion time of all tasks excluding lambda tasks.
Definition tree.hpp:221
Gecode::Int::Unary::OmegaLambdaTree::linsert
void linsert(int i)
Insert task with index i to lambda.
Definition tree.hpp:183
Gecode::Int::Unary::OmegaLambdaTree::ect
int ect(void) const
Return earliest completion time of all tasks.
Definition tree.hpp:215
Gecode::Int::Unary::OmegaNode
Node for an omega tree.
Definition unary.hh:660
Gecode::Int::Unary::OmegaNode::ect
int ect
Earliest completion time for subtree.
Definition unary.hh:665
Gecode::Int::Unary::OmegaNode::init
void init(const OmegaNode &l, const OmegaNode &r)
Initialize node from left child l and right child r.
Definition tree.hpp:43
Gecode::Int::Unary::OmegaNode::p
int p
Processing time for subtree.
Definition unary.hh:663
Gecode::Int::Unary::OmegaNode::update
void update(const OmegaNode &l, const OmegaNode &r)
Update node from left child l and right child r.
Definition tree.hpp:48
Gecode::Int::Unary::OmegaTree::insert
void insert(int i)
Insert task with index i.
Definition tree.hpp:65
Gecode::Int::Unary::OmegaTree::ect
int ect(void) const
Return earliest completion time of all tasks.
Definition tree.hpp:80
Gecode::Int::Unary::OmegaTree::remove
void remove(int i)
Remove task with index i.
Definition tree.hpp:73
Gecode::Int::Unary::OmegaTree::OmegaTree
OmegaTree(Region &r, const TaskViewArray< TaskView > &t)
Initialize tree for tasks t.
Definition tree.hpp:55
Gecode::Region
Handle to region.
Definition region.hpp:55
Gecode::Int::OffsetView::update
void update(const NoOffset &)
Integer-precision integer scale view.
Definition view.hpp:638
Gecode::Int::Limits::infinity
const int infinity
Infinity for integers.
Definition int.hh:120
Gecode::Int::Unary
Int for unary resources
Gecode::Int
Finite domain integers.
Gecode::Int::plus
int plus(int x, int y)
Safe addition in case x is -Int::Limits::infinity.
Definition tree.hpp:39
Gecode
Gecode toplevel namespace
forceinline
#define forceinline
Definition config.hpp:187