From the few lines of code you sent us, I think you might want to have
a look at this tutorial
(http://deap.gel.ulaval.ca/doc/0.8-dev/tutorials/types.html#a-funky-one)
that creates an individual cycling with floats and integers.
To know exactly what the toolbox contains at anytime you can print its
content with the "dir" function:
toolbox.register("attr_int_material", random.randint, 0, 3)
toolbox.register("attr_bool_method", random.randint, 0, 1)
print(dir(toolbox))
This shall show you that both methods are registered under there given
names (None overwrote the other). You then need to use them both for
initialization (with initCycle) of your individuals.
I hope I answered your question. If not, I would need a little bit more
code and context to know exactly what you want to accomplish. (Consider
using http://pastebin.com/ or http://gist.github.com/ to send more code.)
Thanks for using DEAP,
Fran�ois-Michel
Le 12-03-23 09:08, Cristina a �crit :
> Hi all,
>
> I'm not sure how I can associate multiple atributes to an individual.
>
> For instance, I'd like an individual with a boolean and an integer
> attribute, so I did the following:
> toolbox.register("attr_int_material", random.randint, 0, 3)
> toolbox.register("attr_bool_method", random.randint, 0, 1)
>
> But I've noticed that the last register() overwrites the previous
> attribute, so the individual ends up having only the attr_bool_method
> attribute.
>
> Is this not possible at all, or am I not doing something wrong? If
> it's not possible, I will concatenate my attributes and convert them
> into a binary string representation, but I'd like to be sure that DEAP
> doesn't support multiple attributes.
>
> Thanks!
> --Cristina.
Traceback (most recent call last):
File "pastebin_example.py", line 104, in <module>
main()
File "pastebin_example.py", line 89, in main
pop = toolbox.population(n=MU)
File "/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/deap/tools.py", line 64, in initRepeat
return container(func() for _ in xrange(n))
File "/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/deap/tools.py", line 64, in <genexpr>
return container(func() for _ in xrange(n))
TypeError: initCycle() takes at least 2 arguments (2 given)
from functools import partial
fitnesses = toolbox.map(partial(toolbox.evaluate, data_dictionary, variable_1, variable_2, variable_3), invalid_ind)
Traceback (most recent call last):
File "es-zelluf_5.py", line 262, in <module>
main()
File "es-zelluf_5.py", line 202, in main
ind.fitness.values = fit
File "/usr/local/lib/python2.7/dist-packages/deap/base.py", line 174, in setValues
self.wvalues = tuple(map(operator.mul, values, self.weights))
TypeError: Both weights and assigned values must be a sequence of numbers when assigning to values of <class 'deap.creator.FitnessMax'>.
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax, strategy=None)
Thanks for the elaborate answer again. Since I will be using both floats and integers in every individual the array is therefore unfortunately not possible to use for me. However, optimisation is for future consideration anyway and lists perform fine for now.
Thanks for the information on partial as well. It immediately helped me in passing more arguments in a map() function using something like this:from functools import partial
fitnesses = toolbox.map(partial(toolbox.evaluate, data_dictionary, variable_1, variable_2, variable_3), invalid_ind)
So I worked further on my system but I now ran into a new problem involving the base.py file. I ran my system and I now get an error saying the following:Traceback (most recent call last):
File "es-zelluf_5.py", line 262, in <module>
main()
File "es-zelluf_5.py", line 202, in main
ind.fitness.values = fit
File "/usr/local/lib/python2.7/dist-packages/deap/base.py", line 174, in setValues
self.wvalues = tuple(map(operator.mul, values, self.weights))
TypeError: Both weights and assigned values must be a sequence of numbers when assigning to values of <class 'deap.creator.FitnessMax'>.
So I checked my creator.fitnessMax, which is defined like this:creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax, strategy=None)
As far as I understand I absolutely needed the comma after the 1.0 weight in order to make it iterable (i.e. "a sequence of numbers", even though it is only one number). But it apparently still isn't working. So I checked the base.py file but found nothing usefull (i.e. nothing that I could comprehend as being usefull). I did find a line (134) in which weights is set to None, but I guess this is done for some reason or another?
ps. I also did some tests with both DTM and the multiprocessing module one my simple quadcore machine (no cluster) and found that the multiprocessing module performs significantly better than the DTM-module. Do I understand it correctly if I say that the multiprocessing module can better be used when running on a single machine, and the DTM can better be used when running on a cluster (since mpi4py needs to be used in order to send the pickled streams around)?