cpg

CPG brains for modular robots.

Package Contents

Classes

BrainCpgInstance

CPG network brain.

BrainCpgNetworkNeighbor

A CPG brain with active hinges that are connected if they are within 2 jumps in the modular robot tree structure.

BrainCpgNetworkNeighborRandom

A cpg brain with random weights between neurons.

BrainCpgNetworkStatic

A CPG (central pattern generator) brain with CPGs and connections defined by the user.

CpgNetworkStructure

Describes the structure of a CPG network.

Functions

active_hinges_to_cpg_network_structure_neighbor(...)

Create the structure of a CPG network based on a list of active hinges.

class BrainCpgInstance(initial_state: numpy.typing.NDArray[numpy.float_], weight_matrix: numpy.typing.NDArray[numpy.float_], output_mapping: list[tuple[int, modular_robot.body.base.ActiveHinge]])

Bases: modular_robot.brain._brain_instance.BrainInstance

Inheritance diagram of modular_robot.brain.cpg.BrainCpgInstance

CPG network brain.

A state array that is integrated over time following the differential equation X’=WX. W is a weight matrix that is multiplied by the state array. The outputs of the controller are defined by the outputs, a list of indices for the state array.

control(dt: float, sensor_state: modular_robot.sensor_state.ModularRobotSensorState, control_interface: modular_robot._modular_robot_control_interface.ModularRobotControlInterface) None

Control the modular robot.

Sets the active hinge targets to the values in the state array as defined by the mapping provided in the constructor.

Parameters:
  • dt – Elapsed seconds since last call to this function.

  • sensor_state – Interface for reading the current sensor state.

  • control_interface – Interface for controlling the robot.

class BrainCpgNetworkNeighbor(body: modular_robot.body.base.Body)

Bases: modular_robot.brain._brain.Brain

Inheritance diagram of modular_robot.brain.cpg.BrainCpgNetworkNeighbor

A CPG brain with active hinges that are connected if they are within 2 jumps in the modular robot tree structure.

That means, NOT grid coordinates, but tree distance.

make_instance() modular_robot.brain._brain_instance.BrainInstance

Create an instance of this brain.

Returns:

The created instance.

class BrainCpgNetworkNeighborRandom(body: modular_robot.body.base.Body, rng: numpy.random.Generator)

Bases: modular_robot.brain.cpg._brain_cpg_network_neighbor.BrainCpgNetworkNeighbor

Inheritance diagram of modular_robot.brain.cpg.BrainCpgNetworkNeighborRandom

A cpg brain with random weights between neurons.

The weights are randomly generated when this object is created, so they will be the same for every controller instance.

class BrainCpgNetworkStatic(initial_state: numpy.typing.NDArray[numpy.float_], weight_matrix: numpy.typing.NDArray[numpy.float_], output_mapping: list[tuple[int, modular_robot.body.base.ActiveHinge]])

Bases: modular_robot.brain._brain.Brain

Inheritance diagram of modular_robot.brain.cpg.BrainCpgNetworkStatic

A CPG (central pattern generator) brain with CPGs and connections defined by the user.

A state vector is integrated over time using a weight matrix which multiplication with the state vector sum defines the derivative of the state vector. I.e X’ = WX

The first num_output_neurons in the state vector are the outputs for the controller created by this brain.

classmethod uniform_from_params(params: numpy.typing.NDArray[numpy.float_], cpg_network_structure: modular_robot.brain.cpg._cpg_network_structure.CpgNetworkStructure, initial_state_uniform: float, output_mapping: list[tuple[int, modular_robot.body.base.ActiveHinge]]) BrainCpgNetworkStatic

Create and initialize an instance of this brain from the provided parameters, assuming uniform initial state.

Parameters:
  • params – Parameters for the weight matrix to be created.

  • cpg_network_structure – The cpg network structure.

  • initial_state_uniform – Initial state to use for all neurons.

  • output_mapping – Marks neurons as controller outputs and map them to the correct active hinge.

Returns:

The created brain.

make_instance() modular_robot.brain._brain_instance.BrainInstance

Create an instance of this brain.

Returns:

The created instance.

class CpgNetworkStructure(cpgs: list[Cpg], connections: set[CpgPair])

Describes the structure of a CPG network.

Can generate parameters for a CPG network, such as the initial state.

property num_connections: int

Get the number of connections in the structure.

Returns:

The number of connections.

property num_states: int

Get the number of states in a cpg network of this structure.

This would be twice the number of CPGs.

Returns:

The number of states.

property num_cpgs: int

Get the number of CPGs in the structure.

Returns:

The number of CPGs.

property output_indices: list[int]

Get the index in the state array for each cpg, matching the order the CPGs were provided in.

Returns:

The indices.

cpgs: list[Cpg]
connections: set[CpgPair]
static make_cpgs(num_cpgs: int) list[Cpg]

Create a list of CPGs.

Parameters:

num_cpgs – The number of CPGs to create.

Returns:

The created list of CPGs.

make_connection_weights_matrix(internal_connection_weights: dict[Cpg, float], external_connection_weights: dict[CpgPair, float]) numpy.typing.NDArray[numpy.float_]

Create a weight matrix from internal and external weights.

Parameters:
  • internal_connection_weights – The internal weights.

  • external_connection_weights – The external weights.

Returns:

The created matrix.

make_connection_weights_matrix_from_params(params: list[float]) numpy.typing.NDArray[numpy.float_]

Create a connection weights matrix from a list if connections.

Parameters:

params – The connections to create the matrix from.

Returns:

The created matrix.

make_uniform_state(value: float) numpy.typing.NDArray[numpy.float_]

Make a state array by repeating the same value.

Will match the required number of states in this structure.

Parameters:

value – The value to use for all states

Returns:

The array of states.

active_hinges_to_cpg_network_structure_neighbor(active_hinges: list[modular_robot.body.base.ActiveHinge]) tuple[modular_robot.brain.cpg._cpg_network_structure.CpgNetworkStructure, list[tuple[int, modular_robot.body.base.ActiveHinge]]]

Create the structure of a CPG network based on a list of active hinges.

The order of the active hinges matches the order of the CPGs. I.e. every active hinges has a corresponding CPG, and these are stored in the order the hinges are provided in.

Parameters:

active_hinges – The active hinges to base the structure on.

Returns:

The created structure and a mapping between state indices and active hinges.