source: framspy/evolalg/dissimilarity/levenshtein.py @ 1182

Last change on this file since 1182 was 1182, checked in by Maciej Komosinski, 19 months ago

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

File size: 893 bytes
Line 
1import Levenshtein as lev
2
3from evolalg.dissimilarity.dissimilarity import Dissimilarity
4
5
6class LevenshteinDissimilarity(Dissimilarity):
7    def __init__(self, reduction="mean", output_field="dissim", *args, **kwargs):
8        super(LevenshteinDissimilarity, self).__init__(reduction, output_field, *args, **kwargs)
9
10    def call(self, population):
11        super(LevenshteinDissimilarity, self).call(population)
12        if len(population) == 0:
13            return []
14        dissim = []
15        for i, p in enumerate(population):
16            gen_dis = []
17            for i2, p2 in enumerate(population):
18                gen_dis.append(lev.distance(p.genotype, p2.genotype))
19            dissim.append(gen_dis)
20        dissim = self.reduce(dissim, self.fn_reduce, self.knn)
21        for d, ind in zip(dissim, population):
22            setattr(ind, self.output_field, d)
23        return population
Note: See TracBrowser for help on using the repository browser.