py4design.pyoptimise.nsga2 module

py4design.pyoptimise.nsga2.frange(start, end=None, inc=None)

A range function, that does accept float increments.

Parameters:

start : float

The starting number of the sequence.

end : float, optional

Generate numbers up to, but not including this number, Default = None. When None, end == start and start = 0.0.

inc : float, optional

The difference between each number in the sequence, Default = None. When None, inc = 1.0.

Returns:

sequence of floats : list of floats

A list of float.

class py4design.pyoptimise.nsga2.Gene(gene_type, value_range)

Bases: object

An object that contains all the gene information for running a NSGA2 optimisation.

Parameters:

gene_type : str

The type of the gene. There are four options: “int_range”, “int_choice”, “float_range”, “float_choice”.

value_range : list of int/floats

List of ints/floats. If the gene_type is “int_range” or “float_range”, the list has three elements. The first element is the starting number, the second element is the ending number (not included in the sequence), and the last element is the difference between each number in the sequence. If the gene_type is “int_choice” or “float_choice”, the list is made up of all the possible options.

Attributes

gene_type (str) see Parameters.
value_range (list of int/floats) see Parameters.
position (int) The position of the gene in the genotype. If the position is 0 the gene is in the first postion.
class py4design.pyoptimise.nsga2.GenotypeMeta

Bases: object

An object that contains all the meta information of a genotype for running a NSGA2 optimisation.

Attributes

gene_list (list of Gene class instances) The list of Gene class instances that will be developed in the Genotype class.
add_gene(gene)

This function adds a gene to the gene list.

Parameters:

gene : Gene class instance

The gene to be added to the Genotype class.

gene_position()

This function assigns a position to each gene in the gene list.

length()

This function returns the number of genes in the gene list.

Returns:

length : int

The number of genes in the gene list.

class py4design.pyoptimise.nsga2.Genotype(genotype_meta)

Bases: object

An object that contains all the information and methods for developing a genotype for an individual.

Parameters:

genotype_meta : GenotypeMeta class instance

The meta information of the genotype.

Attributes

genotype_meta (GenotypeMeta class instance) See Parameters.
values (list of floats/ints) The Genotype values of an individual to develop the individual’s phenotype.
randomise()

This function randomly generates the Genotype values

mutate(mutation_prob)

This function mutates the genotype values.

Parameters:

mutation_prob : float

The mutation probability, the probability is between 0 to 1.

read_str(string)

This function reads a string and convert it into a list for values attribute.

Parameters:

string : str

The values in string format, e.g. “0.2 3.2 5 0 6.5”. The string will be converted to a list and saved as the values attribute.

write_str()

This function writes the values attribute into a string.

Returns:

string : str

The values is converted from [0.2, 3.2, 5, 0, 6.5] to string format “0.2 3.2 5 0 6.5”.

class py4design.pyoptimise.nsga2.DerivedParams

Bases: object

An object that contains all the information of derived parameters extracted from the individual.

Attributes

values (list of floats/ints) The values of each derived parameter of an individual.
add_param(param)

This function adds a derived parameter value.

Parameters:

param : float

The value of a derived parameter.

length()

This function returns the number of derived parameter values in the values attribute.

Returns:

length : int

The number of derived parameter values in the values attribute.

read_str(string)

This function reads a string and convert it into a list for values attribute.

Parameters:

string : str

The values in string format, e.g. “0.2 3.2 5 0 6.5”. The string will be converted to a list and saved as the values attribute.

write_str()

This function writes the values attribute into a string.

Returns:

string : str

The values is converted from [0.2, 3.2, 5, 0, 6.5] to string format “0.2 3.2 5 0 6.5”.

class py4design.pyoptimise.nsga2.Individual(idx, genotype_meta, score_meta)

Bases: object

An object that contains all the information and methods for developing an individual.

Parameters:

idx : int

A unique number to identify the individual.

genotype_meta : GenotypeMeta class instance

A GenotypeMeta class instance that provide all the information for developing the Genotype.

score_meta : ScoreMeta class instance

The meta information of the performance objectives of the individual.

Attributes

idx (int) See Parameters.
genotype_meta (GenotypeMeta class instance) See Parameters.
genotype (Genotype class instance) A Genotype class instance that provide all the information for developing an individual.
live (bool) True or False. If True the individual is alive, if False individual is dead.
scores (list of floats) The list containing the performance of this individual.
derivedparams (DerivedParams class instance) A DerivedParams class instance containing all the derived parameters informaiton.
generation (int) The generation this individual is borned in.
distance (float) The crowding distance of this individual.
rank (int) The Pareto rank of this individual.
randomise()

This function randomly generates the Genotype values

set_score(index, score)

This function set the score for the individual.

Parameters:

index : int

The index of the score in the score list to be set.

score : float

The value of the score.

get_score(index)

This function get the score for the individual.

Parameters:

index : int

The index of the score in the score list to get.

Returns:

score : float

The value of the score.

is_not_evaluated()

This function checks if the individual has already been evaluated.

Returns:

is not evaluated : bool

True or False, if True the individual is not evaluated, if False the individual is evaluated.

add_derivedparams(derivedparams)

This function adds derived parameter values to the individual.

Parameters:

derivedparams : list of floats/ints

The list of derived parameter values to be added to the individual.

add_generation(generation)

This function adds the generation this individual is borned in to the individual.

Parameters:

generation : int

The generation this individual is borned in.

xml()

This function writes the individual to an xml format.

Returns:

xml individual : str

The xml string of the individual.

class py4design.pyoptimise.nsga2.ScoreMeta(score_names, scores_min_max)

Bases: object

An object that contains all the information of the scores of an individual.

Parameters:

score_names : list of str

A list of the names of the score.

scores_min_max : list of int

A list of int. The int can only be either 0 or 1. O indicates the score has to be minimise, 1 to be maximised.

Attributes

See Parameters.  
MIN = 0
MAX = 1
get_num_scores()

This function count the number of scores.

Returns:

number of scores : int

The number of scores.

class py4design.pyoptimise.nsga2.Population(size, genotype_meta, score_meta, live_xml_filepath, dead_xml_filepath, mutation_prob, crossover_rate)

Bases: object

An object that contains all the information and methods for optimising a population of individuals.

Parameters:

size : int

The population size.

genotype_meta : GenotypeMeta class instance

A GenotypeMeta class instance that provide all the information for developing the Genotype.

score_meta : ScoreMeta class instance

The meta information of the performance objectives of the individual.

live_xml_filepath : str

The file path of the XML file that documents all the living individuals.

dead_xml_filepath : str

The file path of the XML file that documents all the dead individuals.

mutation_prob : float

The mutation probability, the probability is between 0 to 1. The usual mutation probability is about 0.01.

crossover_rate : float

The crossover rate, the rate is between 0 to 1. The usual crossover rate is about 0.8.

Attributes

size (int) See Parameters.
genotype_meta (GenotypeMeta class instance) See Parameters.
individuals (list of Individuals class instances) List of individuals of the population.
live_xml_filepath (str) See Parameters.
dead_xml_filepath (str) See Parameters.
score_meta (ScoreMeta class instance) See Parameters.
mutation_prob (float) See Parameters.
crossover_rate (float) See Parameters.
num_archived_individuals (int) The number of individuals on the dead xml file.
select_random_inds(num_inds)

This function randomly selects a number of individuals.

Parameters:

num_inds : int

The number of individuals to select.

Returns:

selected individuals : list of Individual class instances

The list of randomly chosen individuals.

rank(inds)

This function Pareto ranks the individuals.

Parameters:

inds : list of Individual class instances

The list of individuals’ to be ranked.

Returns:

ranked individuals : 2d list of Individual class instances

Each list of list of individuals are ranked accordingly. The first list of individuals in the 2d list has the best Pareto rankings.

crowd_distance_assignment(individuals)

This function calculates and assigns the crowding distance for the individuals.

Parameters:

individuals : list of Individual class instances

The list of individuals’ to be assigned their crowding distance.

Returns:

assigned individuals : list of Individual class instances

Each individual is assigned a crowding distance.

crowded_comparison(ind1, ind2)

This function compare the two individuals based on crowded comparison.

Parameters:

ind1 : Individual class instance

Check if this Individual is better than the other individual.

ind2 : Individual class instance

Individual 2.

Returns:

comparison : int

If 1 ind1 is better than ind2, if -1 ind2 is better than ind1, if 0 means both individuals are the same.

sort_objectives(individuals, obj_idx)

This function arranges the individuals in ascending orders according to a chosen performance objective.

Parameters:

individuals : list of Individual class instances

The list of individuals’ to arranged.

obj_idx : int

The index of the chosen objective for arranging the individuals.

Returns:

sorted individuals : list of Individual class instances

The sorted list of individuals in ascending orders according to the chosen objective.

crossover(ind1, ind2, generation)

This function performs crossover between two individuals.

Parameters:

ind1 : Individual class instance

The parent individual.

ind2 : Individual class instance

The parent individual.

genertaion : int

The generation of the child individual.

Returns:

child individual : Individual class instance

The child individual from the crossover.

reproduce(individuals, generation)

This function performs reproduction for the current generation of individuals, kills of the current population and replace them with the reproduced population.

Parameters:

individuals : list of Individual class instances

The list of individuals’ for reproduction.

genertaion : int

The generation of the new reproduced population.

get_max_id()

This function gets total number of individuals.

Returns:

total number of individuals : int

The total number of individuals.

randomise()

This function randomly generates a population of individuals.

write()

This function writes all the individuals into xml, the living individuals into the live xml file, the dead ones in the dead xml file.

read()

This function reads the xml files into Python objects.