File: ga_util.orig

package info (click to toggle)
python-scipy 0.5.2-0.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 33,888 kB
  • ctags: 44,231
  • sloc: ansic: 156,256; cpp: 90,347; python: 89,604; fortran: 73,083; sh: 1,318; objc: 424; makefile: 342
file content (61 lines) | stat: -rw-r--r-- 1,419 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
57
58
59
60
61
#base definitions for genetic algorithms
import scipy.stats as rv
stats = rv

GAError = 'GA Error'

def nop(x): return x
def flip_coin(p): return (rv.random() < p)

import random

def flip_coin2(p): return (random.random() < p)
class empty_class: pass

def shallow_clone(item):
    new = empty_class()
    new.__class__ = item.__class__
    new.__dict__.update(item.__dict__)
    return new
#these are exacly correct, but htey prevent problems with -Inf and Inf
def my_std(s):
#       try:
    a = remove_NaN(s)
    if len(a) > 1: return stats.std(a)
    else: return 0.
#       except:
#               import pdb
#               pdb.set_trace()
def my_mean(s):
    a = remove_NaN(s)
    if len(a) > 1: return stats.mean(a)
    else: return 0.

def testflip():

    import time
    b = time.clock()
    for i in range(10000): a = flip_coin(.5)
    e = time.clock()
    print 'rv_flip',e-b
    b = time.clock()
    for i in range(10000): a = flip_coin2(.5)
    e = time.clock()
    print 'wh_flip',e-b
    from rv import random
    b = time.clock()
    for i in range(10000):
        a = random() < .5
    e = time.clock()
    print 'rv',e-b
    from random import random
    b = time.clock()
    for i in range(10000):
        a = random() < .5
    e = time.clock()
    print 'wh',e-b


def remove_NaN(z):
    from numpy import isnan, isinf, compress, logical_not
    return compress(logical_not( isnan(z)+isinf(z)),z)