Changeset 1271 for framspy/evolalg/base


Ignore:
Timestamp:
08/04/23 01:30:21 (13 months ago)
Author:
Maciej Komosinski
Message:

Improved boundary checks, descriptions and comments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • framspy/evolalg/base/experiment_niching_abc.py

    r1195 r1271  
    11import time
    22from abc import ABC, abstractmethod
    3 from tkinter import W
    43
    54import numpy as np
     
    2928        self.fit = fit
    3029        self.normalize = normalize
    31         self.knn_niching = knn_niching
     30        self.knn_niching = knn_niching # this parameter is used for local novelty and local niching
    3231        self.knn_nslc = knn_nslc
    3332        self.archive_size=archive_size
    34         if popsize < self.knn_niching:
    35             self.knn_niching = popsize - 2
    36         if popsize < self.knn_nslc:
    37             self.knn_nslc = popsize - 2
     33
     34        # np.argpartition requires these parameters to be at most popsize-2; popsize is decreased by 1 because we remove_diagonal()
     35        if self.knn_niching > popsize - 2:
     36            raise ValueError("knn_niching (%d) should be at most popsize-2 (%d)" % (self.knn_niching, popsize-2))
     37        if self.knn_nslc > popsize - 2:
     38            raise ValueError("knn_nslc (%d) should be at most popsize-2 (%d)" % (self.knn_nslc, popsize-2))
     39
    3840
    3941    def transform_indexes(self, i, index_array):
     
    4143
    4244    def normalize_dissim(self, dissim_matrix):
    43         dissim_matrix = remove_diagonal(np.array(dissim_matrix))
     45        dissim_matrix = remove_diagonal(np.array(dissim_matrix)) # on the diagonal we usually have zeros (an individual is identical with itself, so the dissimilarity is 0). In some techniques we need to find "k" most similar individuals, so we remove the diagonal so that self-similarity of individuals does not interfere with finding "k" other most similar individuals. The matrix from square n*n turns into n*(n-1).
    4446        if self.normalize == "none":
    4547            return dissim_matrix
     
    7274        else:
    7375            raise Exception("Wrong fit type: ", self.fit,
    74                             f" chose correct one or implement new behavior")
     76                            f" choose the correct one or implement a new behavior.")
    7577        population_structures.update_archive(dissim_matrix, population_archive)
    7678
     
    9092                temp_dissim, index_array, axis=-1))
    9193            temp_fitness = population[i].rawfitness
    92             population_of_most_different = list(
     94            population_of_most_similar = list(
    9395                map(population.__getitem__, self.transform_indexes(i, index_array)))
    9496            temp_ind_fit = sum(
    95                 [1 for ind in population_of_most_different if ind.rawfitness < temp_fitness])
     97                [1 for ind in population_of_most_similar if ind.rawfitness < temp_fitness])
    9698            population[i].fitness = DeapFitness(
    9799                tuple((dissim_value, temp_ind_fit)))
     
    165167
    166168            if type(self.population_structures.population[0].fitness) == DeapFitness:
    167                 self.population_structures.population = self.make_new_population_nsga2(
     169                self.population_structures.population = self.make_new_population_nsga2(  # used both for nsga2 and nslc
    168170                    self.population_structures.population, pmut, pxov)
    169171            else:
     
    184186    def get_args_for_parser():
    185187        parser = ExperimentABC.get_args_for_parser()
    186         parser.add_argument("-dissim",type= int, default= "frams",
    187                    help="Dissimilarity measure type. Availible -2:emd, -1:lev, 1:frams1 (default}, 2:frams2")
    188         parser.add_argument("-fit",type= str, default= "raw",
    189                         help="Fitness type, availible  types: niching, novelty, nsga2, nslc and raw (default}")
    190         parser.add_argument("-archive",type= int, default= 50,
     188        parser.add_argument("-dissim",type= int, default=1,
     189                   help="Dissimilarity measure type. Available: -3:freq, -2:dens, -1:Leven, 1:frams-struct (default}, 2:frams-descr")
     190        parser.add_argument("-fit",type= str, default="raw",
     191                        help="Fitness type, availible types: niching, novelty, knn_niching (local), knn_novelty (local), nsga2, nslc and raw (default)")
     192        parser.add_argument("-archive",type= int, default=50,
    191193                            help="Maximum archive size")
    192194        parser.add_argument("-normalize",type= str, default= "max",
    193                             help="What normalization use for dissimilarity matrix, max (default}, sum and none")
    194         parser.add_argument("-knn",type= int, default= 0,
    195                         help="Nearest neighbors parameter for local novelty/niching, if knn==0 global is performed. Default: 0")
     195                            help="What normalization to use for the dissimilarity matrix: max (default}, sum, or none")
     196        parser.add_argument("-knn_niching",type= int, default=5,
     197                        help="The number of nearest neighbors for local novelty/niching. If knn==0, global is performed. Default: 5")
     198        parser.add_argument("-knn_nslc",type= int, default=5,
     199                        help="The number of nearest neighbors for NSLC. If knn==0, global is performed. Default: 5")
    196200        return parser
    197201       
Note: See TracChangeset for help on using the changeset viewer.