-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem.cpp
More file actions
648 lines (586 loc) · 20.4 KB
/
Copy pathproblem.cpp
File metadata and controls
648 lines (586 loc) · 20.4 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/***************************************************************************
* *
* 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 "frepple/model.h"
namespace frepple {
bool Plannable::anyChange = false;
bool Plannable::computationBusy = false;
const MetaCategory* Problem::metadata;
const MetaClass *ProblemMaterialShortage::metadata,
*ProblemInvalidData::metadata, *ProblemPrecedence::metadata,
*ProblemBeforeCurrent::metadata, *ProblemCapacityOverload::metadata,
*ProblemAwaitSupply::metadata, *ProblemSyncDemand::metadata;
const MetaClass *ConstraintOverdueDemand::metadata,
*ConstraintPurchasingLeadTime::metadata,
*ConstraintDistributionLeadTime::metadata,
*ConstraintManufacturingLeadTime::metadata;
int Problem::initialize() {
// Initialize the problem metadata.
metadata = MetaCategory::registerCategory<Problem>("problem", "problems");
registerFields<Problem>(const_cast<MetaCategory*>(metadata));
// Register classes.
// We register them as default to avoid saving an xsi:type header. This
// has no further impact as there is no factory method.
ProblemMaterialShortage::metadata =
MetaClass::registerClass<ProblemMaterialShortage>(
"problem", "material shortage", true);
ProblemInvalidData::metadata = MetaClass::registerClass<ProblemInvalidData>(
"problem", "invalid data", true);
ProblemPrecedence::metadata = MetaClass::registerClass<ProblemPrecedence>(
"problem", "precedence", true);
ProblemBeforeCurrent::metadata =
MetaClass::registerClass<ProblemBeforeCurrent>("problem",
"before current", true);
ProblemAwaitSupply::metadata = MetaClass::registerClass<ProblemAwaitSupply>(
"problem", "await supply", true);
ProblemSyncDemand::metadata = MetaClass::registerClass<ProblemSyncDemand>(
"problem", "sync demand", true);
ProblemCapacityOverload::metadata =
MetaClass::registerClass<ProblemCapacityOverload>("problem", "overload",
true);
ConstraintOverdueDemand::metadata =
MetaClass::registerClass<ConstraintOverdueDemand>("problem", "overdue",
true);
ConstraintPurchasingLeadTime::metadata =
MetaClass::registerClass<ConstraintPurchasingLeadTime>(
"problem", "purchasing lead time", true);
ConstraintDistributionLeadTime::metadata =
MetaClass::registerClass<ConstraintDistributionLeadTime>(
"problem", "distribution lead time", true);
ConstraintManufacturingLeadTime::metadata =
MetaClass::registerClass<ConstraintManufacturingLeadTime>(
"problem", "manufacturing lead time", true);
// Initialize the Python type
auto& x = PythonExtension<Problem>::getPythonType();
x.setName("problem");
x.setDoc("frePPLe problem");
x.supportgetattro();
x.supportstr();
x.addMethod("toXML", toXML, METH_VARARGS, "return a XML representation");
metadata->setPythonClass(x);
return x.typeReady();
}
bool Problem::operator<(const Problem& a) const {
// 1. Sort based on entity
assert(owner == a.owner);
// 2. Sort based on type
if (getType() != a.getType()) return getType() < a.getType();
// 3. Sort based on start date
return getDates().getStart() < a.getDates().getStart();
}
void Problem::addProblem() {
assert(owner);
if ((owner->firstProblem && *this < *(owner->firstProblem)) ||
!owner->firstProblem) {
// Insert as the first problem in the list
nextProblem = owner->firstProblem;
owner->firstProblem = this;
} else {
// Insert in the middle or at the end of the list
Problem* curProblem = owner->firstProblem->nextProblem;
Problem* prevProblem = owner->firstProblem;
while (curProblem && !(*this < *curProblem)) {
prevProblem = curProblem;
curProblem = curProblem->nextProblem;
}
nextProblem = curProblem;
prevProblem->nextProblem = this;
}
}
void Problem::removeProblem() {
// Fast delete method: the code triggering this method is responsible of
// maintaining the problem container
if (!owner) return;
if (owner->firstProblem == this)
// Removal from the head of the list
owner->firstProblem = nextProblem;
else {
// Removal from the middle of the list
Problem* prev = owner->firstProblem;
for (Problem* cur = owner->firstProblem; cur; cur = cur->nextProblem) {
if (cur == this) {
// Found it!
prev->nextProblem = nextProblem;
return;
}
prev = cur;
}
// The problem wasn't found in the list. This shouldn't happen...
throw LogicException("Corrupted problem list");
}
}
void Plannable::setDetectProblems(bool b) {
if (useProblemDetection && !b)
// We are switching from 'yes' to 'no': delete all existing problems
Problem::clearProblems(*this);
else if (!useProblemDetection && b)
// We are switching from 'no' to 'yes': mark as changed for the next
// problem detection call
setChanged();
// Update the flag
useProblemDetection = b;
}
void Problem::List::transfer(HasProblems* newowner) {
if (!newowner) return;
if (!newowner->firstProblem) {
newowner->firstProblem = first;
} else {
auto* ptr = newowner->firstProblem;
while (ptr->nextProblem) ptr = ptr->nextProblem;
ptr->nextProblem = first;
}
first = nullptr;
}
void Plannable::computeProblems() {
// Exit immediately if the list is up to date
if (!anyChange && !computationBusy) return;
computationBusy = true;
// Get exclusive access to this function in a multi-threaded environment.
static mutex computationbusy;
{
lock_guard<mutex> l(computationbusy);
// Another thread may already have computed it while this thread was
// waiting for the lock
while (anyChange) {
// Reset to change flag. Note that during the computation the flag
// could be switched on again by some model change in a different thread.
anyChange = false;
// Loop through all entities
for (HasProblems::EntityIterator i; i != HasProblems::endEntity(); ++i) {
Plannable* e = i->getEntity();
if (e->getChanged() && e->getDetectProblems()) i->updateProblems();
}
// Mark the entities as unchanged
for (HasProblems::EntityIterator j; j != HasProblems::endEntity(); ++j) {
Plannable* e = j->getEntity();
if (e->getChanged() && e->getDetectProblems()) e->setChanged(false);
}
}
// Unlock the exclusive access to this function
computationBusy = false;
}
}
void Problem::clearProblems() {
// Loop through all entities, and call clearProblems(i)
for (HasProblems::EntityIterator i = HasProblems::beginEntity();
i != HasProblems::endEntity(); ++i) {
clearProblems(*i);
i->getEntity()->setChanged(true);
}
}
void Problem::clearConstraints(Object& p) {
for (auto dmd = Demand::begin(); dmd != Demand::end(); ++dmd)
dmd->getConstraints().erase(p);
}
void Problem::clearProblems(HasProblems& p, bool setchanged,
bool includeInvalidData) {
// Nothing to do
if (!p.firstProblem) return;
// Delete all problems in the list
Problem* keepfirst = nullptr;
for (Problem* cur = p.firstProblem; cur;) {
Problem* del = cur;
cur = cur->nextProblem;
if (includeInvalidData || typeid(*del) != typeid(ProblemInvalidData)) {
del->owner = nullptr;
delete del;
} else if (!keepfirst) {
keepfirst = del;
if (keepfirst) keepfirst->nextProblem = del;
del->nextProblem = nullptr;
}
}
p.firstProblem = keepfirst;
// Mark as changed
if (setchanged) {
auto tmp = p.getEntity();
if (tmp) tmp->setChanged();
}
}
Problem::iterator Plannable::getProblems() const {
if (getChanged()) const_cast<Plannable*>(this)->updateProblems();
return Problem::iterator(firstProblem);
}
HasProblems::EntityIterator::EntityIterator() : type(0) {
// Buffer
bufIter = new Buffer::iterator(Buffer::begin());
if (*bufIter != Buffer::end()) return;
// Move on to resource if there are no buffers
delete bufIter;
type = 1;
resIter = new Resource::iterator(Resource::begin());
if (*resIter != Resource::end()) return;
// Move on to operationplans if there are no resources either
delete resIter;
type = 2;
operIter = new OperationPlan::iterator(OperationPlan::begin());
if (*operIter != OperationPlan::end()) return;
// Move on to demands if there are no operationplans either
delete operIter;
type = 3;
demIter = new Demand::iterator(Demand::begin());
if (*demIter != Demand::end()) return;
// Move on to operations if there are no demands either
delete demIter;
type = 4;
opIter = new Operation::iterator(Operation::begin());
if (*opIter == Operation::end()) {
// There is nothing at all in this model
delete opIter;
type = 5;
}
}
HasProblems::EntityIterator& HasProblems::EntityIterator::operator++() {
switch (type) {
case 0:
// Buffer
if (*bufIter != Buffer::end())
if (++(*bufIter) != Buffer::end()) return *this;
++type;
delete bufIter;
resIter = new Resource::iterator(Resource::begin());
if (*resIter != Resource::end()) return *this;
// Note: no break statement
case 1:
// Resource
if (*resIter != Resource::end())
if (++(*resIter) != Resource::end()) return *this;
++type;
delete resIter;
operIter = new OperationPlan::iterator(OperationPlan::begin());
if (*operIter != OperationPlan::end()) return *this;
// Note: no break statement
case 2:
// Operationplan
if (*operIter != OperationPlan::end())
if (++(*operIter) != OperationPlan::end()) return *this;
++type;
delete operIter;
demIter = new Demand::iterator(Demand::begin());
if (*demIter != Demand::end()) return *this;
// Note: no break statement
case 3:
// Demand
if (*demIter != Demand::end())
if (++(*demIter) != Demand::end()) return *this;
++type;
delete demIter;
opIter = new Operation::iterator(Operation::begin());
if (*opIter != Operation::end()) return *this;
// Note: no break statement
case 4:
// Operation
if (*opIter != Operation::end())
if (++(*opIter) != Operation::end()) return *this;
// Ended recursing of all entities
++type;
delete opIter;
opIter = nullptr;
return *this;
}
throw LogicException("Unreachable code reached");
}
HasProblems::EntityIterator::~EntityIterator() {
switch (type) {
case 0:
delete bufIter;
return;
case 1:
delete resIter;
return;
case 2:
delete operIter;
return;
case 3:
delete demIter;
return;
case 4:
delete opIter;
return;
}
}
HasProblems::EntityIterator::EntityIterator(const EntityIterator& o) {
// Delete old iterator
this->~EntityIterator();
// Populate new values
type = o.type;
if (type == 0)
bufIter = new Buffer::iterator(*(o.bufIter));
else if (type == 1)
resIter = new Resource::iterator(*(o.resIter));
else if (type == 2)
operIter = new OperationPlan::iterator(*(o.operIter));
else if (type == 3)
demIter = new Demand::iterator(*(o.demIter));
else if (type == 4)
opIter = new Operation::iterator(*(o.opIter));
}
HasProblems::EntityIterator& HasProblems::EntityIterator::operator=(
const EntityIterator& o) {
// Gracefully handle self assignment
if (this == &o) return *this;
// Delete old iterator
this->~EntityIterator();
// Populate new values
type = o.type;
if (type == 0)
bufIter = new Buffer::iterator(*(o.bufIter));
else if (type == 1)
resIter = new Resource::iterator(*(o.resIter));
else if (type == 2)
operIter = new OperationPlan::iterator(*(o.operIter));
else if (type == 3)
demIter = new Demand::iterator(*(o.demIter));
else if (type == 4)
opIter = new Operation::iterator(*(o.opIter));
return *this;
}
bool HasProblems::EntityIterator::operator!=(const EntityIterator& t) const {
// Different iterator type, thus always different and return false
if (type != t.type) return true;
// Same iterator type, more granular comparison required
switch (type) {
case 0:
return *bufIter != *(t.bufIter);
case 1:
return *resIter != *(t.resIter);
case 2:
return *operIter != *(t.operIter);
case 3:
return *demIter != *(t.demIter);
case 4:
return *opIter != *(t.opIter);
default:
// Always return true for higher type numbers. This should happen only
// when comparing with the end of list element.
return false;
}
}
HasProblems& HasProblems::EntityIterator::operator*() const {
switch (type) {
case 0:
return **bufIter;
case 1:
return **resIter;
case 2:
return **operIter;
case 3:
return **demIter;
case 4:
return **opIter;
default:
throw LogicException("Unknown problem entity found");
}
}
HasProblems* HasProblems::EntityIterator::operator->() const {
switch (type) {
case 0:
return &**bufIter;
case 1:
return &**resIter;
case 2:
return &**operIter;
case 3:
return &**demIter;
case 4:
return &**opIter;
default:
throw LogicException("Unknown problem entity found");
}
}
HasProblems::EntityIterator HasProblems::beginEntity() {
return EntityIterator();
}
HasProblems::EntityIterator HasProblems::endEntity() {
// Note that we give call a constructor with type 5, in order to allow
// a fast comparison.
return EntityIterator(5);
}
Problem::iterator& Problem::iterator::operator++() {
// Incrementing beyond the end
if (!iter) return *this;
// Move to the next problem
iter = iter->nextProblem;
// Move to the next entity
// We need a while loop here because some entities can be without problems
while (!iter && !owner && eiter && *eiter != HasProblems::endEntity()) {
++(*eiter);
if (*eiter != HasProblems::endEntity()) iter = (*eiter)->firstProblem;
}
return *this;
}
Problem::iterator Problem::begin() { return iterator(); }
Problem::iterator Problem::begin(HasProblems* i, bool refresh) {
// Null pointer passed, loop through the full list anyway
if (!i) return begin();
// Return an iterator for a single entity
if (refresh) i->updateProblems();
return iterator(i);
}
const Problem::iterator Problem::end() {
return iterator(static_cast<Problem*>(nullptr));
}
void Problem::List::clear(Problem* c) {
// Unchain the predecessor
if (c) {
for (Problem* x = first; x; x = x->nextProblem)
if (x->nextProblem == c) {
x->nextProblem = nullptr;
break;
}
}
// Delete each constraint in the list
for (Problem* cur = c ? c : first; cur;) {
Problem* del = cur;
cur = cur->nextProblem;
del->owner = nullptr;
del->resetReferenceCount();
delete del;
}
// Set the header to nullptr
if (!c) first = nullptr;
}
void Problem::List::erase(Object& p) {
Problem* prev = nullptr;
for (Problem* x = first; x;) {
if (x->getOwner() == &p ||
(x->getOwner() && x->getOwner()->hasType<OperationPlan>() &&
p.hasType<Operation>() &&
static_cast<OperationPlan*>(x->getOwner())->getOperation() == &p)) {
// Remove from the list
auto tmp = x;
if (prev)
prev->nextProblem = x->nextProblem;
else
first = x->nextProblem;
x = x->nextProblem;
delete tmp;
} else {
prev = x;
x = x->nextProblem;
}
}
}
Problem* Problem::List::push(const MetaClass* m, const Object* o, Date st,
Date nd, double w, Operation* oper) {
// Find the end of the list
Problem* cur = first;
while (cur && cur->nextProblem && cur->getOwner() != o)
cur = cur->nextProblem;
if (cur && cur->getOwner() == o)
// Duplicate problem: stop here.
return cur;
// Create a new problem
Problem* p = nullptr;
if (m == ProblemCapacityOverload::metadata) {
p = new ProblemCapacityOverload(
const_cast<Resource*>(dynamic_cast<const Resource*>(o)), st, nd, w,
false);
if (oper)
static_cast<ProblemCapacityOverload*>(p)->setOperation(oper);
else
throw LogicException(
"Logging a capacity constraint should always be on an operation");
} else if (m == ProblemMaterialShortage::metadata)
p = new ProblemMaterialShortage(
const_cast<Buffer*>(dynamic_cast<const Buffer*>(o)), st, nd, w, false);
else if (m == ProblemBeforeCurrent::metadata) {
auto oper = dynamic_cast<const Operation*>(o);
if (oper->hasType<OperationItemDistribution>())
p = new ConstraintDistributionLeadTime(const_cast<Operation*>(oper), st,
nd);
else if (oper->hasType<OperationItemSupplier>())
p = new ConstraintPurchasingLeadTime(const_cast<Operation*>(oper), st,
nd);
else if (!oper->hasType<OperationDelivery>())
p = new ConstraintManufacturingLeadTime(const_cast<Operation*>(oper), st,
nd);
} else if (m == ProblemAwaitSupply::metadata) {
auto owner = const_cast<Buffer*>(dynamic_cast<const Buffer*>(o));
if (owner)
p = new ProblemAwaitSupply(owner, st, nd);
else {
auto owner = const_cast<Operation*>(dynamic_cast<const Operation*>(o));
if (owner) p = new ProblemAwaitSupply(owner, st, nd);
}
} else if (m == ProblemSyncDemand::metadata)
p = new ProblemSyncDemand(
const_cast<Demand*>(dynamic_cast<const Demand*>(o)), st, nd);
else
throw LogicException("Problem factory can't create this type of problem");
if (p) {
// Link the problem in the list
if (cur)
cur->nextProblem = p;
else
first = p;
Py_INCREF(p);
}
return p;
}
void Problem::List::pop(Problem* p) {
Problem* q = nullptr;
if (p) {
// Skip the problem that was passed as argument
q = p->nextProblem;
p->nextProblem = nullptr;
} else {
// nullptr argument: delete all
q = first;
first = nullptr;
}
// Delete each constraint after the marked one
while (q) {
Problem* del = q;
q = q->nextProblem;
del->owner = nullptr;
del->resetReferenceCount();
delete del;
}
}
Problem* Problem::List::top() const {
for (Problem* p = first; p; p = p->nextProblem)
if (!p->nextProblem) return p;
return nullptr;
}
void Problem::List::push(Problem* p) {
// Find the end of the list
Problem* cur = first;
while (cur && cur->nextProblem && cur != p) cur = cur->nextProblem;
if (!cur)
// Link at the start of the list
first = p;
else if (cur == p)
// Duplicate problem: stop here.
return;
else
// Link at the end of the list
cur->nextProblem = p;
Py_INCREF(p);
}
void Problem::List::clean(const Demand* d) const {
// Check all manufacturing lead time constraints, and keep only the
// critical path.
}
} // namespace frepple