cgexplore.systems_optimisation.ChromosomeGenerator

class cgexplore.systems_optimisation.ChromosomeGenerator(present_beads, vdw_bond_cutoff, prefix, chromosome_map=<factory>, chromosome_types=<factory>, chromosomed_terms=<factory>, definer_dict=<factory>)[source]

Bases: object

Hold all information for chromosome iteration.

Methods

add_forcefield_dict

Add genes based on a forcefield dictionary.

add_gene

Add a gene to the chromosome generator, to be iterated over.

crossover_population

Crossover chromosomes.

dedupe_population

Deduplicate the list of chromosomes.

define_chromosomes

Get all chromosomes that have been defined.

get_bc_ids

Get chromosome indices associated with building block configs.

get_num_chromosomes

Return the number of chromosomes.

get_population_neighbours

Get all nearest neighbours of provided chromosomes.

get_prec_ids

Get chromosome indices associated with precursors.

get_term_ids

Get chromosome indices associated with terms.

get_topo_ids

Get chromosome indices associated with topology.

get_va_ids

Get chromosome indices associated with vertex alignments.

mutate_population

Mutate chromosomes in the gene range only.

select_chromosome

Get chromosome.

select_random_population

Select size instances from population.

select_similar_chromosome

Select chromosomes where only one gene is allowed to change.

yield_chromosomes

Yield chromosomes.

Attributes

Parameters:
add_forcefield_dict(definer_dict)[source]

Add genes based on a forcefield dictionary.

Parameters:

definer_dict (dict[str, tuple]) – The forcefield dictionary. Format:

Return type:

None

add_gene(iteration, gene_type)[source]

Add a gene to the chromosome generator, to be iterated over.

Parameters:
  • iteration (Iterable) – The range of values to use in the chromosome. For term: use the add_forcefield_dict

  • gene_type (str) – A string defining the gene type. Can be term, topology, precursor, forcefield, vertex_alignment, building_block_configuration.

Return type:

None

crossover_population(chromosomes, generator, selection, num_to_select, database)[source]

Crossover chromosomes.

Available selections for which chromosomes to mutate:

random:

uses generator.choice()

roulette:

adds weight to generator.choice() based on fitness/sum(fitness)

Parameters:
  • chromosomes (dict[str, Chromosome]) – Dictionary of chromosomes with keys corresponding to their keys in the provided database.

  • generator (Generator) – Random number generator.

  • selection (str) – How to select chromosomes to mutate (random or roulette).

  • num_to_select (int) – Number of chromosomes to mutate.

  • database (AtomliteDatabase) – atomlite database containing the chromosome data.

Returns:

List of chromosomes to add to population.

Return type:

Sequence[Chromosome]

dedupe_population(list_of_chromosomes)[source]

Deduplicate the list of chromosomes.

Parameters:

list_of_chromosomes (Sequence[Chromosome])

Return type:

Sequence[Chromosome]

define_chromosomes()[source]

Get all chromosomes that have been defined.

If the chromosome space is large, this can be super expensive!

Return type:

None

get_bc_ids()[source]

Get chromosome indices associated with building block configs.

Return type:

Sequence[int]

get_num_chromosomes()[source]

Return the number of chromosomes.

Return type:

int

get_population_neighbours(chromosomes, gene_range, selection)[source]

Get all nearest neighbours of provided chromosomes.

Available selections for which chromosomes to mutate:

all:

selects all chromosomes.

Parameters:
  • chromosomes (dict[str, Chromosome]) – Dictionary of chromosomes with keys corresponding to their keys in the provided database.

  • gene_range (Sequence[int]) – Range of genes, by their indices in the chromosome, to mutate.

  • selection (str) – How to select chromosomes to mutate (all).

Returns:

List of chromosomes to add to population.

Return type:

Sequence[Chromosome]

get_prec_ids()[source]

Get chromosome indices associated with precursors.

Return type:

Sequence[int]

get_term_ids()[source]

Get chromosome indices associated with terms.

Return type:

Sequence[int]

get_topo_ids()[source]

Get chromosome indices associated with topology.

Return type:

Sequence[int]

get_va_ids()[source]

Get chromosome indices associated with vertex alignments.

Return type:

Sequence[int]

mutate_population(chromosomes, generator, gene_range, selection, num_to_select, database)[source]

Mutate chromosomes in the gene range only.

Available selections for which chromosomes to mutate:

random:

uses generator.choice()

roulette:

adds weight to generator.choice() based on fitness/sum(fitness)

Parameters:
  • chromosomes (dict[str, Chromosome]) – Dictionary of chromosomes with keys corresponding to their keys in the provided database.

  • generator (Generator) – Random number generator.

  • gene_range (Sequence[int]) – Range of genes, by their indices in the chromosome, to mutate.

  • selection (str) – How to select chromosomes to mutate (random or roulette).

  • num_to_select (int) – Number of chromosomes to mutate.

  • database (AtomliteDatabase) – atomlite database containing the chromosome data.

Returns:

List of chromosomes to add to population.

Return type:

Sequence[Chromosome]

select_chromosome(chromosome)[source]

Get chromosome.

Parameters:

chromosome (Sequence[int])

Return type:

Chromosome

select_random_population(generator, size)[source]

Select size instances from population.

Parameters:
  • generator (Generator)

  • size (int)

Return type:

Sequence[Chromosome]

select_similar_chromosome(chromosome, free_gene_id)[source]

Select chromosomes where only one gene is allowed to change.

Parameters:
Return type:

Sequence[Chromosome]

yield_chromosomes()[source]

Yield chromosomes.

Return type:

Iterable[Chromosome]

chromosome_map: dict[int, dict]
chromosome_types: dict[int, str]
chromosomed_terms: dict[str, list[int]]
definer_dict: dict[str, tuple]
prefix: str
present_beads: Sequence[CgBead]
vdw_bond_cutoff: int