Skip to content

Functions

src.utils.functions

View source code

Game helper functions: - genetic crossover utility - game object factories - ball spin/skew on paddle hit - math helpers

ActivationsMismatchError

Bases: SyntaxError

Custom exception class for activations mismatch error in neural network layers.

activation1 = activation1 instance-attribute

activation2 = activation2 instance-attribute

layer_index = layer_index instance-attribute

__init__(layer_index, activation1, activation2)

crossover_neuron_masks(network, cut_points: tuple[int, int]) -> list[list[bool]]

Map a half-open crossover interval onto each layer's output neurons.

crossover_parameter_masks(network, cut_points: tuple[int, int]) -> tuple[list[list[list[bool]]], list[list[bool]]]

Map selected neurons to the incoming-weight and bias parameters they inherit.

two_point_crossover(player_1, player_2, *, cut_points: tuple[int, int] | None = None)

Crossover contiguous neurons, including each selected neuron's outgoing weights in the following layer.

skew_ball_direction(ball, paddle, is_cpu=False) -> None

Skew/tilt the ball direction based on hit position on paddle.

The farther from the paddle center, the stronger the skew.

Parameters:

Name Type Description Default
ball

Ball object.

required
paddle

Paddle object.

required
is_cpu

If True, invert the skew (for the top paddle).

False

squash(value, factor) -> float

Squash 'value' using tanh(value/factor) and scale it back by 'factor'.

timeit(func)

A decorator to time a function's execution and print the duration.

create_paddle(screen_width, screen_height, color, is_cpu=False, width=80) -> Paddle

Create a paddle positioned at top (CPU) or bottom (Player).

Parameters:

Name Type Description Default
screen_width

Width of the screen.

required
screen_height

Height of the screen.

required
color

RGB tuple.

required
is_cpu

If True, place at top; else bottom.

False

create_ball() -> Ball

Create a new Ball object with random velocity and color.