-
Notifications
You must be signed in to change notification settings - Fork 0
/
boltzmann_selector.py
71 lines (51 loc) · 2.41 KB
/
boltzmann_selector.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
from math import fsum
from numpy import exp as np_exp
from pygenalgo.genome.chromosome import Chromosome
from pygenalgo.operators.selection.select_operator import SelectionOperator
class BoltzmannSelector(SelectionOperator):
"""
Description:
Boltzmann Selector implements an object that performs selection by choosing an
individual from a set of individuals by sampling solutions from a Boltzmann
distribution depending on their fitness's.
"""
def __init__(self, select_probability: float = 1.0, k: float = 100.0):
"""
Construct a 'BoltzmannSelector' object with a given probability value.
:param select_probability: (float) in [0, 1].
:param k: constant of the Boltzmann distribution.
"""
# Call the super constructor with the provided probability value.
super().__init__(select_probability)
# Make sure 'k' is float and at least 50.
self._items = max(float(k), 50.0)
# _end_def_
def select(self, population: list[Chromosome]):
"""
Select the individuals, from the input population, that will be passed on
to the next genetic operations of crossover and mutation to form the new
population of solutions.
NOTE: the Boltzmann constant is held in the '_items' variable.
:param population: a list of chromosomes to select the parents from.
:return: the selected parents population (as list of chromosomes).
"""
# Compute the Temperature.
T = max(0.1, np_exp(-self.iter/self._items))
# Get the length of the population list.
N = len(population)
# Extract the fitness value of each chromosome.
# This assumes that the fitness values are all positive.
exp_fitness = np_exp([-p.fitness/T for p in population]).tolist()
# Calculate sum of all fitness.
sum_fitness = fsum(exp_fitness)
# Calculate the selection probabilities of each member
# in the population (Boltzmann distribution).
selection_probs = [f/sum_fitness for f in exp_fitness]
# Select 'N' new individuals (indexes).
index = self.rng.choice(N, size=N, p=selection_probs, replace=True, shuffle=False)
# Increase the selection counter.
self.inc_counter()
# Return the new parents (individuals).
return [population[i] for i in index]
# _end_def_
# _end_class_