-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathScaling.py
More file actions
130 lines (104 loc) · 3.7 KB
/
Scaling.py
File metadata and controls
130 lines (104 loc) · 3.7 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
:mod:`Scaling` -- scaling schemes module
===========================================================
This module have the *scaling schemes* like Linear scaling, etc.
"""
import Consts
import Util
import math
import logging
def LinearScaling(pop):
""" Linear Scaling scheme
.. warning :: Linear Scaling is only for positive raw scores
"""
logging.debug("Running linear scaling.")
pop.statistics()
c = Consts.CDefScaleLinearMultiplier
a = b = delta = 0.0
pop_rawAve = pop.stats["rawAve"]
pop_rawMax = pop.stats["rawMax"]
pop_rawMin = pop.stats["rawMin"]
if pop_rawAve == pop_rawMax:
a = 1.0
b = 0.0
elif pop_rawMin > (c * pop_rawAve - pop_rawMax / c - 1.0):
delta = pop_rawMax - pop_rawAve
a = (c - 1.0) * pop_rawAve / delta
b = pop_rawAve * (pop_rawMax - (c * pop_rawAve)) / delta
else:
delta = pop_rawAve - pop_rawMin
a = pop_rawAve / delta
b = -pop_rawMin * pop_rawAve / delta
for i in xrange(len(pop)):
f = pop[i].score
if f < 0.0:
Util.raiseException("Score %r is negative, linear scaling not supported !" % (f,), ValueError)
f = f * a + b
if f < 0:
f = 0.0
pop[i].fitness = f
def SigmaTruncScaling(pop):
""" Sigma Truncation scaling scheme, allows negative scores """
logging.debug("Running sigma truncation scaling.")
pop.statistics()
c = Consts.CDefScaleSigmaTruncMultiplier
pop_rawAve = pop.stats["rawAve"]
pop_rawDev = pop.stats["rawDev"]
for i in xrange(len(pop)):
f = pop[i].score - pop_rawAve
f += c * pop_rawDev
if f < 0:
f = 0.0
pop[i].fitness = f
def PowerLawScaling(pop):
""" Power Law scaling scheme
.. warning :: Power Law Scaling is only for positive raw scores
"""
logging.debug("Running power law scaling.")
k = Consts.CDefScalePowerLawFactor
for i in xrange(len(pop)):
f = pop[i].score
if f < 0.0:
Util.raiseException("Score %r is negative, power law scaling not supported !" % (f,), ValueError)
f = math.pow(f, k)
pop[i].fitness = f
def BoltzmannScaling(pop):
""" Boltzmann scaling scheme. You can specify the **boltz_temperature** to the
population parameters, this parameter will set the start temperature. You
can specify the **boltz_factor** and the **boltz_min** parameters, the **boltz_factor**
is the value that the temperature will be subtracted and the **boltz_min** is the
minimum temperature of the scaling scheme.
.. versionadded: 0.6
The `BoltzmannScaling` function.
"""
boltz_temperature = pop.getParam("boltz_temperature", Consts.CDefScaleBoltzStart)
boltz_factor = pop.getParam("boltz_factor", Consts.CDefScaleBoltzFactor)
boltz_min = pop.getParam("boltz_min", Consts.CDefScaleBoltzMinTemp)
boltz_temperature -= boltz_factor
boltz_temperature = max(boltz_temperature, boltz_min)
pop.setParams(boltzTemperature=boltz_temperature)
boltz_e = []
avg = 0.0
for i in xrange(len(pop)):
val = math.exp(pop[i].score / boltz_temperature)
boltz_e.append(val)
avg += val
avg /= len(pop)
for i in xrange(len(pop)):
pop[i].fitness = boltz_e[i] / avg
def ExponentialScaling(pop):
""" Exponential Scaling Scheme. The fitness will be the same as (e^score).
.. versionadded: 0.6
The `ExponentialScaling` function.
"""
for i in xrange(len(pop)):
score = pop[i].score
pop[i].fitness = math.exp(score)
def SaturatedScaling(pop):
""" Saturated Scaling Scheme. The fitness will be the same as 1.0-(e^score)
.. versionadded: 0.6
The `SaturatedScaling` function.
"""
for i in xrange(len(pop)):
score = pop[i].score
pop[i].fitness = 1.0 - math.exp(score)