This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLittleNode.cpp
336 lines (265 loc) · 9.25 KB
/
LittleNode.cpp
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
#include "LittleNode.h"
LittleNode::LittleNode() {
matrix = nullptr;
value = -1;
parentValue = 0;
segments = vector<Segment>();
}
LittleNode::LittleNode(MatrixXd *matrix) : matrix(matrix) {
value = -1;
parentValue = 0;
segments = vector<Segment>();
}
LittleNode::LittleNode(MatrixXd *matrix, double parentValue, const vector<Segment> &segments) : matrix(matrix),
parentValue(
parentValue),
segments(segments) {
value = -1;
}
LittleNode::LittleNode(MatrixXd *matrix, double value, double parentValue, const vector<Segment> &segments) : matrix(
matrix), value(value), parentValue(parentValue), segments(segments) {}
double LittleNode::getValue() const {
return value;
}
void LittleNode::setValue(double value) {
LittleNode::value = value;
}
const vector<Segment> &LittleNode::getSegments() const {
return segments;
}
void LittleNode::setSegments(const vector<Segment> &segments) {
LittleNode::segments = segments;
}
MatrixXd *LittleNode::getMatrix() const {
return matrix;
}
void LittleNode::setMatrix(MatrixXd *matrix) {
LittleNode::matrix = matrix;
}
double LittleNode::reduceMatrixRow(long row) {
if (matrix->rows() < row || row < 0) {
throw out_of_range("Provided row index is out of matrix bounds.");
}
double rowMin = getRowMin(row);
for (size_t col = 0; col < matrix->cols(); ++col) {
if ((*matrix)(row, col) >= 0) {
(*matrix)(row, col) -= rowMin;
}
}
return rowMin;
}
double LittleNode::reduceMatrixCol(long col) {
if (matrix->cols() < col || col < 0) {
throw out_of_range("Provided column index is out of matrix bounds.");
}
double colMin = getColMin(col);
for (size_t row = 0; row < matrix->rows(); ++row) {
if ((*matrix)(row, col) >= 0) {
(*matrix)(row, col) -= colMin;
}
}
return colMin;
}
double LittleNode::reduceMatrixRows() {
double reducedSum = 0;
for (size_t row = 0; row < matrix->rows(); ++row) {
reducedSum += reduceMatrixRow(row);
}
return reducedSum;
}
double LittleNode::reduceMatrixCols() {
double reducedSum = 0;
for (size_t col = 0; col < matrix->rows(); ++col) {
reducedSum += reduceMatrixCol(col);
}
return reducedSum;
}
double LittleNode::reduceMatrix() {
double reducedSum = 0;
reducedSum += reduceMatrixRows();
reducedSum += reduceMatrixCols();
return reducedSum;
}
void LittleNode::removeMatrixRow(long row) {
if (row > matrix->rows() || row < 0) {
throw out_of_range("Provided row index is out of matrix range.");
}
for (size_t col = 0; col < matrix->cols(); ++col) {
disableSegment(Segment(row, col));
}
}
void LittleNode::removeMatrixCol(long col) {
if (col > matrix->cols() || col < 0) {
throw out_of_range("Provided column index is out of matrix range.");
}
for (size_t row = 0; row < matrix->rows(); ++row) {
disableSegment(Segment(row, col));
}
}
Regret LittleNode::getMaxRegret() {
vector<Regret> regrets = getRegrets();
stable_sort(regrets.begin(), regrets.end());
return regrets.back();
}
vector<Regret> LittleNode::getRegrets() {
vector<Regret> regrets = vector<Regret>();
vector<Segment> regretsSegments = getRegretsSegments();
for (Segment regretSegment : regretsSegments) {
regrets.push_back(computeRegret(regretSegment));
}
return regrets;
}
vector<Segment> LittleNode::getRegretsSegments() {
vector<Segment> segments = vector<Segment>();
for (size_t row = 0; row < matrix->rows(); ++row) {
for (size_t col = 0; col < matrix->cols(); ++col) {
if ((*matrix)(row, col) == 0) {
segments.push_back(Segment(row, col));
}
}
}
return segments;
}
Regret LittleNode::computeRegret(Segment segment) {
Regret regret = Regret(segment);
double minRow = numeric_limits<double>::max();
double minCol = numeric_limits<double>::max();
if ((*matrix)(segment.getFrom(), segment.getTo()) != 0) {
throw invalid_argument("Given segment is not equals to 0.");
}
//Compute the minimal value in the specified row
if (matrix->row(segment.getFrom()).maxCoeff() < 0) {
minRow = 0;
} else {
for (size_t col = 0; col < matrix->cols(); ++col) {
if (col != segment.getTo()) {
double value = (*matrix)(segment.getFrom(), col);
if (value >= 0 && value < minRow) {
minRow = value;
}
}
}
}
if (matrix->col(segment.getTo()).maxCoeff() < 0) {
minCol = 0;
} else {
//Compute the minimal value in the specified column
for (size_t row = 0; row < matrix->rows(); ++row) {
if (row != segment.getFrom()) {
double value = (*matrix)(row, segment.getTo());
if (value >= 0 && value < minCol) {
minCol = (*matrix)(row, segment.getTo());
}
}
}
}
regret.setValue(minRow + minCol);
return regret;
}
void LittleNode::disableLoops() {
vector<Segment> segmentToExamine = segments;
vector<Loop> loops = vector<Loop>();
if (segmentToExamine.size() > 0) {
while (segmentToExamine.size() > 0) {
bool found = false;
for (size_t loopIndex = 0; loopIndex < loops.size(); ++loopIndex) {
vector<Segment>::iterator it = segmentToExamine.begin();
while (it != segmentToExamine.end()) {
bool match = false;
if (it->getTo() == loops.at(loopIndex).getStart()) {
vector<Segment> loopSegments = loops.at(loopIndex).getLoop();
loopSegments.insert(loopSegments.begin(), *it);
loops.at(loopIndex).setLoop(loopSegments);
match = true;
found = true;
}
if (it->getFrom() == loops.at(loopIndex).getEnd()) {
vector<Segment> loopSegments = loops.at(loopIndex).getLoop();
loopSegments.push_back(*it);
loops.at(loopIndex).setLoop(loopSegments);
match = true;
found = true;
}
if (match) {
segmentToExamine.erase(it);
it = segmentToExamine.begin();
} else {
++it;
}
}
}
if (!found) {
Loop loop = Loop();
vector<Segment> loopSegments = loop.getLoop();
vector<Segment>::iterator it = segmentToExamine.begin();
loopSegments.push_back(*it);
segmentToExamine.erase(it);
loop.setLoop(loopSegments);
loops.push_back(loop);
}
}
for (size_t loopIndex = 0; loopIndex < loops.size(); ++loopIndex) {
Segment closing = loops.at(loopIndex).computeClosing();
disableSegment(closing);
// cout << "Disabling "
// << closing
// << " for loop "
// << loops.at(loopIndex)
// << endl;
}
}
}
double LittleNode::getRowMin(long row) {
double rowMin = numeric_limits<double>::max();
if (row > matrix->rows() || row < 0) {
throw out_of_range("Provided row index is out of matrix's bounds.");
}
if (matrix->row(row).maxCoeff() < 0) {
return 0;
}
for (int col = 0; col < matrix->cols(); ++col) {
double value = matrix->row(row)(0, col);
if (value >= 0 && value < rowMin) {
rowMin = value;
}
}
return rowMin;
}
double LittleNode::getColMin(long col) {
double colMin = numeric_limits<double>::max();
if (col > matrix->cols() || col < 0) {
throw out_of_range("Provided column index is out of matrix's bounds.");
}
if (matrix->col(col).maxCoeff() < 0) {
return 0;
}
for (int row = 0; row < matrix->rows(); ++row) {
double value = matrix->col(col)(row, 0);
if (value >= 0 && value < colMin) {
colMin = value;
}
}
return colMin;
}
void LittleNode::disableSegment(Segment segment) {
long from = segment.getFrom();
long to = segment.getTo();
if (from < 0 || from > matrix->rows() || to < 0 || to > matrix->cols()) {
throw out_of_range("Provided segment is not in the matrix.");
}
if ((*matrix)(segment.getFrom(), segment.getTo()) != -1) {
(*matrix)(segment.getFrom(), segment.getTo()) = -1;
// cout << "Disabling segment "
// << segment
// << endl;
}
}
double LittleNode::getParentValue() const {
return parentValue;
}
void LittleNode::setParentValue(double parentValue) {
LittleNode::parentValue = parentValue;
}
LittleNode::~LittleNode() {
// delete matrix;
}