-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperation.cpp
More file actions
2651 lines (2455 loc) · 99.4 KB
/
Copy pathoperation.cpp
File metadata and controls
2651 lines (2455 loc) · 99.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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* *
* 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 <ranges>
#include "frepple/model.h"
namespace frepple {
template <class Operation>
Tree utils::HasName<Operation>::st;
const MetaCategory* Operation::metadata;
const MetaClass *OperationFixedTime::metadata, *OperationTimePer::metadata,
*OperationRouting::metadata, *OperationSplit::metadata,
*OperationAlternate::metadata;
Operation::Operationlist Operation::nosubOperations;
int Operation::initialize() {
// Initialize the metadata
metadata = MetaCategory::registerCategory<Operation>(
"operation", "operations", reader, finder);
registerFields<Operation>(const_cast<MetaCategory*>(metadata));
// Initialize the Python class
auto& x = FreppleCategory<Operation>::getPythonType();
x.addMethod("decoupledLeadTime", &getDecoupledLeadTimePython, METH_VARARGS,
"return the total lead time");
x.addMethod("setFence", &setFencePython, METH_VARARGS,
"Update the fence based on date");
x.addMethod("getFence", &getFencePython, METH_NOARGS,
"Retrieve the fence date");
return FreppleCategory<Operation>::initialize();
}
int OperationFixedTime::initialize() {
// Initialize the metadata
metadata = MetaClass::registerClass<OperationFixedTime>(
"operation", "operation_fixed_time", Object::create<OperationFixedTime>,
true);
registerFields<OperationFixedTime>(const_cast<MetaClass*>(metadata));
// Initialize the Python class
auto& x = PythonExtension<
FreppleClass<OperationFixedTime, Operation>>::getPythonType();
x.addMethod("decoupledLeadTime", &getDecoupledLeadTimePython, METH_VARARGS,
"return the total lead time");
x.addMethod("setFence", &setFencePython, METH_VARARGS,
"Update the fence based on date");
x.addMethod("getFence", &getFencePython, METH_NOARGS,
"Retrieve the fence date");
return FreppleClass<OperationFixedTime, Operation>::initialize();
}
int OperationTimePer::initialize() {
// Initialize the metadata
metadata = MetaClass::registerClass<OperationTimePer>(
"operation", "operation_time_per", Object::create<OperationTimePer>);
registerFields<OperationTimePer>(const_cast<MetaClass*>(metadata));
// Initialize the Python class
auto& x = PythonExtension<
FreppleClass<OperationTimePer, Operation>>::getPythonType();
x.addMethod("decoupledLeadTime", &getDecoupledLeadTimePython, METH_VARARGS,
"return the total lead time");
x.addMethod("setFence", &setFencePython, METH_VARARGS,
"Update the fence based on date");
x.addMethod("getFence", &getFencePython, METH_NOARGS,
"Retrieve the fence date");
return FreppleClass<OperationTimePer, Operation>::initialize();
}
int OperationSplit::initialize() {
// Initialize the metadata
metadata = MetaClass::registerClass<OperationSplit>(
"operation", "operation_split", Object::create<OperationSplit>);
registerFields<OperationSplit>(const_cast<MetaClass*>(metadata));
// Initialize the Python class
auto& x =
PythonExtension<FreppleClass<OperationSplit, Operation>>::getPythonType();
x.addMethod("decoupledLeadTime", &getDecoupledLeadTimePython, METH_VARARGS,
"return the total lead time");
x.addMethod("setFence", &setFencePython, METH_VARARGS,
"Update the fence based on date");
x.addMethod("getFence", &getFencePython, METH_NOARGS,
"Retrieve the fence date");
return FreppleClass<OperationSplit, Operation>::initialize();
}
int OperationAlternate::initialize() {
// Initialize the metadata
metadata = MetaClass::registerClass<OperationAlternate>(
"operation", "operation_alternate", Object::create<OperationAlternate>);
registerFields<OperationAlternate>(const_cast<MetaClass*>(metadata));
// Initialize the Python class
auto& x = FreppleClass<OperationAlternate, Operation>::getPythonType();
x.addMethod("decoupledLeadTime", &getDecoupledLeadTimePython, METH_VARARGS,
"return the total lead time");
x.addMethod("setFence", &setFencePython, METH_VARARGS,
"Update the fence based on date");
x.addMethod("getFence", &getFencePython, METH_NOARGS,
"Retrieve the fence date");
return FreppleClass<OperationAlternate, Operation>::initialize();
}
int OperationRouting::initialize() {
// Initialize the metadata
metadata = MetaClass::registerClass<OperationRouting>(
"operation", "operation_routing", Object::create<OperationRouting>);
registerFields<OperationRouting>(const_cast<MetaClass*>(metadata));
// Initialize the Python class
auto& x = PythonExtension<
FreppleClass<OperationRouting, Operation>>::getPythonType();
x.addMethod("decoupledLeadTime", &getDecoupledLeadTimePython, METH_VARARGS,
"return the total lead time");
x.addMethod("setFence", &setFencePython, METH_VARARGS,
"Update the fence based on date");
x.addMethod("getFence", &getFencePython, METH_NOARGS,
"Retrieve the fence date");
return FreppleClass<OperationRouting, Operation>::initialize();
}
Operation::~Operation() {
// Delete all existing operationplans (even locked ones)
deleteOperationPlans(true);
// The Flow and Load objects are automatically deleted by the destructor
// of the Association list class.
// Unlink from item
if (item) {
if (item->firstOperation == this)
// Remove from head
item->firstOperation = next;
else {
// Remove from middle
Operation* j = item->firstOperation;
while (j->next && j->next != this) j = j->next;
if (j)
j->next = next;
else
logger << "Error: Corrupted Operation list on Item\n";
}
}
// Remove the reference to this operation from all demands
for (auto& l : Demand::all())
if (l.getOperation() == this) l.setOperation(nullptr);
// Remove the reference to this operation from all buffers
for (auto& m : Buffer::all())
if (m.getProducingOperation() == this) m.setProducingOperation(nullptr);
// Remove the operation from its super-operations and sub-operations
if (getOwner()) {
auto subops = getOwner()->getSubOperations();
auto i = subops.begin();
while (i != subops.end()) {
if ((*i)->getOperation() == this) {
SubOperation* tmp = *i;
// note: erase also advances the iterator
i = subops.erase(i);
delete tmp;
} else
++i;
}
}
// Clear dependencies
while (!dependencies.empty()) delete dependencies.front();
// Problems are automatically deleted by the HasProblem class.
// Constraints need to be cleared explicitly.
Problem::clearConstraints(*this);
}
OperationRouting::~OperationRouting() {
// Note that we are not using a for-loop since our function is actually
// updating the list of super-operations at the same time as we move
// through it.
while (!getSubOperations().empty()) delete *getSubOperations().begin();
}
bool OperationRouting::useDependencies() const {
for (auto step : getSubOperations()) {
for (auto& dpd : step->getOperation()->getDependencies()) {
if ((dpd->getBlockedBy() == step->getOperation() &&
dpd->getOperation()->getOwner() == this) ||
(dpd->getOperation() == step->getOperation() &&
dpd->getBlockedBy()->getOwner() == this))
return true;
}
}
return false;
}
OperationSplit::~OperationSplit() {
// Note that we are not using a for-loop since our function is actually
// updating the list of super-operations at the same time as we move
// through it.
while (!getSubOperations().empty()) delete *getSubOperations().begin();
}
OperationAlternate::~OperationAlternate() {
// Note that we are not using a for-loop since our function is actually
// updating the list of super-operations at the same time as we move
// through it.
while (!getSubOperations().empty()) delete *getSubOperations().begin();
}
OperationPlan::iterator Operation::getOperationPlans() const {
return OperationPlan::iterator(this);
}
Date Operation::getFence(const OperationPlan* opplan) const {
if (fence > 0L)
return calculateOperationTime(opplan, Plan::instance().getCurrent(), fence,
true)
.getEnd();
else if (fence < 0L)
return calculateOperationTime(opplan, Plan::instance().getCurrent(), -fence,
false)
.getStart();
else
return Plan::instance().getCurrent();
}
void Operation::setFence(Date d) {
Duration tmp;
calculateOperationTime(nullptr, Plan::instance().getCurrent(), d, &tmp, true);
setFence(tmp);
}
PyObject* Operation::setFencePython(PyObject* self, PyObject* args) {
// Pick up the date argument
PyObject* pydate;
if (!PyArg_ParseTuple(args, "O:setFence", &pydate)) return nullptr;
try {
PythonData dt(pydate);
static_cast<Operation*>(self)->setFence(dt.getDate());
return Py_BuildValue("");
} catch (...) {
PythonType::evalException();
return nullptr;
}
}
PyObject* Operation::getFencePython(PyObject* self, PyObject*) {
try {
auto oper = static_cast<Operation*>(self);
auto result = oper->getFence()
? oper->calculateOperationTime(
nullptr, Plan::instance().getCurrent(),
oper->getFence(), true, nullptr, true)
.getEnd()
: Plan::instance().getCurrent();
return PythonData(result);
} catch (...) {
PythonType::evalException();
return nullptr;
}
}
Duration Operation::getMaxEarly() const {
Duration tmp = Duration::MAX;
for (const auto& ld : getLoads())
if (ld.getResource() && ld.getResource()->getMaxEarly() < tmp)
tmp = ld.getResource()->getMaxEarly();
return tmp;
}
Duration OperationAlternate::getMaxEarly() const {
Duration tmp = Operation::getMaxEarly();
for (auto& sub : getSubOperations()) {
auto t = sub->getOperation()->getMaxEarly();
// Note: skipping 0-priority
if (sub->getPriority() && t < tmp) tmp = t;
}
return tmp;
}
Duration OperationSplit::getMaxEarly() const {
Duration tmp = Operation::getMaxEarly();
for (auto& sub : getSubOperations()) {
auto t = sub->getOperation()->getMaxEarly();
// Note: skipping 0-priority
if (sub->getPriority() && t < tmp) tmp = t;
}
return tmp;
}
Duration OperationRouting::getMaxEarly() const {
Duration tmp = Operation::getMaxEarly();
for (auto& sub : getSubOperations()) {
auto t = sub->getOperation()->getMaxEarly();
if (t < tmp) tmp = t;
}
return tmp;
}
OperationPlan* Operation::createOperationPlan(
double q, Date s, Date e, const PooledString& batch, Demand* l,
OperationPlan* ow, bool makeflowsloads, bool roundDown, const string& ref,
double q_completed, const string& status,
const vector<Resource*>* assigned_resources) const {
auto* opplan = new OperationPlan(const_cast<Operation*>(this));
if (!batch.empty()) opplan->setBatch(batch);
if (!ref.empty()) opplan->setName(ref);
if (q_completed) opplan->setQuantityCompletedRaw(q_completed);
if (l) opplan->setDemand(l);
// Setting the owner first. Note that the order is important here!
// For alternates & routings the quantity needs to be set through the owner.
if (ow) opplan->setOwner(ow, true);
// Setting the dates and quantity
setOperationPlanParameters(opplan, q, s, e, true, true, roundDown);
if (status == "confirmed" && s != Date::infinitePast &&
e != Date::infinitePast)
opplan->setStartEndAndQuantity(s, e, q);
// Create the loadplans and flowplans, if allowed
if (makeflowsloads || (assigned_resources && !assigned_resources->empty())) {
opplan->createFlowLoads(assigned_resources);
// Now that we know the assigned resource the duration can change
// eg different availability or efficienicy)
if (status != "confirmed")
setOperationPlanParameters(opplan, q, s, e, true, true, roundDown);
}
// Update flow and loadplans, and mark for problem detection
opplan->update();
return opplan;
}
DateRange Operation::calculateOperationTime(
const OperationPlan* opplan, Date thedate, Duration duration, bool forward,
Duration* actualduration, bool considerResourceCalendars) const {
// Account for negative durations
if (duration < 0L) {
forward = !forward;
duration = -duration;
}
// Default actual duration
if (actualduration) *actualduration = duration;
// Collect calendars
Calendar::EventIterator cals[10];
auto numCalendars = collectCalendars(cals, thedate, opplan, forward,
considerResourceCalendars);
// First case: no calendars at all
if (!numCalendars)
return forward ? DateRange(thedate, thedate + duration)
: DateRange(thedate - duration, thedate);
DateRange result;
Date curdate = thedate;
Date selected;
bool status = false;
Duration curduration = duration;
bool available;
// Second case: a single calendar only.
// We handle it seperate for performance reasons.
if (numCalendars == 1) {
while (true) {
// Find the closest event date
selected = forward ? Date::infiniteFuture : Date::infinitePast;
if ((forward && cals[0].getDate() < selected) ||
(!forward && cals[0].getDate() > selected))
selected = cals[0].getDate();
// Check whether all calendars are available at the next event date
if (forward) {
if (cals[0].getDate() == selected && cals[0].getValue() == 0)
available = false;
else if (cals[0].getDate() != selected && cals[0].getPrevValue() == 0)
available = false;
else
available = true;
} else {
if (cals[0].getCalendar()->getValue(selected, forward) == 0)
available = false;
else
available = true;
}
if (!duration) {
// A special case for 0-time operations.
if (available && forward) {
result.setEnd(curdate);
result.setStart(curdate);
return result;
} else if (!available) {
available =
(cals[0].getCalendar()->getValue(selected, !forward) != 0);
if (available) {
result.setEnd(curdate);
result.setStart(curdate);
return result;
}
}
}
curdate = selected;
if (available && !status) {
// Becoming available after unavailable period
thedate = curdate;
status = true;
if (forward && result.getStart() == Date::infinitePast)
// First available time - make operation start at this time
result.setStart(curdate);
else if (!forward && result.getEnd() == Date::infiniteFuture)
// First available time - make operation end at this time
result.setEnd(curdate);
} else if (!available && status) {
// Becoming unavailable after available period
status = false;
if (forward) {
// Forward
Duration delta = curdate - thedate;
if (delta >= curduration) {
result.setEnd(thedate + curduration);
return result;
} else
curduration -= delta;
} else {
// Backward
Duration delta = thedate - curdate;
if (delta >= curduration) {
result.setStart(thedate - curduration);
return result;
} else
curduration -= delta;
}
} else if (forward && curdate == Date::infiniteFuture) {
// End of forward iteration
if (available) {
Duration delta = curdate - thedate;
if (delta >= curduration)
result.setEnd(thedate + curduration);
else if (actualduration)
*actualduration = duration - curduration;
} else if (actualduration)
*actualduration = duration - curduration;
return result;
} else if (!forward && curdate == Date::infinitePast) {
// End of backward iteration
if (available) {
Duration delta = thedate - curdate;
if (delta >= curduration)
result.setStart(thedate - curduration);
else if (actualduration)
*actualduration = duration - curduration;
} else if (actualduration)
*actualduration = duration - curduration;
return result;
}
// Advance to the next event
if (forward) {
if (cals[0].getDate() == selected) ++cals[0];
} else {
if (cals[0].getDate() == selected) --cals[0];
}
}
return result;
}
// Third case: more than 1 calendar
while (true) {
// Find the closest event date
Date selected = forward ? Date::infiniteFuture : Date::infinitePast;
for (unsigned short t = 0; t < numCalendars; ++t) {
if ((forward && cals[t].getDate() < selected) ||
(!forward && cals[t].getDate() > selected))
selected = cals[t].getDate();
}
// Check whether all calendars are available at the next event date
available = true;
if (forward) {
for (unsigned short t = 0; t < numCalendars && available; ++t) {
if (cals[t].getDate() == selected && cals[t].getValue() == 0)
available = false;
else if (cals[t].getDate() != selected && cals[t].getPrevValue() == 0)
available = false;
}
} else {
for (unsigned short t = 0; t < numCalendars && available; ++t) {
if (cals[t].getCalendar()->getValue(selected, forward) == 0)
available = false;
}
}
if (!duration) {
// A special case for 0-time operations.
if (available && forward) {
result.setEnd(curdate);
result.setStart(curdate);
return result;
} else if (!available) {
available = true;
for (unsigned short t = 0; t < numCalendars && available; ++t)
available =
(cals[t].getCalendar()->getValue(selected, !forward) != 0);
if (available) {
result.setEnd(curdate);
result.setStart(curdate);
return result;
}
}
}
curdate = selected;
if (available && !status) {
// Becoming available after unavailable period
thedate = curdate;
status = true;
if (forward && result.getStart() == Date::infinitePast)
// First available time - make operation start at this time
result.setStart(curdate);
else if (!forward && result.getEnd() == Date::infiniteFuture)
// First available time - make operation end at this time
result.setEnd(curdate);
} else if (!available && status) {
// Becoming unavailable after available period
status = false;
if (forward) {
// Forward
Duration delta = curdate - thedate;
if (delta >= curduration) {
result.setEnd(thedate + curduration);
break;
} else
curduration -= delta;
} else {
// Backward
Duration delta = thedate - curdate;
if (delta >= curduration) {
result.setStart(thedate - curduration);
break;
} else
curduration -= delta;
}
} else if (forward && curdate == Date::infiniteFuture) {
// End of forward iteration
if (available) {
Duration delta = curdate - thedate;
if (delta >= curduration)
result.setEnd(thedate + curduration);
else if (actualduration)
*actualduration = duration - curduration;
} else if (actualduration)
*actualduration = duration - curduration;
break;
} else if (!forward && curdate == Date::infinitePast) {
// End of backward iteration
if (available) {
Duration delta = thedate - curdate;
if (delta >= curduration)
result.setStart(thedate - curduration);
else if (actualduration)
*actualduration = duration - curduration;
} else if (actualduration)
*actualduration = duration - curduration;
break;
}
// Advance to the next event
if (forward) {
for (unsigned short t = 0; t < numCalendars; ++t)
if (cals[t].getDate() == selected) ++cals[t];
} else {
for (unsigned short t = 0; t < numCalendars; ++t)
if (cals[t].getDate() == selected) --cals[t];
}
}
return result;
}
unsigned short Operation::collectCalendars(
Calendar::EventIterator cals[], Date start, const OperationPlan* opplan,
bool forward, bool considerResourceCalendars) const {
auto nolocationcalendar = getNoLocationCalendar();
unsigned short calcount = 0;
// a) operation
if (available)
cals[calcount++] = Calendar::EventIterator(available, start, forward);
// b) operation location
if (loc && loc->getAvailable() && getAvailable() != loc->getAvailable() &&
!nolocationcalendar)
cals[calcount++] =
Calendar::EventIterator(loc->getAvailable(), start, forward);
if (!considerResourceCalendars) return calcount;
if (opplan && opplan->getLoadPlans() != opplan->endLoadPlans()) {
// Iterate over loadplans
for (auto g = opplan->getLoadPlans(); g != opplan->endLoadPlans(); ++g) {
if (g->getQuantity() > 0) continue;
Resource* res = g->getResource();
if (res->getAvailable()) {
// c) resource
bool exists = false;
for (unsigned short t = 0; t < calcount; ++t) {
if (cals[t].getCalendar() == res->getAvailable()) {
exists = true;
break;
}
}
if (!exists) {
cals[calcount++] =
Calendar::EventIterator(res->getAvailable(), start, forward);
if (calcount > 9)
throw DataException("Excessive number of calendars on operation '" +
getName() + "'");
}
}
if (res->getLocation() && res->getLocation()->getAvailable() &&
!nolocationcalendar) {
bool exists = false;
for (unsigned short t = 0; t < calcount; ++t) {
// d) resource location
if (cals[t].getCalendar() == res->getLocation()->getAvailable()) {
exists = true;
break;
}
}
if (!exists) {
cals[calcount++] = Calendar::EventIterator(
res->getLocation()->getAvailable(), start, forward);
if (calcount > 9)
throw DataException("Excessive number of calendars on operation '" +
getName() + "'");
}
}
}
} else {
// Iterate over loads
for (const auto& g : loaddata) {
Resource* res = g.getResource();
if (res->getAvailable()) {
// c) resource
bool exists = false;
for (unsigned short t = 0; t < calcount; ++t) {
if (cals[t].getCalendar() == res->getAvailable()) {
exists = true;
break;
}
}
if (!exists) {
cals[calcount++] =
Calendar::EventIterator(res->getAvailable(), start, forward);
if (calcount > 9)
throw DataException("Excessive number of calendars on operation '" +
getName() + "'");
}
}
if (res->getLocation() && res->getLocation()->getAvailable() &&
!nolocationcalendar) {
bool exists = false;
for (unsigned short t = 0; t < calcount; ++t) {
// d) resource location
if (cals[t].getCalendar() == res->getLocation()->getAvailable()) {
exists = true;
break;
}
}
if (!exists) {
cals[calcount++] = Calendar::EventIterator(
res->getLocation()->getAvailable(), start, forward);
if (calcount > 9)
throw DataException("Excessive number of calendars on operation '" +
getName() + "'");
}
}
}
}
return calcount;
}
DateRange Operation::calculateOperationTime(
const OperationPlan* opplan, Date start, Date end, Duration* actualduration,
bool considerResourceCalendars) const {
// Switch start and end if required
if (end < start) {
Date tmp = start;
start = end;
end = tmp;
}
// Default actual duration
if (actualduration) *actualduration = 0L;
// Build a list of involved calendars
Calendar::EventIterator cals[10];
auto numCalendars =
collectCalendars(cals, start, opplan, considerResourceCalendars);
// First case: no calendars at all
if (!numCalendars) {
if (actualduration) *actualduration = end - start;
return DateRange(start, end);
}
DateRange result;
Date curdate = start;
Date selected;
bool status = false;
bool available;
// Second case: only a single calendar.
// We handle it seperate for performance reasons.
if (numCalendars == 1) {
while (true) {
// Find the closest event date
selected = cals[0].getDate();
curdate = selected;
// Check whether the calendar is available at the next event date
if (cals[0].getDate() == selected && cals[0].getValue() == 0)
available = false;
else if (cals[0].getDate() != selected && cals[0].getPrevValue() == 0)
available = false;
else
available = true;
if (available && !status) {
// Becoming available after unavailable period
if (curdate >= end) {
// Leaving the desired date range
result.setEnd(start);
return result;
}
start = curdate;
status = true;
if (result.getStart() == Date::infinitePast)
// First available time - make operation start at this time
result.setStart(curdate);
} else if (!available && status) {
// Becoming unavailable after available period
if (curdate >= end) {
// Leaving the desired date range
if (actualduration) *actualduration += end - start;
result.setEnd(end);
return result;
}
status = false;
if (actualduration) *actualduration += curdate - start;
start = curdate;
} else if (curdate >= end) {
// Leaving the desired date range
if (available) {
if (actualduration) *actualduration += end - start;
result.setEnd(end);
return result;
} else
result.setEnd(start);
return result;
}
// Advance to the next event
++cals[0];
}
}
// Third case: more than 1 calendar
while (true) {
// Find the closest event date
selected = Date::infiniteFuture;
for (unsigned short t = 0; t < numCalendars; ++t) {
if (cals[t].getDate() < selected) selected = cals[t].getDate();
}
curdate = selected;
// Check whether all calendars are available at the next event date
available = true;
for (unsigned short t = 0; t < numCalendars && available; ++t) {
if (cals[t].getDate() == selected && cals[t].getValue() == 0)
available = false;
else if (cals[t].getDate() != selected && cals[t].getPrevValue() == 0)
available = false;
}
if (available && !status) {
// Becoming available after unavailable period
if (curdate >= end) {
// Leaving the desired date range
result.setEnd(start);
return result;
}
start = curdate;
status = true;
if (result.getStart() == Date::infinitePast)
// First available time - make operation start at this time
result.setStart(curdate);
} else if (!available && status) {
// Becoming unavailable after available period
if (curdate >= end) {
// Leaving the desired date range
if (actualduration) *actualduration += end - start;
result.setEnd(end);
return result;
}
status = false;
if (actualduration) *actualduration += curdate - start;
start = curdate;
} else if (curdate >= end) {
// Leaving the desired date range
if (available) {
if (actualduration) *actualduration += end - start;
result.setEnd(end);
} else
result.setEnd(start);
return result;
}
// Advance to the next event
for (unsigned short t = 0; t < numCalendars; ++t)
if (cals[t].getDate() == selected) ++cals[t];
}
return result;
}
Operation::SetupInfo Operation::calculateSetup(OperationPlan* opplan,
Date setupend,
SetupEvent* setupevent,
SetupEvent** prevevent) const {
// Shortcuts: there are no setup matrices or resources
if (SetupMatrix::empty() || getLoads().empty() || !opplan ||
!opplan->getQuantity() || opplan->getNoSetup())
return SetupInfo(nullptr, nullptr, PooledString());
// Loop over each load or loadplan and see check what setup time they need
bool firstResourceWithSetup = true;
auto ldplan = opplan->beginLoadPlans();
if (ldplan == opplan->endLoadPlans()) {
// First case: This operationplan doesn't have any loadplans yet.
for (const auto& ld : getLoads()) {
if (ld.getSetup().empty() || !ld.getResource()->getSetupMatrix())
// There is no setup on this load
continue;
// An operation can load only a single resource with a setup matrix
if (firstResourceWithSetup)
firstResourceWithSetup = false;
else
throw DataException(
"Only a single resource with a setup matrix is allowed per "
"operation");
// Calculate the setup time
SetupEvent* cursetup =
setupevent ? setupevent->getSetupBefore()
: ld.getResource()->getSetupAt(setupend, opplan);
if (prevevent) *prevevent = cursetup;
return SetupInfo(
ld.getResource(),
ld.getResource()->getSetupMatrix()->calculateSetup(
cursetup ? cursetup->getSetup() : PooledString::emptystring,
ld.getSetup(), ld.getResource()),
ld.getSetup());
}
} else {
// Second case: This operationplan already has loadplans. Using them
// is more efficient, and some of them may already be switched to
// alternate resources.
for (; ldplan != opplan->endLoadPlans(); ++ldplan) {
if (ldplan->getQuantity() < 0 || !ldplan->getLoad() ||
ldplan->getLoad()->getSetup().empty() ||
!ldplan->getResource()->getSetupMatrix())
// Not a consuming loadplan or there is no setup on this loadplan
continue;
// An operation can load only a single resource with a setup matrix
if (firstResourceWithSetup)
firstResourceWithSetup = false;
else
throw DataException(
"Only a single resource with a setup matrix is allowed per "
"operation");
if (ldplan->getResource()->getFrozenSetups()) {
// Return the current setup event
auto ev = opplan->getSetupEvent();
if (ev)
return SetupInfo(ldplan->getResource(), ev->getRule(),
ldplan->getLoad()->getSetup());
else
return SetupInfo(nullptr, nullptr, PooledString());
} else {
// Calculate the setup time
SetupEvent* cursetup =
ldplan->getResource()->getSetupAt(setupend, opplan);
if (prevevent) *prevevent = cursetup;
return SetupInfo(
ldplan->getResource(),
ldplan->getResource()->getSetupMatrix()->calculateSetup(
cursetup ? cursetup->getSetup() : PooledString::emptystring,
ldplan->getLoad()->getSetup(), ldplan->getResource()),
ldplan->getLoad()->getSetup());
}
}
}
return SetupInfo(nullptr, nullptr, PooledString());
}
Flow* Operation::findFlow(const Buffer* b, Date d) const {
for (const auto& fl : flowdata) {
if (!fl.effectivity.within(d)) continue;
if (fl.getBuffer() == b)
return const_cast<Flow*>(&fl);
else if (!fl.getBuffer() && fl.getItem() == b->getItem() &&
getLocation() == b->getLocation())
return const_cast<Flow*>(&fl);
else if (fl.getBuffer() && b->getBatch() && fl.getItem() == b->getItem() &&
fl.getBuffer()->getLocation() == b->getLocation() &&
!fl.getBuffer()->getBatch())
// Generic buffer on flow matches a MTO buffer
return const_cast<Flow*>(&fl);
}
return nullptr;
}
void Operation::deleteOperationPlans(bool deleteLockedOpplans) {
OperationPlan::deleteOperationPlans(this, deleteLockedOpplans);
}
OperationPlanState OperationFixedTime::setOperationPlanParameters(
OperationPlan* opplan, double q, Date s, Date e, bool preferEnd,
bool execute, bool roundDown, bool later) const {
// Invalid call to the function
if (!opplan || q < 0)
throw LogicException("Incorrect parameters for fixedtime operationplan");
// Confirmed operationplans are untouchable
if (opplan->getConfirmed() && !opplan->getForcedUpdate())
return OperationPlanState(opplan);
// Compute the start and end date
Duration production_duration;
Duration setup_duration;
DateRange production_dates;
DateRange setup_dates;
Operation::SetupInfo setuptime_required(nullptr, nullptr, PooledString());
double efficiency = opplan->getEfficiency(s ? s : e);
bool forward;
if (e && s) {
if (preferEnd)
forward = false;
else
forward = true;
} else if (s)
forward = true;
else
forward = false;
Date d = s;
Duration production_wanted_duration =
efficiency > 0.0 ? Duration(double(duration) / efficiency)
: Duration::MAX;
if (opplan && hasType<OperationDelivery>() && opplan->getDemand())
// Special case to have the duration of deliveries demand-dependent when
// they use the default delivery operation.
production_wanted_duration = opplan->getDemand()->getDeliveryDuration();
Duration setup_wanted_duration;
while (true) {
if (forward) {
// Compute forward from the start date
setuptime_required = calculateSetup(opplan, d, opplan->getSetupEvent());
if (get<0>(setuptime_required) || opplan->getSetupOverride() >= 0L) {
if ((get<1>(setuptime_required) && efficiency > 0.0) ||