-
Notifications
You must be signed in to change notification settings - Fork 59
/
ga.py
67 lines (57 loc) · 2.43 KB
/
ga.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import numpy as np
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
import random
def genetic_algorithm(total_set, nvar, objective_function, ngen=50, mu=20,
lam=40, cxpb=0.7, mutpb=0.2, tournsize=3,
verbose=False):
# Creates a new class name set_mod which is based on the standard python
# set. This means set_mod is just like set, with the addion of a fitness
# attribute.
creator.create("Fitness", base.Fitness, weights=(-1.0,))
creator.create("Individual", set, fitness=creator.Fitness)
set_mod = creator.Individual
def random_samp(size):
"""Function to initlize individual in the population."""
return set_mod(random.sample(total_set, size))
def evaluation(individual):
"""Evaluate the objective function."""
return objective_function(list(individual)),
def cxSet(ind1, ind2):
"""Apply a crossover operation on two sets."""
full_set = list(ind1 | ind2)
ind1 = set_mod(random.sample(full_set, nvar))
ind2 = set_mod(random.sample(full_set, nvar))
return ind1, ind2
def mutSet(individual):
"""Mutation that randomly removes and item and randomly adds an item.
"""
temp_set = set_mod(random.sample(individual, nvar-1))
set_to_choose = np.array(list(temp_set ^ total_set))
new = random.choice(set_to_choose)
temp_set.add(new)
return temp_set,
toolbox = base.Toolbox()
# set up the population
toolbox.register("individual", random_samp, nvar)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
# set up the GA functions
toolbox.register("evaluate", evaluation)
toolbox.register("mate", cxSet)
toolbox.register("mutate", mutSet)
# toolbox.register("select", tools.selNSGA2)
toolbox.register("select", tools.selTournament, tournsize=tournsize)
# initialize the population
pop = toolbox.population(n=mu)
hof = tools.ParetoFront()
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean, axis=0)
stats.register("std", np.std, axis=0)
stats.register("min", np.min, axis=0)
stats.register("max", np.max, axis=0)
# run the GA
algorithms.eaMuPlusLambda(pop, toolbox, mu, lam, cxpb, mutpb, ngen, stats,
halloffame=hof, verbose=verbose)
return pop, hof, stats