forked from martinpilat/evaTeaching-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeap_partition.py
65 lines (47 loc) · 1.81 KB
/
deap_partition.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
import random
import numpy
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
import utils
K = 10
# reads the input set of values of objects
def read_weights(filename):
with open(filename) as f:
return list(map(int, f.readlines()))
# computes the bin weights
# - bins are the indices of bins into which the object belongs
def bin_weights(weights, bins):
bw = [0]*K
for w, b in zip(weights, bins):
bw[b] += w
return bw
# the fitness function
def fitness(ind, weights):
bw = bin_weights(weights, ind)
return max(bw) - min(bw),
weights = read_weights('inputs/partition-easy.txt')
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
toolbox = base.Toolbox()
toolbox.register("attr_bool", random.randint, 0, 9)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, len(weights))
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", fitness, weights=weights)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutUniformInt, low=0, up=9, indpb=0.005)
toolbox.register("select", tools.selTournament, tournsize=3)
def main():
pop = toolbox.population(n=100)
hof = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", numpy.mean)
stats.register("std", numpy.std)
stats.register("min", numpy.min)
stats.register("max", numpy.max)
pop, log = algorithms.eaSimple(pop, toolbox, cxpb=0.2, mutpb=0.8, ngen=100, stats=stats, halloffame=hof, verbose=True)
return pop, log, hof
if __name__ == "__main__":
pop, log, hof = main()
print('Best individual fitness:', hof[0].fitness.values)