Esempio n. 1
0
def init_population(pop_size, bounds, mode="minimization"):
    """
        initialize non-zero positions and interaction matrix
    :param pop_size: population size
    :param bounds: upper and lower bounds
    :param mode: whether minimization or maximization
    :return:
    """
    population = Population(pop_size, mode)
    vec_len = len(bounds)
    half = vec_len // 2
    for i in range(pop_size):
        vector = []
        # for defining interaction structure
        for j in range(half):
            if random.uniform(bounds[j][0], bounds[j][1]) < 0.5:
                vector.append(0)
            else:
                vector.append(1)
        # for defining interactions
        for j in range(half):
            vector.append(
                random.uniform(bounds[half + j][0], bounds[half + j][1]))
        population.add_individual(Individual(vector))
    return population
Esempio n. 2
0
def uniform_random(pop_size, bounds, mode="minimization"):
    population = Population(pop_size, mode)
    for i in range(pop_size):
        vector = []
        for j in range(len(bounds)):
            vector.append(random.uniform(bounds[j][0], bounds[j][1]))
        population.add_individual(Individual(vector))
    return population