File: ga_util.py

package info (click to toggle)
python-scipy 0.3.2-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,572 kB
  • ctags: 20,326
  • sloc: ansic: 87,138; fortran: 51,876; python: 47,747; cpp: 2,134; objc: 384; makefile: 175; sh: 83
file content (61 lines) | stat: -rw-r--r-- 1,285 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 scipy import isnan, isinf, compress, logical_not
	return compress(logical_not( isnan(z)+isinf(z)),z)