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.

In 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 to be used 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.topology.mixins.DimensionsMixin, xentica.core.mixins.BscaDetectorMixin

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 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 C code calculating cell’s index. No assignment, only a valid expression needed.
index_to_coord(idx, bsca)

Obtain 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 NumPy array of indices.
  • bscaxentica.core.CellularAutomaton instance, to access field 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 cell’s index.

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

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

A string with C code, doing all necessary to process index variable and store coordinates to variables with 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 C code testing coordinate 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)

Implement cell’s index obtaining by coordinates in C.

See Lattice.coord_to_index_code() for details.

index_to_coord(idx, bsca)

Implement coordinates obtaining by cell’s index in Python.

See Lattice.index_to_coord() for details.

index_to_coord_code(index_name, coord_prefix)

Implement coordinates obtaining by cell’s index in C.

See Lattice.index_to_coord_code() for details.

is_off_board_code(coord_prefix)

Implement off board cell obtaining in C.

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 to be used 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.topology.mixins.DimensionsMixin

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 C code to obtain neighbor coordinates by its index.

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

Parameters:
  • index – Neighbor index, a non-negative integer less than 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 C code doing all necessary to get neighbor state from RAM. No assignment, only a valid expression needed.

neighbor_state(neighbor_index, state_index, coord_prefix)

Generate C code to obtain neighbor state by its index.

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

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

A string with C code doing all necessary to process neighbors’s coordinates and store them to neighbor 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

Base class for neighborhoods on 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)

Implement neighbor coordinates obtaining by its index, in C.

See Neighborhood.neighbor_coords() for details.

neighbor_state(neighbor_index, state_index, coord_prefix)

Implement state obtaining by neighbor/state index, in C.

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 borders.

All classes there are intended to be used 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.topology.mixins.DimensionsMixin

Base class for all types of borders.

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

class xentica.core.topology.border.WrappedBorder

Bases: xentica.core.topology.border.Border

Base class for borders wrapping 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 cell’s coordinates.
class xentica.core.topology.border.GeneratedBorder

Bases: xentica.core.topology.border.Border

Base class for borders generating 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 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)

Impement 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]

Mixins (xentica.core.topology.mixins)

The collection of mixins to be used in core classes.

Would be interesting only if you are planning to hack into Xentica core functionality.

class xentica.core.topology.mixins.DimensionsMixin

Bases: object

The base functionality for classes, operating on a number of dimensions.

Adds dimensions property to a class, and checks it automatically over a list of allowed dimensions.

allowed_dimension(num_dim)

Test if particular dimensionality is allowed.

Parameters:num_dim – Numbers of dimensions to test
Returns:Boolean value, either dimensionality is allowed or not.
dimensions

Get a number of dimensions.

supported_dimensions = []

A list of integers, containing supported dimensionality. You must set it manually for every class using DimensionsMixin.