Ignore:
Timestamp:
07/07/21 14:38:57 (15 months ago)
Author:
Maciej Komosinski
Message:

Added niching and novelty search with limited (i.e., local) competition ("nearest neighbors" according to dissimilarity measure)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • framspy/evolalg/dissimilarity/dissimilarity.py

    r1113 r1145  
    77class Dissimilarity(Step, ABC):
    88
    9     def __init__(self, reduction="mean", output_field="dissim", *args, **kwargs):
     9    def __init__(self, reduction="mean", output_field="dissim", knn=None, *args, **kwargs):
    1010        super(Dissimilarity, self).__init__(*args, **kwargs)
    1111
    1212        self.output_field = output_field
    1313        self.fn_reduce = None
    14         if reduction == "mean":
     14        self.knn = knn
     15        if reduction == "mean": # TODO change this 'elif' sequence to dictionary?
    1516            self.fn_reduce = np.mean
    1617        elif reduction == "max":
     
    2021        elif reduction == "sum":
    2122            self.fn_reduce = np.sum
    22         elif reduction == "none" or reduction == None:
     23        elif reduction == "knn_mean":
     24            self.fn_reduce = self.knn_mean
     25        elif reduction == "none" or reduction is None:
    2326            self.fn_reduce = None
    2427        else:
    25             raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, none")
     28            raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, knn_mean, none")
    2629
    2730    def reduce(self, dissim_matrix):
     
    2932            return dissim_matrix
    3033        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 TracChangeset for help on using the changeset viewer.