Modules
EvoNP.py
generationRun(points, population, k, nChromosomes, sortedDistancesTree, pointsNearestIndices, pointsNearestDistances, bestChromosomeInAllGenerations, bestFitnessInAllGenerations, bestLabelsPredInAllGenerations)
The main method of EvoNP
Parameters
———-
points : list
The list of points. A two dimensional array where each row represents
a point containing the features values for the point
population : list
The list of chromosomes
k : int
Number of clusters
nChromosomes: int
Number of chrmosome in a population
sortedDistancesTree : cKDTree
A tree of all the points sorted in a way where the nearest point to another point is easily retrieved
pointsNearestIndices : list
A matrix of nearest points for each previously elected point
pointsNearestDistances : list
A matrix of the distances between the nearest points and the Elected for each previously elected point
bestChromosomeInAllGenerations : int
The chromosome with the minimum fitness in all the generations
bestFitnessInAllGenerations: float
The minimum fitness in all the generations
bestLabelsPredInAllGenerations: list
The predicted label of the chromosome with the minimum fitness in all the generations
Returns
——-
list
population : The list of chromosomes
list
fitness : The list of fitness value for each chromosome
int
bestChromosomeInAllGenerations: The chromosome with the minimum fitness in all the generations
list
bestLabelsPredInAllGenerations: The predicted label of the chromosome with the minimum fitness in all the generations
float
bestFitnessInAllGenerations: The minimum fitness in all the generations
The main method of EvoNP
Parameters
———-
points : list
The list of points. A two dimensional array where each row represents
a point containing the features values for the point
population : list
The list of chromosomes
k : int
Number of clusters
nChromosomes: int
Number of chrmosome in a population
sortedDistancesTree : cKDTree
A tree of all the points sorted in a way where the nearest point to another point is easily retrieved
pointsNearestIndices : list
A matrix of nearest points for each previously elected point
pointsNearestDistances : list
A matrix of the distances between the nearest points and the Elected for each previously elected point
bestChromosomeInAllGenerations : int
The chromosome with the minimum fitness in all the generations
bestFitnessInAllGenerations: float
The minimum fitness in all the generations
bestLabelsPredInAllGenerations: list
The predicted label of the chromosome with the minimum fitness in all the generations
Returns
——-
list
population : The list of chromosomes
list
fitness : The list of fitness value for each chromosome
int
bestChromosomeInAllGenerations: The chromosome with the minimum fitness in all the generations
list
bestLabelsPredInAllGenerations: The predicted label of the chromosome with the minimum fitness in all the generations
float
bestFitnessInAllGenerations: The minimum fitness in all the generations
run(points, nPoints, k, nChromosomes, nGenerations, crossoverProbability, mutationProbability)
The main method of EvoNP
Parameters
———-
points : list
The list of points. A two dimensional array where each row represents
a point containing the features values for the point
nPoints:
Number of points (instances) in the dataset
k : int
Number of clusters
nChromosomes: int
Number of chrmosome in a population
nGenerations: int
Number of generations
crossoverProbability : float
The probability of crossover
allBestFitness : list
A matrix of all the values of the fitness for each generation
mutationProbability : float
The probability of mutation
Returns
——-
int
bestChromosomeInAllGenerations: The chromosome with the minimum fitness in all the generations
list
bestLabelsPredInAllGenerations: The predicted label of the chromosome with the minimum fitness in all the generations
float
bestFitnessInAllGenerations: The minimum fitness in all the generations
list
allBestFitness: A matrix of all the values of the fitness for each generation
The main method of EvoNP
Parameters
———-
points : list
The list of points. A two dimensional array where each row represents
a point containing the features values for the point
nPoints:
Number of points (instances) in the dataset
k : int
Number of clusters
nChromosomes: int
Number of chrmosome in a population
nGenerations: int
Number of generations
crossoverProbability : float
The probability of crossover
allBestFitness : list
A matrix of all the values of the fitness for each generation
mutationProbability : float
The probability of mutation
Returns
——-
int
bestChromosomeInAllGenerations: The chromosome with the minimum fitness in all the generations
list
bestLabelsPredInAllGenerations: The predicted label of the chromosome with the minimum fitness in all the generations
float
bestFitnessInAllGenerations: The minimum fitness in all the generations
list
allBestFitness: A matrix of all the values of the fitness for each generation
NP.py
NP(points_, sortedDistancesTree_, k_, initialPointsIndices_, randomPointsIndices_, pointsNearestIndices_, pointsNearestDistances_)
This is the implementation of the NP clustering technique
Parameters
———-
points_ : list
The list of points. A two dimensional array where each row represents
a point containing the features values for the point
sortedDistancesTree_ : cKDTree
A tree of all the points sorted in a way where the nearest point to another point is easily retrieved
k : int
Number of clusters
initialPointsIndices_ : list
The indices of the initial points of the clusters which are retrieved from the first part of the chromosome
randomPointsIndices_ : list
The indices of the elected points of the clusters which are retrieved from the second part of the chromosome
pointsNearestIndices_ : list
A matrix of nearest points for each previously elected point
pointsNearestDistances_ : list
A matrix of the distances between the nearest points and the Elected for each previously elected point
Returns
——-
list
labelsPred: the predicted values of the data
list
chromosome: the updated chromosome generated after applying NP
list
pointsNearestIndices_ : A matrix of the updated nearest points for each previously elected point
list
pointsNearestDistances_ : A matrix of the updated distances between the nearest points and the Elected for each previously elected point
This is the implementation of the NP clustering technique
Parameters
———-
points_ : list
The list of points. A two dimensional array where each row represents
a point containing the features values for the point
sortedDistancesTree_ : cKDTree
A tree of all the points sorted in a way where the nearest point to another point is easily retrieved
k : int
Number of clusters
initialPointsIndices_ : list
The indices of the initial points of the clusters which are retrieved from the first part of the chromosome
randomPointsIndices_ : list
The indices of the elected points of the clusters which are retrieved from the second part of the chromosome
pointsNearestIndices_ : list
A matrix of nearest points for each previously elected point
pointsNearestDistances_ : list
A matrix of the distances between the nearest points and the Elected for each previously elected point
Returns
——-
list
labelsPred: the predicted values of the data
list
chromosome: the updated chromosome generated after applying NP
list
pointsNearestIndices_ : A matrix of the updated nearest points for each previously elected point
list
pointsNearestDistances_ : A matrix of the updated distances between the nearest points and the Elected for each previously elected point
addNearestToElectedCluster(electedIndex, nearestIndex, distance)
Assignment operation: aSSIGN the Nearest to the cluster of the Elected.
The Nearest is not yet assigned
Parameters
———-
electedIndex : int
The index of the Elected
nearestIndex: int
The index of the Nearest
distance: float
The distance between the Elected and the Nearest
Returns
——-
N/A
Assignment operation: aSSIGN the Nearest to the cluster of the Elected.
The Nearest is not yet assigned
Parameters
———-
electedIndex : int
The index of the Elected
nearestIndex: int
The index of the Nearest
distance: float
The distance between the Elected and the Nearest
Returns
——-
N/A
addPointToCluster(clusterNo, pointIndex, assignerIndex, assignerDistance)
Adds a point to a cluster
Parameters
———-
clusterNo : int
The cluster where the point should be added
pointIndex : int
The index of the point to be added
assignerIndex: int
The index of the Assigner point for the point to be added
assignerDistance: float
The distance between the point and the Assigner
Returns
——-
N/A
Adds a point to a cluster
Parameters
———-
clusterNo : int
The cluster where the point should be added
pointIndex : int
The index of the point to be added
assignerIndex: int
The index of the Assigner point for the point to be added
assignerDistance: float
The distance between the point and the Assigner
Returns
——-
N/A
arePointsInSameCluster(pointIndex1, pointIndex2)
Checks if two points are assigned to the same cluster
Parameters
———-
pointIndex1 : int
The index of the first point to be checked
pointIndex2: int
The index of the second point to be checked
Returns
——-
bool
true/false indicating if the two points are assigned to the same cluster
Checks if two points are assigned to the same cluster
Parameters
———-
pointIndex1 : int
The index of the first point to be checked
pointIndex2: int
The index of the second point to be checked
Returns
——-
bool
true/false indicating if the two points are assigned to the same cluster
calculate()
The main method
Assigns points to clusters until all points are assigned by performing the calculations of the algorithm including the election, selection, and assignment operators
Parameters
———-
N/A
Returns
——-
N/A
The main method
Assigns points to clusters until all points are assigned by performing the calculations of the algorithm including the election, selection, and assignment operators
Parameters
———-
N/A
Returns
——-
N/A
createInitialPoints()
Creates initial points for each cluster
Parameters
———-
N/A
Returns
——-
N/A
Creates initial points for each cluster
Parameters
———-
N/A
Returns
——-
N/A
getElectedIndex()
The Election Operation: Select the next point from the chromosome if exists. If not, randomly elect a point from the set of clustered points and add it to the chromosome. Name the point as Elected (E)
Parameters
———-
N/A
Returns
——-
int
electedIndex: the index of the Elected
The Election Operation: Select the next point from the chromosome if exists. If not, randomly elect a point from the set of clustered points and add it to the chromosome. Name the point as Elected (E)
Parameters
———-
N/A
Returns
——-
int
electedIndex: the index of the Elected
getNearestIndexAndDistance(pointIndex, point, distanceVectorIndexArray)
Returns the index and distance of the Nearest point according to the distance vector index
Parameters
———-
point : ndarray
The point that we need to find its Nearest
pointIndex : int
The index of the point that we need to find its Nearest
distanceVectorIndexArray : int
The distance vector index which indicates the number of Nearest points previously selected by Elected
Returns
——-
int
The index of the Nearest
float
The distance between the point and the Nearest
Returns the index and distance of the Nearest point according to the distance vector index
Parameters
———-
point : ndarray
The point that we need to find its Nearest
pointIndex : int
The index of the point that we need to find its Nearest
distanceVectorIndexArray : int
The distance vector index which indicates the number of Nearest points previously selected by Elected
Returns
——-
int
The index of the Nearest
float
The distance between the point and the Nearest
getNearestPoint(electedIndex)
The Selection operation: Select the next nearest point for the Elected
Parameters
———-
electedIndex : int
The index of the Elected
Returns
——-
int
The index of the Nearest
float
The distance between the Nearest and the Elected
The Selection operation: Select the next nearest point for the Elected
Parameters
———-
electedIndex : int
The index of the Elected
Returns
——-
int
The index of the Nearest
float
The distance between the Nearest and the Elected
getRandomAssignedPoint()
select a random point that is already assigned to a cluster
Parameters
———-
N/A
Returns
——-
int
The index of the random assigned point
select a random point that is already assigned to a cluster
Parameters
———-
N/A
Returns
——-
int
The index of the random assigned point
init()
Initializes the variables and data structures and creates the initial points for the clusters
Parameters
———-
N/A
Returns
——-
N/A
Initializes the variables and data structures and creates the initial points for the clusters
Parameters
———-
N/A
Returns
——-
N/A
isPointInCluster(pointIndex)
Checks if point is already assigned to a cluster
Parameters
———-
pointIndex : int
The index of the point to be checked
Returns
——-
bool
true/false indicating if the point is already assigned to a cluster
Checks if point is already assigned to a cluster
Parameters
———-
pointIndex : int
The index of the point to be checked
Returns
——-
bool
true/false indicating if the point is already assigned to a cluster
moveNearestToElectedCluster(electedIndex, nearestIndex, distance)
Assignment operation: reassign the Nearest to the cluster of the Elected.
The Nearest is already assigned
Parameters
———-
electedIndex : int
The index of the Elected
nearestIndex: int
The index of the Nearest
distance: float
The distance between the Elected and the Nearest
Returns
——-
N/A
Assignment operation: reassign the Nearest to the cluster of the Elected.
The Nearest is already assigned
Parameters
———-
electedIndex : int
The index of the Elected
nearestIndex: int
The index of the Nearest
distance: float
The distance between the Elected and the Nearest
Returns
——-
N/A
shouldPointMoveToNearerCluster(nearestIndex, nearestDist)
Checks if the Nearest should move to the cluster of the Elected
Parameters
———-
nearestIndex : int
The index of the Nearest point
nearestDist: float
The distance between the Elected and the Nearest
Returns
——-
bool
true/false if the Nearest should move to the cluster of the Elected
Checks if the Nearest should move to the cluster of the Elected
Parameters
———-
nearestIndex : int
The index of the Nearest point
nearestDist: float
The distance between the Elected and the Nearest
Returns
——-
bool
true/false if the Nearest should move to the cluster of the Elected
updateAssignmentsTree(pointIndex, assignerIndex)
Updates the assignment tree. The assignment tree contains the points that
are already assigned and their assigners and children in a tree data structure
Parameters
———-
pointIndex : int
The index of the point to be updated/added
assignerIndex: int
The index of the Assigner point for the point to be updated/added
Returns
——-
N/A
Updates the assignment tree. The assignment tree contains the points that
are already assigned and their assigners and children in a tree data structure
Parameters
———-
pointIndex : int
The index of the point to be updated/added
assignerIndex: int
The index of the Assigner point for the point to be updated/added
Returns
——-
N/A
EAoperators.py
crossover(chromosomeLength, parent1, parent2)
The crossover operator
Parameters
———-
parent1 : list
The first parent chromosome of the pair
parent2 : list
The second parent chromosome of the pair
chromosomeLength: int
The maximum index of the crossover
Returns
——-
list
offspring1: The first updated parent chromosome of the pair
list
offspring2: The second updated parent chromosome of the pair
The crossover operator
Parameters
———-
parent1 : list
The first parent chromosome of the pair
parent2 : list
The second parent chromosome of the pair
chromosomeLength: int
The maximum index of the crossover
Returns
——-
list
offspring1: The first updated parent chromosome of the pair
list
offspring2: The second updated parent chromosome of the pair
elitism(population, fitness, labelsPred, bestChromosomeInAllGenerations, bestFitnessInAllGenerations, bestLabelsPredInAllGenerations)
This method performs the elitism operator
Parameters
———-
population : list
The list of chromosomes
fitness : list
The list of fitness values for each chromosome
labelPred : list
A list of predicted labels for the points for each chroomosome
bestChromosomeInAllGenerations : list
A chromosome of the previous generation having the best fitness value
bestFitnessInAllGenerations : float
The best fitness value of the previous generation
bestLabelsPredInAllGenerations :
A list of predicted labels for the previous generation having the best fitness value
Returns
——-
list
population : The updated population after applying the elitism
list
fitness : The updated list of fitness values for each chromosome after applying the elitism
list
labelsPred : The updated list of predicted labels for the points for each chroomosome after applying the elitism
list
bestChromosomeInAllGenerations : A chromosome of the current generation having the best fitness value
float
bestFitnessInAllGenerations : The best fitness value of the current generation
list
bestLabelsPredInAllGenerations : A list of predicted labels for the current generation having the best fitness value
This method performs the elitism operator
Parameters
———-
population : list
The list of chromosomes
fitness : list
The list of fitness values for each chromosome
labelPred : list
A list of predicted labels for the points for each chroomosome
bestChromosomeInAllGenerations : list
A chromosome of the previous generation having the best fitness value
bestFitnessInAllGenerations : float
The best fitness value of the previous generation
bestLabelsPredInAllGenerations :
A list of predicted labels for the previous generation having the best fitness value
Returns
——-
list
population : The updated population after applying the elitism
list
fitness : The updated list of fitness values for each chromosome after applying the elitism
list
labelsPred : The updated list of predicted labels for the points for each chroomosome after applying the elitism
list
bestChromosomeInAllGenerations : A chromosome of the current generation having the best fitness value
float
bestFitnessInAllGenerations : The best fitness value of the current generation
list
bestLabelsPredInAllGenerations : A list of predicted labels for the current generation having the best fitness value
mutation(offspring, chromosomeLength, nPoints)
The mutation operator
Parameters
———-
offspring : list
A generated chromosome after the crossover
chromosomeLength: int
The maximum index of the crossover
nPoints:
Number of points (instances) in the dataset
Returns
——-
list
offspring: The updated offspring chromosome
The mutation operator
Parameters
———-
offspring : list
A generated chromosome after the crossover
chromosomeLength: int
The maximum index of the crossover
nPoints:
Number of points (instances) in the dataset
Returns
——-
list
offspring: The updated offspring chromosome
pairSelection(population, fitness, nChromosomes)
This is used to select one pair of parents using roulette Wheel Selection mechanism
Parameters
———-
population : list
The list of chromosomes
fitness : list
The list of fitness values for each chromosome
nChromosomes: int
Number of chrmosome in a population
Returns
——-
list
parent1: The first parent chromosome of the pair
list
parent2: The second parent chromosome of the pair
This is used to select one pair of parents using roulette Wheel Selection mechanism
Parameters
———-
population : list
The list of chromosomes
fitness : list
The list of fitness values for each chromosome
nChromosomes: int
Number of chrmosome in a population
Returns
——-
list
parent1: The first parent chromosome of the pair
list
parent2: The second parent chromosome of the pair
rouletteWheelSelectionId(fitness, sumFitness, nChromosomes)
A roulette Wheel Selection mechanism for selecting a chromosome
Parameters
———-
fitness : list
The list of fitness values for each chromosome
sumFitness : float
The summation of all the fitness values for all chromosomes in a generation
nChromosomes: int
Number of chrmosome in a population
Returns
——-
id
chromosomeId: The id of the chromosome selected
A roulette Wheel Selection mechanism for selecting a chromosome
Parameters
———-
fitness : list
The list of fitness values for each chromosome
sumFitness : float
The summation of all the fitness values for all chromosomes in a generation
nChromosomes: int
Number of chrmosome in a population
Returns
——-
id
chromosomeId: The id of the chromosome selected
runOperators(population, fitness, crossoverProbability, mutationProbability, nChromosomes, nPoints)
This is the main method where the evolutionary operators are called
Parameters
———-
population : list
The list of chromosomes
fitness : list
The list of fitness values for each chromosome
crossoverProbability : float
The probability of crossover
mutationProbability : float
The probability of mutation
nChromosomes: int
Number of chrmosome in a population
nPoints:
Number of points (instances) in the dataset
Returns
——-
list
newPopulation: the new generated population after applying the genetic operations
This is the main method where the evolutionary operators are called
Parameters
———-
population : list
The list of chromosomes
fitness : list
The list of fitness values for each chromosome
crossoverProbability : float
The probability of crossover
mutationProbability : float
The probability of mutation
nChromosomes: int
Number of chrmosome in a population
nPoints:
Number of points (instances) in the dataset
Returns
——-
list
newPopulation: the new generated population after applying the genetic operations
selectBestChromosome(fitness)
It is used to get the best chromosome in a population based n the fitness value
Parameters
———-
fitness : list
The list of fitness values for each chromosome
Returns
——-
int
maxFitnessId: The chromosome id of the best fitness value
It is used to get the best chromosome in a population based n the fitness value
Parameters
———-
fitness : list
The list of fitness values for each chromosome
Returns
——-
int
maxFitnessId: The chromosome id of the best fitness value
selectWorstChromosome(fitness)
It is used to get the worst chromosome in a population based n the fitness value
Parameters
———-
fitness : list
The list of fitness values for each chromosome
Returns
——-
int
maxFitnessId: The chromosome id of the worst fitness value
It is used to get the worst chromosome in a population based n the fitness value
Parameters
———-
fitness : list
The list of fitness values for each chromosome
Returns
——-
int
maxFitnessId: The chromosome id of the worst fitness value