The Topology (xentica.core.topology)

This package helps you build the topology for CA models.

All xentica.core.CellularAutomaton subclasses must have Topology class declared inside. This class describes:

  • dimensions: the number of dimensions your CA model operates on.
  • lattice: the type of lattice of your CA board. Built-in lattice types are available in xentica.core.topology.lattice module.
  • neighborhood: the type of neighborhood for a single cell. Built-in neighborhood types are available in xentica.core.topology.neighborhood module.
  • border: the type of border effect, e.g. how to process off-board cells. Built-in border types are available in xentica.core.topology.border module.

For example, you can declare the topology for a 2-dimensional orthogonal lattice with Moore neighborhood wrapped to a 3-torus, as follows:

class Topology:

    dimensions = 2
    lattice = core.OrthogonalLattice()
    neighborhood = core.MooreNeighborhood()
    border = core.TorusBorder()

Lattice (xentica.core.topology.lattice)

The collection of classes describing different lattice topologies.

All classes there are intended for use inside Topology for lattice class variable definition. They are also available via xentica.core shortcut. The example:

from xentica.core import CellularAutomaton, OrthogonalLattice

class MyCA(CellularAutomaton):
    class Topology:
        lattice = OrthogonalLattice()
        # ...
    # ...
class xentica.core.topology.lattice.Lattice

Bases: xentica.core.mixins.DimensionsMixin, xentica.core.mixins.BscaDetectorMixin

The base class for all lattices.

For correct behavior, lattice classes should be inherited from this class. You should also implement the following functions:

See the detailed description below.

coord_to_index_code(coord_prefix)

Generate C code for obtaining the cell’s index by coordinates.

This is an abstract method, you must implement it in Lattice subclasses.

Parameters:coord_prefix – The prefix for variables, containing coordinates.
Returns:A string with the C code calculating cell’s index. No assignment, only a valid expression needed.
index_to_coord(idx, bsca)

Obtain the cell’s coordinates by its index, in pure Python.

This is an abstract method, you must implement it in Lattice subclasses.

Parameters:
  • idx – Cell’s index, a positive integer, or a NumPy array of indices.
  • bscaxentica.core.CellularAutomaton instance, to access the field’s size and number of dimensions.
Returns:

Tuple of integer coordinates, or NumPy arrays of coords per each axis.

index_to_coord_code(index_name, coord_prefix)

Generate C code to obtain coordinates by the cell’s index.

This is an abstract method, you must implement it in Lattice subclasses.

Parameters:
  • index_name – The name of a variable containing the cell’s index.
  • coord_prefix – The prefix for resulting variables, containing coordinates.
Returns:

A string with the C code, doing all necessary to process the index variable and store coordinates to variables with the given prefix.

is_off_board_code(coord_prefix)

Generate C code to test if the cell’s coordinates are off board.

This is an abstract method, you must implement it in Lattice subclasses.

Parameters:coord_prefix – The prefix for variables, containing coordinates.
Returns:A string with the C code testing coordinates’ variables. No assignment, only a valid expression with boolean result needed.
width_prefix = '_w'

The prefix to be used in C code for field size constants.

class xentica.core.topology.lattice.OrthogonalLattice

Bases: xentica.core.topology.lattice.Lattice

N-dimensional orthogonal lattice.

Points are all possible positive integer coordinates.

coord_to_index_code(coord_prefix)

Generate C code for obtaining the cell’s index by coordinates.

See Lattice.coord_to_index_code() for details.

index_to_coord(idx, bsca)

Obtain the cell’s coordinates by its index, in pure Python.

See Lattice.index_to_coord() for details.

index_to_coord_code(index_name, coord_prefix)

Generate C code for obtaining the cell’s index by coordinates.

See Lattice.index_to_coord_code() for details.

is_off_board_code(coord_prefix)

Generate C code to test if the cell’s coordinates are off board.

See Lattice.is_off_board_code() for details.

supported_dimensions = [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]

Overridden value for supported dimensions.

Neighborhood (xentica.core.topology.neighborhood)

The collection of classes describing different neighborhood topologies.

All classes there are intended for use inside Topology for neighborhood class variable definition. They are also available via xentica.core shortcut. The example:

from xentica.core import CellularAutomaton, MooreNeighborhood

class MyCA(CellularAutomaton):
    class Topology:
        neighborhood = MooreNeighborhood()
        # ...
    # ...
class xentica.core.topology.neighborhood.Neighborhood

Bases: xentica.core.mixins.DimensionsMixin

The base class for all neighborhood topologies.

For correct behavior, neighborhood classes should be inherited from this class. You should also implement the following functions:

See the detailed description below.

neighbor_coords(index, coord_prefix, neighbor_prefix)

Generate the C code to obtain neighbor coordinates by its index.

This is an abstract method, you must implement it in Neighborhood subclasses.

Parameters:
  • index – Neighbor’s index, a non-negative integer less than the number of neighbors.
  • coord_prefix – The prefix for variables containing main cell’s coordinates.
  • neighbor_prefix – The prefix for resulting variables containing neighbor coordinates.
Returns:

A string with the C code doing all necessary to get neighbor’s state from the RAM. No assignment, only a valid expression needed.

neighbor_state(neighbor_index, state_index, coord_prefix)

Generate the C code to obtain a neighbor’s state by its index.

This is an abstract method, you must implement it in Neighborhood subclasses.

Parameters:
  • neighbor_index – Neighbor’s index, a non-negative integer less than the number of neighbors.
  • state_index – State’s index, a non-negative integer less than the number of neighbors for buffered states or -1 for main state.
  • coord_prefix – The prefix for variables containing neighbor coordinates.
Returns:

A string with the C code doing all necessary to process neighbor’s coordinates and store them to neighbor’s coordinates variables.

num_neighbors = None

Number of neighbors, you must re-define it in sub-classes.

topology = None

A reference to Topology holder class, will be set in BSCA metaclass.

class xentica.core.topology.neighborhood.OrthogonalNeighborhood

Bases: xentica.core.topology.neighborhood.Neighborhood

The base class for neighborhoods on an orthogonal lattice.

It is implementing all necessary Neighborhood abstract methods, the only thing you should override is dimensions() setter. In dimensions(), you should correctly set num_neighbors and _neighbor_deltas attributes.

neighbor_coords(index, coord_prefix, neighbor_prefix)

Generate the C code to obtain neighbor coordinates by its index.

See Neighborhood.neighbor_coords() for details.

neighbor_state(neighbor_index, state_index, coord_prefix)

Generate the C code to obtain a neighbor’s state by its index.

See Neighborhood.neighbor_coords() for details.

supported_dimensions = [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]

Any number of dimentions is supported, 100 is just to limit your hyperspatial hunger.

class xentica.core.topology.neighborhood.MooreNeighborhood

Bases: xentica.core.topology.neighborhood.OrthogonalNeighborhood

N-dimensional Moore neighborhood implementation.

The neighbors are all cells, sharing at least one vertex.

dimensions

Get a number of dimensions.

class xentica.core.topology.neighborhood.VonNeumannNeighborhood

Bases: xentica.core.topology.neighborhood.OrthogonalNeighborhood

N-dimensional Von Neumann neighborhood implementation.

The neighbors are adjacent cells in all possible orthogonal directions.

dimensions

Get a number of dimensions.

Border (xentica.core.topology.border)

The collection of classes describing different types of field’s borders.

All classes there are intended for use inside Topology for border class variable definition. They are also available via xentica.core shortcut. The example:

from xentica.core import CellularAutomaton, TorusBorder

class MyCA(CellularAutomaton):
    class Topology:
        border = TorusBorder()
        # ...
    # ...
class xentica.core.topology.border.Border

Bases: xentica.core.mixins.DimensionsMixin

The base class for all types of borders.

You should not inherit your borders directly from this class, use either WrappedBorder or GeneratedBorder base subclasses instead.

class xentica.core.topology.border.WrappedBorder

Bases: xentica.core.topology.border.Border

The base class for borders that wraps the field into different manifolds.

For correct behavior, you should implement wrap_coords() method.

See the detailed description below.

wrap_coords(coord_prefix)

Generate C code to translate off-board coordinates to on-board ones.

This is an abstract method, you must implement it in WrappedBorder subclasses.

Parameters:coord_prefix – The prefix for variables containing the cell’s coordinates.
class xentica.core.topology.border.GeneratedBorder

Bases: xentica.core.topology.border.Border

The base class for borders that generates states of the off-board cells.

For correct behavior, you should implement off_board_state() method.

See the detailed description below.

off_board_state(coord_prefix)

Generate C code to obtain off-board cell’s state.

This is an abstract method, you must implement it in GeneratedBorder subclasses.

Parameters:coord_prefix – The prefix for variables containing the cell’s coordinates.
class xentica.core.topology.border.TorusBorder

Bases: xentica.core.topology.border.WrappedBorder

Wraps the entire field into N-torus manifold.

This is the most common type of border, allowing you to generate seamless tiles for wallpapers.

supported_dimensions = [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]

Any number of dimentions is supported, 100 is just to limit your hyperspatial hunger.

wrap_coords(coord_prefix)

Implement coordinates wrapping to torus.

See WrappedBorder.wrap_coords() for details.

class xentica.core.topology.border.StaticBorder(value=0)

Bases: xentica.core.topology.border.GeneratedBorder

Generates a static value for every off-board cell.

This is acting like your field is surrounded by cells with the same pre-defined state.

The default is just an empty (zero) state.

off_board_state(coord_prefix)

Impement off-board cells’ values obtaining.

See GeneratedBorder.off_board_state() for details.

supported_dimensions = [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]