2020#include < limits>
2121#include < random>
2222#include < vector>
23+ #include < fstream>
2324
2425#include < gtest/gtest.h>
2526#include < ros/package.h>
2627
2728#include " polygon_coverage_solvers/glkh.h"
2829
30+
2931using namespace polygon_coverage_planning ;
3032using namespace glkh ;
3133
3234const std::string kPackageName = " polygon_coverage_solvers" ;
3335
36+ int32_t read_int_from_stream (std::ifstream &stream)
37+ {
38+ int32_t num;
39+ stream.read ((char *)&num, sizeof (num));
40+ return num;
41+ }
42+
43+ Task read_binary_gtsp (const std::string& file) {
44+ // Read GTSP file (binary format)
45+ std::ifstream input (file, std::ios::binary);
46+
47+ int dimension = read_int_from_stream (input);
48+ std::cout << dimension << std::endl;
49+ int num_clusters = read_int_from_stream (input);
50+ // 3 if symmetric and triangle, 2 if asymmetric and triangle, 1 if symmetric and non-triangle and 0 otherwise.
51+ // bool is_symmetric = read_int_from_stream(input) % 2 == 1;
52+ read_int_from_stream (input);
53+
54+ std::vector<std::vector<int >> clusters (num_clusters);
55+ for (int i = 0 ; i < num_clusters; i++)
56+ {
57+ int32_t cluster_size;
58+ cluster_size = read_int_from_stream (input);
59+ clusters[i].resize (cluster_size);
60+ for (int j = 0 ; j < cluster_size; j++)
61+ {
62+ clusters[i][j] = read_int_from_stream (input);
63+ }
64+ }
65+
66+ std::vector<std::vector<int >> m (dimension);
67+ for (int i = 0 ; i < dimension; i++)
68+ {
69+ m[i].resize (dimension);
70+ for (int j = 0 ; j < dimension; j++)
71+ {
72+ m[i][j] = read_int_from_stream (input);
73+ }
74+ }
75+
76+ return Task (m, clusters);
77+ }
78+
3479TEST (Glkh, LoadFromFile) {
3580 Glkh& instance = Glkh::getInstance ();
3681
@@ -46,7 +91,8 @@ TEST(Glkh, LoadFromFile) {
4691 " 40d198.gtsp" , " 65rbg323.gtsp" };
4792 for (const std::string& instance_name : instance_names) {
4893 std::string file = instances_path + instance_name;
49- instance.setSolver (file, true );
94+ Task task = read_binary_gtsp (file);
95+ instance.setSolver (task);
5096 EXPECT_TRUE (instance.solve ());
5197 EXPECT_FALSE (instance.getSolution ().empty ());
5298 }
@@ -61,7 +107,7 @@ TEST(Glkh, LoadFromTask) {
61107 for (size_t i = 0 ; i < m.size (); ++i) {
62108 for (size_t j = 0 ; j < m[i].size (); ++j) {
63109 if (i == j) {
64- m[i][j] = std::numeric_limits<int >::max ();
110+ m[i][j] = std::numeric_limits<int >::max (); // TODO: overflow!
65111 } else {
66112 m[i][j] = rand () % 100 ;
67113 }
0 commit comments