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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
/*!
@authors Andrei Novikov (pyclustering@yandex.ru)
@date 2014-2020
@copyright BSD-3-Clause
*/
#pragma once
#include <memory>
#include <functional>
#include <cmath>
#include <ostream>
#include <pyclustering/container/adjacency.hpp>
namespace pyclustering {
namespace container {
/**
*
* @brief Enumeration of pre-defined structures of connections between nodes in collection.
*
*/
enum class connection_t {
/*!< Connections does not exists. */
CONNECTION_NONE = 0,
/*!< Each node is connected with all nodes except itself. */
CONNECTION_ALL_TO_ALL = 1,
/*!< Each node is connected with four neighbors: left, upper, right and lower. */
CONNECTION_GRID_FOUR = 2,
/*!< Each node is connected with eight neighbors: left, left-upper, upper, upper-right, right, right-lower, lower and lower-left. */
CONNECTION_GRID_EIGHT = 3,
/*!< Each node is connected with two neighbors: left and right. */
CONNECTION_LIST_BIDIRECTIONAL = 4,
};
/**
*
* @brief Generates a sequence of characters with the representation of structure of connections.
*
*/
std::ostream & operator<<(std::ostream & p_stream, const connection_t & p_structure);
/**
*
* @brief Class for creating pre-defined most popular structures by establishing connections
* between nodes in unweight adjacency collections.
*
*/
template <typename TypeCollection = adjacency_collection>
class adjacency_connector {
protected:
using connector_controller = std::function<void(const std::size_t, const std::size_t, TypeCollection &)>;
protected:
connector_controller m_connector;
public:
/*
@brief Default constructor of connector.
*/
adjacency_connector() : m_connector([](const size_t index1, const size_t index2, TypeCollection & collection) {
collection.set_connection(index1, index2);
})
{ }
public:
/**
*
* @brief Creates connections between nodes in adjacency collection in line with specified
* structure.
* @details In case of grid structures it creates only square grids only, otherwise special
* methods should be used such as 'create_grid_four_connections(...)' or
* 'create_grid_eight_connections(...)'.
*
* @param[in] structure_type: structure of connections in adjacency collection that should be created.
* @param[out] output_adjacency_collection: adjacency collection whose connections should be updated.
*
*/
virtual void create_structure(const connection_t structure_type, TypeCollection & output_adjacency_collection) {
switch(structure_type) {
case connection_t::CONNECTION_NONE:
create_none_connections(output_adjacency_collection);
break;
case connection_t::CONNECTION_ALL_TO_ALL:
create_all_to_all_connections(output_adjacency_collection);
break;
case connection_t::CONNECTION_GRID_FOUR:
create_grid_four_connections(output_adjacency_collection);
break;
case connection_t::CONNECTION_GRID_EIGHT:
create_grid_eight_connections(output_adjacency_collection);
break;
case connection_t::CONNECTION_LIST_BIDIRECTIONAL:
create_list_bidir_connections(output_adjacency_collection);
break;
default:
throw std::runtime_error("Type of connection is not supported.");
}
}
/**
*
* @brief Removes all connections in adjacency collection.
*
* @param[out] output_adjacency_collection: adjacency collection whose connections should be updated.
*
*/
virtual void create_none_connections(TypeCollection & output_adjacency_collection) {
for (size_t i = 0; i < output_adjacency_collection.size(); i++) {
output_adjacency_collection.erase_connection(i, i);
for (size_t j = i + 1; j < output_adjacency_collection.size(); j++) {
output_adjacency_collection.erase_connection(i, j);
output_adjacency_collection.erase_connection(j, i);
}
}
}
/**
*
* @brief Creates connections between all nodes where each node has connection with others.
* @details This method does not connect node with itself.
*
* @param[out] output_adjacency_collection: adjacency collection whose connections should be updated.
*
*/
virtual void create_all_to_all_connections(TypeCollection & output_adjacency_collection) {
for (size_t i = 0; i < output_adjacency_collection.size(); i++) {
output_adjacency_collection.erase_connection(i, i);
for (size_t j = i + 1; j < output_adjacency_collection.size(); j++) {
m_connector(i, j, output_adjacency_collection);
m_connector(j, i, output_adjacency_collection);
}
}
}
/**
*
* @brief Creates connections where each node is connected with two node-neighbors (except the
* first and the last node): left and right in line with following scheme: 1 <-> 2 <-> 3 <- ... ->
* (N - 2) <-> (N - 1) <-> N.
*
* @param[out] output_adjacency_collection: adjacency collection whose connections should be updated.
*
*/
virtual void create_list_bidir_connections(TypeCollection & output_adjacency_collection) {
create_none_connections(output_adjacency_collection);
for (size_t i = 0; i < output_adjacency_collection.size(); i++) {
if (i > 0) {
m_connector(i, i - 1, output_adjacency_collection);
}
if (i < (output_adjacency_collection.size() - 1)) {
m_connector(i, i + 1, output_adjacency_collection);
}
}
}
/**
*
* @brief Creates connections where each node is connected with four node-neighbors: left, right,
* upper and lower.
* @details This method does not receive arguments that specify grid description: width and height.
* Every adjacency collection is considered as a square and if root cannot be extracted
* from amount of nodes then exception will be generated.
*
* @param[out] output_adjacency_collection: adjacency collection whose connections should be updated.
*
*/
virtual void create_grid_four_connections(TypeCollection & output_adjacency_collection) {
const double conv_side_size = std::sqrt((double)output_adjacency_collection.size());
if (conv_side_size - std::floor(conv_side_size) > 0) {
throw std::runtime_error("Invalid number of nodes in the adjacency for the square grid structure.");
}
const size_t edge = (size_t) conv_side_size;
create_grid_four_connections(edge, edge, output_adjacency_collection);
}
/**
*
* @brief Creates connections where each node is connected with four node-neighbors: left, right,
* upper and lower.
*
* @param[in] width: width of created grid structure that is defined by amount of nodes in a column.
* @param[in] height: height of created grid structure that is defined by amount of nodes in a row.
* @param[out] output_adjacency_collection: adjacency collection whose connections should be updated.
*
*/
virtual void create_grid_four_connections(const size_t width, const size_t height, TypeCollection & output_adjacency_collection) {
if (width * height != output_adjacency_collection.size()) {
throw std::runtime_error("Invalid number of nodes in the adjacency for the grid structure.");
}
create_none_connections(output_adjacency_collection);
const int signed_width = static_cast<int>(width);
for (int index = 0; index < static_cast<int>(output_adjacency_collection.size()); index++) {
const int upper_index = index - signed_width;
const int lower_index = index + signed_width;
const int left_index = index - 1;
const int right_index = index + 1;
const int node_row_index = (int) std::ceil(index / signed_width);
if (upper_index >= 0) {
m_connector(index, upper_index, output_adjacency_collection);
}
if (lower_index < (int) output_adjacency_collection.size()) {
m_connector(index, lower_index, output_adjacency_collection);
}
if ((left_index >= 0) && (std::ceil(left_index / signed_width) == node_row_index)) {
m_connector(index, left_index, output_adjacency_collection);
}
if ((right_index < (int) output_adjacency_collection.size()) && (std::ceil(right_index / signed_width) == node_row_index)) {
m_connector(index, right_index, output_adjacency_collection);
}
}
}
/**
*
* @brief Creates connections where each node is connected with eight node-neighbors: left,
* left-upper, upper, upper-right, right, right-lower, lower, lower-left.
* @details This method does not receive arguments that specify grid description: width and height.
* Every adjacency collection is considered as a square and if root cannot be extracted
* from amount of nodes then exception will be generated.
*
* @param[out] output_adjacency_collection: adjacency collection whose connections should be updated.
*
*/
virtual void create_grid_eight_connections(TypeCollection & output_adjacency_collection) {
const double conv_side_size = std::sqrt((double)output_adjacency_collection.size());
if (conv_side_size - std::floor(conv_side_size) > 0) {
throw std::runtime_error("Invalid number of nodes in the adjacency for the square grid structure.");
}
const size_t edge = (size_t) conv_side_size;
create_grid_eight_connections(edge, edge, output_adjacency_collection);
}
/**
*
* @brief Creates connections where each node is connected with eight node-neighbors: left,
* left-upper, upper, upper-right, right, right-lower, lower, lower-left.
*
* @param[in] width: width of created grid structure that is defined by amount of nodes in a column.
* @param[in] height: height of created grid structure that is defined by amount of nodes in a row.
* @param[out] output_adjacency_collection: adjacency collection whose connections should be updated.
*
*/
virtual void create_grid_eight_connections(const size_t width, const size_t height, TypeCollection & output_adjacency_collection) {
create_grid_four_connections(width, height, output_adjacency_collection); /* create connection with right, upper, left, lower neighbor */
for (int index = 0; index < (int) output_adjacency_collection.size(); index++) {
const int upper_left_index = index - 1 - (int) width;
const int upper_right_index = index + 1 - (int) width;
const int lower_left_index = index - 1 + (int) width;
const int lower_right_index = index + 1 + (int) width;
const int node_row_index = (int) std::floor(index / width);
const int upper_row_index = node_row_index - 1;
const int lower_row_index = node_row_index + 1;
if ((upper_left_index >= 0) && (std::floor(upper_left_index / width) == upper_row_index)) {
m_connector(index, upper_left_index, output_adjacency_collection);
}
if ((upper_right_index >= 0) && (std::floor(upper_right_index / width) == upper_row_index)) {
m_connector(index, upper_right_index, output_adjacency_collection);
}
if ((lower_left_index < (int) output_adjacency_collection.size()) && (std::floor(lower_left_index / width) == lower_row_index)) {
m_connector(index, lower_left_index, output_adjacency_collection);
}
if ((lower_right_index < (int) output_adjacency_collection.size()) && (std::floor(lower_right_index / width) == lower_row_index)) {
m_connector(index, lower_right_index, output_adjacency_collection);
}
}
}
/**
*
* @brief Creates rectangle grid structure in line with specify type of connections.
* @details It throws exception if non-grid structures is specify as a grid type.
*
* @param[in] p_grid: type of grid structure (for example, four grid or eight grid).
* @param[in] p_width: width of created grid structure that is defined by amount of nodes in a column.
* @param[in] p_height: height of created grid structure that is defined by amount of nodes in a row.
* @param[out] p_output_adjacency_collection: adjacency collection whose connections should be updated.
*
*/
virtual void create_grid_structure(const connection_t p_grid, const size_t p_width, const size_t p_height, TypeCollection & p_output_adjacency_collection) {
switch (p_grid) {
case connection_t::CONNECTION_GRID_FOUR:
create_grid_four_connections(p_width, p_height, p_output_adjacency_collection);
break;
case connection_t::CONNECTION_GRID_EIGHT:
create_grid_eight_connections(p_width, p_height, p_output_adjacency_collection);
break;
default:
throw std::runtime_error("Grid structure of connection is expected");
}
}
};
/**
*
* @brief Class for creating pre-defined most popular structures by establishing connections
* between nodes in weight adjacency collections.
*
*/
template <typename TypeCollection>
class adjacency_weight_connector : public adjacency_connector<TypeCollection> {
public:
using adjacency_weight_initializer = std::function<double()>;
protected:
adjacency_weight_initializer m_initializer;
public:
/**
*
* @brief Default constructor of connector where weight initializer is not specified.
* @details In this case adjacency collection defines default value of weight by itself in
* method 'set_connection()'.
*
*/
adjacency_weight_connector() { }
/**
*
* @brief Constructor of connector with weight initializer.
* @details Initializer is used during creating connections between nodes for assigning weight
* for each connection. Generally the initializer represents pointer to some function
* that is used to assign weight, for example, if constant value is required then
* the function should return always the same value, otherwise some logic can be
* implemented.
*
* @param[in] initializer: initializer that is used for setting value of each weight connection.
*
*/
explicit adjacency_weight_connector(const adjacency_weight_initializer & initializer) {
if (initializer) {
m_initializer = initializer; /* [this](const size_t index1, const size_t index2, TypeCollection & collection) {
collection.set_connection_weight(index1, index2, initializer());
}; */
}
}
};
}
}
|