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.
Actually, you can use random.sample() inside DEAP. You just have to register the function and then pass it to the individual when registering it:
# Example of dataset (300 permutations of [0,1,...,99]
data_set = [random.sample(range(100), 100) for i in range(300)]
toolbox = base.Toolbox()
# The sampling is used to select one individual from the dataset
toolbox.register("random_sampling", random.sample, data_set, 1)
# An individual is generated by calling the function registered in
# random_sampling, with the input paramters given
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.random_sampling)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
Notice that each individual will be composed of a list containing the list of values (something like [[7, 40, 87, ...]]
. If you want to remove the outer list (to have [7, 40, 87, ...]
instead), you should replace random_sampling
by:
toolbox.register("random_sampling", lambda x,y: random.sample(x,y)[0], data_set, 1)