Last change
on this file since 1163 was
1145,
checked in by Maciej Komosinski, 3 years ago
|
Added niching and novelty search with limited (i.e., local) competition ("nearest neighbors" according to dissimilarity measure)
|
File size:
1.2 KB
|
Line | |
---|
1 | from abc import ABC |
---|
2 | |
---|
3 | from evolalg.base.step import Step |
---|
4 | import numpy as np |
---|
5 | |
---|
6 | |
---|
7 | class Dissimilarity(Step, ABC): |
---|
8 | |
---|
9 | def __init__(self, reduction="mean", output_field="dissim", knn=None, *args, **kwargs): |
---|
10 | super(Dissimilarity, self).__init__(*args, **kwargs) |
---|
11 | |
---|
12 | self.output_field = output_field |
---|
13 | self.fn_reduce = None |
---|
14 | 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 |
---|
27 | else: |
---|
28 | raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, knn_mean, none") |
---|
29 | |
---|
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) |
---|
34 | |
---|
35 | def knn_mean(self, dissim_matrix,axis): |
---|
36 | return np.mean(np.partition(dissim_matrix, self.knn)[:,:self.knn],axis=axis) |
---|
Note: See
TracBrowser
for help on using the repository browser.