-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblems_operationplan.cpp
More file actions
270 lines (249 loc) · 9.71 KB
/
Copy pathproblems_operationplan.cpp
File metadata and controls
270 lines (249 loc) · 9.71 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
/***************************************************************************
* *
* 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 {
void Operation::updateProblems() {
// Find all operationplans, and delegate the problem detection to them
if (getDetectProblems())
for (auto o = first_opplan; o; o = o->next) o->updateProblems();
}
//
// BEFORECURRENT, BEFOREFENCE, PRECEDENCE
//
void OperationPlan::updateProblems() {
// A flag for each problem type that may need to be created
bool needsPrecedence(false);
if (!getCompleted() && !getClosed()) {
if (dependencies.empty()) {
// Note: 1 second grace period to avoid rounding issues
// TODO hard safety time not considered for the precedence problem
if (nextsubopplan &&
getEnd() > nextsubopplan->getStart() + Duration(1L) &&
!nextsubopplan->getConfirmed() && owner &&
!owner->getOperation()->hasType<OperationSplit>())
needsPrecedence = true;
} else {
for (auto d : dependencies) {
if (this != d->getSecond()) continue;
Date nd = d->getFirst()->getEnd();
if (d->getOperationDependency())
nd += d->getOperationDependency()->getHardSafetyLeadtime();
if (nd > getStart() + Duration(1L)) {
needsPrecedence = true;
break;
}
}
}
}
// Loop through the existing problems
for (auto j = Problem::begin(this, false); j != Problem::end();) {
// Need to increment now and define a pointer to the problem, since the
// problem can be deleted soon (which invalidates the iterator).
Problem& curprob = *j;
++j;
// The if-statement keeps the problem detection code concise and
// concentrated. However, a drawback of this design is that a new problem
// subclass will also require a new demand subclass. I think such a link
// is acceptable.
if (typeid(curprob) == typeid(ProblemPrecedence)) {
if (needsPrecedence)
needsPrecedence = false;
else if (this == curprob.getOwner())
delete &curprob;
}
}
// Create the problems that are required but aren't existing yet.
if (needsPrecedence) new ProblemPrecedence(this);
}
OperationPlan::ProblemIterator::ProblemIterator(const OperationPlan* o,
bool include_related)
: Problem::iterator(o->firstProblem), opplan(o) {
if (!include_related) return;
// Adding related capacity problems
for (auto ldpln = opplan->beginLoadPlans(); ldpln != opplan->endLoadPlans();
++ldpln) {
if (!ldpln->getResource()->getConstrained()) continue;
auto prob_iter = ldpln->getResource()->getProblems();
if (ldpln->getResource()->hasType<ResourceBuckets>()) {
while (Problem* prob = prob_iter.next()) {
if (prob->getDates().within(opplan->getStart()) &&
!prob->isFeasible()) {
relatedproblems.push_back(&*prob);
break;
}
}
} else {
while (Problem* prob = prob_iter.next()) {
if (prob->getDates().overlap(opplan->getDates()) &&
!prob->isFeasible()) {
relatedproblems.push_back(&*prob);
break;
}
}
}
}
// Adding local and upstream material problems
for (PeggingIterator p(o, false); p; --p) {
const OperationPlan* m = p.getOperationPlan();
if (m->getCompleted() || m->getClosed()) continue;
if (m != o) {
ProblemIterator probs(m, false);
while (auto p = probs.next()) {
if (p->isFeasible() ||
!p->hasType<ProblemBeforeCurrent, ProblemPrecedence>())
continue;
bool exists = false;
for (auto& x : relatedproblems)
if (static_cast<OperationPlan*>(x->getOwner())->getOperation() ==
static_cast<OperationPlan*>(p->getOwner())->getOperation()) {
exists = true;
break;
}
if (!exists) relatedproblems.push_back(&*p);
}
}
for (auto fp = m->beginFlowPlans(); fp != m->endFlowPlans(); ++fp) {
if (fp->getOnhandAfterDate() < -ROUNDING_ERROR)
for (Problem::iterator prob(fp->getBuffer()); prob != Problem::end();
++prob) {
if (prob->isFeasible()) continue;
if (prob->getDates().overlap(m->getDates()) && !prob->isFeasible()) {
bool exists = false;
for (auto& x : relatedproblems)
if (x->getOwner() == prob->getOwner()) {
exists = true;
break;
}
if (!exists) relatedproblems.push_back(&*prob);
break;
}
}
}
}
// Update the first problem pointer
if (!relatedproblems.empty()) iter = relatedproblems.back();
}
OperationPlan::ProblemIterator& OperationPlan::ProblemIterator::operator++() {
// Incrementing beyond the end
if (!iter) return *this;
if (!relatedproblems.empty()) {
relatedproblems.pop_back();
if (relatedproblems.empty())
iter = opplan->firstProblem;
else
iter = relatedproblems.back();
return *this;
}
// Move to the next problem
iter = iter->getNextProblem();
return *this;
}
bool OperationPlan::updateFeasible() {
if (!getOperation()->getDetectProblems() || getCompleted() || getClosed()) {
// No problems to be flagged on this operation
setFeasible(true);
return true;
}
// The implementation of this method isn't really cleanly object oriented. It
// uses logic which only the different resource and buffer implementation
// classes should be aware.
if (firstsubopplan) {
// Check feasibility of child operationplans
for (auto i = firstsubopplan; i; i = i->nextsubopplan) {
if (!i->updateFeasible()) {
setFeasible(false);
return false;
}
}
} else {
// Before current and before fence problems are only detected on child
// operationplans
if (getConfirmed()) {
if (getEnd() < Plan::instance().getCurrent()) {
// Before current violation
setFeasible(false);
return false;
}
} else {
if (getStart() < Plan::instance().getCurrent()) {
// Before current violation
setFeasible(false);
return false;
} else if (getProposed() && getStart() < oper->getFence(this)) {
// Before fence violation
setFeasible(false);
return false;
}
}
}
if (nextsubopplan && getEnd() > nextsubopplan->getStart() + Duration(1L) &&
!nextsubopplan->getConfirmed() && owner &&
!owner->getOperation()->hasType<OperationSplit>()) {
// Precedence violation
// Note: 1 second grace period for precedence problems to avoid rounding
// issues
setFeasible(false);
return false;
}
// Verify the capacity constraints
for (auto ldplan = getLoadPlans(); ldplan != endLoadPlans(); ++ldplan) {
if (((ldplan->getQuantity() > 0 &&
ldplan->getResource()->hasType<ResourceDefault>()) ||
(ldplan->getQuantity() < 0 &&
ldplan->getResource()->hasType<ResourceBuckets>())) &&
!ldplan->getFeasible()) {
setFeasible(false);
return false;
}
}
// Verify the material constraints
for (auto flplan = beginFlowPlans(); flplan != endFlowPlans(); ++flplan) {
if (!flplan->getFlow()->isConsumer() ||
flplan->getBuffer()->hasType<BufferInfinite>())
continue;
if (flplan->getBuffer()->getOnHand(Date::infiniteFuture) <
-ROUNDING_ERROR) {
// Material shortage
setFeasible(false);
return false;
}
auto flplaniter = flplan->getBuffer()->getFlowPlans();
for (auto cur = flplaniter.begin(&*flplan);
cur != flplaniter.end() && cur->getDate() < getEnd(); ++cur) {
if (cur->getOnhand() < -ROUNDING_ERROR && cur->isLastOnDate()) {
// Material shortage
setFeasible(false);
return false;
}
}
}
// After all checks, it turns out to be feasible
setFeasible(true);
return true;
}
PyObject* OperationPlan::updateFeasiblePython(PyObject* self, PyObject*) {
return PythonData(static_cast<OperationPlan*>(self)->updateFeasible());
}
} // namespace frepple