def moveRandomly(self, individual, step_length): ind = population.Individual() ind.loadIndividual(individual) while 1: random = nbmath.randomDouble()*0.25 x_change = math.log(random)/math.log(0.25)*step_length random = nbmath.randomDouble()*0.25 y_change = math.log(random)/math.log(0.25)*step_length if nbmath.randomNumber(100) < 50: x_change = 0-x_change if nbmath.randomNumber(100) < 50: y_change = 0-y_change if self.pixelColourAt(ind.X+x_change,ind.Y+y_change) != (255, 255, 255): break; ind.X += x_change ind.Y += y_change return ind.getIndividual()
def moveToward(self, individual, X, Y, distance, deviance): ind = population.Individual() ind.loadIndividual(individual) k = abs(ind.X-float(X))/abs(ind.Y-float(Y)) a = (0-k+math.sqrt(abs(math.pow(k,2)-8*(math.pow(0-distance,2)+math.pow(k,2)))))/4 y = a*distance/math.sqrt(math.pow(a,2)+math.pow(a*k,2)) x = math.sqrt(math.pow(distance,2)-math.pow(y,2)) rand = nbmath.randomNumber(deviance*2) rand -= deviance x_disp = rand y_disp = math.sqrt(math.pow(deviance,2)-math.pow(x_disp,2)) x += x_disp y += y_disp if X < ind.X: ind.X -= x else: ind.X += x if Y < ind.Y: ind.Y -= y else: ind.Y += y return ind.getIndividual()
def produceOffspring(self, mother, father): offspring_genotype = [] _mother = population.Individual() _father = population.Individual() _offspring = population.Individual() _mother.loadIndividual(mother) _father.loadIndividual(father) _offspring.ID = 0 #_offspring.Y = 0.0 #_offspring.X = 0.0 _offspring.ID_father = _father.ID _offspring.ID_mother = _mother.ID if len(_father.genotype) > 1: for x in range(0,len(_father.genotype)): temp = _father.genotype[x] if nbmath.randomNumber(100) < 50: offspring_genotype.append(temp[0]) else: offspring_genotype.append(temp[1]) temp = _mother.genotype[x] if nbmath.randomNumber(100) < 50: offspring_genotype[x] += temp[0] else: offspring_genotype[x] += temp[1] _offspring.genotype = offspring_genotype else: _offspring.genotype = '' if nbmath.randomNumber(100) < 50: _offspring.gender = 'f' else: _offspring.gender = 'm' _offspring.X = _mother.X _offspring.Y = _mother.Y return _offspring.getIndividual()
def generateRandomGenotype(self, seed): genotype = [] loci = 0 alternatives = '' length = 0 temp1 = 0 temp2 = 0 loci = len(seed) for x in range(0,loci): alternatives = seed[x] length = len(alternatives) temp1 = nbmath.randomNumber(length-1) temp2 = nbmath.randomNumber(length-1) genotype.append(alternatives[temp1] + alternatives[temp2]) return genotype
def generateIndividual(self, ID_mother, ID_father, age, gender, genes, social, group, X, Y, misc): ind = Individual(); if gender == 'r': if nbmath.randomNumber(100) < 50: g = 'm'; else: g = 'f'; else: g = gender; _ind = ind.combineVariables(self.getPopulationSize(), ID_mother, ID_father, g, age, genes, social, group, X, Y, misc); self.insertIndividual(_ind);
def returnRandom(self, arguments = None): if arguments == None: return self.Selected[nbmath.randomNumber(len(self.Selected))-1] else: pos = 0; temp_list = self.Selected + self.notSelected for arg in arguments: while pos < len(temp_list): ind = temp_list[pos]; if isTrue(arg, ind) == True: pos += 1; else: del(temp_list[pos]); pos = 0; if len(temp_list) > 0: return temp_list[nbmath.randomNumber(len(temp_list)-1)] else: return 0
def selectRandom(self): index = nbmath.randomNumber(len(self.Selected))-1 self.selectIndividual(index) return self.selectedIndividual