Skip to content

Commit 3df8c87

Browse files
committed
fix: fix issues when reading the adjustment matrix
1 parent 64e6c78 commit 3df8c87

4 files changed

Lines changed: 29 additions & 13 deletions

File tree

src/AdjustmentMatrix.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ static bool check_duplicates(std::span<const AdjustmentArc> arcs) {
3030
}
3131
#endif
3232

33+
void AdjustmentMatrix::normalize_arcs() {
34+
if (_type == AdjustmentMatrixType::symmetric) {
35+
for (auto& a : _arcs) {
36+
if (a.from > a.to) { std::swap(a.from, a.to); }
37+
}
38+
}
39+
std::sort(
40+
_arcs.begin(), _arcs.end(), [](const auto& a, const auto& b) -> bool {
41+
return a.from < b.from || (a.from == b.from && a.to < b.to);
42+
});
43+
}
44+
3345
/* assume that the matrix is sorted */
3446
/* need to check that:
3547
* - No duplicates
@@ -55,7 +67,7 @@ static bool is_valid(std::span<const AdjustmentArc> arcs) {
5567
return a.size() == b.size();
5668
});
5769

58-
return check_duplicates(arcs) && (decreasing != equal);
70+
return check_duplicates(arcs) && ((decreasing != increasing) != equal);
5971
#else
6072
std::unordered_set<
6173
std::pair<decltype(AdjustmentArc::from), decltype(AdjustmentArc::to)> >
@@ -82,7 +94,8 @@ void AdjustmentMatrix::read_arcs(CSVReader& reader,
8294
_region_count = area_names.size();
8395
#ifdef __cpp_lib_ranges_zip
8496
auto reverse_area_name_map =
85-
std::views::zip(area_names, std::views::iota(0ul, area_names.size()))
97+
std::views::zip(std::views::reverse(area_names),
98+
std::views::iota(0ul, area_names.size()))
8699
| std::ranges::to<std::unordered_map>();
87100
#else
88101
std::unordered_map<std::string, size_t> reverse_area_name_map;
@@ -104,11 +117,6 @@ void AdjustmentMatrix::read_arcs(CSVReader& reader,
104117
.dist = row.get<double>(dist_key)});
105118
}
106119
} catch (std::exception& err) { throw CSVValueError{err.what()}; }
107-
108-
std::sort(
109-
_arcs.begin(), _arcs.end(), [](const auto& a, const auto& b) -> bool {
110-
return a.from < b.from || (a.from == b.from && a.to < b.to);
111-
});
112120
}
113121

114122
AdjustmentMatrix::AdjustmentMatrix(const std::filesystem::path& filename,
@@ -152,6 +160,9 @@ AdjustmentMatrix::AdjustmentMatrix(std::istringstream&& matstream,
152160
}
153161

154162
_type = determine_matrix_symmetry(_arcs, _region_count);
163+
164+
normalize_arcs();
165+
if (!is_valid(_arcs)) { _type = AdjustmentMatrixType::invalid; }
155166
if (_type == AdjustmentMatrixType::invalid) {
156167
throw std::runtime_error{"Adjustment matrix is invalid"};
157168
}
@@ -186,9 +197,6 @@ AdjustmentMatrixType AdjustmentMatrix::determine_matrix_symmetry(
186197
} else if (arcs.size() == (region_count * (region_count - 1))) {
187198
type = AdjustmentMatrixType::nonsymmetric;
188199
}
189-
if (type == AdjustmentMatrixType::invalid || !is_valid(arcs)) {
190-
return AdjustmentMatrixType::invalid;
191-
}
192200
return type;
193201
}
194202
} // namespace lagrange

src/AdjustmentMatrix.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class AdjustmentMatrix {
4343

4444
void read_arcs(CSVReader&, const std::vector<std::string>&);
4545

46+
void normalize_arcs();
47+
4648
size_t _region_count;
4749
AdjustmentMatrixType _type;
4850
std::vector<AdjustmentArc> _arcs;

src/ConfigFile.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,13 @@ class ConfigFile {
8787
try {
8888
p.adjustment_matrix = AdjustmentMatrix{
8989
p.adjustment_matrix_filename.value(), _area_names};
90-
} catch (const std::exception& err) { OllKorrect = false; }
90+
} catch (const std::exception& err) {
91+
LOG_ERROR("Failed to read adjustment matrix {}: {}",
92+
p.adjustment_matrix_filename->c_str(),
93+
err.what());
94+
95+
OllKorrect = false;
96+
}
9197
}
9298
}
9399
return OllKorrect;

tests/src/adjustment_matrix.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
TEST(AdjustmentMatrix, simple_symmetric) {
55
constexpr auto csv_string =
66
"from, to, dist\na, b, 1.0\n a, c, 2.0\n b, c, 3.0";
7-
std::vector<std::string> areanames = {"a", "b", "c"};
7+
std::vector<std::string> areanames = {"c", "b", "a"};
88
lagrange::AdjustmentMatrix adj(std::istringstream{csv_string}, areanames);
99
EXPECT_EQ(adj.type(), lagrange::AdjustmentMatrixType::symmetric);
1010
auto matrix = adj.to_matrix();
@@ -32,7 +32,7 @@ TEST(AdjustmentMatrix, simple_nonsymmetric) {
3232
"b, c, 3.0\n"
3333
"b, a, 4.0\n"
3434
"c, b, 6.0";
35-
std::vector<std::string> areanames = {"a", "b", "c"};
35+
std::vector<std::string> areanames = {"c", "b", "a"};
3636
lagrange::AdjustmentMatrix adj(std::istringstream{csv_string}, areanames);
3737
EXPECT_EQ(adj.type(), lagrange::AdjustmentMatrixType::nonsymmetric);
3838
auto matrix = adj.to_matrix();

0 commit comments

Comments
 (0)