-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
159 lines (139 loc) · 3.92 KB
/
main.cpp
File metadata and controls
159 lines (139 loc) · 3.92 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
// -- STL -- //
#include <iostream>
#include <iterator>
#include <chrono> // for high_resolution_clock
// -- OpenCV -- //
#include <opencv2/core/persistence.hpp>
// -- Project -- //
#include "graph.h"
using namespace std;
using namespace cv;
cv::Mat readFromFile(std::string _file)
{
cv::Mat matrix;
cv::FileStorage fs(_file, FileStorage::READ);
fs["data"] >> matrix;
return matrix;
}
cv::Mat generateCase1()
{
cv::Mat matrix(6, 6, CV_32FC1);
matrix.setTo(0.0f);
matrix.at<float>(0, 1) = 0.8f;
matrix.at<float>(1, 2) = 0.1f;
matrix.at<float>(1, 4) = 0.9f;
matrix.at<float>(1, 3) = 0.3f;
matrix.at<float>(2, 5) = 0.5f;
matrix.at<float>(4, 5) = 0.4f;
return matrix;
}
cv::Mat generateCase2()
{
cv::Mat matrix(5, 5, CV_32FC1);
matrix.setTo(0.0f);
matrix.at<float>(0, 2) = 0.5f;
matrix.at<float>(1, 2) = 0.5f;
matrix.at<float>(2, 3) = 0.5f;
matrix.at<float>(2, 4) = 0.5f;
return matrix;
}
cv::Mat generateCase3()
{
cv::Mat matrix(6, 6, CV_32FC1);
matrix.setTo(0.0f);
matrix.at<float>(0, 3) = 0.65f;
matrix.at<float>(0, 4) = 0.39f;
matrix.at<float>(0, 5) = 0.48f;
matrix.at<float>(1, 2) = 0.77f;
matrix.at<float>(1, 3) = 0.48f;
matrix.at<float>(1, 4) = 0.66f;
matrix.at<float>(1, 5) = 0.31f;
matrix.at<float>(2, 3) = 0.48f;
matrix.at<float>(2, 4) = 0.74f;
matrix.at<float>(2, 5) = 0.36f;
matrix.at<float>(3, 4) = 0.10f;
matrix.at<float>(3, 5) = 0.89f;
return matrix;
}
cv::Mat generateCase4()
{
cv::Mat matrix(7, 7, CV_32FC1);
matrix.setTo(0.0f);
matrix.at<float>(0, 2) = 0.9f;
matrix.at<float>(1, 3) = 0.8f;
matrix.at<float>(2, 4) = 0.3f;
matrix.at<float>(2, 5) = 0.1f;
matrix.at<float>(2, 6) = 0.1f;
matrix.at<float>(3, 4) = 0.3f;
matrix.at<float>(3, 5) = 0.1f;
matrix.at<float>(3, 6) = 0.1f;
matrix.at<float>(4, 6) = 0.5f;
matrix.at<float>(4, 5) = 0.4f;
return matrix;
}
void writeCypher(const cv::Mat& _mat, std::string _fileName)
{
/* example: writing a Graph in Cypher
MERGE (a:Loc {name:'A'})
MERGE (b:Loc {name:'B'})
MERGE (c:Loc {name:'C'})
MERGE (d:Loc {name:'D'})
MERGE (e:Loc {name:'E'})
MERGE (f:Loc {name:'F'})
MERGE (a)-[:ROAD {cost:50}]->(b)
MERGE (a)-[:ROAD {cost:50}]->(c)
MERGE (a)-[:ROAD {cost:100}]->(d)
MERGE (b)-[:ROAD {cost:40}]->(d)
MERGE (c)-[:ROAD {cost:40}]->(d)
MERGE (c)-[:ROAD {cost:80}]->(e)
MERGE (d)-[:ROAD {cost:30}]->(e)
MERGE (d)-[:ROAD {cost:80}]->(f)
MERGE (e)-[:ROAD {cost:40}]->(f);
*/
std::ofstream myfile(_fileName);
if(myfile.is_open())
{
for (int row = 0; row < _mat.rows; ++row)
{
myfile << "MERGE (id" << row << ":Node {name:'" << row << "'})" << endl;
}
for (int row = 0; row < _mat.rows; ++row)
{
for (int col = 0; col < _mat.cols; ++col)
{
float val = _mat.at<float>(row, col);
if (val > 0.0f)
{
myfile << "MERGE (id" << row << ")-[:Link {cost:" << 1.0 - val << "}]->(id" << col << ")" << endl;
}
}
}
}
myfile.close();
}
int main()
{
//cv::Mat matrix = readFromFile("data.yml");
//cv::Mat matrix = generateCase1();
//cv::Mat matrix = generateCase2();
//cv::Mat matrix = generateCase3();
cv::Mat matrix = generateCase4();
// Export to Cypher, so the graph can be visualized using Neo4j
writeCypher(matrix, "cypher_data.txt");
// Run Graph
bool verbose = false;
std::cout << "Input Graph: " << matrix.rows << " x " << matrix.cols << " nodes." << std::endl;
auto start = std::chrono::high_resolution_clock::now();
Graph graph(matrix, verbose);
auto finish = std::chrono::high_resolution_clock::now();
std::vector<Graph::Path> paths = graph.getBestPaths();
std::cout << "Found " << paths.size() << " paths: " << std::endl;
for(auto p : paths)
p.printf();
std::chrono::duration<double> elapsed = finish - start;
std::cout << "Elapsed time: " << elapsed.count() << " s\n";
// Finish
cout << "Press Enter to Finish...";
cin.ignore();
return 0;
}