-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRouteBuilder.cs
More file actions
334 lines (273 loc) · 11 KB
/
RouteBuilder.cs
File metadata and controls
334 lines (273 loc) · 11 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
using System.Collections.Generic;
using System.Drawing;
namespace Clusterization_algorithms
{
public class RouteBuilder
{
private Point _startPoint = new Point(0, 0);
private List<Point> _all_points = new List<Point> { }; // start point position
private List<Point> _routeList = new List<Point> { }; // finish point position
private List<Point> _convex_hull = new List<Point> { };
private List<Point> _inside_points = new List<Point> { };
private double _length;
public List<Point> Convex_hull { get => _convex_hull; }
public List<Point> RouteList { get => _routeList; set => _routeList = value; }
public RouteBuilder() { }
public void SetPoints(List<Point> points)
{
_all_points.Clear();
_all_points.AddRange(points);
}
public List<Point> CalculateRouteByConvexHullInsertion()
{
JarvisMarch();
ConvexHullInsertion();
List<Point> unsort_route = new List<Point> { };
unsort_route.AddRange(_routeList);
_routeList.Clear();
SortInsidePoints();
ConvexHullInsertion();
double unsorted_route_length = Calculator.CalcRouteLength(unsort_route);
double sorted_route_length = Calculator.CalcRouteLength(_routeList);
if (unsorted_route_length < sorted_route_length)
{
_routeList.Clear();
_routeList.AddRange(unsort_route);
}
return _routeList;
}
private List<Point> ConvexHullInsertion()
{
_routeList.AddRange(_convex_hull);
for (int i = 0; i < _inside_points.Count; i++)
{
double minCos = Calculator.GetCOS(_routeList[0], _inside_points[i], _routeList[1]); // variables value when j = 0 in loop
int pos = 0; // first selected position in route
for (int j = 1; j < _routeList.Count - 1; j++)
{
double cos = Calculator.GetCOS(_routeList[j], _inside_points[i], _routeList[j + 1]);
if (cos >= minCos)
{
minCos = cos;
pos = j;
}
}
AddToRouteAtPos(pos + 1, _inside_points[i]);
}
return _routeList;
}
/// <summary>
/// Add point at the selected position in route
/// </summary>
private void AddToRouteAtPos(int position, Point point)
{
List<Point> newRoute = new List<Point> { };
for (int i = 0; i < _routeList.Count; i++)
{
if (i == position)
newRoute.Add(point);
newRoute.Add(_routeList[i]);
}
_routeList.Clear();
_routeList.AddRange(newRoute);
}
/// <summary>
/// Jarvis march algorithm, also known as gift wrappening algorithm.
/// If not stop it on a start point when wrappening is finished, will create a spiral route
/// </summary>
public void JarvisMarch(bool getSpiralRoute = false)
{
_routeList.Clear();
_convex_hull.Clear();
_inside_points.Clear();
List<Point> points = new List<Point> { };
points.AddRange(_all_points);
Point falsePoint = new Point(_startPoint.X - 1, _startPoint.Y); // line(vector) point where wrappening starts (counterclockwise)
// axes directions -> ↓(Y) →(X)
_convex_hull.Add(_startPoint);
_convex_hull.Add(falsePoint);
Point point = points[0];
if(!getSpiralRoute)
points.Add(_startPoint); // if we want to wrap points
int ki = 0;
do
{
double max_cos = -1; // cos(0°...90°...180°) = 1...0...-1
Point nextPoint = _startPoint;
for (int i = 0; i < points.Count; i++)
{ // where cos is min that point is next
double cos = Calculator.GetCOS(_convex_hull[ki], _convex_hull[ki + 1], points[i]); // шукаємо косинус кута між векторами
//Console.WriteLine("cos = " +
if (cos > max_cos) // the less cos the larger angle between vectors
{
max_cos = cos;
nextPoint = points[i];
}
}
point = nextPoint;
ki++;
points.Remove(nextPoint);
_convex_hull.Add(nextPoint);
}
while (point != _startPoint);
_convex_hull.Remove(falsePoint);
_inside_points.AddRange(points);
if (getSpiralRoute)
{
_routeList = _convex_hull;
}
}
/// <summary>
/// Sort points inside convex hull by they mass center
/// </summary>
private void SortInsidePoints() // TODO: I definitely wanted to improve something here.
{
Point center = Calculator.FindCentroid(_all_points);
Dictionary<Point, double> dict = new Dictionary<Point, double> { };
for (int i = 0; i < _inside_points.Count; i++)
{
double dist = Calculator.CalcDistance(center, _inside_points[i]);
dict.Add(_inside_points[i], dist);
}
dict = Calculator.SortDictionaryByValue(dict);
_inside_points.Clear();
_inside_points.AddRange(Calculator.DictionaryToList(dict));
}
public List<Point> CalculateRouteBruteForce(List<Point> pointList)
{
if (pointList.Count < 10)
{
List<Point> points = new List<Point> { };
points.Add(_startPoint);
points.AddRange(pointList);
points.Add(_startPoint);
_length = double.MaxValue;
RecursivePermutation(1, points);
}
return _routeList;
}
private void RecursivePermutation(int startPos, List<Point> points) // first and last points are fixed
{
int size = points.Count;
if (startPos == size - 1)
{
double newLength = Calculator.CalcRouteLength(points);
if (newLength < _length)
{
_length = newLength;
_routeList.Clear();
_routeList.AddRange(points);
}
}
else
{
for (int j = startPos; j < size - 1; ++j)
{
Swap(startPos, j, points);
startPos++;
RecursivePermutation(startPos, points);
startPos--;
Swap(startPos, j, points);
}
}
}
private void Swap(int pos_1, int pos_2, List<Point> points)
{
Point point = points[pos_1];
points[pos_1] = points[pos_2];
points[pos_2] = point;
}
public List<Point> CalculateRouteByNearestNeighbour(List<Point> pointList)
{
List<Point> points = new List<Point> { };
points.AddRange(pointList);
List<bool> pointIsUsed = new List<bool> {};
foreach (Point point in points)
{
pointIsUsed.Add(false);
}
List<Point> route = new List<Point> { _startPoint };
Point current_point = _startPoint;
for (int j = 0; j < points.Count; j++)
{
int min_point_index = 0;
double min_distance = double.MaxValue;
for (int i = 0; i < points.Count; i++)
{
if (!pointIsUsed[i])
{
double distance = Calculator.CalcDistance(current_point, points[i]);
if (distance < min_distance)
{
min_distance = distance;
min_point_index = i;
}
}
}
current_point = points[min_point_index];
pointIsUsed[min_point_index] = true;
route.Add(current_point);
}
route.Add(_startPoint);
_routeList = route;
return route;
}
public List<Point> CalculateRouteByFPPWR(List<Point> pointList, int x_count, int y_count, double width, double heigth)
{
List<Point> points = new List<Point> { };
points.AddRange(pointList);
List<Point> route = new List<Point> { _startPoint };
// cell sides size
double x_side = width / x_count;
double y_side = heigth / y_count;
for (int iy = 0; iy < y_count; iy++)
{
double y_up = y_side * iy;
double y_down = y_side * (iy + 1);
if (iy % 2 == 0)
{
for (int ix = 0; ix < x_count; ix++)
{
double x_left = x_side * ix;
double x_right = x_side * (ix + 1);
List<Point> cell = GetCellPointList(points, x_left, x_right, y_up, y_down, route);
cell = Calculator.SortPointListByX(cell);
route.AddRange(cell);
}
}
else {
for (int ix = x_count; ix > 0; ix--)
{
double x_right = x_side * ix;
double x_left = x_side * (ix - 1);
List<Point> cell = GetCellPointList(points, x_left, x_right, y_up, y_down, route);
cell = Calculator.SortPointListByX(cell);
cell.Reverse();
route.AddRange(cell);
}
}
}
route.Add(_startPoint);
_routeList = route;
return route;
}
private List<Point> GetCellPointList(List<Point> points, double x_left, double x_rigth, double y_up, double y_down, List<Point> route)
{
List<Point> cell = new List<Point> { };
foreach (Point point in points)
{
if ((point.X >= x_left) && (point.X) <= x_rigth)
{
if ((point.Y >= y_up) && (point.Y) <= y_down)
{
if (!route.Contains(point))
{
cell.Add(point);
}
}
}
}
return cell;
}
}
}