-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleveled.cpp
More file actions
375 lines (343 loc) · 15 KB
/
Copy pathleveled.cpp
File metadata and controls
375 lines (343 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/***************************************************************************
* *
* Copyright (C) 2007-2015 by frePPLe bv *
* *
* 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 <climits>
#include <ranges>
#include "frepple/model.h"
// Uncomment the following line to create debugging statements during the
// clustering and leveling algorithm.
// #define CLUSTERDEBUG
namespace frepple {
bool HasLevel::recomputeLevels = false;
bool HasLevel::computationBusy = false;
int HasLevel::numberOfClusters = 0;
short HasLevel::numberOfLevels = 0;
void HasLevel::computeLevels() {
computationBusy = true;
// Get exclusive access to this function in a multi-threaded environment.
static mutex levelcomputationbusy;
lock_guard<mutex> l(levelcomputationbusy);
// Another thread may already have computed the levels while this thread was
// waiting for the lock. In that case the while loop will be skipped.
multimap<Operation*, Demand*> clustereddeliveries;
while (recomputeLevels) {
// Force creation of all delivery operations
for (auto& gdem : Demand::all()) {
auto dlvr = gdem.getDeliveryOperation();
if (dlvr && gdem.getOwner() && gdem.getOwner()->hasType<DemandGroup>() &&
static_cast<DemandGroup*>(gdem.getOwner())->getPolicy() !=
Demand::POLICY_INDEPENDENT) {
auto search = clustereddeliveries.equal_range(dlvr);
bool exists = false;
for (auto it = search.first; it != search.second; ++it) {
if (it->second == gdem.getOwner()) {
exists = true;
break;
}
}
if (!exists)
clustereddeliveries.insert(make_pair(dlvr, gdem.getOwner()));
}
}
// Reset current levels on buffers, resources and operations.
// Creating the producing operations of the buffers can cause new buffers
// to be created. We repeat this loop until no new buffers are being added.
// This isn't the most efficient loop, but it remains cheap and fast...
auto numbufs = Buffer::size();
while (true) {
for (auto& gop : Operation::all()) {
gop.cluster = 0;
gop.lvl = -1;
for (auto& fl : gop.getFlows()) fl.getBuffer();
}
for (auto& gbuf : Buffer::all()) {
gbuf.cluster = 0;
gbuf.lvl = -1;
gbuf.getProducingOperation();
}
auto numbufs_after = Buffer::size();
if (numbufs == numbufs_after)
break;
else
numbufs = numbufs_after;
}
for (auto& gres : Resource::all()) {
gres.cluster = 0;
gres.lvl = -1;
}
// When during the computation below the recomputeLevels flag is switched
// on again by some model change, the while loop will rerun the calculation
// again.
recomputeLevels = false;
// Loop through all operations
stack<pair<Operation*, int> > opstack;
Operation* cur_oper;
int cur_level;
Buffer* cur_buf;
const Flow* cur_Flow;
bool search_level;
int cur_cluster;
numberOfLevels = 0;
numberOfClusters = 0;
map<Operation*, short> visited;
for (auto& g : Operation::all()) {
// Select a new cluster number
if (g.cluster)
cur_cluster = g.cluster;
else {
// Detect hanging operations
if (g.getFlows().empty() && g.getLoads().empty() && !g.getOwner() &&
g.getSubOperations().empty() && g.getDependencies().empty()) {
// Cluster 0 keeps all dangling operations
g.lvl = 0;
continue;
}
cur_cluster = ++numberOfClusters;
if (numberOfClusters >= INT_MAX)
throw LogicException("Too many clusters");
}
#ifdef CLUSTERDEBUG
logger << "Investigating operation '" << g << "' - current cluster "
<< g.cluster << '\n';
#endif
// Do we need to activate the level search?
// Criterion are:
// - Not used in a super operation
// - Have a producing flow on the operation itself
// or on any of its sub operations
search_level = false;
if (!g.getOwner()) {
search_level = true;
// Does the operation itself have producing flows?
for (auto fl = g.getFlows().begin();
fl != g.getFlows().end() && search_level; ++fl)
if (fl->isProducer() && fl->getBuffer()->hasConsumingFlows())
search_level = false;
if (search_level) {
// Do suboperations have a producing flow?
for (auto i = g.getSubOperations().rbegin();
i != g.getSubOperations().rend() && search_level; ++i)
for (auto fl = (*i)->getOperation()->getFlows().begin();
fl != (*i)->getOperation()->getFlows().end() && search_level;
++fl)
if (fl->isProducer() && fl->getBuffer()->hasConsumingFlows())
search_level = false;
}
}
// If both the level and the cluster are de-activated, then we can move on
if (!search_level && g.cluster) continue;
// Start recursing
// Note that as soon as push an operation on the stack we set its
// cluster and/or level. This is avoid that operations are needlessly
// pushed a second time on the stack.
opstack.emplace(&g, search_level ? 0 : -1);
visited.clear();
g.cluster = cur_cluster;
if (search_level) g.lvl = 0;
while (!opstack.empty()) {
// Take the top of the stack
cur_oper = opstack.top().first;
cur_level = opstack.top().second;
opstack.pop();
// Keep track of the maximum number of levels
if (cur_level > numberOfLevels) numberOfLevels = cur_level;
#ifdef CLUSTERDEBUG
logger << " Recursing in Operation '" << *(cur_oper)
<< "' - current level " << cur_level << '\n';
#endif
// Detect loops in the supply chain
auto detectloop = visited.find(cur_oper);
if (detectloop == visited.end())
// Keep track of operations already visited
visited.insert(make_pair(cur_oper, 0));
else if (++(detectloop->second) > 1)
// Already visited this operation enough times - don't repeat
continue;
// Push sub operations on the stack
for (auto& i :
std::ranges::reverse_view(cur_oper->getSubOperations())) {
if (i->getOperation()->lvl < cur_level) {
// Search level and cluster
opstack.emplace(i->getOperation(), cur_level);
i->getOperation()->lvl = cur_level;
i->getOperation()->cluster = cur_cluster;
} else if (!i->getOperation()->cluster) {
// Search for clusters information only
opstack.emplace(i->getOperation(), -1);
i->getOperation()->cluster = cur_cluster;
}
// else: no search required
}
// Push super operations on the stack
if (cur_oper->getOwner()) {
if (cur_oper->getOwner()->lvl < cur_level) {
// Search level and cluster
opstack.emplace(cur_oper->getOwner(), cur_level);
cur_oper->getOwner()->lvl = cur_level;
cur_oper->getOwner()->cluster = cur_cluster;
} else if (!cur_oper->getOwner()->cluster) {
// Search for clusters information only
opstack.emplace(cur_oper->getOwner(), -1);
cur_oper->getOwner()->cluster = cur_cluster;
}
// else: no search required
}
// Push dependencies on the stack
for (auto dpd : cur_oper->getDependencies()) {
auto new_oper = dpd->getOperation();
if (new_oper == cur_oper) new_oper = dpd->getBlockedBy();
if (new_oper->lvl < cur_level + 1) {
// Search level and cluster
opstack.emplace(new_oper, cur_level + 1);
new_oper->lvl = cur_level + 1;
new_oper->cluster = cur_cluster;
} else if (!new_oper->cluster) {
// Search for clusters information only
opstack.emplace(new_oper, -1);
new_oper->cluster = cur_cluster;
}
// else: no search required
}
// Update level of resources linked to current operation
for (const auto& gres : cur_oper->getLoads()) {
stack<Resource*> rsrc;
auto resptr = gres.getResource();
while (resptr->getOwner()) resptr = resptr->getOwner();
rsrc.push(resptr);
while (!rsrc.empty()) {
resptr = rsrc.top();
rsrc.pop();
// Update the level of the resource
if (resptr->lvl < cur_level) resptr->lvl = cur_level;
// Update the cluster of the resource and operations using it
if (!resptr->cluster) {
resptr->cluster = cur_cluster;
// Find more operations connected to this cluster by the resource
for (const auto& resops : resptr->getLoads()) {
if (!resops.getOperation()->cluster) {
opstack.emplace(resops.getOperation(), -1);
resops.getOperation()->cluster = cur_cluster;
}
}
}
// Add all child resources to the stack
for (auto chld = resptr->getMembers(); chld != Resource::end();
++chld)
rsrc.push(&*chld);
}
}
// Now loop through all flows of the operation
for (const auto& gflow : cur_oper->getFlows()) {
cur_Flow = &gflow;
cur_buf = cur_Flow->getBuffer();
// Check whether the level search needs to continue
search_level = cur_level != -1 && cur_buf->lvl < cur_level + 1;
// Check if the buffer needs processing
if (search_level || !cur_buf->cluster) {
// Update the cluster of the current buffer
cur_buf->cluster = cur_cluster;
// Loop through all flows of the buffer
for (auto buffl = cur_buf->getFlows().begin();
buffl != cur_buf->getFlows().end(); ++buffl) {
// Check level recursion
if (cur_Flow->isConsumer() && search_level &&
(!cur_Flow->getOperation()
->hasType<OperationItemDistribution>() ||
static_cast<OperationItemDistribution*>(
cur_Flow->getOperation())
->getPriority())) {
if (buffl->getOperation()->lvl < cur_level + 1 &&
&*buffl != cur_Flow && buffl->isProducer()) {
opstack.emplace(buffl->getOperation(), cur_level + 1);
buffl->getOperation()->lvl = cur_level + 1;
buffl->getOperation()->cluster = cur_cluster;
} else if (!buffl->getOperation()->cluster) {
opstack.emplace(buffl->getOperation(), -1);
buffl->getOperation()->cluster = cur_cluster;
}
if (cur_level + 1 > numberOfLevels)
numberOfLevels = cur_level + 1;
cur_buf->lvl = cur_level + 1;
}
// Check cluster recursion
else if (!buffl->getOperation()->cluster) {
opstack.emplace(buffl->getOperation(), -1);
buffl->getOperation()->cluster = cur_cluster;
}
}
} // End of needs-processing if statement
else if (cur_buf->lvl < 0 && !cur_Flow->isConsumer())
cur_buf->lvl = 0;
// Add all buffers for this item to the same cluster
Item::bufferIterator buf_iter(cur_Flow->getBuffer()->getItem());
while (Buffer* tmpbuf = buf_iter.next())
if (!tmpbuf->cluster) {
tmpbuf->cluster = cur_cluster;
for (const auto& buffl : tmpbuf->getFlows()) {
if (!buffl.getOperation()->cluster) {
opstack.emplace(buffl.getOperation(), -1);
buffl.getOperation()->cluster = cur_cluster;
}
}
}
} // End of flow loop
// Push grouped deliveries on the stack
auto search = clustereddeliveries.equal_range(cur_oper);
for (auto it = search.first; it != search.second; ++it) {
for (auto m = it->second->getMembers(); m != Demand::end(); ++m) {
auto dlvr = m->getDeliveryOperation();
if (dlvr && !dlvr->cluster) {
opstack.emplace(dlvr, -1);
dlvr->cluster = cur_cluster;
}
}
}
} // End while stack not empty
} // End of Operation loop
// Copy the level from generic buffers to the specific mto-buffers
// TODO this logic will no longer apply when the mto-buffers can
// have their own producing operation.
for (auto& gbuf : Buffer::all()) {
if (!gbuf.getBatch() || !gbuf.getItem()) continue;
Buffer* generic = nullptr;
Item::bufferIterator buf_iter(gbuf.getItem());
while (Buffer* tmpbuf = buf_iter.next()) {
if (!tmpbuf->getBatch()) {
generic = tmpbuf;
break;
}
}
if (generic) {
Item::bufferIterator buf_iter(gbuf.getItem());
while (Buffer* tmpbuf = buf_iter.next()) {
if (tmpbuf->getBatch()) tmpbuf->copyLevelAndCluster(generic);
}
}
}
} // End of while recomputeLevels. The loop will be repeated as long as model
// changes are done during the recomputation.
// Unlock the exclusive access to this function
computationBusy = false;
}
} // namespace frepple