Skip to content

Commit 0541daa

Browse files
author
Marc Geilen
committed
Merge branch 'clang-tidy' of git.ics.ele.tue.nl:computational-modeling/maxpluslib into clang-tidy
2 parents 17d53fe + 30c1e2c commit 0541daa

File tree

4 files changed

+71
-30
lines changed

4 files changed

+71
-30
lines changed

include/maxplus/base/analysis/mcm/mcmhoward.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ namespace Graphs {
6060
* The function converts a weighted directed graph used in the MCM algorithms
6161
* to a sparse matrix input for Howard's algorithm.
6262
*/
63-
void convertMCMgraphToMatrix(const MCMgraph& g, std::shared_ptr<std::vector<int>> *ij, std::shared_ptr<std::vector<CDouble>> *A);
63+
void convertMCMgraphToMatrix(MCMgraph& g, std::shared_ptr<std::vector<int>> *ij, std::shared_ptr<std::vector<CDouble>> *A);
6464

6565
/**
6666
* Howard ()
@@ -78,13 +78,13 @@ void convertMCMgraphToMatrix(const MCMgraph& g, std::shared_ptr<std::vector<int>
7878
*
7979
*/
8080

81-
void Howard(const std::vector<std::vector<int>>& ij,
82-
const std::vector<std::vector<CDouble>>& A,
81+
void Howard(const std::vector<int> &ij,
82+
const std::vector<CDouble> &A,
8383
int nr_nodes,
8484
int nr_arcs,
85-
std::vector<CDouble> &chi,
86-
std::vector<CDouble>& v,
87-
std::shared_ptr<std::vector<int>> (*policy),
85+
std::shared_ptr<std::vector<CDouble>> *chi,
86+
std::shared_ptr<std::vector<CDouble>> *v,
87+
std::shared_ptr<std::vector<int>>(*policy),
8888
int *nr_iterations,
8989
int *nr_components);
9090

src/base/analysis/mcm/mcmdg.cc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ namespace Graphs {
5656
*/
5757
CDouble mcmDG(MCMgraph &mcmGraph) {
5858
// Allocate memory
59-
const unsigned int n = mcmGraph.nrVisibleNodes();
59+
const int n = mcmGraph.nrVisibleNodes();
6060
std::vector<int> level(n);
6161
std::vector<std::vector<int>> pi(n + 1, std::vector<int>(n));
6262
std::vector<std::vector<int>> d(n + 1, std::vector<int>(n));
6363

6464
// Initialize
65-
for (unsigned int i = 0; i < n; i++) {
65+
for (int i = 0; i < n; i++) {
6666
level[i] = -1;
6767
}
6868
d[0][0] = 0;
@@ -74,13 +74,12 @@ CDouble mcmDG(MCMgraph &mcmGraph) {
7474
Q_u.push_back(&(mcmGraph.getNodes().front()));
7575

7676
// Compute the distances
77-
unsigned int k = Q_k.front();
77+
int k = Q_k.front();
7878
Q_k.pop_front();
7979
MCMnode* u = Q_u.front();
8080
Q_u.pop_front();
8181
do {
82-
for (auto iter = u->out.begin(); iter != u->out.end(); iter++) {
83-
MCMedge* e = *iter;
82+
for (auto& e: u->out) {
8483
MCMnode* v = e->dst;
8584

8685
if (level[v->id] < static_cast<int>(k + 1)) {

src/base/analysis/mcm/mcmhoward.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class AlgHoward {
111111
Update_Policy();
112112
New_Build_Inverse();
113113
(*NIterations)++;
114-
} while ((!improved) && *NIterations < MAX_NIterations);
114+
} while (improved && *NIterations < MAX_NIterations);
115115

116116
End_Message();
117117
}
@@ -127,7 +127,7 @@ class AlgHoward {
127127
int *NIterations;
128128
int *NComponents;
129129

130-
std::shared_ptr<std::vector<int>> new_pi = nullptr; /* new policy */
130+
std::shared_ptr<std::vector<int>> new_pi = std::make_shared<std::vector<int>>(); /* new policy */
131131
/* The inverse policy is coded by a linearly chained list.
132132
* pi_inv_idx[i]= pointer to the chain of inverses of node i.
133133
*/
@@ -328,7 +328,7 @@ class AlgHoward {
328328
if (chir[ij[i * 2 + 1]] == new_chi[ij[i * 2]]) {
329329
CDouble w = a[i] + vr[ij[i * 2 + 1]] - chir[ij[i * 2 + 1]];
330330
if (w > v_aux[ij[i * 2]] + epsilon) {
331-
*improved = 1;
331+
*improved = true;
332332
v_aux[ij[i * 2]] = w;
333333
(*new_pi)[ij[i * 2]] = ij[i * 2 + 1];
334334
new_c[ij[i * 2]] = a[i];
@@ -341,7 +341,7 @@ class AlgHoward {
341341
for (int i = 0; i < narcs; i++) {
342342
CDouble w = a[i] + vr[ij[i * 2 + 1]] - chir[ij[i * 2 + 1]];
343343
if (w > v_aux[ij[i * 2]] + epsilon) {
344-
*improved = 1;
344+
*improved = true;
345345
v_aux[ij[i * 2]] = w;
346346
(*new_pi)[ij[i * 2]] = ij[i * 2 + 1];
347347
new_c[ij[i * 2]] = a[i];
@@ -497,9 +497,9 @@ void convertMCMgraphToMatrix(MCMgraph &g,
497497
}
498498

499499
*ij = std::make_shared<std::vector<int>>(2 * g.getEdges().size());
500-
auto ijr = **ij;
500+
auto& ijr = *(*ij);
501501
*A = std::make_shared<std::vector<CDouble>>(g.getEdges().size());
502-
auto Ar = **A;
502+
auto& Ar = *(*A);
503503

504504
// Create an entry in the matrices for each edge
505505
for (const auto &e : g.getEdges()) {

src/testbench/base/mcmtest.cc

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "algebra/mptype.h"
44
#include "base/analysis/mcm/mcmdg.h"
55
#include "base/analysis/mcm/mcmgraph.h"
6+
#include <base/analysis/mcm/mcmhoward.h>
67
#include "mcmtest.h"
78
#include "testing.h"
89

@@ -17,31 +18,72 @@ void MCMTest::Run() {
1718
this->test_yto();
1819
};
1920

21+
MCMgraph makeGraph1() {
22+
MCMgraph g;
23+
24+
MCMnode &n0 = *g.addNode(0);
25+
MCMnode &n1 = *g.addNode(1);
26+
MCMnode &n2 = *g.addNode(2);
27+
MCMnode &n3 = *g.addNode(3);
28+
MCMnode &n4 = *g.addNode(4);
29+
g.addEdge(0, n0, n1, 1.0, 1.0);
30+
g.addEdge(1, n1, n2, 2.0, 0.0);
31+
g.addEdge(2, n2, n3, 3.0, 0.0);
32+
g.addEdge(3, n3, n0, 4.0, 0.0);
33+
g.addEdge(4, n3, n3, 2.0, 0.0);
34+
g.addEdge(5, n3, n4, 10.0, 0.0);
35+
g.addEdge(6, n4, n4, 1.0, 0.0);
36+
return g;
37+
}
38+
2039
// Test mcmdg.
2140
void MCMTest::test_dg() {
2241
std::cout << "Running test: MCM-dg" << std::endl;
23-
MCMgraph g;
24-
25-
MCMnode& n0 = *g.addNode(0);
26-
MCMnode& n1 = *g.addNode(1);
27-
MCMnode& n2 = *g.addNode(2);
28-
MCMnode& n3 = *g.addNode(3);
29-
g.addEdge(0, n0, n1, 2.0, 1.0);
30-
g.addEdge(0, n1, n2, 2.0, 0.0);
31-
g.addEdge(0, n2, n3, 2.0, 0.0);
32-
g.addEdge(0, n3, n0, 2.0, 0.0);
3342

43+
MCMgraph g = makeGraph1();
3444
CDouble result = mcmDG(g);
35-
ASSERT_APPROX_EQUAL(1.0, result, 1e-5);
45+
ASSERT_APPROX_EQUAL(2.5, result, 1e-5);
3646
}
3747

3848
/// Test MCM Howard.
3949
void MCMTest::test_howard() {
4050
std::cout << "Running test: MCM-Howard" << std::endl;
41-
ASSERT_APPROX_EQUAL(1.0, 1.0, 1e-5);
51+
MCMgraph g = makeGraph1();
52+
53+
std::shared_ptr<std::vector<int>> ij = nullptr;
54+
std::shared_ptr<std::vector<CDouble>> A = nullptr;
55+
std::shared_ptr<std::vector<CDouble>> chi = nullptr;
56+
std::shared_ptr<std::vector<CDouble>> v = nullptr;
57+
std::shared_ptr<std::vector<int>> policy = nullptr;
58+
int nr_iterations;
59+
int nr_components;
60+
61+
convertMCMgraphToMatrix(g, &ij, &A);
62+
63+
Howard(*ij,
64+
*A,
65+
static_cast<int>(g.getNodes().size()),
66+
static_cast<int>(g.getEdges().size()),
67+
&chi,
68+
&v,
69+
&policy,
70+
&nr_iterations,
71+
&nr_components);
72+
73+
74+
ASSERT_APPROX_EQUAL(2.5, chi->at(0), 1e-5);
75+
ASSERT_APPROX_EQUAL(2.5, chi->at(1), 1e-5);
76+
ASSERT_APPROX_EQUAL(2.5, chi->at(2), 1e-5);
77+
ASSERT_APPROX_EQUAL(2.5, chi->at(3), 1e-5);
78+
ASSERT_APPROX_EQUAL(1.0, chi->at(4), 1e-5);
79+
ASSERT_EQUAL(1, policy->at(0));
80+
ASSERT_EQUAL(2, policy->at(1));
81+
ASSERT_EQUAL(3, policy->at(2));
82+
ASSERT_EQUAL(0, policy->at(3));
83+
ASSERT_EQUAL(4, policy->at(4));
4284
}
4385

44-
/// Test MCM Howard.
86+
/// Test MCM Karp.
4587
void MCMTest::test_karp() {
4688
std::cout << "Running test: MCM-Karp" << std::endl;
4789
ASSERT_APPROX_EQUAL(1.0, 1.0, 1e-5);

0 commit comments

Comments
 (0)