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>
* Guido Tack <tack@gecode.org>
*
* Copyright:
* Christian Schulte, 2009
* Guido Tack, 2010
*
* 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>
#include <cmath>
namespace Gecode { namespace Int { namespace Cumulative {
/*
* Omega tree
*/
forceinline void
OmegaNode::init(const OmegaNode&, const OmegaNode&) {
e = 0; env = -Limits::llinfinity;
}
forceinline void
OmegaNode::update(const OmegaNode& l, const OmegaNode& r) {
e = l.e + r.e; env = std::max(plus(l.env,r.e), r.env);
}
template<class TaskView>
OmegaTree<TaskView>::OmegaTree(Region& r, int c0,
const TaskViewArray<TaskView>& t)
: TaskTree<TaskView,OmegaNode>(r,t), c(c0) {
for (int i=0; i<tasks.size(); i++) {
leaf(i).e = 0; leaf(i).env = -Limits::llinfinity;
}
init();
}
template<class TaskView>
forceinline void
OmegaTree<TaskView>::insert(int i) {
leaf(i).e = tasks[i].e();
leaf(i).env =
static_cast<long long int>(c)*tasks[i].est()+tasks[i].e();
update(i);
}
template<class TaskView>
forceinline void
OmegaTree<TaskView>::remove(int i) {
leaf(i).e = 0; leaf(i).env = -Limits::llinfinity;
update(i);
}
template<class TaskView>
forceinline long long int
OmegaTree<TaskView>::env(void) const {
return root().env;
}
/*
* Extended Omega tree
*/
forceinline void
ExtOmegaNode::init(const ExtOmegaNode& l, const ExtOmegaNode& r) {
OmegaNode::init(l,r);
cenv = -Limits::llinfinity;
}
forceinline void
ExtOmegaNode::update(const ExtOmegaNode& l, const ExtOmegaNode& r) {
OmegaNode::update(l,r);
cenv = std::max(plus(l.cenv,r.e), r.cenv);
}
template<class TaskView> void
ExtOmegaTree<TaskView>::init(int ci0) {
ci = ci0;
for (int i=0; i<tasks.size(); i++) {
leaf(i).e = 0;
leaf(i).env = leaf(i).cenv = -Limits::llinfinity;
}
init();
}
template<class TaskView> template<class Node>
ExtOmegaTree<TaskView>::ExtOmegaTree(Region& r, int c0,
const TaskTree<TaskView,Node>& t)
: TaskTree<TaskView,ExtOmegaNode>(r,t), c(c0) {}
template<class TaskView>
forceinline long long int
ExtOmegaTree<TaskView>::env(int i) {
// Enter task i
leaf(i).e = tasks[i].e();
leaf(i).env =
static_cast<long long int>(c)*tasks[i].est()+tasks[i].e();
leaf(i).cenv =
static_cast<long long int>(c-ci)*tasks[i].est()+tasks[i].e();
TaskTree<TaskView,ExtOmegaNode>::update(i);
// Perform computation of node for task with minest
int met = 0;
{
long long int e = 0;
while (!n_leaf(met)) {
if (plus(node[n_right(met)].cenv,e) >
static_cast<long long int>(c-ci) * tasks[i].lct()) {
met = n_right(met);
} else {
e += node[n_right(met)].e; met = n_left(met);
}
}
}
/*
* The following idea to compute the cut in one go is taken from:
* Joseph Scott, Filtering Algorithms for Discrete Resources,
* Master Thesis, Uppsala University, 2010 (in preparation).
*/
// Now perform split from leaf met upwards
long long int a_e = node[met].e;
long long int a_env = node[met].env;
long long int b_e = 0;
while (!n_root(met)) {
if (left(met)) {
b_e += node[n_right(n_parent(met))].e;
} else {
a_env = std::max(a_env, plus(node[n_left(n_parent(met))].env,a_e));
a_e += node[n_left(n_parent(met))].e;
}
met = n_parent(met);
}
return plus(a_env,b_e);
}
/*
* Omega lambda tree
*/
forceinline void
OmegaLambdaNode::init(const OmegaLambdaNode& l, const OmegaLambdaNode& r) {
OmegaNode::init(l,r);
le = 0; lenv = -Limits::llinfinity;
resLe = undef; resLenv = undef;
}
forceinline void
OmegaLambdaNode::update(const OmegaLambdaNode& l, const OmegaLambdaNode& r) {
OmegaNode::update(l,r);
if (l.le + r.e > l.e + r.le) {
le = l.le + r.e;
resLe = l.resLe;
} else {
le = l.e + r.le;
resLe = r.resLe;
}
if ((r.lenv >= plus(l.env,r.le)) && (r.lenv >= plus(l.lenv,r.e))) {
lenv = r.lenv; resLenv = r.resLenv;
} else if (plus(l.env,r.le) >= plus(l.lenv,r.e)) {
assert(plus(l.env,r.le) > r.lenv);
lenv = plus(l.env,r.le); resLenv = r.resLe;
} else {
assert((plus(l.lenv,r.e) > r.lenv) &&
(plus(l.lenv,r.e) > plus(l.env,r.le)));
lenv = plus(l.lenv,r.e); resLenv = l.resLenv;
}
}
template<class TaskView>
OmegaLambdaTree<TaskView>::OmegaLambdaTree(Region& r, int c0,
const TaskViewArray<TaskView>& t)
: TaskTree<TaskView,OmegaLambdaNode>(r,t), c(c0) {
// Enter all tasks into tree (omega = all tasks, lambda = empty)
for (int i=0; i<tasks.size(); i++) {
leaf(i).e = tasks[i].e();
leaf(i).le = 0;
leaf(i).env = static_cast<long long int>(c)*tasks[i].est()+tasks[i].e();
leaf(i).lenv = -Limits::llinfinity;
leaf(i).resLe = OmegaLambdaNode::undef;
leaf(i).resLenv = OmegaLambdaNode::undef;
}
update();
}
template<class TaskView>
forceinline void
OmegaLambdaTree<TaskView>::shift(int i) {
// i is in omega
assert(leaf(i).env > -Limits::llinfinity);
leaf(i).le = leaf(i).e;
leaf(i).e = 0;
leaf(i).lenv = leaf(i).env;
leaf(i).env = -Limits::llinfinity;
leaf(i).resLe = i;
leaf(i).resLenv = i;
update(i);
}
template<class TaskView>
forceinline void
OmegaLambdaTree<TaskView>::lremove(int i) {
// i not in omega but in lambda
assert(leaf(i).env == -Limits::llinfinity);
assert(leaf(i).lenv > -Limits::llinfinity);
leaf(i).le = 0;
leaf(i).lenv = -Limits::llinfinity;
leaf(i).resLe = OmegaLambdaNode::undef;
leaf(i).resLenv = OmegaLambdaNode::undef;
update(i);
}
template<class TaskView>
forceinline bool
OmegaLambdaTree<TaskView>::lempty(void) const {
return root().resLenv < 0;
}
template<class TaskView>
forceinline int
OmegaLambdaTree<TaskView>::responsible(void) const {
return root().resLenv;
}
template<class TaskView>
forceinline long long int
OmegaLambdaTree<TaskView>::env(void) const {
return root().env;
}
template<class TaskView>
forceinline long long int
OmegaLambdaTree<TaskView>::lenv(void) const {
return root().lenv;
}
}}}
// STATISTICS: int-prop
Gecode::Int::Cumulative::ExtOmegaNode
Node for an extended omega tree.
Definition cumulative.hh:582
Gecode::Int::Cumulative::ExtOmegaNode::update
void update(const ExtOmegaNode &l, const ExtOmegaNode &r)
Update node from left child l and right child r.
Definition tree.hpp:98
Gecode::Int::Cumulative::ExtOmegaNode::init
void init(const ExtOmegaNode &l, const ExtOmegaNode &r)
Initialize node from left child l and right child r.
Definition tree.hpp:92
Gecode::Int::Cumulative::ExtOmegaNode::cenv
long long int cenv
Energy envelope for subtree.
Definition cumulative.hh:585
Gecode::Int::Cumulative::ExtOmegaTree::c
int c
Capacity.
Definition cumulative.hh:612
Gecode::Int::Cumulative::ExtOmegaTree::ci
int ci
Definition cumulative.hh:612
Gecode::Int::Cumulative::ExtOmegaTree::env
long long int env(int i)
Compute update for task with index i.
Definition tree.hpp:120
Gecode::Int::Cumulative::ExtOmegaTree::ExtOmegaTree
ExtOmegaTree(Region &r, int c, const TaskTree< TaskView, Node > &t)
Initialize tree for tasks t and capacity c.
Definition tree.hpp:114
Gecode::Int::Cumulative::ExtOmegaTree::init
void init(int ci)
Initialize tasks for current capacity ci.
Definition tree.hpp:104
Gecode::Int::Cumulative::OmegaLambdaNode
Node for an omega lambda tree.
Definition cumulative.hh:625
Gecode::Int::Cumulative::OmegaLambdaNode::le
long long int le
Energy for subtree.
Definition cumulative.hh:630
Gecode::Int::Cumulative::OmegaLambdaNode::init
void init(const OmegaLambdaNode &l, const OmegaLambdaNode &r)
Initialize node from left child l and right child r.
Definition tree.hpp:174
Gecode::Int::Cumulative::OmegaLambdaNode::update
void update(const OmegaLambdaNode &l, const OmegaLambdaNode &r)
Update node from left child l and right child r.
Definition tree.hpp:181
Gecode::Int::Cumulative::OmegaLambdaNode::lenv
long long int lenv
Energy envelope for subtree.
Definition cumulative.hh:632
Gecode::Int::Cumulative::OmegaLambdaNode::resLe
int resLe
Node which is responsible for le.
Definition cumulative.hh:634
Gecode::Int::Cumulative::OmegaLambdaNode::undef
static const int undef
Undefined task.
Definition cumulative.hh:628
Gecode::Int::Cumulative::OmegaLambdaNode::resLenv
int resLenv
Node which is responsible for lenv.
Definition cumulative.hh:636
Gecode::Int::Cumulative::OmegaLambdaTree::responsible
int responsible(void) const
Return responsible task.
Definition tree.hpp:254
Gecode::Int::Cumulative::OmegaLambdaTree::lenv
long long int lenv(void) const
Return energy envelope of all tasks excluding lambda tasks.
Definition tree.hpp:266
Gecode::Int::Cumulative::OmegaLambdaTree::shift
void shift(int i)
Shift task with index i from omega to lambda.
Definition tree.hpp:221
Gecode::Int::Cumulative::OmegaLambdaTree::OmegaLambdaTree
OmegaLambdaTree(Region &r, int c, const TaskViewArray< TaskView > &t)
Initialize tree for tasks t and capcity c with all tasks included in omega.
Definition tree.hpp:204
Gecode::Int::Cumulative::OmegaLambdaTree::c
int c
Capacity.
Definition cumulative.hh:653
Gecode::Int::Cumulative::OmegaLambdaTree::lremove
void lremove(int i)
Remove task with index i from lambda.
Definition tree.hpp:235
Gecode::Int::Cumulative::OmegaLambdaTree::env
long long int env(void) const
Return energy envelope of all tasks.
Definition tree.hpp:260
Gecode::Int::Cumulative::OmegaLambdaTree::lempty
bool lempty(void) const
Whether has responsible task.
Definition tree.hpp:248
Gecode::Int::Cumulative::OmegaNode
Node for an omega tree.
Definition cumulative.hh:547
Gecode::Int::Cumulative::OmegaNode::init
void init(const OmegaNode &l, const OmegaNode &r)
Initialize node from left child l and right child r.
Definition tree.hpp:46
Gecode::Int::Cumulative::OmegaNode::update
void update(const OmegaNode &l, const OmegaNode &r)
Update node from left child l and right child r.
Definition tree.hpp:51
Gecode::Int::Cumulative::OmegaNode::e
long long int e
Energy for subtree.
Definition cumulative.hh:550
Gecode::Int::Cumulative::OmegaNode::env
long long int env
Energy envelope for subtree.
Definition cumulative.hh:552
Gecode::Int::Cumulative::OmegaTree::remove
void remove(int i)
Remove task with index i.
Definition tree.hpp:76
Gecode::Int::Cumulative::OmegaTree::OmegaTree
OmegaTree(Region &r, int c, const TaskViewArray< TaskView > &t)
Initialize tree for tasks t and capacity c.
Definition tree.hpp:56
Gecode::Int::Cumulative::OmegaTree::c
int c
Capacity.
Definition cumulative.hh:569
Gecode::Int::Cumulative::OmegaTree::env
long long int env(void) const
Return energy envelope of all tasks.
Definition tree.hpp:83
Gecode::Int::Cumulative::OmegaTree::insert
void insert(int i)
Insert task with index i.
Definition tree.hpp:67
Gecode::Int::TaskTree< TaskView, OmegaNode >::tasks
const TaskViewArray< TaskView > & tasks
Definition task.hh:369
Gecode::Int::TaskTree< TaskView, ExtOmegaNode >::n_right
static int n_right(int i)
Definition tree.hpp:85
Gecode::Int::TaskTree< TaskView, OmegaNode >::TaskTree
TaskTree(Region &r, const TaskViewArray< TaskView > &t)
Definition tree.hpp:141
Gecode::Int::TaskTree< TaskView, ExtOmegaNode >::n_left
static int n_left(int i)
Definition tree.hpp:73
Gecode::Int::TaskTree< TaskView, ExtOmegaNode >::left
static bool left(int i)
Definition tree.hpp:78
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, ExtOmegaNode >::node
ExtOmegaNode * node
Definition task.hh:371
Gecode::Int::TaskTree< TaskView, ExtOmegaNode >::n_leaf
bool n_leaf(int i) const
Definition tree.hpp:68
Gecode::Int::TaskTree< TaskView, OmegaNode >::root
const OmegaNode & root(void) const
Definition tree.hpp:109
Gecode::Int::TaskTree< TaskView, ExtOmegaNode >::n_parent
static int n_parent(int i)
Definition tree.hpp:97
Gecode::Int::TaskTree< TaskView, OmegaNode >::leaf
OmegaNode & leaf(int i)
Definition tree.hpp:103
Gecode::Int::TaskTree::update
void update(void)
Update all inner nodes of tree after leaves have been initialized.
Definition tree.hpp:122
Gecode::Int::TaskTree< TaskView, ExtOmegaNode >::n_root
static bool n_root(int i)
Definition tree.hpp:63
Gecode::Int::TaskViewArray
Task view array.
Definition task.hh:233
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::Cumulative
Scheduling for cumulative resources
Definition cumulative.cpp:40
Gecode::Int::Limits::llinfinity
const long long int llinfinity
Infinity for long long integers.
Definition int.hh:126
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