chromosme encoding as a dict in GA ?

237 views
Skip to first unread message

Wahdat Safia

unread,
Dec 30, 2015, 7:48:00 AM12/30/15
to deap-users
Hello,

My problem is to design a GA for virtual machine placement problem in cloud computing. I have to represent a solution in the form of python dict like eg:-
{
   "PM1": [0,1,2],
    "PM2":[3,4],
    "PM3":[5,6]
}
where dict item name indicates physical machine and its value is an array indicating all set of requested VMs to be placed on that particular physical machine.
Please suggest any suitable data-structure through which i can represent my chromosome in accordance with DEAP framework.
Also, Initial random population is not possible in this problem, so i want to define my initial guess of each individual to form a population, Please suggest how i can implement this.
Thankyou.

Marc-André Gardner

unread,
Dec 30, 2015, 11:40:53 PM12/30/15
to deap-users
Hi Wahdat,

As explained in the Creating types section of the documentation, you can basically derive an individual from anything, including, in your case, a dictionnary. For instance (assuming a maximising fitness) :

creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", dict, fitness=creator.FitnessMax)

 Now, you do have to provide mutation and/or crossover algorithms able to cope with these individuals. You can draw inspiration from the Knapsack example, where we derive individuals from set (admittedly, it is not dict, but quite close and in any way you can see how one can adapt crossover and mutation algorithms for specific types).

In a similar manner, you can create your own initialization function to fit your needs. Just register it through the toolbox and DEAP will use it without asking anything else :)

Have fun with DEAP and do not hesitate if you have any remaining questions,

Marc-André

Wahdat Safia

unread,
Dec 31, 2015, 12:22:43 AM12/31/15
to deap-users

Dear Marc-Andre,

Thanks for the suggestions, Since I am new to DEAP framework so m facing lots of difficulties. Below i am giving my code in which i have defined Individual type as set  and also giving the input file.


CODE:

array=[]
creator.create("FitnessMin",base.Fitness,weights=(-1.0,))
creator.create("Individual",set,fitness=creator.FitnessMin)

def initPopulation(pcls, ind_init, filename):

    for line in open(filename).xreadlines():
         #print line
         if not line:
             sys.exit()
         else:
             array.append(line)
           
    return pcls(ind_init(c) for c in array)

toolbox = base.Toolbox()

toolbox.register("population_guess", initPopulation, list, creator.Individual, "my_guess.json")

population = toolbox.population_guess()

print population

INPUT FILE:-
{"0":[0,1],"1":[2],"2":[3],"3":[4]}
{"0":[0,2],"1":[3],"2":[],"3":[2,4]}
{"0":[0,2],"1":[],"2":[3],"3":[2,4]}
{"0":[0,1],"1":[2],"2":[],"3":[3,4]}
{"0":[0,1],"1":[],"2":[2],"3":[3,4]}

The above code read the input file line by line and then initialize each individual of population.In this code i have used set as individual type and i am getting error in this if i use dict type.
Please help.

Thanks for the help.
Reply all
Reply to author
Forward
0 new messages