Exemplo n.º 1
0
def test_update_fit_history(
    size,
    row_limits,
    col_limits,
    distributions,
    weights,
    max_iter,
    best_prop,
    lucky_prop,
    crossover_prob,
    mutation_prob,
    shrinkage,
    maximise,
):
    """ Test that the DataOptimiser can update its fitness history. """

    families = [edo.Family(dist) for dist in distributions]

    do = DataOptimiser(
        trivial_fitness,
        size,
        row_limits,
        col_limits,
        families,
        weights,
        max_iter,
        best_prop,
        lucky_prop,
        crossover_prob,
        mutation_prob,
        shrinkage,
        maximise,
    )

    do.random_state = np.random.RandomState(size)
    do._initialise_run(4)
    do._update_fit_history()
    fit_history = do.fit_history
    assert fit_history.shape == (size, 3)
    assert list(fit_history.columns) == ["fitness", "generation", "individual"]
    assert list(fit_history["fitness"].values) == do.pop_fitness
    assert list(fit_history["generation"].unique()) == [0]
    assert list(fit_history["individual"]) == list(range(size))

    do.generation += 1
    do._update_fit_history()
    fit_history = do.fit_history
    assert fit_history.shape == (size * 2, 3)
    assert list(fit_history["fitness"].values) == do.pop_fitness * 2
    assert list(fit_history["generation"].unique()) == [0, 1]
    assert list(fit_history["individual"]) == list(range(size)) * 2
Exemplo n.º 2
0
def test_get_fit_history(
    size,
    row_limits,
    col_limits,
    distributions,
    weights,
    max_iter,
    best_prop,
    lucky_prop,
    crossover_prob,
    mutation_prob,
    shrinkage,
    maximise,
):
    """ Test that the DataOptimiser can get the fitness hsitory on disk. """

    families = [edo.Family(dist) for dist in distributions]

    do = DataOptimiser(
        trivial_fitness,
        size,
        row_limits,
        col_limits,
        families,
        weights,
        max_iter,
        best_prop,
        lucky_prop,
        crossover_prob,
        mutation_prob,
        shrinkage,
        maximise,
    )

    do.random_state = np.random.RandomState(size)
    do._initialise_run(4)
    do._write_generation(root=".testcache")

    fit_history = _get_fit_history(".testcache")
    assert isinstance(fit_history, dd.DataFrame)
    assert list(fit_history.columns) == ["fitness", "generation", "individual"]
    assert list(fit_history["fitness"].compute()) == do.pop_fitness
    assert list(fit_history["generation"].unique().compute()) == [0]
    assert list(fit_history["individual"].compute()) == list(range(size))

    os.system("rm -r .testcache")
Exemplo n.º 3
0
def test_update_subtypes(
    size,
    row_limits,
    col_limits,
    distributions,
    weights,
    max_iter,
    best_prop,
    lucky_prop,
    crossover_prob,
    mutation_prob,
    shrinkage,
    maximise,
):
    """ Test that the DataOptimiser can update the subtypes present. """

    families = [edo.Family(dist) for dist in distributions]

    do = DataOptimiser(
        trivial_fitness,
        size,
        row_limits,
        col_limits,
        families,
        weights,
        max_iter,
        best_prop,
        lucky_prop,
        crossover_prob,
        mutation_prob,
        shrinkage,
        maximise,
    )

    do.random_state = np.random.RandomState(size)
    do._initialise_run(4)
    parents = do.population[:max(int(size / 5), 1)]
    parent_subtypes = do._get_current_subtypes(parents)

    do._update_subtypes(parents)
    updated_subtypes = {
        family: list(family.subtypes.keys())
        for family in parent_subtypes
    }

    assert parent_subtypes == updated_subtypes
Exemplo n.º 4
0
def test_get_next_generation(
    size,
    row_limits,
    col_limits,
    distributions,
    weights,
    max_iter,
    best_prop,
    lucky_prop,
    crossover_prob,
    mutation_prob,
    shrinkage,
    maximise,
):
    """ Test that the EA can find the next generation. """

    families = [edo.Family(dist) for dist in distributions]

    do = DataOptimiser(
        trivial_fitness,
        size,
        row_limits,
        col_limits,
        families,
        weights,
        max_iter,
        best_prop,
        lucky_prop,
        crossover_prob,
        mutation_prob,
        shrinkage,
        maximise,
    )

    do.random_state = np.random.RandomState(size)
    do._initialise_run(None)
    do._get_next_generation(None)
    assert isinstance(do.population, list)
    assert len(do.population) == len(do.pop_fitness)
    assert len(do.population) == size

    for individual, fitness in zip(do.population, do.pop_fitness):
        assert isinstance(individual, Individual)
        assert isinstance(fitness, float)
Exemplo n.º 5
0
def test_update_pop_history(
    size,
    row_limits,
    col_limits,
    distributions,
    weights,
    max_iter,
    best_prop,
    lucky_prop,
    crossover_prob,
    mutation_prob,
    shrinkage,
    maximise,
):
    """ Test that the DataOptimiser can update its population history. """

    families = [edo.Family(dist) for dist in distributions]

    do = DataOptimiser(
        trivial_fitness,
        size,
        row_limits,
        col_limits,
        families,
        weights,
        max_iter,
        best_prop,
        lucky_prop,
        crossover_prob,
        mutation_prob,
        shrinkage,
        maximise,
    )

    do.random_state = np.random.RandomState(size)
    do._initialise_run(4)
    do._update_pop_history()
    assert len(do.pop_history) == 1
    assert len(do.pop_history[0]) == size
    for i, individual in enumerate(do.population):
        hist_ind = do.pop_history[0][i]
        assert hist_ind.dataframe.equals(individual.dataframe)
        assert hist_ind.metadata == individual.metadata
Exemplo n.º 6
0
def test_get_pop_history(
    size,
    row_limits,
    col_limits,
    distributions,
    weights,
    max_iter,
    best_prop,
    lucky_prop,
    crossover_prob,
    mutation_prob,
    shrinkage,
    maximise,
):
    """ Test that the DataOptimiser can get the population history on disk. """

    families = [edo.Family(dist) for dist in distributions]

    do = DataOptimiser(
        trivial_fitness,
        size,
        row_limits,
        col_limits,
        families,
        weights,
        max_iter,
        best_prop,
        lucky_prop,
        crossover_prob,
        mutation_prob,
        shrinkage,
        maximise,
    )

    do.random_state = np.random.RandomState(size)
    do._initialise_run(4)
    do._write_generation(root=".testcache")

    pop_history = _get_pop_history(".testcache", 1, distributions)
    assert isinstance(pop_history, list)
    for generation in pop_history:

        assert isinstance(generation, list)
        for i, individual in enumerate(generation):

            pop_ind = do.population[i]
            assert isinstance(individual, Individual)
            assert isinstance(individual.dataframe, dd.DataFrame)
            assert isinstance(individual.metadata, list)

            assert np.allclose(pop_ind.dataframe.values,
                               individual.dataframe.values.compute())

            for ind_meta, pop_ind_meta in zip(individual.metadata,
                                              pop_ind.metadata):
                assert ind_meta.family.name == pop_ind_meta.family.name
                assert (ind_meta.family.distribution is
                        pop_ind_meta.family.distribution)
                assert ind_meta.to_dict() == pop_ind_meta.to_dict()

    os.system("rm -r .testcache")
Exemplo n.º 7
0
def test_write_generation(
    size,
    row_limits,
    col_limits,
    distributions,
    weights,
    max_iter,
    best_prop,
    lucky_prop,
    crossover_prob,
    mutation_prob,
    shrinkage,
    maximise,
):
    """Test that the DataOptimiser can write a generation and its fitness to
    file with a single core."""

    families = [edo.Family(dist) for dist in distributions]

    do = DataOptimiser(
        trivial_fitness,
        size,
        row_limits,
        col_limits,
        families,
        weights,
        max_iter,
        best_prop,
        lucky_prop,
        crossover_prob,
        mutation_prob,
        shrinkage,
        maximise,
    )

    do.random_state = np.random.RandomState(size)
    do._initialise_run(4)
    do._write_generation(root=".testcache")
    path = Path(".testcache")

    assert (path / "fitness.csv").exists()
    fit = pd.read_csv(path / "fitness.csv")
    assert list(fit.columns) == ["fitness", "generation", "individual"]
    assert list(fit.dtypes) == [float, int, int]
    assert list(fit["generation"].unique()) == [0]
    assert list(fit["individual"]) == list(range(size))
    assert np.allclose(fit["fitness"].values, do.pop_fitness)

    path /= "0"
    for i, ind in enumerate(do.population):
        ind_path = path / str(i)
        assert (ind_path / "main.csv").exists()
        assert (ind_path / "main.meta").exists()

        df = pd.read_csv(ind_path / "main.csv")
        with open(ind_path / "main.meta", "r") as meta_file:
            meta = yaml.load(meta_file, Loader=yaml.FullLoader)

        assert np.allclose(df.values, ind.dataframe.values)
        assert meta == [m.to_dict() for m in ind.metadata]

    os.system("rm -r .testcache")