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

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

Location:
framspy/evolalg/dissimilarity
Files:
2 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)
  • framspy/evolalg/dissimilarity/frams_dissimilarity.py

    r1139 r1145  
    66from evolalg.dissimilarity.dissimilarity import Dissimilarity
    77
     8#TODO eliminate overlap with dissimilarity.py
     9
    810
    911class FramsDissimilarity(FramsStep):
    1012
    11     def __init__(self, frams_lib, reduction="mean", output_field="dissim", *args, **kwargs):
     13    def __init__(self, frams_lib, reduction="mean", output_field="dissim", knn=None, *args, **kwargs):
    1214        super(FramsDissimilarity, self).__init__(frams_lib, *args, **kwargs)
    1315
    1416        self.output_field = output_field
    1517        self.fn_reduce = None
     18        self.knn = knn
    1619        if reduction == "mean":
    1720            self.fn_reduce = np.mean
     
    2225        elif reduction == "sum":
    2326            self.fn_reduce = np.sum
     27        elif reduction == "knn_mean":
     28            self.fn_reduce = self.knn_mean
    2429        elif reduction == "none" or reduction is None:
    2530            self.fn_reduce = None
    2631        else:
    27             raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, none")
     32            raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, knn_mean, none")
    2833
    2934    def reduce(self, dissim_matrix):
     
    4146            setattr(ind, self.output_field, d)
    4247        return population
     48
     49    def knn_mean(self, dissim_matrix,axis):
     50        return np.mean(np.partition(dissim_matrix, self.knn)[:,:self.knn],axis=axis)
Note: See TracChangeset for help on using the changeset viewer.