1

I have written my own function to generate an individual

def generate_Individual(arr1,arr2):
    np.random.shuffle(arr1)
    np.random.shuffle(arr2)
    Candidate = tuple(zip(arr1,arr2))
    return Candidate

def generate_Fitness(Individual):
    sum_some = 0
    for i in  range (0,len(Individual)):
        sum_some = sum_some + cals(Individual[i][0],Individual[i][1])
    return sum_some

This i am registering to the DEAP toolbox

import random
from deap import base
from deap import creator
from deap import tools
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("Individual", generate_Individual,arr1,arr2)
toolbox.register("population", tools.initRepeat, list, toolbox.Individual)

Now say i call a population of 4 with this code

pop = toolbox.population(n=4)
pop[0]
pop[3]

It turns out all 4 individuals in the population are the same even though randomness I built into the generator function

Why is this happening?

Leothorn
  • 1,345
  • 1
  • 23
  • 45

1 Answers1

0

If I use for instance

arr1=[1, 2, 3, 4]
arr2=[5, 6, 7, 8]

then with your code, the individuals in the population typically are different.

In fact, unless len(arr1)<=1 or len(arr2)<=1, you will get different individuals at least half the time. So if your individuals are the same, then either make longer lists for arr1 and arr2, or run pop = toolbox.population(n=4) a second time.

usernumber
  • 1,958
  • 1
  • 21
  • 58