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

Last change on this file since 1113 was 1113, checked in by Maciej Komosinski, 4 years ago

Added a framework for evolutionary algorithms cooperating with FramsticksLib?.py

File size: 804 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        if len(population) == 0:
12            return []
13        dissim = []
14        for i, p in enumerate(population):
15            gen_dis = []
16            for i2, p2 in enumerate(population):
17                gen_dis.append(lev.distance(p.genotype, p2.genotype))
18            dissim.append(gen_dis)
19        dissim = self.reduce(dissim)
20        for d, ind in zip(dissim, population):
21            setattr(ind, self.output_field, d)
22        return population
Note: See TracBrowser for help on using the repository browser.