-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.cpp
165 lines (132 loc) · 4.45 KB
/
point.cpp
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
#include "point.h"
extern bool debug;
Point::Point(int N)
{
adjacentMatrix = (float **)malloc(N * sizeof(int *));
for (int i = 0; i < N ; ++i)
adjacentMatrix[i] = (float *)malloc(N * sizeof(int));
pointNum = N;
}
Point::~Point()
{
for (int i = 0; i < pointNum; i++)
free(adjacentMatrix[i]);
free(adjacentMatrix);
}
//Added for EC2
void Point::generateProblemFile()
{
pointset.clear();
int xp = 0;
int yp = 0;
ofstream Problem_File("Problem_File");
Problem_File << "NAME : Problem_File" << endl;
Problem_File << "COMMENT : Initial randomly generated pointset" << endl;
Problem_File << "TYPE : TSP" << endl;
Problem_File << "DIMENSION : " << pointNum << endl;
Problem_File << "EDGE_WEIGHT_TYPE : EUC_2D" << endl;
Problem_File << "NODE_COORD_SECTION" << endl;
//uniform distribution generation
std::default_random_engine x_generator;
std::default_random_engine y_generator;
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator(seed);
std::uniform_int_distribution<int> x_distribution(0, height);
std::uniform_int_distribution<int> y_distribution(0, width);
int count = 1;
for (int i = 0; i < pointNum; i++) {
xp = x_distribution(generator);
yp = y_distribution(generator);
// Print points to file for LKH
Problem_File << count << " " << xp << " " << yp << endl;
pair<int, int> point(xp, yp);
pointset.insert(point);
count++;
}
Problem_File.close();
}
void Point::generatePoint(unsigned int H, unsigned int W, unsigned int N)
{
int row = 0, col = 0;
int xp = 0;
int yp = 0;
if (N > MAX_N || W > MAX_N || H > MAX_N) {
cout << "W,H,N should be less than " << MAX_N << endl;
return;
} else if (N > W + H) {
cout << "N should be less than H or W" << endl;
return;
}
pointNum = N;
this->width = W;
this->height = H;
// Open file and write points/parameters for LKH
ofstream Problem_File("Problem_File");
Problem_File << "NAME : Problem_File" << endl;
Problem_File << "COMMENT : Initial randomly generated pointset" << endl;
Problem_File << "TYPE : TSP" << endl;
Problem_File << "DIMENSION : " << N << endl;
Problem_File << "EDGE_WEIGHT_TYPE : EUC_2D" << endl;
Problem_File << "NODE_COORD_SECTION" << endl;
//uniform distribution generation
std::default_random_engine x_generator;
std::default_random_engine y_generator;
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator(seed);
std::uniform_int_distribution<int> x_distribution(0, H);
std::uniform_int_distribution<int> y_distribution(0, W);
int count = 1;
for (unsigned int i = 0; i < N; i++) {
xp = x_distribution(generator);
yp = y_distribution(generator);
// Print points to file for LKH
Problem_File << count << " " << xp << " " << yp << endl;
pair<int, int> point(xp, yp);
pointset.insert(point);
count++;
}
for (set<pair<int, int>>::iterator it = pointset.begin() ; it != pointset.end() ; ++it, ++row) {
int x1 = (*it).first;
int y1 = (*it).second;
col = 0;
for (set<pair<int, int>>::iterator that = pointset.begin() ; that != pointset.end() ; ++that, ++col) {
int x2 = (*that).first;
int y2 = (*that).second;
adjacentMatrix[row][col] = getEuclideanDistance(x1, y1, x2, y2);
}
if (debug) {
if (row % 100 == 0) //show progress
cout << ".";
}
}
Problem_File.close();
}
void Point::printPointset()
{
cout << "Generated pointset list: " << endl;
for (set<pair<int, int>>::iterator it = pointset.begin() ; it != pointset.end() ; ++it)
cout << (*it).first << " , " << (*it).second << endl;
cout << endl;
cout << "adjacency matrix: " << endl;
for (int i = 0; i < pointNum ; ++i) {
for (int j = 0; j < pointNum ; ++j)
cout << adjacentMatrix[i][j] << " ";
cout << endl;
}
}
float Point::getEuclideanDistance(int x1, int y1, int x2, int y2)
{
return sqrt(pow((double)(x1 - x2), 2) + pow((double)(y1 - y2), 2));
}
set<pair<int, int>> Point::getPointset()
{
return pointset;
}
float **Point::getAdjacentMatrix()
{
return adjacentMatrix;
}
int Point::getPointNum()
{
return pointNum;
}