~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

Viewing changes to Lib/ga/ga_util.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T. Chen (new)
  • Date: 2005-03-16 02:15:29 UTC
  • Revision ID: james.westby@ubuntu.com-20050316021529-xrjlowsejs0cijig
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#base definitions for genetic algorithms
 
2
import scipy.stats as rv
 
3
stats = rv
 
4
 
 
5
GAError = 'GA Error'
 
6
 
 
7
def nop(x): return x
 
8
def flip_coin(p): return (rv.random() < p)
 
9
 
 
10
import random
 
11
 
 
12
def flip_coin2(p): return (random.random() < p)
 
13
class empty_class: pass
 
14
 
 
15
def shallow_clone(item):
 
16
        new = empty_class()
 
17
        new.__class__ = item.__class__
 
18
        new.__dict__.update(item.__dict__)
 
19
        return new
 
20
#these are exacly correct, but htey prevent problems with -Inf and Inf  
 
21
def my_std(s):
 
22
#       try:
 
23
                a = remove_NaN(s)
 
24
                if len(a) > 1: return stats.std(a)
 
25
                else: return 0.
 
26
#       except: 
 
27
#               import pdb
 
28
#               pdb.set_trace()
 
29
def my_mean(s):
 
30
        a = remove_NaN(s)
 
31
        if len(a) > 1: return stats.mean(a)
 
32
        else: return 0.
 
33
        
 
34
def testflip():
 
35
        
 
36
        import time
 
37
        b = time.clock()
 
38
        for i in range(10000): a = flip_coin(.5)
 
39
        e = time.clock()
 
40
        print 'rv_flip',e-b
 
41
        b = time.clock()
 
42
        for i in range(10000): a = flip_coin2(.5)
 
43
        e = time.clock()
 
44
        print 'wh_flip',e-b
 
45
        from rv import random
 
46
        b = time.clock()
 
47
        for i in range(10000): 
 
48
                a = random() < .5
 
49
        e = time.clock()
 
50
        print 'rv',e-b
 
51
        from random import random
 
52
        b = time.clock()
 
53
        for i in range(10000): 
 
54
                a = random() < .5
 
55
        e = time.clock()
 
56
        print 'wh',e-b
 
57
 
 
58
 
 
59
def remove_NaN(z):
 
60
        from scipy import isnan, isinf, compress, logical_not
 
61
        return compress(logical_not( isnan(z)+isinf(z)),z)