-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbil.py
85 lines (65 loc) · 2.72 KB
/
pbil.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# This file is part of DEAP.
#
# DEAP is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# DEAP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with DEAP. If not, see <http://www.gnu.org/licenses/>.
import array
import random
import numpy
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
class PBIL(object):
def __init__(self, ndim, learning_rate, mut_prob, mut_shift, lambda_):
self.prob_vector = [0.5] * ndim
self.learning_rate = learning_rate
self.mut_prob = mut_prob
self.mut_shift = mut_shift
self.lambda_ = lambda_
def sample(self):
return (random.random() < prob for prob in self.prob_vector)
def generate(self, ind_init):
return [ind_init(self.sample()) for _ in range(self.lambda_)]
def update(self, population):
best = max(population, key=lambda ind: ind.fitness)
for i, value in enumerate(best):
# Update the probability vector
self.prob_vector[i] *= 1.0 - self.learning_rate
self.prob_vector[i] += value * self.learning_rate
# Mutate the probability vector
if random.random() < self.mut_prob:
self.prob_vector[i] *= 1.0 - self.mut_shift
self.prob_vector[i] += random.randint(0, 1) * self.mut_shift
def evalOneMax(individual):
return sum(individual),
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", array.array, typecode='b', fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("evaluate", evalOneMax)
def main(seed):
random.seed(seed)
NGEN = 50
#Initialize the PBIL EDA
pbil = PBIL(ndim=50, learning_rate=0.3, mut_prob=0.1,
mut_shift=0.05, lambda_=20)
toolbox.register("generate", pbil.generate, creator.Individual)
toolbox.register("update", pbil.update)
# Statistics computation
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, logbook = algorithms.eaGenerateUpdate(toolbox, NGEN, stats=stats, verbose=True)
if __name__ == "__main__":
main(seed=None)