jeudi 1 mars 2018

Custom representation of population with Python package deap

I am using Python package deap. My problem is to get my population from dataset insted of generate it from genes. For example: I have [[1,2,0,0,...],[1,3,4,0,...],...] as data set and I want to select random n elements from this data set to be my population. Here is the code of making Population of random binary numbers 0 or 1's and the vector is 100 in len:

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("attr_bool", random.randint, 0, 1)



toolbox.register("individual", tools.initRepeat, creator.Individual,
    toolbox.attr_bool, 100)

# define the population to be a list of individuals
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

Note that I could simply use random.sample(Data_set, Num_of_ind) to make my population but this will not work with deap package. I need a solution working with Deap package.




Aucun commentaire:

Enregistrer un commentaire