Last change
on this file since 1312 was
1185,
checked in by Maciej Komosinski, 2 years ago
|
Renamed a module; new name is "evolalg_steps"
|
File size:
899 bytes
|
Line | |
---|
1 | import Levenshtein as lev |
---|
2 | |
---|
3 | from evolalg_steps.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 | 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.