Using an existing 2 dimensional numpy.ndarray as population

822 views
Skip to first unread message

GGowda

unread,
Nov 22, 2017, 9:17:34 AM11/22/17
to deap-users
Hello,
I am very new to DEAP and I am trying to use an existing numpy array as my population for the first guess. I did follow the procedure described on http://deap.gel.ulaval.ca/doc/0.9/tutorials/types.html#seeding-a-population . However, even after replacing the 'list' with 'numpy.ndarray' , it doesn't seem to consider the data in the .json file as the population input.  It would be great if somebody points me to the correct method of doing this or towards any documentation which might help.
I have included a sample of the code I am working with. 
Thanks!



from deap import base
from deap import creator
from deap import tools
import numpy
import json


creator.create("FitnessMulti",base.Fitness,weights=(-1.0,-1.0))
creator.create("Individual", numpy.ndarray, fitness=creator.FitnessMulti)

def initIndividual(icls, content):
    return icls(content)
def initPopulation(pcls, ind_init, filename):
    contents = json.load(open(filename, "r"))
    return pcls(ind_init(c) for c in contents)

toolbox = base.Toolbox()
toolbox.register("population_guess", initPopulation, numpy.ndarray, creator.Individual, "data.json")


Error message: 
TypeError: expected sequence object with len >= 0 or a single integer

GGowda

unread,
Nov 22, 2017, 11:39:17 AM11/22/17
to deap-users
I tried to read the initial guess from an array. But the output from population, when specified as 'numpy.ndarray', I still get an error. I have attached the code below.

## 
import random
from deap import base
from deap import creator
from deap import tools
import numpy
import Function_Optimisation2
from functools import wraps
# Counting the number of times the function is called #
def counter(func):
    @wraps(func)
    def tmp(*args, **kwargs):
        tmp.count += 1
        return func(*args, **kwargs)
    tmp.count = 0
    return tmp
creator.create("FitnessMulti", base.Fitness, weights=(-1.0,-1.0))
creator.create("Individual",numpy.ndarray , fitness=creator.FitnessMulti)


# Simple Example #i
C = numpy.array([[1.0, 2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0], [3.0, 4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0]])
@counter
def cp():
        i = cp.count - 1
        CP = C[i,:]
        return(CP)

toolbox = base.Toolbox()
toolbox.register("attr_bool", cp)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, 1)
toolbox.register("population", tools.initRepeat, numpy.ndarray, toolbox.individual)
pop = toolbox.population(n=2)
# Evaluation Function #
pop.fitness.values = Function_Optimisation2.Function_optimisation(pop)

I still get the same error. I want to mention that I am trying to evaluate the array C row-wise as each of the 13 columns correspond to my variables which are the input to my evaluation function.

Any leads to solving this error is appreciated. 
Thanks!

Manuel Belmadani

unread,
Nov 23, 2017, 2:12:44 PM11/23/17
to deap-users
I think the issue is the way the population of type numpy.darray is being initialized.

toolbox.register("population", tools.initRepeat, numpy.ndarray, toolbox.individual)

This block of code works if you switch for list instead:

# Simple Example #i                                                                                                                                                                                                                            

@counter
def cp():
    i
= cp.count - 1
    CP
= C[i,:]

   
#CP = C[0][i,:]                                                                                                                                                                                                                            
   
return(CP)


C
= numpy.array([ [1.0, 2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0], [3.0, 4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0] ]) # 1 x 2D-List                                                                                    
IND_LENGTH
=1



creator
.create("FitnessMulti", base.Fitness, weights=(-1.0,-1.0))
creator
.create("Individual",numpy.ndarray , fitness=creator.FitnessMulti)



toolbox
= base.Toolbox()
toolbox
.register("attr_bool", cp)
toolbox
.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, IND_LENGTH)
toolbox
.register("population", tools.initRepeat, list, toolbox.individual)
pop
= toolbox.population(n=2)


print "Population:", pop
print ""


i
=0
for ind in pop:
   
print "Ind #"+str(i),":", ind
    i
+=1


# Evaluation Function #                                                                                                                                                                                                                        
## pop.fitness.values = Function_Optimisation2.Function_optimisation(pop)    
$ python arrdeap2.py
Population: [Individual([[  1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,
             
11.,  12.,  13.]]), Individual([[  3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.,  11.,  12.,
             
13.,  14.,  15.]])]


Ind #0 : [[  1.   2.   3.   4.   5.   6.   7.   8.   9.  10.  11.  12.  13.]]
Ind #1 : [[  3.   4.   5.   6.   7.   8.   9.  10.  11.  12.  13.  14.  15.]]

I'm not sure what's the best practice to have an individuals as 2D numpy arrays (versus this list of 1D numpy arrays). This older thread seems relevant: https://groups.google.com/forum/#!searchin/deap-users/numpy|sort:date/deap-users/dwRPLOsh_6Y/zN-5KZFNAwAJ

GGowda

unread,
Nov 25, 2017, 7:44:21 AM11/25/17
to deap-users
Hello Manuel Belmadani,
Thank you for the answer. I changed my code like you suggested. However, after evaluation, I still get the error message   AttributeError: 'list' object has no attribute 'fitness'  My evaluation function returns a tuple everytime it is called. I am not sure how to proceed. 

François-Michel De Rainville

unread,
Nov 25, 2017, 9:15:00 AM11/25/17
to deap-users
The population has no fitness, each individual does.

--
You received this message because you are subscribed to the Google Groups "deap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to deap-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

GGowda

unread,
Nov 28, 2017, 6:45:55 AM11/28/17
to deap-users
Hello François-Michel De Rainville,
Thank you for the answer. I managed to make the loop work. 
To unsubscribe from this group and stop receiving emails from it, send an email to deap-users+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages