def firstTrial(): # create LocatedModels and add them into populationArray[] for i in range(sizeOfPopulation): locatedModel = MODEL.LocatedModel( numberOfNodes) # create default locatedModel # define 1 chromosomes in DNA of current locatedModel randomly untill ones is equal to the pmedian counter = 0 while counter < pmedian: randomIndex = randint(0, len(locatedModel.DNA) - 1) if (locatedModel.DNA[randomIndex] is not 1): locatedModel.DNA[randomIndex] = 1 counter += 1 FitnessLevelFinder( locatedModel ) # calculate fitness value for the current locatedModel locatedModelList.append( locatedModel ) # after all of properties of the current locatedModel have been defined add into locatedModelList[]
def newLocatedModel(dad, mom): baby = MODEL.LocatedModel(numberOfNodes) # create a default baby # UNIFORM CROSSOVER chromosomes are chosen radomly form dad and mom for i in range(len(baby.DNA)): randomChromosome = randint(0, 1) if randomChromosome is 0: baby.DNA[i] = dad.DNA[i] else: baby.DNA[i] = mom.DNA[i] # In this part fix amount of ones in DNA if there are more or less than pmedian onesIndexes = baby.getOnes() difference = pmedian - len( onesIndexes ) # difference<0 means number of ones more than pmedian so we need to remove some ones randomly if difference < 0: for i in range(abs(difference)): r = randint(0, len(onesIndexes) - 1) randomDnaIndex = onesIndexes[r] baby.DNA[randomDnaIndex] = 0 onesIndexes.remove(onesIndexes[r]) elif difference > 0: dnaLength = len(baby.DNA) i = 0 while i is not difference: randomDnaIndex = randint(0, dnaLength - 1) if (baby.DNA[randomDnaIndex] is 0): baby.DNA[randomDnaIndex] = 1 i += 1 else: pass mutation(baby) # check mutation factor for baby FitnessLevelFinder(baby) # get fitness value of baby return baby