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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
|
"""!
@brief Neural and oscillatory network module. Consists of models of bio-inspired networks.
@authors Andrei Novikov (pyclustering@yandex.ru)
@date 2014-2020
@copyright BSD-3-Clause
"""
import math;
from enum import IntEnum;
class initial_type(IntEnum):
"""!
@brief Enumerator of types of oscillator output initialization.
"""
## Output of oscillators are random in line with gaussian distribution.
RANDOM_GAUSSIAN = 0;
## Output of oscillators are equidistant from each other (uniformly distributed, not randomly).
EQUIPARTITION = 1;
class solve_type(IntEnum):
"""!
@brief Enumerator of solver types that are used for network simulation.
"""
## Forward Euler first-order method.
FAST = 0; # Usual calculation: x(k + 1) = x(k) + f(x(k)).
## Classic fourth-order Runge-Kutta method (fixed step).
RK4 = 1;
## Runge-Kutta-Fehlberg method with order 4 and 5 (float step)."
RKF45 = 2;
class conn_type(IntEnum):
"""!
@brief Enumerator of connection types between oscillators.
"""
## No connection between oscillators.
NONE = 0;
## All oscillators have connection with each other.
ALL_TO_ALL = 1;
## Connections between oscillators represent grid where one oscillator can be connected with four neighbor oscillators: right, upper, left, lower.
GRID_FOUR = 2;
## Connections between oscillators represent grid where one oscillator can be connected with eight neighbor oscillators: right, right-upper, upper, upper-left, left, left-lower, lower, lower-right.
GRID_EIGHT = 3;
## Connections between oscillators represent bidirectional list.
LIST_BIDIR = 4;
## Connections are defined by user or by network during simulation.
DYNAMIC = 5;
class conn_represent(IntEnum):
"""!
@brief Enumerator of internal network connection representation between oscillators.
"""
## Each oscillator has list of his neighbors.
LIST = 0;
## Connections are represented my matrix connection NxN, where N is number of oscillators.
MATRIX = 1;
class network:
"""!
@brief Common network description that consists of information about oscillators and connection between them.
"""
_num_osc = 0;
_osc_conn = None;
_conn_represent = None;
__conn_type = None;
__height = 0;
__width = 0;
@property
def height(self):
"""!
@brief Height of the network grid (that is defined by amout of oscillators in each column), this value is zero in case of non-grid structure.
@note This property returns valid value only for network with grid structure.
"""
return self.__height;
@property
def width(self):
"""!
@brief Width of the network grid, this value is zero in case of non-grid structure.
@note This property returns valid value only for network with grid structure.
"""
return self.__width;
@property
def structure(self):
"""!
@brief Type of network structure that is used for connecting oscillators.
"""
return self.__conn_type;
def __init__(self, num_osc, type_conn = conn_type.ALL_TO_ALL, conn_repr = conn_represent.MATRIX, height = None, width = None):
"""!
@brief Constructor of the network.
@param[in] num_osc (uint): Number of oscillators in the network that defines size of the network.
@param[in] type_conn (conn_type): Type of connections that are used in the network between oscillators.
@param[in] conn_repr (conn_represent): Type of representation of connections.
@param[in] height (uint): Number of oscillators in column of the network, this argument is used
only for network with grid structure (GRID_FOUR, GRID_EIGHT), for other types this argument is ignored.
@param[in] width (uint): Number of oscillotors in row of the network, this argument is used only
for network with grid structure (GRID_FOUR, GRID_EIGHT), for other types this argument is ignored.
"""
self._num_osc = num_osc;
self._conn_represent = conn_repr;
self.__conn_type = type_conn;
if (conn_repr is None):
self._conn_represent = conn_represent.MATRIX;
if ( (type_conn == conn_type.GRID_EIGHT) or (type_conn == conn_type.GRID_FOUR) ):
if ( (height is not None) and (width is not None) ):
self.__height = height;
self.__width = width;
else:
side_size = self._num_osc ** (0.5);
if (side_size - math.floor(side_size) > 0):
raise NameError("Invalid number of oscillators '" + str(num_osc) + "' in the network in case of grid structure (root square should be extractable for the number of oscillators).");
self.__height = int(side_size);
self.__width = self.__height;
if (self.__height * self.__width != self._num_osc):
raise NameError('Width (' + str(self.__width) + ') x Height (' + str(self.__height) + ') must be equal to Size (' + str(self._num_osc) + ') in case of grid structure');
self._create_structure(type_conn);
def __len__(self):
"""!
@brief Returns size of the network that is defined by amount of oscillators.
"""
return self._num_osc;
def __create_connection(self, index1, index2):
if (self._conn_represent == conn_represent.MATRIX):
self._osc_conn[index1][index2] = True;
else:
self._osc_conn[index1].append(index2);
def __create_all_to_all_connections(self):
"""!
@brief Creates connections between all oscillators.
"""
if (self._conn_represent == conn_represent.MATRIX):
for index in range(0, self._num_osc, 1):
self._osc_conn.append([True] * self._num_osc);
self._osc_conn[index][index] = False;
elif (self._conn_represent == conn_represent.LIST):
for index in range(0, self._num_osc, 1):
self._osc_conn.append([neigh for neigh in range(0, self._num_osc, 1) if index != neigh]);
def __create_grid_four_connections(self):
"""!
@brief Creates network with connections that make up four grid structure.
@details Each oscillator may be connected with four neighbors in line with 'grid' structure: right, upper, left, lower.
"""
side_size = self.__width;
if (self._conn_represent == conn_represent.MATRIX):
self._osc_conn = [[0] * self._num_osc for index in range(0, self._num_osc, 1)];
elif (self._conn_represent == conn_represent.LIST):
self._osc_conn = [[] for index in range(0, self._num_osc, 1)];
else:
raise NameError("Unknown type of representation of connections");
for index in range(0, self._num_osc, 1):
upper_index = index - side_size;
lower_index = index + side_size;
left_index = index - 1;
right_index = index + 1;
node_row_index = math.ceil(index / side_size);
if (upper_index >= 0):
self.__create_connection(index, upper_index);
if (lower_index < self._num_osc):
self.__create_connection(index, lower_index);
if ( (left_index >= 0) and (math.ceil(left_index / side_size) == node_row_index) ):
self.__create_connection(index, left_index);
if ( (right_index < self._num_osc) and (math.ceil(right_index / side_size) == node_row_index) ):
self.__create_connection(index, right_index);
def __create_grid_eight_connections(self):
"""!
@brief Creates network with connections that make up eight grid structure.
@details Each oscillator may be connected with eight neighbors in line with grid structure: right, right-upper, upper, upper-left, left, left-lower, lower, lower-right.
"""
self.__create_grid_four_connections(); # create connection with right, upper, left, lower.
side_size = self.__width;
for index in range(0, self._num_osc, 1):
upper_left_index = index - side_size - 1;
upper_right_index = index - side_size + 1;
lower_left_index = index + side_size - 1;
lower_right_index = index + side_size + 1;
node_row_index = math.floor(index / side_size);
upper_row_index = node_row_index - 1;
lower_row_index = node_row_index + 1;
if ( (upper_left_index >= 0) and (math.floor(upper_left_index / side_size) == upper_row_index) ):
self.__create_connection(index, upper_left_index);
if ( (upper_right_index >= 0) and (math.floor(upper_right_index / side_size) == upper_row_index) ):
self.__create_connection(index, upper_right_index);
if ( (lower_left_index < self._num_osc) and (math.floor(lower_left_index / side_size) == lower_row_index) ):
self.__create_connection(index, lower_left_index);
if ( (lower_right_index < self._num_osc) and (math.floor(lower_right_index / side_size) == lower_row_index) ):
self.__create_connection(index, lower_right_index);
def __create_list_bidir_connections(self):
"""!
@brief Creates network as bidirectional list.
@details Each oscillator may be conneted with two neighbors in line with classical list structure: right, left.
"""
if (self._conn_represent == conn_represent.MATRIX):
for index in range(0, self._num_osc, 1):
self._osc_conn.append([0] * self._num_osc);
self._osc_conn[index][index] = False;
if (index > 0):
self._osc_conn[index][index - 1] = True;
if (index < (self._num_osc - 1)):
self._osc_conn[index][index + 1] = True;
elif (self._conn_represent == conn_represent.LIST):
for index in range(self._num_osc):
self._osc_conn.append([]);
if (index > 0):
self._osc_conn[index].append(index - 1);
if (index < (self._num_osc - 1)):
self._osc_conn[index].append(index + 1);
def __create_none_connections(self):
"""!
@brief Creates network without connections.
"""
if (self._conn_represent == conn_represent.MATRIX):
for _ in range(0, self._num_osc, 1):
self._osc_conn.append([False] * self._num_osc);
elif (self._conn_represent == conn_represent.LIST):
self._osc_conn = [[] for _ in range(0, self._num_osc, 1)];
def __create_dynamic_connection(self):
"""!
@brief Prepare storage for dynamic connections.
"""
if (self._conn_represent == conn_represent.MATRIX):
for _ in range(0, self._num_osc, 1):
self._osc_conn.append([False] * self._num_osc);
elif (self._conn_represent == conn_represent.LIST):
self._osc_conn = [[] for _ in range(0, self._num_osc, 1)];
def _create_structure(self, type_conn = conn_type.ALL_TO_ALL):
"""!
@brief Creates connection in line with representation of matrix connections [NunOsc x NumOsc].
@param[in] type_conn (conn_type): Connection type (all-to-all, bidirectional list, grid structure, etc.) that is used by the network.
"""
self._osc_conn = list();
if (type_conn == conn_type.NONE):
self.__create_none_connections();
elif (type_conn == conn_type.ALL_TO_ALL):
self.__create_all_to_all_connections();
elif (type_conn == conn_type.GRID_FOUR):
self.__create_grid_four_connections();
elif (type_conn == conn_type.GRID_EIGHT):
self.__create_grid_eight_connections();
elif (type_conn == conn_type.LIST_BIDIR):
self.__create_list_bidir_connections();
elif (type_conn == conn_type.DYNAMIC):
self.__create_dynamic_connection();
else:
raise NameError('The unknown type of connections');
def has_connection(self, i, j):
"""!
@brief Returns True if there is connection between i and j oscillators and False - if connection doesn't exist.
@param[in] i (uint): index of an oscillator in the network.
@param[in] j (uint): index of an oscillator in the network.
"""
if (self._conn_represent == conn_represent.MATRIX):
return (self._osc_conn[i][j]);
elif (self._conn_represent == conn_represent.LIST):
for neigh_index in range(0, len(self._osc_conn[i]), 1):
if (self._osc_conn[i][neigh_index] == j):
return True;
return False;
else:
raise NameError("Unknown type of representation of coupling");
def set_connection(self, i, j):
"""!
@brief Couples two specified oscillators in the network with dynamic connections.
@param[in] i (uint): index of an oscillator that should be coupled with oscillator 'j' in the network.
@param[in] j (uint): index of an oscillator that should be coupled with oscillator 'i' in the network.
@note This method can be used only in case of DYNAMIC connections, otherwise it throws expection.
"""
if (self.structure != conn_type.DYNAMIC):
raise NameError("Connection between oscillators can be changed only in case of dynamic type.");
if (self._conn_represent == conn_represent.MATRIX):
self._osc_conn[i][j] = True;
self._osc_conn[j][i] = True;
else:
self._osc_conn[i].append(j);
self._osc_conn[j].append(i);
def get_neighbors(self, index):
"""!
@brief Finds neighbors of the oscillator with specified index.
@param[in] index (uint): index of oscillator for which neighbors should be found in the network.
@return (list) Indexes of neighbors of the specified oscillator.
"""
if (self._conn_represent == conn_represent.LIST):
return self._osc_conn[index]; # connections are represented by list.
elif (self._conn_represent == conn_represent.MATRIX):
return [neigh_index for neigh_index in range(self._num_osc) if self._osc_conn[index][neigh_index] == True];
else:
raise NameError("Unknown type of representation of connections");
|