def test_init_random_population_real(optim, type):
    size, dim, interval = 9, 1, (-5, 5)

    if type == 'real':
        ga = RealGA(fitness_test_linear_func, optim=optim)
        dga = DiffusionGA(ga)

        dga.init_random_population(size, dim, interval)
    else:
        ga = BinaryGA(test_bin_data, fitness_test_func, optim=optim)
        dga = DiffusionGA(ga)
        dga.init_random_population(size)

    assert dga.population[0].size == size
    assert dga.population[1].size == size

    shape = dga.population[0].shape
    if optim == 'max':
        for row in range(shape[0]):
            for column in range(shape[1]):
                assert dga.population[1][row][column] <= dga.best_solution[1]
    else:
        for row in range(shape[0]):
            for column in range(shape[1]):
                assert dga.population[1][row][column] >= dga.best_solution[1]
def test_valid_run(optim):
    """
    "Run" function is the same for both types of diffusion GA: RealGA and Binary GA and thus,
    it is not necessary to test it twice.
    """
    ga = RealGA(fitness_test_sin_func, optim=optim)
    dga = DiffusionGA(ga)
    dga.init_random_population(11, 1, (-5, 5))  # size is 11 but actually wil be 9 (truncated square root from 11)

    init_best = dga.best_solution

    generations = 10
    fitness_progress = dga.run(generations)

    assert len(fitness_progress) == generations + 1

    if optim == 'min':
        assert init_best[1] >= dga.best_solution[1]
    else:
        assert init_best[1] <= dga.best_solution[1]