Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Concepts
RandomGridFactory.hpp
Go to the documentation of this file.
1
6// Copyright (c) 2017 Czech Technical University in Prague | Faculty of Information Technology. All rights reserved.
7
8#pragma once
9
10#include <alib/pair>
11#include <alib/tuple>
12#include <alib/vector>
13#include <random>
14
15namespace graph {
16
17namespace generate {
18
20// ---------------------------------------------------------------------------------------------------------------------
21 public:
22
23 template<typename TGrid>
24 static
26 randomGrid(unsigned long height, unsigned long widht, unsigned long max_obstacles);
27
28// ---------------------------------------------------------------------------------------------------------------------
29
30};
31
32// =====================================================================================================================
33
34template<typename TGrid>
36RandomGridFactory::randomGrid(unsigned long height,
37 unsigned long width,
38 unsigned long max_obstacles) {
39 using coordinate_type = typename TGrid::coordinate_type;
40 using node_type = typename TGrid::node_type;
41
42 // Seed with a real random value, if available
43 std::random_device r;
44 std::default_random_engine e1(r());
45
46 // Create distribution
47 std::uniform_int_distribution<unsigned long> number_of_obstacles(0, max_obstacles); // Generate number of nodes
48 std::uniform_int_distribution<coordinate_type> random_height(0, height - 1); // Generate height coordinate
49 std::uniform_int_distribution<coordinate_type> random_width(0, width - 1); // Generate width coordinate
50 unsigned long obstacle_cnt = number_of_obstacles(e1);
51
52 TGrid grid(height, width);
53
54 // Generate start and goal node
55 node_type start(random_height(e1), random_width(e1));
56 node_type goal(random_height(e1), random_width(e1));
57
58 // Generate obstacles
59 for (unsigned long i = 0; i < obstacle_cnt; ++i) {
60 node_type obstacle(random_height(e1), random_width(e1));
61
62 // Try to avoid creating obstacle on place of start or goal vertex
63 if (obstacle == start || obstacle == goal) {
64 continue;
65 }
66
67 grid.addObstacle(std::move(obstacle));
68 }
69
70 return ext::make_tuple(grid, start, goal);
71}
72
73// ---------------------------------------------------------------------------------------------------------------------
74
75} // namespace generate
76
77} // namespace graph
78
Class extending the tuple class from the standard library. Original reason is to allow printing of th...
Definition: tuple.hpp:42
Definition: RandomGridFactory.hpp:19
static ext::tuple< TGrid, typename TGrid::node_type, typename TGrid::node_type > randomGrid(unsigned long height, unsigned long widht, unsigned long max_obstacles)
Definition: RandomGridFactory.hpp:36
int i
Definition: AllEpsilonClosure.h:118
constexpr auto make_tuple(Elements &&... args)
Helper of extended tuple construction. The tuple is constructed from values pack, types are deduced.
Definition: tuple.hpp:203
Definition: ReconstructPath.hpp:14
Definition: GridDirection.hpp:12
void start(measurements::stealth_string name, measurements::Type type)
Definition: measurements.cpp:14