Changeset 1182


Ignore:
Timestamp:
08/31/22 00:05:43 (2 years ago)
Author:
Maciej Komosinski
Message:

More concise code and less redundancy in dissimilarity classes, added support for archive of genotypes, added hard limit on the number of genotype chars

Location:
framspy
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • framspy/evolalg/dissimilarity/dissimilarity.py

    r1145 r1182  
    1111
    1212        self.output_field = output_field
    13         self.fn_reduce = None
     13        self.fn_reduce = Dissimilarity.get_reduction_by_name(reduction)
    1414        self.knn = knn
    15         if reduction == "mean": # TODO change this 'elif' sequence to dictionary?
    16             self.fn_reduce = np.mean
    17         elif reduction == "max":
    18             self.fn_reduce = np.max
    19         elif reduction == "min":
    20             self.fn_reduce = np.min
    21         elif reduction == "sum":
    22             self.fn_reduce = np.sum
    23         elif reduction == "knn_mean":
    24             self.fn_reduce = self.knn_mean
    25         elif reduction == "none" or reduction is None:
    26             self.fn_reduce = None
     15
     16
     17    @staticmethod
     18    def reduce(dissim_matrix, fn_reduce, knn):
     19        if fn_reduce is None:
     20            return dissim_matrix
     21        elif fn_reduce is Dissimilarity.knn_mean:
     22            return fn_reduce(dissim_matrix, 1, knn)
    2723        else:
    28             raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, knn_mean, none")
     24            return fn_reduce(dissim_matrix, axis=1)
    2925
    30     def reduce(self, dissim_matrix):
    31         if self.fn_reduce is None:
    32             return dissim_matrix
    33         return self.fn_reduce(dissim_matrix, axis=1)
    3426
    35     def knn_mean(self, dissim_matrix,axis):
    36         return np.mean(np.partition(dissim_matrix, self.knn)[:,:self.knn],axis=axis)
     27    @staticmethod
     28    def knn_mean(dissim_matrix, axis, knn):
     29        return np.mean(np.partition(dissim_matrix, knn)[:, :knn], axis=axis)
     30
     31
     32    @staticmethod
     33    def get_reduction_by_name(reduction: str):
     34
     35        if reduction not in REDUCTION_FUNCTION:
     36            raise ValueError(f"Unknown reduction type '{reduction}'. Supported: {','.join(REDUCTION_FUNCTION.keys())}")
     37
     38        return REDUCTION_FUNCTION[reduction]
     39
     40
     41
     42REDUCTION_FUNCTION = {
     43            "mean": np.mean,
     44            "max": np.max,
     45            "min": np.min,
     46            "sum": np.sum,
     47            "knn_mean": Dissimilarity.knn_mean,
     48            "none": None
     49        }
  • framspy/evolalg/dissimilarity/frams_dissimilarity.py

    r1145 r1182  
    66from evolalg.dissimilarity.dissimilarity import Dissimilarity
    77
    8 #TODO eliminate overlap with dissimilarity.py
    9 
    108
    119class FramsDissimilarity(FramsStep):
    1210
    13     def __init__(self, frams_lib, reduction="mean", output_field="dissim", knn=None, *args, **kwargs):
     11    def __init__(self, frams_lib, reduction="mean", output_field="dissim",knn=None, *args, **kwargs):
    1412        super(FramsDissimilarity, self).__init__(frams_lib, *args, **kwargs)
    1513
    1614        self.output_field = output_field
    17         self.fn_reduce = None
     15        self.fn_reduce = Dissimilarity.get_reduction_by_name(reduction)
    1816        self.knn = knn
    19         if reduction == "mean":
    20             self.fn_reduce = np.mean
    21         elif reduction == "max":
    22             self.fn_reduce = np.max
    23         elif reduction == "min":
    24             self.fn_reduce = np.min
    25         elif reduction == "sum":
    26             self.fn_reduce = np.sum
    27         elif reduction == "knn_mean":
    28             self.fn_reduce = self.knn_mean
    29         elif reduction == "none" or reduction is None:
    30             self.fn_reduce = None
    31         else:
    32             raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, knn_mean, none")
    3317
    34     def reduce(self, dissim_matrix):
    35         if self.fn_reduce is None:
    36             return dissim_matrix
    37         return self.fn_reduce(dissim_matrix, axis=1)
    3818
    3919    def call(self, population):
     
    4121        if len(population) == 0:
    4222            return []
    43         dissim_matrix = self.frams.dissimilarity([_.genotype for _ in population])
    44         dissim = self.reduce(dissim_matrix)
     23        dissim_matrix = self.frams.dissimilarity([_.genotype for _ in population], 1)
     24        dissim = Dissimilarity.reduce(dissim_matrix, self.fn_reduce, self.knn)
    4525        for d,ind in zip(dissim, population):
    4626            setattr(ind, self.output_field, d)
    4727        return population
    48 
    49     def knn_mean(self, dissim_matrix,axis):
    50         return np.mean(np.partition(dissim_matrix, self.knn)[:,:self.knn],axis=axis)
  • framspy/evolalg/dissimilarity/levenshtein.py

    r1139 r1182  
    1818                gen_dis.append(lev.distance(p.genotype, p2.genotype))
    1919            dissim.append(gen_dis)
    20         dissim = self.reduce(dissim)
     20        dissim = self.reduce(dissim, self.fn_reduce, self.knn)
    2121        for d, ind in zip(dissim, population):
    2222            setattr(ind, self.output_field, d)
  • framspy/evolalg/examples/niching_novelty.py

    r1149 r1182  
    1111from evolalg.base.lambda_step import LambdaStep
    1212from evolalg.base.step import Step
     13from evolalg.dissimilarity.archive import ArchiveDissimilarity
    1314from evolalg.dissimilarity.frams_dissimilarity import FramsDissimilarity
    1415from evolalg.dissimilarity.levenshtein import LevenshteinDissimilarity
     
    5657        description='Run this program with "python -u %s" if you want to disable buffering of its output.' % sys.argv[
    5758            0])
    58     parser.add_argument('-path', type=ensureDir, required=True, help='Path to the Framsticks library without trailing slash.')
     59    parser.add_argument('-path', type=ensureDir, required=True,
     60                        help='Path to the Framsticks library without trailing slash.')
    5961    parser.add_argument('-opt', required=True,
    6062                        help='optimization criteria: vertpos, velocity, distance, vertvel, lifespan, numjoints, numparts, numneurons, numconnections (or other as long as it is provided by the .sim file and its .expdef). For multiple criteria optimization, see multicriteria.py.')
     
    6365    parser.add_argument('-genformat', required=False, default="1",
    6466                        help='Genetic format for the demo run, for example 4, 9, or B. If not given, f1 is assumed.')
    65     parser.add_argument('-sim', required=False, default="eval-allcriteria.sim", help="Name of the .sim file with all parameter values. If you want to provide more files, separate them with a semicolon ';'.")
     67    parser.add_argument('-sim', required=False, default="eval-allcriteria.sim",
     68                        help="Name of the .sim file with all parameter values. If you want to provide more files, separate them with a semicolon ';'.")
    6669    parser.add_argument('-fit', required=False, default=Fitness.raw, type=Fitness,
    6770                        help=' Fitness criteria, default: raw', choices=list(Fitness))
    6871    parser.add_argument('-dissim', required=False, type=Dissim, default=Dissim.frams,
    6972                        help='Dissimilarity measure, default: frams', choices=list(Dissim))
    70     parser.add_argument('-knn', type=int, help="'k' value for knn-based fitness criteria (knn-niching and knn-novelty).")
     73    parser.add_argument('-knn', type=int,
     74                        help="'k' value for knn-based fitness criteria (knn-niching and knn-novelty).")
    7175    parser.add_argument('-popsize', type=int, default=50, help="Population size, default: 50.")
    7276    parser.add_argument('-generations', type=int, default=5, help="Number of generations, default: 5.")
     
    7781    parser.add_argument('-max_numneurons', type=int, default=None, help="Maximum number of Neurons. Default: no limit")
    7882    parser.add_argument('-max_numconnections', type=int, default=None, help="Maximum number of Neural connections. Default: no limit")
    79 
     83    parser.add_argument('-max_numgenochars', type=int, default=10000, help="Maximum number of characters in genotype, to disable this option set it to -1. Default: 10 000")
    8084    parser.add_argument('-hof_size', type=int, default=10, help="Number of genotypes in Hall of Fame. Default: 10.")
    81     parser.add_argument('-hof_evaluations', type=int, default=20, help="Number of final evaluations of each genotype in Hall of Fame to obtain reliable (averaged) fitness. Default: 20.")
     85    parser.add_argument('-hof_evaluations', type=int, default=20,
     86                        help="Number of final evaluations of each genotype in Hall of Fame to obtain reliable (averaged) fitness. Default: 20.")
    8287    parser.add_argument('-checkpoint_path', required=False, default=None, help="Path to the checkpoint file")
    8388    parser.add_argument('-checkpoint_interval', required=False, type=int, default=100, help="Checkpoint interval")
    8489    parser.add_argument('-debug', dest='debug', action='store_true', help="Prints names of steps as they are executed")
     90    parser.add_argument('-archive_size', type=int, default=0, help="Size of the archive size for dissimilarity calculation")
    8591    parser.set_defaults(debug=False)
    8692    return parser.parse_args()
     
    131137        return individual.numconnections > self.max_number
    132138
     139class NumCharsHigher(Remove):
     140    def __init__(self, max_number):
     141        super(NumCharsHigher, self).__init__()
     142        self.max_number = max_number
     143
     144    def remove(self, individual):
     145        return len(individual.genotype) > self.max_number
    133146
    134147class ReplaceWithHallOfFame(Step):
     
    136149        super(ReplaceWithHallOfFame, self).__init__(*args, **kwargs)
    137150        self.hof = hof
     151
    138152    def call(self, population, *args, **kwargs):
    139153        super(ReplaceWithHallOfFame, self).call(population)
     
    166180    parsed_args = parseArguments()
    167181    frams_lib = FramsticksLib(parsed_args.path, parsed_args.lib,
    168                           parsed_args.sim.split(";"))
     182                              parsed_args.sim.split(";"))
    169183    # Steps for generating first population
    170184    init_stages = [
     
    187201
    188202    fitness_raw = FitnessStep(frams_lib, fields={parsed_args.opt: "fitness_raw",
    189                                              "numparts": "numparts",
    190                                              "numjoints": "numjoints",
    191                                              "numneurons": "numneurons",
    192                                              "numconnections": "numconnections"},
     203                                                 "numparts": "numparts",
     204                                                 "numjoints": "numjoints",
     205                                                 "numneurons": "numneurons",
     206                                                 "numconnections": "numconnections"},
    193207                              fields_defaults={parsed_args.opt: None, "numparts": float("inf"),
    194208                                               "numjoints": float("inf"), "numneurons": float("inf"),
    195209                                               "numconnections": float("inf")},
    196210                              evaluation_count=1)
    197 
    198211
    199212    fitness_end = FitnessStep(frams_lib, fields={parsed_args.opt: "fitness_raw"},
     
    213226    if parsed_args.max_numconnections is not None:
    214227        remove.append(NumConnectionsHigher(parsed_args.max_numconnections))
     228    if parsed_args.max_numgenochars is not -1:
     229        remove.append(NumCharsHigher(parsed_args.max_numgenochars))
    215230
    216231    remove_step = UnionStep(remove)
     
    245260        generation_modifications.append(raw)
    246261
    247     if parsed_args.fit == Fitness.niching: # TODO reduce redundancy in the four cases below: dictionary?
     262    if parsed_args.fit == Fitness.niching:  # TODO reduce redundancy in the four cases below: dictionary?
     263
    248264        niching = UnionStep([
    249             dissim,
     265            ArchiveDissimilarity(parsed_args.archive_size, dissim),
    250266            LambdaStep(func_niching)
    251267        ])
     
    255271    if parsed_args.fit == Fitness.novelty:
    256272        novelty = UnionStep([
    257             dissim,
     273            ArchiveDissimilarity(parsed_args.archive_size, dissim),
    258274            LambdaStep(func_novelty)
    259275        ])
    260276        init_stages.append(novelty)
    261277        generation_modifications.append(novelty)
    262    
     278
    263279    if parsed_args.fit == Fitness.knn_niching:
    264280        knn_niching = UnionStep([
    265             dissim,
     281            ArchiveDissimilarity(parsed_args.archive_size, dissim),
    266282            LambdaStep(func_knn_niching)
    267283        ])
    268284        init_stages.append(knn_niching)
    269285        generation_modifications.append(knn_niching)
    270    
     286
    271287    if parsed_args.fit == Fitness.knn_novelty:
    272288        knn_novelty = UnionStep([
    273             dissim,
     289            ArchiveDissimilarity(parsed_args.archive_size, dissim),
    274290            LambdaStep(func_knn_novelty)
    275291        ])
     
    302318        fitness_end,
    303319        PopulationSave("halloffame.gen", provider=hall_of_fame.halloffame, fields={"genotype": "genotype",
    304                                                                                   "fitness": "fitness_raw"})]
     320                                                                                   "fitness": "fitness_raw"})]
    305321    # ...but custom fields can be added, e.g. "custom": "recording"
    306322
    307323    # -------------------------------------------------
    308324
    309 
    310 
    311325    # Experiment creation
    312 
    313326
    314327    experiment = Experiment(init_population=init_stages,
     
    354367
    355368
    356 
    357369if __name__ == '__main__':
    358 
    359370    main()
  • framspy/run-evolalg-examples.cmd

    r1158 r1182  
    1616
    1717
    18 rem simple one-criterion evolution but more options available in examples.niching_novelty, here: hard limit on the number of parts and debugging messages
     18rem simple one-criterion evolution but more options available in examples.niching_novelty, here: hard limit on the number of Parts and debugging messages
    1919python -m evolalg.examples.niching_novelty   -path %DIR_WITH_FRAMS_LIBRARY%   -opt velocity   -max_numparts 6   -debug
    2020
Note: See TracChangeset for help on using the changeset viewer.