Esempio n. 1
0
def test_pareto_front_clear():
    """ Tests that the calling clear empties the Pareto front. """
    pf = ParetoFront([(1, 2), (2, 1)])
    assert list(pf) == [(1, 2), (2, 1)]

    pf.clear()
    assert list(pf) == []
Esempio n. 2
0
def test_pareto_update_unique():
    """ Test creating Pareto front by updating one by one. """
    list_ = [(1, 2, 3), (3, 2, 1), (0, 5, 0)]
    pf = ParetoFront()

    for i in range(len(list_)):
        pf.update(list_[i])
        assert list(pf) == list_[:i + 1]
Esempio n. 3
0
def select_from_pareto(population, select_n, pareto_fronts_n):
    pareto_fronts = []
    population_left = population
    while len(pareto_fronts) != pareto_fronts_n:
        pareto_fronts.append(
            ParetoFront(start_list=population_left,
                        get_values_fn=lambda ind: ind.fitness.values))
        population_left = [
            ind for ind in population_left if ind not in pareto_fronts[-1]
        ]
        if len(population_left) == 0 and len(pareto_fronts) != pareto_fronts_n:
            # log.debug("Desired amount of pareto fronts could not be constructed.")
            break

    selected_individuals = []
    for _ in range(select_n):
        if len(pareto_fronts) == 1:
            selected_front = pareto_fronts[0]
        elif len(pareto_fronts) == 2:
            selected_front = numpy.random.choice(pareto_fronts,
                                                 p=[2 / 3, 1 / 3])
        elif len(pareto_fronts) > 2:
            n = len(pareto_fronts)
            selected_front = numpy.random.choice(
                pareto_fronts,
                p=[1 / 2**i for i in range(1, n)] + [1 / 2**(n - 1)])
        else:
            raise RuntimeError("Did not create any pareto front.")

        selected_individuals.append(random.choice(selected_front))
    return selected_individuals
Esempio n. 4
0
def test_pareto_initialization_empty():
    """ Test initialization of empty front. """
    pf = ParetoFront()

    assert list(pf) == []
    assert str(pf) == '[]'
    assert repr(pf) == 'ParetoFront([])'
Esempio n. 5
0
def test_pareto_initialization_with_duplicates():
    """ Test initialization with duplicate elements in list. """
    list_ = [(1, 2), (3, 1), (1, 2)]
    pf = ParetoFront(list_)

    assert list(pf) == [(1, 2), (3, 1)]
    assert str(pf) == '[(1, 2), (3, 1)]'
    assert repr(pf) == 'ParetoFront([(1, 2), (3, 1)])'
Esempio n. 6
0
def test_pareto_initialization_with_inferiors():
    """" Test initialization with a list containing elements that should not be in the Pareto front. """
    list_ = [(1, 2), (4, 3), (4, 5), (5, 4)]
    pf = ParetoFront(list_)

    assert list(pf) == [(4, 5), (5, 4)]
    assert str(pf) == '[(4, 5), (5, 4)]'
    assert repr(pf) == 'ParetoFront([(4, 5), (5, 4)])'
Esempio n. 7
0
def test_pareto_initialization_pareto_front():
    """ Test initialization with a list which only has Pareto front elements. """
    list_ = [(1, 2, 3), (3, 2, 1), (0, 5, 0)]
    pf = ParetoFront(list_)

    assert list(pf) == [(1, 2, 3), (3, 2, 1), (0, 5, 0)]
    assert str(pf) == '[(1, 2, 3), (3, 2, 1), (0, 5, 0)]'
    assert repr(pf) == 'ParetoFront([(1, 2, 3), (3, 2, 1), (0, 5, 0)])'
Esempio n. 8
0
def test_pareto_initialization_with_duplicates():
    """ Initialization with duplicate elements. """
    list_ = [(1, 2), (3, 1), (1, 2)]
    pf = ParetoFront(list_)

    assert list(pf) == [(1, 2), (3, 1)]
    assert str(pf) == "[(1, 2), (3, 1)]"
    assert repr(pf) == "ParetoFront([(1, 2), (3, 1)])"
Esempio n. 9
0
def test_pareto_initialization_pareto_front():
    """ Initialization with only Pareto front elements. """
    list_ = [(1, 2, 3), (3, 2, 1), (0, 5, 0)]
    pf = ParetoFront(list_)

    assert list(pf) == [(1, 2, 3), (3, 2, 1), (0, 5, 0)]
    assert str(pf) == "[(1, 2, 3), (3, 2, 1), (0, 5, 0)]"
    assert repr(pf) == "ParetoFront([(1, 2, 3), (3, 2, 1), (0, 5, 0)])"
Esempio n. 10
0
def eliminate_from_pareto(pop, n):
    # For now we only eliminate one at a time so this will do.
    if n != 1:
        raise NotImplemented("Currently only n=1 is supported.")

    def inverse_fitness(ind):
        return [-value for value in ind.fitness.values]

    pareto_worst = ParetoFront(pop, inverse_fitness)
    return [random.choice(pareto_worst)]
Esempio n. 11
0
def test_pareto_front_custom_function():
    """ Test construction of Pareto front with custom object and value function. """
    class A:
        def __init__(self, v1, v2):
            self.v1 = v1
            self.v2 = v2

    item1, item2, item3 = A(1, 2), A(2, 1), A(3, 1)
    pf = ParetoFront(get_values_fn=lambda x: (x.v1, x.v2))

    pf.update(item1)
    assert list(pf) == [item1]

    pf.update(item2)
    assert list(pf) == [item1, item2]

    pf.update(item3)
    assert list(pf) == [item1, item3]