-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathfap_utilities.cc
185 lines (168 loc) · 7.81 KB
/
fap_utilities.cc
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
// Copyright 2010-2025 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "examples/cpp/fap_utilities.h"
#include <algorithm>
#include <cstdint>
#include <vector>
#include "absl/container/btree_map.h"
#include "absl/container/btree_set.h"
#include "absl/types/span.h"
#include "ortools/base/logging.h"
#include "ortools/base/map_util.h"
namespace operations_research {
bool CheckConstraintSatisfaction(
absl::Span<const FapConstraint> data_constraints,
absl::Span<const int> variables,
const absl::btree_map<int, int>& index_from_key) {
bool status = true;
for (const FapConstraint& ct : data_constraints) {
const int index1 = gtl::FindOrDie(index_from_key, ct.variable1);
const int index2 = gtl::FindOrDie(index_from_key, ct.variable2);
CHECK_LT(index1, variables.size());
CHECK_LT(index2, variables.size());
const int var1 = variables[index1];
const int var2 = variables[index2];
const int absolute_difference = abs(var1 - var2);
if ((ct.operation == ">") && (absolute_difference <= ct.value)) {
LOG(INFO) << " Violation of constraint between variable " << ct.variable1
<< " and variable " << ct.variable2 << ".";
LOG(INFO) << " Expected |" << var1 << " - " << var2
<< "| (= " << absolute_difference << ") > " << ct.value << ".";
status = false;
} else if ((ct.operation == "=") && (absolute_difference != ct.value)) {
LOG(INFO) << " Violation of constraint between variable " << ct.variable1
<< " and variable " << ct.variable2 << ".";
LOG(INFO) << " Expected |" << var1 << " - " << var2
<< "| (= " << absolute_difference << ") = " << ct.value << ".";
status = false;
}
}
return status;
}
bool CheckVariablePosition(
const absl::btree_map<int, FapVariable>& data_variables,
absl::Span<const int> variables,
const absl::btree_map<int, int>& index_from_key) {
bool status = true;
for (const auto& it : data_variables) {
const int index = gtl::FindOrDie(index_from_key, it.first);
CHECK_LT(index, variables.size());
const int var = variables[index];
if (it.second.hard && (it.second.initial_position != -1) &&
(var != it.second.initial_position)) {
LOG(INFO) << " Change of position of hard variable " << it.first << ".";
LOG(INFO) << " Expected " << it.second.initial_position
<< " instead of given " << var << ".";
status = false;
}
}
return status;
}
int NumberOfAssignedValues(absl::Span<const int> variables) {
absl::btree_set<int> assigned(variables.begin(), variables.end());
return static_cast<int>(assigned.size());
}
void PrintElapsedTime(const int64_t time1, const int64_t time2) {
LOG(INFO) << "End of solving process.";
LOG(INFO) << "The Solve method took " << (time2 - time1) / 1000.0
<< " seconds.";
}
void PrintResultsHard(SolutionCollector* const collector,
const std::vector<IntVar*>& variables,
IntVar* const objective_var,
const absl::btree_map<int, FapVariable>& data_variables,
absl::Span<const FapConstraint> data_constraints,
const absl::btree_map<int, int>& index_from_key,
absl::Span<const int> key_from_index) {
LOG(INFO) << "Printing...";
LOG(INFO) << "Number of Solutions: " << collector->solution_count();
for (int solution_index = 0; solution_index < collector->solution_count();
++solution_index) {
Assignment* const solution = collector->solution(solution_index);
std::vector<int> results(variables.size());
LOG(INFO) << "------------------------------------------------------------";
LOG(INFO) << "Solution " << solution_index + 1;
LOG(INFO) << "Cost: " << solution->Value(objective_var);
for (int i = 0; i < variables.size(); ++i) {
results[i] = solution->Value(variables[i]);
LOG(INFO) << " Variable " << key_from_index[i] << ": " << results[i];
}
if (CheckConstraintSatisfaction(data_constraints, results,
index_from_key)) {
LOG(INFO) << "All hard constraints satisfied.";
} else {
LOG(INFO) << "Warning! Hard constraint violation detected.";
}
if (CheckVariablePosition(data_variables, results, index_from_key)) {
LOG(INFO) << "All hard variables stayed unharmed.";
} else {
LOG(INFO) << "Warning! Hard variable modification detected.";
}
LOG(INFO) << "Values used: " << NumberOfAssignedValues(results);
LOG(INFO) << "Maximum value used: "
<< *std::max_element(results.begin(), results.end());
LOG(INFO) << " Failures: " << collector->failures(solution_index);
}
LOG(INFO) << " ============================================================";
}
void PrintResultsSoft(SolutionCollector* const collector,
const std::vector<IntVar*>& variables,
IntVar* const total_cost,
const absl::btree_map<int, FapVariable>& hard_variables,
absl::Span<const FapConstraint> hard_constraints,
const absl::btree_map<int, FapVariable>& soft_variables,
absl::Span<const FapConstraint> soft_constraints,
const absl::btree_map<int, int>& index_from_key,
absl::Span<const int> key_from_index) {
LOG(INFO) << "Printing...";
LOG(INFO) << "Number of Solutions: " << collector->solution_count();
for (int solution_index = 0; solution_index < collector->solution_count();
++solution_index) {
Assignment* const solution = collector->solution(solution_index);
std::vector<int> results(variables.size());
LOG(INFO) << "------------------------------------------------------------";
LOG(INFO) << "Solution";
for (int i = 0; i < variables.size(); ++i) {
results[i] = solution->Value(variables[i]);
LOG(INFO) << " Variable " << key_from_index[i] << ": " << results[i];
}
if (CheckConstraintSatisfaction(hard_constraints, results,
index_from_key)) {
LOG(INFO) << "All hard constraints satisfied.";
} else {
LOG(INFO) << "Warning! Hard constraint violation detected.";
}
if (CheckVariablePosition(hard_variables, results, index_from_key)) {
LOG(INFO) << "All hard variables stayed unharmed.";
} else {
LOG(INFO) << "Warning! Hard constraint violation detected.";
}
if (CheckConstraintSatisfaction(soft_constraints, results,
index_from_key) &&
CheckVariablePosition(soft_variables, results, index_from_key)) {
LOG(INFO) << "Problem feasible: "
"Soft constraints and soft variables satisfied.";
LOG(INFO) << " Weighted Sum: " << solution->Value(total_cost);
} else {
LOG(INFO) << "Problem unfeasible. Optimized weighted sum of violations.";
LOG(INFO) << " Weighted Sum: " << solution->Value(total_cost);
}
LOG(INFO) << "Values used: " << NumberOfAssignedValues(results);
LOG(INFO) << "Maximum value used: "
<< *std::max_element(results.begin(), results.end());
LOG(INFO) << " Failures: " << collector->failures(solution_index);
}
LOG(INFO) << " ============================================================";
}
} // namespace operations_research