Line | |
---|
1 | import Levenshtein as lev |
---|
2 | |
---|
3 | from evolalg.dissimilarity.dissimilarity import Dissimilarity |
---|
4 | |
---|
5 | |
---|
6 | class 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.