Still too slow

elsa kerensaelise at hotmail.com
Sat Jan 30 20:58:57 EST 2010


Hi John and others,

sorry about my etiquette errors. As you can tell I'm a newbie, and
appreciate all the help I can get. I'm trying to master this thing
with only the net and a couple of books as tutors.

Here is what I'm running at the interactive prompt:

>>> import myBDM
>>> L=[[100,'NA']]
>>> myBDM.evolve(L,100000)

Ideally, I'd like to bump it up to myBDM.evolve(L,10000000). L keeps
track of the number of subpopulations, how many individuals are in
each subpopulation, and the parent subpopulation each subpopulation
arose from.

Here is my code again:


import random
n=100

def evolve(L,limit):
	"""
	evolves the population until the population size reaches limit, by
choosing an 	individual of a particular subpopulation
        type, then randomly performing
	a birth, death, or mutation on this individual
	"""
	global n
	while n<limit:
		evnt = event()
		if evnt!="None":
			ind = chooseInd(L,n)
			action(evnt,L,ind)

def chooseInd(L,n):
	"""
	randomly choose one subpopulation L[i] , so that the distribution
       	of choices is proportional to the values of L[i][0]
       	"""
	choiceSum=0
	index=0
	choice = random.randint(1,n)
	while choiceSum < choice:
		choiceSum+=L[index][0]
		index +=1
	return (index-1)

def event():
	"""
	randomly chooses a birth (prob .3), death (prob .1) or mutation
(prob .1) event
       	"""
	choice = random.random()
	if choice <= .3:
		event='b'
	elif choice <= .4:
		event ='d'
	elif choice<= .5:
		event = 'm'
	else:
		event = 'None'
	return event

def action(event, L, index):
	"""
	decides what happens to the population/subpopulation, based on
whether
	the event is a birth, death or mutation
       	"""
	global n
	if event == 'b':
		L[index][0]+=1
		n +=1
	elif event == 'd':
		L[index][0]-=1
		n -=1
	elif event == 'm':
		L.append([1,index])
		n +=1




More information about the Python-list mailing list