def mutate (pop, mjumps, mprob, genmprob): ##returns a mutated population, where #pop is the population [number of subjects, size of subject] #mjumps is the number of mutation trials [pn,sn] = pop.shape for j in np.arange(pn): if (rand.random() <= mprob): r = rand.random (sn) ng = rand.random_integers(0,1, np.sum(r <= genmprob)) pop[j][r <= genmprob] = ng return pop
def cross (pop, cjumps=None, cprob=None): ##returns a new population from crossing cjumps pairs in pop with ##probability cprob ## if not given cjumps, then cjumps = popsize ## if not given cprob, then cprob = 1 #random new pop size [pn,sn] = pop.shape if not cjumps: cjumps = pn if cprob > 0 and cprob <= 1: #if only a percentage of selected parents will produce siblings npn = np.sum(rand.random([cjumps,1]) > cprob) else: #if all selected parents will produce siblings npn = cjumps nupop = np.zeros([npn,sn], dtype=int); #Uniform Crossover for j in np.arange(npn): pi1 = rand.randint(0,pn) pi2 = rand.randint(0,pn) while pi1 == pi2: pi2 = rand.randint(0,pn) p1 = pop[pi1,:] p2 = pop[pi2,:] c = np.array([p1,p2]) sel = rand.random_integers(0,1, [1,sn]); nupop[j,:] = np.choose(sel,c)
def Rand(*shape): r = np.empty(np.prod(shape)) for i in range(r.size): r[i] = random.random() # #change # global asdf # for i in range(r.size): # asdf += 1 # r[i] = np.sin(asdf) return r.reshape(shape)