source: framspy/evolalg/dissimilarity/dissimilarity.py @ 1145

Last change on this file since 1145 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
RevLine 
[1113]1from abc import ABC
2
3from evolalg.base.step import Step
4import numpy as np
5
6
7class Dissimilarity(Step, ABC):
8
[1145]9    def __init__(self, reduction="mean", output_field="dissim", knn=None, *args, **kwargs):
[1113]10        super(Dissimilarity, self).__init__(*args, **kwargs)
11
12        self.output_field = output_field
13        self.fn_reduce = None
[1145]14        self.knn = knn
15        if reduction == "mean": # TODO change this 'elif' sequence to dictionary?
[1113]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
[1145]23        elif reduction == "knn_mean":
24            self.fn_reduce = self.knn_mean
25        elif reduction == "none" or reduction is None:
[1113]26            self.fn_reduce = None
27        else:
[1145]28            raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, knn_mean, none")
[1113]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)
[1145]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.