File: scaling.py

package info (click to toggle)
python-scipy 0.6.0-12
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 32,016 kB
  • ctags: 46,675
  • sloc: cpp: 124,854; ansic: 110,614; python: 108,664; fortran: 76,260; objc: 424; makefile: 384; sh: 10
file content (56 lines) | stat: -rw-r--r-- 1,584 bytes parent folder | download
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
#genetic algorithm scaling routines
#based on galib.

from numpy import clip, inf
from ga_util import GAError, my_mean, my_std


# if a score is less the 2 standard deviations below, the average, its score
# is arbitrarily set to zero
class sigma_truncation_scaling(object):
    def __init__(self, scaling = 2):
        self.scaling = scaling

    def scale(self, pop):
        sc = pop.scores()
        avg = my_mean(sc)
        if len(sc) > 1:
            dev = my_std(sc)
        else:
            dev = 0
        f = clip(sc - avg + self.scaling * dev, 0, inf)
        for i in range(len(pop)):
            pop[i].fitness(f[i])
        return pop

class no_scaling(object):
    def scale(self,pop):
        for ind in pop:
            ind.fitness(ind.score())
        return pop

class linear_scaling(object):
    def __init__(self,mult = 1.2):
        self.mult = mult

    def scale(self,pop):
        sc = pop.scores()
        pmin = min(sc)
        if pmin < 0:
            raise GAError('linear scaling does not work with objective scores < 0')
        pmax = max(sc)
        pavg = my_mean(sc)
        if pavg == pmax:
            a = 1.
            b = 0.
        elif pmin > (self.mult * pavg - pmax)/(self.mult - 1.):
            delta = pmax - pavg
            a = (self.mult - 1.) * pavg / delta
            b = pavg * (pmax - self.mult * pavg) / delta
        else:
            delta = pavg - pmin
            a = pavg / delta
            b = -pmin * pavg / delta
        f = clip(sc * a + b, 0, inf)
        for i in range(len(pop)):
            pop[i].fitness(f[i])