5

I am trying to use DEAP to maximise a function.

I understand how to do it with the basic example:

toolbox.register("attr_bool", random.randint, 0, 1)
toolbox.register("individual", tools.initRepeat, creator.Individual, 
    toolbox.attr_bool, 100)

which creates 100 random values or either 0 or 1. You then go on and create a population and mutate ...

How do you build a population when you have for example two parameters:

parameter 1 integer with range [0,1] 
parameter 2 float with range [0,2]

Then create an individual combining both randomly sampled parameters? or for parameter 2 sample with an arbitary step value , for example 0.25.

azuric
  • 2,679
  • 7
  • 29
  • 44

1 Answers1

4

You can simply do as follows to create chromosomes of multiple types:

toolbox.register("attr_int", random.randint, 0, 1)
toolbox.register("attr_flt", random.uniform, 0, 2)
toolbox.register("individual", tools.initCycle, creator.Individual,
             (toolbox.attr_int, toolbox.attr_flt),
             n=1)

and then create a population of size 100:

toolbox.register("population", tools.initRepeat, list, toolbox.individual)
population = toolbox.population(n=100)
Saeide
  • 149
  • 2
  • 14
  • would you mind giving a complete example? i.e. how do I call this type of population in my fitness function and apply DEAP for a minimization problem? Thanks! – Rock Mar 22 '18 at 00:01
  • 1
    @Rock. You can check DEAP documentation. This [link](http://deap.readthedocs.io/en/master/examples/index.html) contains examples that might help you. If you don't find the answer, please tell me. Good Luck :) – Saeide Mar 28 '18 at 06:58
  • Thank you. I'll go through the examples. – Rock Apr 08 '18 at 05:55
  • Thank you for your answer. Given that we are at this point, what mutation mechanism should be used for such cases? cxOrdered seems to not be able to work and gives me an error. Do you have any ideas? – dimrizo Jan 24 '19 at 19:14