2

My GA individual is a random sequence of int (eg: [4, 5, 10, 11, 8, 12, 9, 13, 2, 6, 3, 7, 0, 14, 15, 1]) that follows certain order constraint which is dealt with in another self-defined function.

How can I incorporate my order constraint function in the DEAP individual initialization?

I implemented this way with the inspiration from https://groups.google.com/forum/#!msg/deap-users/KZYYHCGrFyY/x1nXcXpCyscJ:

class MyContainer(list):
    def __init__(self, attributes):
        # Some initialisation with received values
        # self.attr1 = attributes[0]
        pass


def generate_individual(ind_class, size):
    # ind_class will receive a class inheriting from MyContainer
    # individual = ind_class(random.random() for _ in range(size))
    individual = ind_class(random.sample(range(IND_SIZE), k=size))
    individual = make_individual_valid(individual, other_parameters)
    # make_individual_valid is the self-defined order constraint function
    return individual

creator.create('FitnessMin', base.Fitness, weights=(-1.0,))
creator.create('Individual', MyContainer, fitness=creator.FitnessMin)

toolbox = base.Toolbox()
toolbox.register('individual', generate_individual, creator.Individual, size=IND_SIZE)
toolbox.register('population', tools.initRepeat, list, toolbox.individual)

But I got below error:

AttributeError: 'list' object has no attribute 'fitness'
akshat
  • 1,219
  • 1
  • 8
  • 24
Stephanie
  • 51
  • 1
  • 7

4 Answers4

2

I had the same error. I realized that your generate_individual functions returns a list []. So when a new individual is created, it is stored in a list, giving a result a list inside a list [[]]. If you try to print one individual, in order to get your desired list you will have to access it with...

individual[0]

... to have the list you created.

At the moment I don't know how to fix this (since I'm new with DEAP).

EDIT: I've found another StackOverflow question that solved this problem. I think it would be nice if you check it. Look at how the user creates his individual:

Question that helped me

Belberus
  • 21
  • 3
0

An old thread but I ran into it as I want to do something similar.

I am doing the following which seems to do the job.

pop = toolbox.population(n=POPSIZE)
pop[0] = creator.Individual((1.0 for _ in range(INDSIZE))) # add default individual

Maybe someone can point out if anything is wrong with this?

Krrr
  • 452
  • 1
  • 3
  • 15
0

You need to define a custom attribute function and register it using DEAP. toolbox

0

For example the code below creates individuals with 2 separate attributes.

# Structure initializers
toolbox.register("attr_r", random.uniform, R_MIN, R_MAX)
toolbox.register("attr_l", random.uniform, L_MIN, L_MAX)
toolbox.register("individual", tools.initCycle, creator.Individual,(toolbox.attr_l,
    toolbox.attr_r,toolbox.attr_l,toolbox.attr_l,toolbox.attr_r,toolbox.attr_l,toolbox.attr_l,toolbox.attr_r,toolbox.attr_l), n=N_CYCLES)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)