CircuitPopulation.py

Circuit Population

This class was reviewed, and should be fully documented at a basic level.

class CircuitPopulation.CircuitInfo(name, fitness)
_asdict()

Return a new dict which maps field names to their values.

classmethod _make(iterable)

Make a new CircuitInfo object from a sequence or iterable

_replace(**kwds)

Return a new CircuitInfo object replacing specified fields with new values

fitness

Alias for field number 1

name

Alias for field number 0

class CircuitPopulation.CircuitPathInfo(path, fitness)
_asdict()

Return a new dict which maps field names to their values.

classmethod _make(iterable)

Make a new CircuitPathInfo object from a sequence or iterable

_replace(**kwds)

Return a new CircuitPathInfo object replacing specified fields with new values

fitness

Alias for field number 1

path

Alias for field number 0

class CircuitPopulation.CircuitPopulation(mcu, config: Config, logger)[source]

Manages the initializing the population of circuits, updating and recording information about the population throughout evolution, and deciding when to stop evolution

__arr_eq(ar1, ar2)

Returns True if the arrays or equal or False otherwise Compares each element of ar1 and ar2

Parameters:
  • ar1 (list) – an array

  • ar2 (list) – an array

Returns:

True if the arrays are equivalent in content and order, False otherwise.

Return type:

bool

__files_eq(fp1, fp2)

Returns true if the files are equal (have the same content)

Parameters:
  • fp1 (str) – Path to file 1

  • fp2 (str) – Path to file 2

Returns:

True if both files contain the same content, False otherwise.

Return type:

bool

__generate_map()

Generates the elite map for this generation based on variance.

Returns:

A 2D array of circuits catagorized based off of shared characteristics

Return type:

list(list(Circuit))

__generate_pulse_map()

Generates the elite map for this generation based on pulse count.

Returns:

A 2D array of circuits catagorized based off of shared characteristics

Return type:

Circuit[][]

__generate_sine_funcs()

Builds a list of randomly generated sine functions used in the simulation mode.

Returns:

List of randomly generated sine functions

Return type:

list[functions]

__group(n, fillvalue=None)

Collect data into fixed-length chunks or blocks #grouper(‘ABCDEFG’, 3, ‘x’) –> ABC DEF Gxx Taken from python recipes.

__init__(mcu, config: Config, logger)[source]

Generates the initial population of circuits with the following arguments

Parameters:
  • mcu (Microcontroller) – Object containing an instance of Microcontroller class

  • config (Config) – Object containing an instance of Config class

  • logger (Logger) – Object containing an instance of Logger class

__log_error(level, *error)

Emit an error-level log. This function is fulfilled through the logger.

Parameters:
  • level (int) – The level of importance of the logged information (lower level = higher importance)

  • error (tuple[string]) – The message being logged

__log_event(level, *event)

Emit an event-level log. This function is fulfilled through the logger.

Parameters:
  • level (int) – The level of importance of the logged information (lower level = higher importance)

  • event (tuple[string]) – The message being logged

__log_info(level, *info)

Emit an info-level log. This function is fulfilled through the logger.

Parameters:
  • level (int) – The level of importance of the logged information (lower level = higher importance)

  • info (tuple[string]) – The message being logged

__log_warning(level, *warning)

Emit a warning-level log. This function is fulfilled through the logger.

Parameters:
  • level (int) – The level of importance of the logged information (lower level = higher importance)

  • warning (tuple[string]) – The message being logged

__next_epoch()

Moves to the next generation/epoch Currently, only needs to increase the generation by 1 All other generation-specific behavior will be derived from this value

__output_map_file(elite_map)

Writes the map to a file (workspace/maplivedata.log)

Parameters:

elite_map (Circuit[][]) – 2D array of circuits that fell into these groupings depending on their characteristics.

__randomize_until_pulses()

Randomizes population until minimum number of pulses is found. Called by populate(self) Should only be used with pulse count fitness functions

__randomize_until_variance()

Randomizes population until minimum variance fitness is found. called by populate(self) Should only be used with variance maximization fitness function

__randomize_until_voltage()

Randomizes population until a mean voltage is found near the desired value called by populate(self) Should only be used with variance maximization fitness function

__run_classic_tournament()

Selection Algorithm that randomly pairs together circuits, compares their fitness, and preforms crossover on and mutates the “loser”

__run_fitness_proportional_selection()

Selection algorithm that compares every circuit in the population to a random elite (chosen proportionally based on each elite’s fitness). If circuit has a lower fitness, crossover or mutate the circuit

__run_fractional_elite_tournament()

Selection algorithm that compares every circuit in the population to a random elite. If circuit has a lower fitness, crossover or mutate the circuit

__run_map_elites_selection()

Selection Algorithm that is an alternate version of the map elites algorithm from another paper. This version of map elites will protect the highest-fitness individual in each “square” We’re going to have slightly granular squares to make sure that circuits have room to spread out early to hopefully promote diversity Group size length of 50 means we’ll have 21x21 groups

__run_rank_proportional_selection()

Selection algorithm that compares every circuit in the population to a random elite (chosen proportionally based on each elite’s rank). If circuit has a lower fitness, crossover or mutate the circuit

__run_single_elite_tournament()

Selection Algorithm that mutates the hardware of every circuit that is not the current best circuit

__save_generation()

Saves the current generation to the generations directory Each generation gets its own file

Saves all modifiable parts of a generation so it can be reconstructed.

called by __write_to_livedata(self)

__should_continue_evo()

Checks with config whether we have reached any of the end conditions for the simulation run.

Returns:

True if evolution should continue, False otherwise.

Return type:

bool

__single_point_crossover(source, dest)

Copy some series of chiasmas (points of genetic exchange) from fitter circuit into children

Parameters:
  • source (Circuit) – The circuit you are copying data from.

  • dest (Circuit) – The circuit you are overwriting data from source to.

__unique(arrays)

Returns an array of unique arrays from the input

Parameters:

arrays (list[list[T]]) – An array containing arrays

Returns:

Returns a list of all of the unique lists contained in the arrays variable

Return type:

list[T]

__write_to_livedata()

Runs each generation to write data to files used to store data needed for Live plots (PlotEvolutionLive.py)

avg_hamming_dist()[source]

Calculates and returns the average Hamming distance for the population

Returns:

Returns Hamming distance in the population.

Return type:

float

count_differing_bits()[source]

Returns the number of bits in the bistream where 2 circuits have different values

Returns:

Number of bits in the bistream where 2 circuits have different values

Return type:

int

count_unique()[source]

Returns the number of unique files in the population

Returns:

Number of unique circuits in the population

Return type:

int

evolve()[source]

Runs an evolutionary loop and records the circuit with the highest fitness throughout the loop, while also storing statistics in a file for the plot to access.

get_best_epoch()[source]

Returns the generation number that contained the circuit with the highest fitness

Returns:

Generation number that hat the circuit with the highest fitness

Return type:

int

get_current_best_circuit()[source]

Gets the circuit in the current generation with the highest fitness

Returns:

Returns the best circuit in population.

Return type:

Circuit

get_current_epoch()[source]

Returns the generation number

Returns:

Returns the generation number of the current evolution.

Return type:

int

get_differing_bits_str()[source]

Returns an ASCII string that represents the number of circuits with a 1 at each bit in the bitstream :returns: The number of circuits with a 1 at each bit in the bitstream :rtype: str

get_overall_best_circuit_info()[source]

Returns the information of the circuit with the highest fitness throughout the run

Returns:

Returns the info object for the overall best circuit throughout the run.

Return type:

CircuitInfo

populate()[source]

Creates initial population based on the config. 1. Clears the files used to keep track of circuit 2. Uses appropriate initialization method specified by config. 3. Handles randomization until condition in config is met.

run_fitness_sensitity()[source]

Gets the same circuit, runs it repeatedly and reports each fitness. Internally has a while loop to determine how many times to run.

CircuitPopulation.is_pulse_func(config)[source]

Used in multiple places, will be removed soon.

Parameters:

config (Config) – Configuration Class to interact with config

Returns:

True if it is any type of oscilator (uses count pulses), False otherwise.

Return type:

bool