2

I am employing genetic algorithm through DEAP package in python. In this process, I would like to set different upper and lower bounds to each gene in the individual.

I would like to know how can one do that? For more illustration, Suppose my individual has two genes then I would like to give the following bounds

enter image description here

gene1: should be bounded to 0,1 gene2: should be bounded to 2,3

1 Answers1

1

For the creation of a bounded initial population, see this StackOverflow question.

For enforcing bounds in subsequent generations, see this DEAP documentation example.

Xukrao
  • 8,003
  • 5
  • 26
  • 52
  • Thank you for the prompt reply. However, i have one query. Rather than using the decorator, if after the generation of an offspring, i manually check for the upper and lower bound of each gene in the offspring and limit it within the bounds then would it come in the way of the algorithm? – linearprogrammer Dec 13 '18 at 10:21
  • The code: `offspring = toolbox.select(pop, len(pop)) # Clone the selected individuals offspring = list(map(toolbox.clone, offspring)) for child in offspring: for i in range(len(child)): if child[i] > dic[i][1]: child[i] = random.randint(dic[i][0],dic[i][1]) elif child[i] < dic[i][0]: child[i] = random.randint(dic[i][0],dic[i][1])` Would this code come in the way of the randomness of the algorithm? – linearprogrammer Dec 13 '18 at 10:25