Skip to content

[Suggestion] Custom duplicate detection #2

@FontaineRiant

Description

@FontaineRiant

Hello, thanks for your work on this library!

In the problem I need to solve, some parts of individuals (the last 2 values for example) can be permuted and still represent the same solution, so [1, 2, 3] and [1, 3, 2] have to be treated as duplicates.

Currently, GeneticalAlgorithm2's without_dup() is an inner function of a method, so it can't be changed easily as it would have if it could be inherited.

My workaround for now is copy-pasting the whole run() function into a new subclass of GeneticAlgorithm2, which is far from ideal, then redefine without_dup as:

def without_dup(pop: np.ndarray, scores: np.ndarray):
    # individuals are duplicates if the only differences are permutations within the first 6 or the next 2 values
    _, index_of_dups = np.unique(np.concatenate((
    np.sort(pop[:, :6]),
    np.sort(pop[:, 6:8]),
    pop[:, 8:]),
    axis=1), axis=0, return_index=True)
    return pop[index_of_dups], scores[index_of_dups], scores.size - index_of_dups.size

or

def without_dup(pop: np.ndarray, scores: np.ndarray):
    # it's almost impossible in my problem for 2 different solutions to have the same score, so we get the indices of the unique scores instead
    _ , index_of_dups = np.unique(scores, return_index=True)
    return pop[index_of_dups], scores[index_of_dups], scores.size - index_of_dups.size

It would be cleaner to have without_dup() be a class function (that can be redefined by inheritance), or an optional parameter for run().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions