-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestRectangles.cpp
More file actions
293 lines (233 loc) · 9.14 KB
/
TestRectangles.cpp
File metadata and controls
293 lines (233 loc) · 9.14 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
#include "BinaryGASolver.h"
#include "simple_svg_1.0.0.hpp"
#include "Common.h"
#include <iostream>
#include <iomanip>
#include <chrono>
// find path from bottom of screen up with least nodes
// path must not collide with rectangle
#define POPULATION_SIZE 400
#define MUTATION_PROBABILITY 0.01
#define CROSSOVER_FACTOR 0.75
#define MAX_NUMBER_OF_GENERATIONS 15000
#define STOP_AFTER_NUM_GENERATIONS_WITHOUT_CHANGE 300
#define RECTANGLES_SVG_WIDTH 512
#define RECTANGLES_SVG_HEIGHT 512
#define RECTANGLES_NUMBER 100
#define RECTANGLES_SIZE_FROM 15
#define RECTANGLES_SIZE_TO 60
#define BORDER_ON_BOTTOM_AND_UPPER_PART 50
#define START_POINT { RECTANGLES_SVG_WIDTH/2.0, 0.0 }
#define STEP_MAX_LENGTH 10.0
// maximum number of nodes on path
#define MAXIMUM_PATH_SIZE 150
struct Rectangle
{
Common::Point upperLeft;
double width;
double height;
};
struct Problem
{
std::vector<Rectangle> rectangles;
double width;
double height;
};
using Path = std::vector<Common::Point>;
// https://stackoverflow.com/questions/99353/how-to-test-if-a-line-segment-intersects-an-axis-aligned-rectange-in-2d
bool ValidateLineSegmentAgaintRectangle(const Rectangle & rect, const Common::Point & p1, const Common::Point & p2)
{
auto ImplicitLineEquationThroughTwoPoints = [&p1, &p2](double x, double y)
{
// F(x y) = (y2-y1)*x + (x1-x2)*y + (x2*y1-x1*y2)
return (p2.y - p1.y)*x + (p1.x - p2.x)*y + (p2.x*p1.y - p1.x * p2.y);
};
double upperLeftSide = ImplicitLineEquationThroughTwoPoints(rect.upperLeft.x, rect.upperLeft.y);
double upperRightSide = ImplicitLineEquationThroughTwoPoints(rect.upperLeft.x + rect.width, rect.upperLeft.y);
double bottomRightSide = ImplicitLineEquationThroughTwoPoints(rect.upperLeft.x + rect.width, rect.upperLeft.y - rect.height);
double bottomLeftSide = ImplicitLineEquationThroughTwoPoints(rect.upperLeft.x, rect.upperLeft.y - rect.height);
// Check if all four corners of the rectangle are on the same side of the line. The implicit equation for a line through p1 and p2 is:
if ((upperLeftSide > 0.0 && upperRightSide > 0.0 && bottomRightSide > 0.0 && bottomLeftSide > 0.0) ||
(upperLeftSide < 0.0 && upperRightSide < 0.0 && bottomRightSide < 0.0 && bottomLeftSide < 0.0))
return true;
Common::Point bottomLeft{ rect.upperLeft.x, rect.upperLeft.y - rect.height };
Common::Point upperRight{ rect.upperLeft.x + rect.width, rect.upperLeft.y };
// Project the endpoint onto the x axis, and check if the segment's shadow intersects the polygon's shadow. Repeat on the y axis:
if (p1.x > upperRight.x && p2.x > upperRight.x)
return true;
if (p1.x < bottomLeft.x && p2.x < bottomLeft.x)
return true;
if (p1.y > upperRight.y && p2.y > upperRight.y)
return true;
if (p1.y < bottomLeft.y && p2.y < bottomLeft.y)
return true;
return false;
}
// return true of segment does not intercept any rectangle
// otherwise return false
bool ValidateLineSegment(const Problem & problem, const Common::Point & p1, const Common::Point & p2)
{
if (p1.x < 0.0 || p1.y < 0.0 || p2.x < 0.0 || p2.y < 0.0)
return false;
if (p1.x > problem.width || p1.y > problem.height || p2.x > problem.width || p2.y > problem.height)
return false;
for (const auto & rect : problem.rectangles)
{
if (!ValidateLineSegmentAgaintRectangle(rect, p1, p2))
return false;
}
return true;
}
Problem GenerateProblem()
{
Problem problem;
problem.width = RECTANGLES_SVG_WIDTH;
problem.height = RECTANGLES_SVG_HEIGHT;
for (size_t i = 0; i < RECTANGLES_NUMBER; ++i)
{
Rectangle rectangle;
rectangle.width = Common::Frand(RECTANGLES_SIZE_FROM, RECTANGLES_SIZE_TO);
rectangle.height = Common::Frand(RECTANGLES_SIZE_FROM, RECTANGLES_SIZE_TO);
rectangle.upperLeft.x = Common::Frand(0.0, RECTANGLES_SVG_WIDTH - rectangle.width);
rectangle.upperLeft.y = Common::Frand(BORDER_ON_BOTTOM_AND_UPPER_PART + rectangle.height,
RECTANGLES_SVG_HEIGHT - BORDER_ON_BOTTOM_AND_UPPER_PART);
problem.rectangles.push_back(std::move(rectangle));
}
// two explicit rectangles on borders
static const double EXPLICIT_RECT_SIZE = 30.0;
Rectangle leftRect{ { 0.0, RECTANGLES_SVG_HEIGHT/2.0 }, EXPLICIT_RECT_SIZE, EXPLICIT_RECT_SIZE };
Rectangle rightRect{ { RECTANGLES_SVG_WIDTH - EXPLICIT_RECT_SIZE, RECTANGLES_SVG_HEIGHT / 2.0 }, EXPLICIT_RECT_SIZE, EXPLICIT_RECT_SIZE };
problem.rectangles.push_back(leftRect);
problem.rectangles.push_back(rightRect);
return problem;
}
void VisualizeProblem(const char * name, const Problem & problem, Path * solution = nullptr)
{
svg::Document doc(name,
svg::Layout({ problem.width, problem.height }, svg::Layout::BottomLeft));
for (const auto & rectangle : problem.rectangles)
{
doc << svg::Rectangle(svg::Point(rectangle.upperLeft.x, rectangle.upperLeft.y),
rectangle.width, rectangle.height, svg::Fill(svg::Color(100, 200, 120)));
}
if (solution)
{
std::vector<svg::Point> points(solution->size());
for (size_t i = 0; i < solution->size(); ++i)
points[i] = { solution->at(i).x, solution->at(i).y };
doc << svg::Polyline(points, svg::Fill(), svg::Stroke(2.0, svg::Color(200, 100, 120)));
}
doc << svg::Rectangle({ 0.0, problem.height }, problem.width, problem.height,
svg::Fill(), svg::Stroke(1.0, svg::Color::Silver));
doc << svg::Circle(START_POINT, 6.0, svg::Color::Silver);
doc.save();
}
Path ConvertToPath(const Problem & problem, const std::vector<double> & chromosome)
{
Path ret;
Common::Point previous, next;
previous.x = chromosome[0];
previous.y = chromosome[1];
ret.push_back(START_POINT);
previous.x += ret.back().x;
previous.y += ret.back().y;
if (ValidateLineSegment(problem, START_POINT, previous))
{
ret.push_back(previous);
for (size_t i = 2; i < chromosome.size(); i = i + 2)
{
next.x = chromosome[i];
next.y = chromosome[i+1];
next.x += previous.x;
next.y += previous.y;
if (!ValidateLineSegment(problem, previous, next))
break;
previous = next;
ret.push_back(previous);
}
}
return ret;
}
double GetFitnessOfPath(const Path & path)
{
return path.back().y;
// penalize long path
//return path.back().y - (double)path.size();
}
struct EvaluateRectangles
{
BinaryGA::EvaluationResult operator()(uint32_t generation, const std::vector<double> & chromosome)
{
if (generation != currentGeneration)
{
std::cout << "\rGeneration " << std::fixed << generation;
std::cout << " max height " << std::fixed << std::setprecision(2) << maxPath.back().y;
//closestNumber = 0.0;
currentGeneration = generation;
numberOfGenerationsWithCurrentSolution++;
}
auto path = ConvertToPath(problem, chromosome);
if (maxPath.empty() || GetFitnessOfPath(path) > GetFitnessOfPath(maxPath))
{
maxPath = path;
numberOfGenerationsWithCurrentSolution = 0;
return BinaryGA::EvaluationResult::ContinueProcessing;
}
if (numberOfGenerationsWithCurrentSolution >= STOP_AFTER_NUM_GENERATIONS_WITHOUT_CHANGE)
return BinaryGA::EvaluationResult::ObjectiveReached;
if (maxPath.back().y >= RECTANGLES_SVG_HEIGHT)
return BinaryGA::EvaluationResult::ObjectiveReached;
return BinaryGA::EvaluationResult::ContinueProcessing;
}
const Problem & problem;
uint32_t currentGeneration = 0;
uint32_t numberOfGenerationsWithCurrentSolution = 0;
Path maxPath;
};
void TestRectangles()
{
std::cout << "Rectangles" << std::endl;
auto problem = GenerateProblem();
VisualizeProblem("data\\RectanglesProblem.svg", problem);
BinaryGA::Definition<double> definition;
definition.parentSelection = BinaryGA::ParentSelectionType::Ranked;
definition.mutation = BinaryGA::MutationType::Custom;
definition.crossover = BinaryGA::CrossoverType::OnePoint;
definition.populationSize = POPULATION_SIZE;
definition.mutationProbability = MUTATION_PROBABILITY;
definition.crossoverFactor = CROSSOVER_FACTOR;
definition.maxNumberOfGenerations = MAX_NUMBER_OF_GENERATIONS;
definition.numberOfGenes = MAXIMUM_PATH_SIZE * 2;
definition.initializationCustomCallback = [](size_t index) -> std::vector<double>
{
std::vector<double> ret;
double angle = (Common::PI / (double)POPULATION_SIZE) * (double)index;
Common::Point point{ (double)STEP_MAX_LENGTH, 0.0 };
point = Common::Rotate(point, angle);
for (size_t i = 1; i < MAXIMUM_PATH_SIZE; ++i)
{
ret.push_back(point.x);
ret.push_back(point.y);
}
return ret;
};
definition.computeFitness = [&problem](const std::vector<double> & chromosome) -> double
{
auto path = ConvertToPath(problem, chromosome);
return GetFitnessOfPath(path);
};
definition.mutationCustomCallback = [](const double & value, size_t index) -> double
{
return Common::Frand(-STEP_MAX_LENGTH, STEP_MAX_LENGTH);
};
EvaluateRectangles evaluate{ problem };
definition.evaluate = std::ref(evaluate);
auto startTime = std::chrono::high_resolution_clock::now();
auto solution = BinaryGA::Solve(definition);
std::chrono::duration<double, std::milli> solveDuration = std::chrono::high_resolution_clock::now() - startTime;
std::cout << std::endl << "Generation " << evaluate.currentGeneration << " (" << solveDuration.count() << "ms)" << std::endl;
std::cout << "Best found solution: " << std::endl;
std::cout << "height: " << std::fixed << std::setprecision(2) << evaluate.maxPath.back().y << std::endl;
std::cout << std::endl;
VisualizeProblem("data\\RectanglesSolution.svg", problem, &evaluate.maxPath);
}