-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource.cpp
More file actions
1006 lines (907 loc) · 34.7 KB
/
Copy pathresource.cpp
File metadata and controls
1006 lines (907 loc) · 34.7 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 "frepple/model.h"
namespace frepple {
template <class Resource>
Tree utils::HasName<Resource>::st;
const MetaCategory* Resource::metadata;
const MetaClass* ResourceDefault::metadata;
const MetaClass* ResourceInfinite::metadata;
const MetaClass* ResourceBuckets::metadata;
Duration Resource::defaultMaxEarly(100 * 86400L);
int Resource::initialize() {
// Initialize the metadata
metadata = MetaCategory::registerCategory<Resource>("resource", "resources",
reader, finder);
registerFields<Resource>(const_cast<MetaCategory*>(metadata));
// Initialize the Python class
auto& x = FreppleCategory<Resource>::getPythonType();
x.addMethod("plan", Resource::plan, METH_VARARGS,
"Return an iterator with tuples representing the resource plan "
"in each time bucket");
x.addMethod("inspect", inspectPython, METH_VARARGS,
"debugging function to print the resource profile");
return FreppleCategory<Resource>::initialize();
}
int ResourceDefault::initialize() {
// Initialize the metadata
ResourceDefault::metadata = MetaClass::registerClass<ResourceDefault>(
"resource", "resource_default", Object::create<ResourceDefault>, true);
// Initialize the Python class
return FreppleClass<ResourceDefault, Resource>::initialize();
}
int ResourceInfinite::initialize() {
// Initialize the metadata
ResourceInfinite::metadata = MetaClass::registerClass<ResourceInfinite>(
"resource", "resource_infinite", Object::create<ResourceInfinite>);
// Initialize the Python class
return FreppleClass<ResourceInfinite, Resource>::initialize();
}
int ResourceBuckets::initialize() {
// Initialize the metadata
ResourceBuckets::metadata = MetaClass::registerClass<ResourceBuckets>(
"resource", "resource_buckets", Object::create<ResourceBuckets>);
registerFields<ResourceBuckets>(const_cast<MetaClass*>(metadata));
// Initialize the Python class
FreppleClass<ResourceBuckets, Resource>::getPythonType().addMethod(
"computeAvailability", ResourceBuckets::computeBucketAvailability,
METH_VARARGS,
"Convert the maximum and availability calendar into quantities available "
"per capacity bucket");
return FreppleClass<ResourceBuckets, Resource>::initialize();
}
void Resource::inspect(const string& msg, const short i) const {
indent indentstring(i);
logger << indentstring << " Inspecting resource " << getName() << ": ";
if (!msg.empty()) logger << msg;
logger << '\n';
Date earliest = Date::infiniteFuture;
Date latest = Date::infinitePast;
Date prev;
unsigned int cnt = 0;
for (const auto& oo : getLoadPlans()) {
if (oo.getEventType() != 1)
++cnt;
else {
if (oo.getDate() > latest) latest = oo.getDate();
if (oo.getDate() < earliest) earliest = prev;
}
prev = oo.getDate();
}
for (const auto & oo : getLoadPlans()) {
if (cnt > 100) {
// Skip uninteresting events
if (oo.getDate() < earliest - Duration(7L * 24L * 3600L)) continue;
if (oo.getDate() > latest + Duration(7L * 24L * 3600L)) break;
}
logger << indentstring << " " << oo.getDate()
<< " qty:" << oo.getQuantity() << ", oh:" << oo.getOnhand();
switch (oo.getEventType()) {
case 1:
logger << ", " << oo.getOperationPlan() << '\n';
break;
case 2:
logger << ", set onhand to " << oo.getOnhand() << '\n';
break;
case 3:
logger << ", update minimum to " << oo.getMin() << '\n';
break;
case 4:
logger << ", update maximum to " << oo.getMax() << '\n';
break;
case 5:
logger << ", change setup to "
<< static_cast<const SetupEvent*>(&oo)->getSetup();
if (oo.getOperationPlan()) logger << " on " << oo.getOperationPlan();
logger << '\n';
break;
}
}
}
PyObject* Resource::inspectPython(PyObject* self, PyObject* args) {
try {
// Pick up the resource
Resource* res = nullptr;
PythonData c(self);
if (c.check(Resource::metadata))
res = static_cast<Resource*>(self);
else
throw LogicException("Invalid resource type");
// Parse the argument
char* msg = nullptr;
if (!PyArg_ParseTuple(args, "|s:inspect", &msg)) return nullptr;
res->inspect(msg ? msg : "");
return Py_BuildValue("");
} catch (...) {
PythonType::evalException();
return nullptr;
}
}
void Resource::setTool(bool b) {
// All resources in a hierarchy must have the same value of this field.
auto resiter = getTop()->getAllMembers();
if (!resiter.empty()) resiter->tool = b;
while (Resource* res = resiter.next()) res->tool = b;
}
void Resource::setToolPerPiece(bool b) {
// All resources in a hierarchy must have the same value of this field.
auto resiter = getTop()->getAllMembers();
if (!resiter.empty()) resiter->toolperpiece = b;
while (Resource* res = resiter.next()) res->toolperpiece = b;
}
void Resource::setMaximum(double m) {
if (m < 0) {
logger << "Warning: Maximum capacity for resource '" << getName()
<< "' must be postive\n";
return;
}
// There is already a maximum calendar.
if (size_max_cal) {
// We update the field, but don't use it yet.
size_max = m;
return;
}
// Mark as changed
setChanged();
// Set field
size_max = m;
// Create or update a single timeline max event
for (auto& loadplan : loadplans)
if (loadplan.getEventType() == 4) {
// Update existing event
static_cast<loadplanlist::EventMaxQuantity*>(&loadplan)->setMax(size_max);
return;
}
// Create new event
auto* newEvent = new loadplanlist::EventMaxQuantity(Date::infinitePast,
&loadplans, size_max);
loadplans.insert(newEvent);
}
void Resource::setMaximumCalendar(Calendar* c) {
// Resetting the same calendar
if (size_max_cal == c) return;
// Mark as changed
setChanged();
// Remove the current max events.
for (loadplanlist::iterator oo = loadplans.begin(); oo != loadplans.end();)
if (oo->getEventType() == 4) {
loadplans.erase(&(*oo));
delete &(*(oo++));
} else
++oo;
// Null pointer passed. Change back to time independent maximum size.
size_max_cal = c;
if (!c) {
setMaximum(size_max);
return;
}
// Create timeline structures for every bucket.
double curMax = 0.0;
for (CalendarDefault::EventIterator x(size_max_cal);
x.getDate() < Date::infiniteFuture; ++x)
if (curMax != x.getValue()) {
curMax = x.getValue();
auto* newBucket =
new loadplanlist::EventMaxQuantity(x.getDate(), &loadplans, curMax);
loadplans.insert(newBucket);
}
size_max_cal->clearEventList();
}
void ResourceBuckets::setMaximumCalendar(Calendar* c) {
// Resetting the same calendar
if (size_max_cal == c) return;
// Mark as changed
setChanged();
// Remove the current set-onhand events.
for (auto oo = loadplans.begin(); oo != loadplans.end();) {
loadplanlist::Event* tmp = &*oo;
++oo;
if (tmp->getEventType() == 2) {
loadplans.erase(tmp);
delete tmp;
}
}
// Create timeline structures for every bucket.
size_max_cal = c;
double v = 0.0;
// Only create events in the time window from 3 years before current
// till 6 years after the current date
Date minEventDate =
Plan::instance().getCurrent() - Duration(1L * 365L * 86400L);
Date maxEventDate =
Plan::instance().getCurrent() + Duration(6L * 365L * 86400L);
for (CalendarDefault::EventIterator x(size_max_cal);
x.getDate() < maxEventDate; ++x)
if (v != x.getValue() && x.getDate() >= minEventDate) {
v = x.getValue();
auto* newBucket = new loadplanlist::EventSetOnhand(x.getDate(), v);
loadplans.insert(newBucket);
}
size_max_cal->clearEventList();
}
double ResourceBuckets::getMaxBucketCapacity() const {
double tmp = 0.0;
for (const auto & loadplan : loadplans)
if (loadplan.getEventType() == 2 && loadplan.getOnhand() > tmp) tmp = loadplan.getOnhand();
return tmp;
}
void Resource::deleteOperationPlans(bool deleteLocked) {
// Delete the operationplans
for (auto & load : loads)
OperationPlan::deleteOperationPlans(load.getOperation(), deleteLocked);
// Mark to recompute the problems
setChanged();
}
Resource::~Resource() {
// Delete all operationplans
// An alternative logic would be to delete only the loadplans for this
// resource and leave the rest of the plan untouched. The currently
// implemented method is way more drastic...
deleteOperationPlans(true);
// The Load and ResourceSkill objects are automatically deleted by the
// destructor of the Association list class.
// Delete setup event
if (setup) delete setup;
// Clean up references on the itemsupplier and itemdistribution models
for (auto& i : Item::all()) {
Item::supplierlist::const_iterator itmsup_iter = i.getSupplierIterator();
while (ItemSupplier* itmsup = itmsup_iter.next())
if (itmsup->getResource() == this) itmsup->setResource(nullptr);
Item::distributionlist::const_iterator itmdist_iter =
i.getDistributionIterator();
while (ItemDistribution* itmdist = itmdist_iter.next())
if (itmdist->getResource() == this) itmdist->setResource(nullptr);
}
// Problems are automatically deleted by the HasProblem class.
// Constraints need to be cleared explicitly.
Problem::clearConstraints(*this);
}
void Resource::setOwner(Resource* o) {
// Assure that all child resources are of the same type
if (o) {
auto firstchild = o->getFirstChild();
if (firstchild) {
bool parent_bucketized = firstchild->hasType<ResourceBuckets>();
bool me_bucketized = hasType<ResourceBuckets>();
if (parent_bucketized != me_bucketized)
// Loadplans are completely different for both resource types.
// Alternating among resource pool members of different types gets very
// messy and can't be allowed.
throw DataException(
"Aggregate resources can't mix bucketized resources with other "
"types");
}
}
HasHierarchy<Resource>::setOwner(o);
if (getTool() != o->getTool()) {
if (getTool())
o->setTool(true);
else
setTool(true);
}
if (getToolPerPiece() != o->getToolPerPiece()) {
if (getToolPerPiece())
o->setToolPerPiece(true);
else
setToolPerPiece(true);
}
}
extern "C" PyObject* Resource::plan(PyObject* self, PyObject* args) {
// Get the resource model
auto* resource = static_cast<Resource*>(self);
// Parse the Python arguments
PyObject* buckets = nullptr;
if (!PyArg_ParseTuple(args, "O:plan", &buckets)) return nullptr;
// Validate that the argument supports iteration.
PyObject* iter = PyObject_GetIter(buckets);
if (!iter) {
PyErr_Format(PyExc_AttributeError,
"Argument to resource.plan() must support iteration");
return nullptr;
}
// Return the iterator
return new Resource::PlanIterator(resource, iter);
}
int Resource::PlanIterator::initialize() {
// Initialize the type
auto& x = PythonExtension<Resource::PlanIterator>::getPythonType();
x.setName("resourceplanIterator");
x.setDoc("frePPLe iterator for resourceplan");
x.supportiter();
return x.typeReady();
}
Resource::PlanIterator::PlanIterator(Resource* r, PyObject* o)
: bucketiterator(o) {
// Start date of the first bucket
end_date = PyIter_Next(bucketiterator);
if (!end_date) {
logger << "Warning: No valid buckets for exporting resource plan on '" << r
<< "'" << '\n';
bucketiterator = nullptr;
return;
}
// Collect all subresources
if (!r) {
bucketiterator = nullptr;
throw LogicException(
"Creating resource plan iterator for nullptr resource");
} else if (r->isGroup()) {
for (Resource::memberRecursiveIterator i(r); !i.empty(); ++i)
if (!i->isGroup()) {
_res tmp;
tmp.res = &*i;
res_list.push_back(tmp);
}
} else {
_res tmp;
tmp.res = r;
res_list.push_back(tmp);
}
// Initialize the iterator for all resources
for (auto & i : res_list) {
i.ldplaniter =
Resource::loadplanlist::iterator(i.res->getLoadPlans().begin());
i.bucketized = i.res->hasType<ResourceBuckets>();
i.cur_date = PythonData(end_date).getDate();
i.prev_date = i.cur_date;
i.cur_size = 0.0;
i.cur_load = 0.0;
i.cur_load_confirmed = 0.0;
if (i.bucketized) {
// Scan forward to the first relevant bucket
while (i.ldplaniter != i.res->getLoadPlans().end() &&
(i.ldplaniter->getEventType() != 2 ||
i.ldplaniter->getDate() < i.cur_date))
++(i.ldplaniter);
} else {
// Initialize unavailability iterators
i.prev_value = true;
if (i.res->getLocation() && i.res->getLocation()->getAvailable()) {
i.unavailLocIter = Calendar::EventIterator(
i.res->getLocation()->getAvailable(), i.cur_date);
i.prev_value =
(i.unavailLocIter.getCalendar()->getValue(i.cur_date) != 0);
}
if (i.res->getAvailable()) {
i.unavailIter =
Calendar::EventIterator(i.res->getAvailable(), i.cur_date);
if (i.prev_value)
i.prev_value =
(i.unavailIter.getCalendar()->getValue(i.cur_date) != 0);
}
// Advance loadplan iterator just beyond the starting date
i.cur_load_confirmed = 0.0;
while (i.ldplaniter != i.res->getLoadPlans().end() &&
i.ldplaniter->getDate() <= i.cur_date) {
unsigned short tp = i.ldplaniter->getEventType();
if (tp == 4)
// New max size
i.cur_size = i.ldplaniter->getMax();
else if (tp == 1) {
i.cur_load = i.ldplaniter->getOnhand();
if (!i.ldplaniter->getOperationPlan()->getProposed())
i.cur_load_confirmed += i.ldplaniter->getQuantity();
}
++(i.ldplaniter);
}
}
}
}
Resource::PlanIterator::~PlanIterator() {
if (bucketiterator) Py_DECREF(bucketiterator);
if (start_date) Py_DECREF(start_date);
if (end_date) Py_DECREF(end_date);
}
void Resource::PlanIterator::update(Resource::PlanIterator::_res* i,
Date till) {
long timedelta;
if (i->unavailIter.getCalendar() || i->unavailLocIter.getCalendar()) {
// Advance till the iterator exceeds the target date
while ((i->unavailLocIter.getCalendar() &&
i->unavailLocIter.getDate() <= till) ||
(i->unavailIter.getCalendar() && i->unavailIter.getDate() <= till)) {
if (i->unavailIter.getCalendar() &&
(!i->unavailLocIter.getCalendar() ||
i->unavailIter.getDate() < i->unavailLocIter.getDate())) {
timedelta = i->unavailIter.getDate() - i->prev_date;
i->prev_date = i->unavailIter.getDate();
} else {
timedelta = i->unavailLocIter.getDate() - i->prev_date;
i->prev_date = i->unavailLocIter.getDate();
}
if (i->prev_value) {
bucket_available += i->cur_size * timedelta / 3600;
bucket_load += i->cur_load * timedelta / 3600;
bucket_load_confirmed += i->cur_load_confirmed * timedelta / 3600;
} else
bucket_unavailable += i->cur_size * timedelta / 3600;
if (i->unavailIter.getCalendar() &&
i->unavailIter.getDate() == i->prev_date) {
// Increment only resource unavailability iterator
++(i->unavailIter);
if (i->unavailLocIter.getCalendar() &&
i->unavailLocIter.getDate() == i->prev_date)
// Increment both resource and location unavailability iterators
++(i->unavailLocIter);
} else if (i->unavailLocIter.getCalendar() &&
i->unavailLocIter.getDate() == i->prev_date)
// Increment only location unavailability iterator
++(i->unavailLocIter);
else
throw LogicException("Unreachable code");
i->prev_value = true;
if (i->unavailIter.getCalendar())
i->prev_value =
(i->unavailIter.getCalendar()->getValue(i->prev_date) != 0);
if (i->unavailLocIter.getCalendar() && i->prev_value)
i->prev_value =
(i->unavailLocIter.getCalendar()->getValue(i->prev_date) != 0);
}
// Account for time period finishing at the "till" date
timedelta = till - i->prev_date;
if (i->prev_value) {
bucket_available += i->cur_size * timedelta / 3600;
bucket_load += i->cur_load * timedelta / 3600;
bucket_load_confirmed += i->cur_load_confirmed * timedelta / 3600;
} else
bucket_unavailable += i->cur_size * timedelta / 3600;
} else {
// All time is available on this resource
timedelta = till - i->prev_date;
bucket_available += i->cur_size * timedelta / 3600;
bucket_load += i->cur_load * timedelta / 3600;
bucket_load_confirmed += i->cur_load_confirmed * timedelta / 3600;
}
// Remember till which date we already have reported
i->prev_date = till;
}
PyObject* Resource::PlanIterator::iternext() {
if (!bucketiterator) return nullptr;
// Reset counters
bucket_available = 0.0;
bucket_unavailable = 0.0;
bucket_load = 0.0;
bucket_setup = 0.0;
bucket_load_confirmed = 0.0;
if (start_date) Py_DECREF(start_date);
// Repeat until a non-empty bucket is found
do {
// Get the start and end date of the current bucket
start_date = end_date;
end_date = PyIter_Next(bucketiterator);
if (!end_date) return nullptr;
Date cpp_start_date = PythonData(start_date).getDate();
Date cpp_end_date = PythonData(end_date).getDate();
// Find the load of all resources in this bucket
for (auto & i : res_list) {
i.cur_date = cpp_end_date;
if (i.bucketized) {
// Bucketized resource
while (i.ldplaniter != i.res->getLoadPlans().end() &&
i.ldplaniter->getDate() < cpp_end_date) {
// At this point ldplaniter points to a bucket start event in the
// current reporting bucket
if (i.res->isTime())
bucket_available += i.ldplaniter->getOnhand() / 3600;
else
bucket_available += i.ldplaniter->getOnhand();
// Advance the loadplan iterator to the start of the next bucket
++(i.ldplaniter);
while (i.ldplaniter != i.res->getLoadPlans().end() &&
i.ldplaniter->getEventType() != 2) {
if (i.ldplaniter->getEventType() == 1) {
auto tmp = -i.ldplaniter->getQuantity();
if (i.res->isTime()) tmp /= 3600;
bucket_load += tmp;
if (!i.ldplaniter->getOperationPlan()->getProposed())
bucket_load_confirmed += tmp;
}
++(i.ldplaniter);
}
}
} else {
// Default resource
// Measure from beginning of the bucket till the first event in this
// bucket
if (i.ldplaniter != i.res->getLoadPlans().end() &&
i.ldplaniter->getDate() < i.cur_date)
update(&i, i.ldplaniter->getDate());
// Advance the loadplan iterator to the next event date
while (i.ldplaniter != i.res->getLoadPlans().end() &&
i.ldplaniter->getDate() <= i.cur_date) {
// Measure from the previous event till the current one
update(&i, i.ldplaniter->getDate());
// Process the event
unsigned short tp = i.ldplaniter->getEventType();
if (tp == 4)
// New max size
i.cur_size = i.ldplaniter->getMax();
else if (tp == 1) {
i.cur_load = i.ldplaniter->getOnhand();
if (!i.ldplaniter->getOperationPlan()->getProposed())
i.cur_load_confirmed += i.ldplaniter->getQuantity();
}
// Move to the next event
++(i.ldplaniter);
}
// Measure from the previous event till the end of the bucket
update(&i, i.cur_date);
}
// Measure setup
if (i.res->getSetupMatrix() && !i.bucketized) {
DateRange bckt(cpp_start_date, cpp_end_date);
for (auto j = i.res->getLoadPlans().begin();
j != i.res->getLoadPlans().end(); ++j) {
auto opplan = j->getOperationPlan();
if (opplan && j->getQuantity() < 0) {
auto strt = opplan->getStart() > cpp_start_date ? opplan->getStart()
: cpp_start_date;
auto nd = opplan->getSetupEnd() < cpp_end_date
? opplan->getSetupEnd()
: cpp_end_date;
if (strt < nd) {
Duration setupduration;
opplan->getOperation()->calculateOperationTime(opplan, strt, nd,
&setupduration);
bucket_setup -=
static_cast<long>(setupduration) * j->getQuantity();
}
}
}
}
}
} while (!bucket_available && !bucket_unavailable && !bucket_load &&
!bucket_setup);
// Return the result
bucket_setup /= 3600.0;
bucket_load_confirmed -= bucket_setup;
if (bucket_load_confirmed < 0.0) {
bucket_load += bucket_load_confirmed;
bucket_load_confirmed = 0.0;
}
return Py_BuildValue("{s:O,s:O,s:d,s:d,s:d,s:d,s:d,s:d}", "start", start_date,
"end", end_date, "available", bucket_available, "load",
bucket_load, "unavailable", bucket_unavailable, "setup",
bucket_setup, "free",
bucket_available - bucket_load - bucket_setup,
"load_confirmed", bucket_load_confirmed);
}
bool Resource::hasSkill(Skill* s, Date st, Date nd,
ResourceSkill** resSkill) const {
if (!s) {
if (resSkill) *resSkill = nullptr;
return false;
}
Resource::skilllist::const_iterator i = getSkills();
while (ResourceSkill* rs = i.next()) {
if (rs->getSkill() == s && st >= rs->getEffective().getStart() &&
nd <= rs->getEffective().getEnd()) {
if (resSkill) *resSkill = rs;
return true;
}
}
if (resSkill) *resSkill = nullptr;
return false;
}
void Resource::setSetupMatrix(SetupMatrix* s) {
if (setupmatrix == s) return;
if (hasType<ResourceBuckets>())
throw DataException(
"No setup matrix can be defined on bucketized resources");
setupmatrix = s;
updateSetupTime();
}
SetupEvent* Resource::getSetupAt(Date d, OperationPlan* opplan) const {
LoadPlan* ldplan = nullptr;
if (opplan) {
for (auto l = opplan->getLoadPlans(); l != opplan->endLoadPlans(); ++l)
if (l->getResource() == this && l->getQuantity() < 0.0) {
ldplan = &*l;
break;
}
}
auto tmp = ldplan ? getLoadPlans().begin(ldplan) : getLoadPlans().rbegin();
while (tmp != getLoadPlans().end()) {
if (tmp->getEventType() == 5 &&
(!opplan || opplan != tmp->getOperationPlan()) &&
(tmp->getDate() < d ||
(tmp->getDate() == d && opplan && tmp->getOperationPlan() &&
*opplan < *tmp->getOperationPlan())))
return const_cast<SetupEvent*>(static_cast<const SetupEvent*>(&*tmp));
--tmp;
}
return nullptr;
}
void Resource::updateSetupTime() const {
if (!setupmatrix) return;
bool tmp = OperationPlan::setPropagateSetups(false);
// TODO following loop can be inefficiently repeating things
while (true) {
bool changed = false;
for (auto qq = getLoadPlans().rbegin();
qq != getLoadPlans().end() && !changed; --qq) {
if (qq->getEventType() == 1 && qq->getQuantity() < 0.0) {
changed = qq->getOperationPlan()->updateSetupTime();
}
}
if (!changed) break;
};
OperationPlan::setPropagateSetups(tmp);
}
Duration Resource::getAvailable(Date start, Date end) const {
// Get calendars
Calendar::EventIterator cals[2];
short calcount = 0;
if (getAvailable())
cals[calcount++] = Calendar::EventIterator(getAvailable(), start, true);
if (getLocation() && getLocation()->getAvailable() &&
getAvailable() != getLocation()->getAvailable())
cals[calcount++] =
Calendar::EventIterator(getLocation()->getAvailable(), start, true);
// Case 1: Zero calendars
if (!calcount) return end - start;
Duration actualduration = 0L;
Date curdate = start;
Date selected;
bool status = false;
bool available;
// Case 2: One calendar
if (calcount == 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
return actualduration;
}
start = curdate;
status = true;
} else if (!available && status) {
// Becoming unavailable after available period
if (curdate >= end) {
// Leaving the desired date range
actualduration += end - start;
return actualduration;
}
status = false;
actualduration += curdate - start;
start = curdate;
} else if (curdate >= end) {
// Leaving the desired date range
if (available) {
actualduration += end - start;
return actualduration;
}
return actualduration;
}
// Advance to the next event
++cals[0];
}
}
// Case 3: more than 1 calendar
while (true) {
// Find the closest event date
selected = Date::infiniteFuture;
for (unsigned short t = 0; t < calcount; ++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 < calcount && 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
return actualduration;
}
start = curdate;
status = true;
} else if (!available && status) {
// Becoming unavailable after available period
if (curdate >= end) {
// Leaving the desired date range
actualduration += end - start;
return actualduration;
}
status = false;
actualduration += curdate - start;
start = curdate;
} else if (curdate >= end) {
// Leaving the desired date range
if (available) actualduration += end - start;
return actualduration;
}
// Advance to the next event
for (unsigned short t = 0; t < calcount; ++t)
if (cals[t].getDate() == selected) ++cals[t];
}
return actualduration;
}
extern "C" PyObject* ResourceBuckets::computeBucketAvailability(
PyObject* self, PyObject* args) {
// Get the resource model
auto* res = static_cast<ResourceBuckets*>(self);
// Parse the Python arguments
PyObject* pycal = nullptr;
int debug = false;
if (!PyArg_ParseTuple(args, "O|p:computeAvailability", &pycal, &debug))
return nullptr;
if (!PyObject_TypeCheck(pycal, CalendarDefault::metadata->pythonClass)) {
PyErr_SetString(PythonDataException, "argument must be of type calendar");
return nullptr;
}
auto* cal = static_cast<Calendar*>(pycal);
// Mark as changed
res->setChanged();
// Remove the current set-onhand events.
for (auto oo = res->getLoadPlans().begin();
oo != res->getLoadPlans().end();) {
loadplanlist::Event* tmp = &*oo;
++oo;
if (tmp->getEventType() == 2) {
res->getLoadPlans().erase(tmp);
delete tmp;
}
}
// Create timeline structures for every bucket.
if (debug) {
logger << "Computing availability for resource '" << res
<< "' with buckets from calendar '" << cal << "'\n";
logger << " Size calendar: " << res->getMaximumCalendar() << '\n';
logger << " Availability calendar: " << res->getAvailable() << '\n';
logger << " Location availability calendar: "
<< (res->getLocation() ? res->getLocation()->getAvailable()
: nullptr)
<< '\n';
}
CalendarDefault::EventIterator res_max(res->getMaximumCalendar());
CalendarDefault::EventIterator avail_res(res->getAvailable());
CalendarDefault::EventIterator avail_loc(
res->getLocation() ? res->getLocation()->getAvailable() : nullptr);
Date bucketstart;
double cur_size = res->getMaximumCalendar()
? res->getMaximumCalendar()->getDefault()
: res->getMaximum();
bool cur_available = true;
// Only create events in the time window from 3 years before current
// till 6 years after the current date
Date minEventDate =
Plan::instance().getCurrent() - Duration(3L * 365L * 86400L);
Date maxEventDate =
Plan::instance().getCurrent() + Duration(6L * 365L * 86400L);
for (CalendarDefault::EventIterator bckt(cal); bckt.getDate() < maxEventDate;
++bckt) {
// Advance availability and max calendars till we hit the end of the bucket
double available = 0.0;
Date prev_evt = bucketstart;
do {
// Find the next event date
Date evt = bckt.getDate();
if (avail_res.getDate() < evt) evt = avail_res.getDate();
if (avail_loc.getDate() < evt) evt = avail_loc.getDate();
if (res_max.getDate() < evt) evt = res_max.getDate();
// Add availability between the previous and current event
if (cur_available && cur_size > 0.0)
available += cur_size * (evt - prev_evt).getSeconds();
// Update availability and size at the event date
cur_available = true;
if (res->getAvailable()) {
if (avail_res.getDate() == evt && avail_res.getValue() == 0)
cur_available = false;
else if (res->getAvailable() && avail_res.getDate() != evt &&
avail_res.getPrevValue() == 0)
cur_available = false;
}
if (cur_available && res->getLocation() &&
res->getLocation()->getAvailable()) {
if (avail_loc.getDate() == evt && avail_loc.getValue() == 0)
cur_available = false;
else if (avail_loc.getDate() != evt && avail_loc.getPrevValue() == 0)
cur_available = false;
}
if (res->getMaximumCalendar() && res_max.getDate() == evt)
cur_size = res_max.getValue();
// Advance to the next event
if (avail_res.getDate() == evt) ++avail_res;
if (avail_loc.getDate() == evt) ++avail_loc;
if (res_max.getDate() == evt) ++res_max;
prev_evt = evt;
} while (avail_res.getDate() <= bckt.getDate() ||
avail_loc.getDate() <= bckt.getDate() ||
res_max.getDate() <= bckt.getDate());
if (bckt.getDate() > prev_evt && cur_available && cur_size > 0.0)
available += cur_size * (bckt.getDate() - prev_evt).getSeconds();
// Create an event for this bucket in the timeline
if (bucketstart > minEventDate) {
auto* newBucket =
new loadplanlist::EventSetOnhand(bucketstart, available);
res->getLoadPlans().insert(newBucket);
if (debug)
logger << " => Bucket from " << bucketstart << " till "
<< bckt.getDate() << ": " << available << '\n';
}
// Remember the bucket start
bucketstart = bckt.getDate();
}
cal->clearEventList();
// Set a flag that this resource's calendar represents machine-time from now
// onwards
res->computedFromCalendars = true;
// None return value
return Py_BuildValue("");
}
double ResourceDefault::getUtilization(Date st, Date nd) const {
auto prevdate = st;
double curmax = 0.0, curload = 0.0, sumload = 0.0, summax = 0.0;
for (auto& l : getLoadPlans()) {
if (l.getDate() > prevdate) {
auto delta = (l.getDate() > nd ? nd : l.getDate()) - st;
sumload += curload * delta.getSeconds();
summax += curmax * delta.getSeconds();
}
if (l.getDate() > nd) break;
curload = l.getOnhand();
if (l.getEventType() == 4) curmax = l.getMax();
}
return summax ? sumload / summax : sumload;
}
double ResourceBuckets::getUtilization(Date st, Date nd) const {
double curmax = 0.0, curonhand = 0.0, sumload = 0.0, summax = 0.0;
Date bucketstart = Date::infinitePast;
for (auto& l : getLoadPlans()) {
if (l.getEventType() == 2) {
if (bucketstart && st < l.getDate() && nd >= bucketstart) {
// A bucket ended that overlaps with the argument date range
sumload += curmax - curonhand;
summax += curmax;
}
bucketstart = l.getDate();
curmax = l.getOnhand();
}
curonhand = l.getOnhand();
}
return summax ? sumload / summax : sumload;
}