Generated on for Gecode by doxygen 1.18.0
tree.hpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.dev>
5 *
6 * Copyright:
7 * Christian Schulte, 2009
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#include <algorithm>
35
36namespace Gecode { namespace Int { namespace Unary {
37
45 forceinline int
46 clamp_ect(long long int ect) {
47 return static_cast<int>
48 (std::clamp(ect,
49 -static_cast<long long int>(Limits::infinity),
50 static_cast<long long int>(Limits::infinity)));
51 }
52
53 /*
54 * Omega tree
55 */
56
57 forceinline void
59 p = 0; ect = -Limits::llinfinity;
60 }
61
62 forceinline void
64 p = l.p + r.p;
65 ect = std::max(plus(l.ect,r.p), r.ect);
66 }
67
68 template<class TaskView>
69 forceinline
71 : TaskTree<TaskView,OmegaNode>(r,t) {
72 for (int i=0; i<tasks.size(); i++) {
73 leaf(i).p = 0; leaf(i).ect = -Limits::llinfinity;
74 }
75 init();
76 }
77
78 template<class TaskView>
79 forceinline void
81 leaf(i).p = tasks[i].pmin();
82 leaf(i).ect = static_cast<long long int>(tasks[i].est())+tasks[i].pmin();
83 update(i);
84 }
85
86 template<class TaskView>
87 forceinline void
89 leaf(i).p = 0; leaf(i).ect = -Limits::llinfinity;
90 update(i);
91 }
92
93 template<class TaskView>
94 forceinline int
96 return clamp_ect(root().ect);
97 }
98
99 template<class TaskView>
100 forceinline int
102 // Check whether task i is in?
103 OmegaTree<TaskView>& o = const_cast<OmegaTree<TaskView>&>(*this);
104 if (o.leaf(i).ect != -Limits::llinfinity) {
105 o.remove(i);
106 long long int ect = o.root().ect;
107 o.insert(i);
108 return clamp_ect(ect);
109 } else {
110 return clamp_ect(root().ect);
111 }
112 }
113
114
115
116 /*
117 * Omega lambda tree
118 */
119
120 forceinline void
125
126 forceinline void
129 if (l.lp + r.p > l.p + r.lp) {
130 resLp = l.resLp;
131 lp = l.lp + r.p;
132 } else {
133 resLp = r.resLp;
134 lp = l.p + r.lp;
135 }
136 if ((r.lect >= plus(l.ect,r.lp)) && (r.lect >= plus(l.lect,r.p))) {
137 lect = r.lect; resEct = r.resEct;
138 } else if (plus(l.ect,r.lp) >= plus(l.lect,r.p)) {
139 assert(plus(l.ect,r.lp) > r.lect);
140 lect = plus(l.ect,r.lp); resEct = r.resLp;
141 } else {
142 assert((plus(l.lect,r.p) > r.lect) &&
143 (plus(l.lect,r.p) > plus(l.ect,r.lp)));
144 lect = plus(l.lect,r.p); resEct = l.resEct;
145 }
146 }
147
148
149 template<class TaskView>
150 forceinline
153 bool inc)
154 : TaskTree<TaskView,OmegaLambdaNode>(r,t) {
155 if (inc) {
156 // Enter all tasks into tree (omega = all tasks, lambda = empty)
157 for (int i=0; i<tasks.size(); i++) {
158 leaf(i).p = leaf(i).lp = tasks[i].pmin();
159 leaf(i).ect = leaf(i).lect =
160 static_cast<long long int>(tasks[i].est())+tasks[i].pmin();
161 leaf(i).resEct = OmegaLambdaNode::undef;
162 leaf(i).resLp = OmegaLambdaNode::undef;
163 }
164 update();
165 } else {
166 // Enter no tasks into tree (omega = empty, lambda = empty)
167 for (int i=0; i<tasks.size(); i++) {
168 leaf(i).p = leaf(i).lp = 0;
169 leaf(i).ect = leaf(i).lect = -Limits::llinfinity;
170 leaf(i).resEct = OmegaLambdaNode::undef;
171 leaf(i).resLp = OmegaLambdaNode::undef;
172 }
173 init();
174 }
175 }
176
177 template<class TaskView>
178 forceinline void
180 // That means that i is in omega
181 assert(leaf(i).ect > -Limits::llinfinity);
182 leaf(i).p = 0;
183 leaf(i).ect = -Limits::llinfinity;
184 leaf(i).resEct = i;
185 leaf(i).resLp = i;
186 update(i);
187 }
188
189 template<class TaskView>
190 forceinline void
192 leaf(i).p = tasks[i].pmin();
193 leaf(i).ect = static_cast<long long int>(tasks[i].est())+tasks[i].pmin();
194 update(i);
195 }
196
197 template<class TaskView>
198 forceinline void
200 leaf(i).lp = tasks[i].pmin();
201 leaf(i).lect = static_cast<long long int>(tasks[i].est())+tasks[i].pmin();
202 leaf(i).resEct = i;
203 leaf(i).resLp = i;
204 update(i);
205 }
206
207 template<class TaskView>
208 forceinline void
210 leaf(i).lp = 0;
211 leaf(i).lect = -Limits::llinfinity;
212 leaf(i).resEct = OmegaLambdaNode::undef;
213 leaf(i).resLp = OmegaLambdaNode::undef;
214 update(i);
215 }
216
217 template<class TaskView>
218 forceinline bool
220 return root().resEct < 0;
221 }
222
223 template<class TaskView>
224 forceinline int
226 return root().resEct;
227 }
228
229 template<class TaskView>
230 forceinline int
232 return clamp_ect(root().ect);
233 }
234
235 template<class TaskView>
236 forceinline int
238 return clamp_ect(root().lect);
239 }
240
241}}}
242
243// STATISTICS: int-prop
const TaskViewArray< TaskView > & tasks
Definition task.hh:366
TaskTree(Region &r, const TaskViewArray< TaskView > &t)
Definition tree.hpp:135
const OmegaNode & root(void) const
Definition tree.hpp:103
Task view array.
Definition task.hh:233
Node for an omega lambda tree.
Definition unary.hh:695
void update(const OmegaLambdaNode &l, const OmegaLambdaNode &r)
Update node from left child l and right child r.
Definition tree.hpp:127
void init(const OmegaLambdaNode &l, const OmegaLambdaNode &r)
Initialize node from left child l and right child r.
Definition tree.hpp:121
static const int undef
Undefined task.
Definition unary.hh:698
int resEct
Node which is responsible for lect.
Definition unary.hh:704
long long int lp
Processing times for subtree.
Definition unary.hh:700
long long int lect
Earliest completion times for subtree.
Definition unary.hh:702
int resLp
Node which is responsible for lp.
Definition unary.hh:706
int responsible(void) const
Return responsible task.
Definition tree.hpp:225
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:151
void shift(int i)
Shift task with index i from omega to lambda.
Definition tree.hpp:179
void lremove(int i)
Remove task with index i from lambda.
Definition tree.hpp:209
bool lempty(void) const
Whether has responsible task.
Definition tree.hpp:219
void oinsert(int i)
Insert task with index i to omega.
Definition tree.hpp:191
int lect(void) const
Return earliest completion time of all tasks excluding lambda tasks.
Definition tree.hpp:237
void linsert(int i)
Insert task with index i to lambda.
Definition tree.hpp:199
int ect(void) const
Return earliest completion time of all tasks.
Definition tree.hpp:231
Node for an omega tree.
Definition unary.hh:660
void init(const OmegaNode &l, const OmegaNode &r)
Initialize node from left child l and right child r.
Definition tree.hpp:58
long long int ect
Earliest completion time for subtree.
Definition unary.hh:665
void update(const OmegaNode &l, const OmegaNode &r)
Update node from left child l and right child r.
Definition tree.hpp:63
long long int p
Processing time for subtree.
Definition unary.hh:663
void insert(int i)
Insert task with index i.
Definition tree.hpp:80
int ect(void) const
Return earliest completion time of all tasks.
Definition tree.hpp:95
void remove(int i)
Remove task with index i.
Definition tree.hpp:88
OmegaTree(Region &r, const TaskViewArray< TaskView > &t)
Initialize tree for tasks t.
Definition tree.hpp:70
Handle to region.
Definition region.hpp:55
const int infinity
Infinity for integers.
Definition int.hh:125
const long long int llinfinity
Infinity for long long integers.
Definition int.hh:131
Int for unary resources
int clamp_ect(long long int ect)
Clamp a completion time ect of a tree to the int range.
Definition tree.hpp:46
Finite domain integers.
Definition lastval.hh:52
long long int plus(long long int x, long long int y)
Safe addition in case x is -Int::Limits::llinfinity.
Definition tree.hpp:39
Gecode toplevel namespace