import random
random.seed(5)
POPULATION_SIZE = 10
alleles = []
PHENOTYPE_LENGTH = len(alleles)
class Phenotype:
alleles = []
length = PHENOTYPE_LENGTH
chromosome = []
fitness = 0
def __init__(self, alleles = []):
self.length = len(alleles)
self.alleles = alleles
self.initialBuild()
def initialBuild(self):
alleleSet = self.alleles
for i in range (self.length):
#random.seed(i)
value = random.sample(alleleSet, 1)
alleleSet.remove(value[0])
self.chromosome.append(value[0])
print "Chromosome = ", self.chromosome
class World:
population = []
def __init__(self):
alleles = self.readFromFile()
population = self.buildPopulation(alleles)
def buildPopulation(self, alleles):
#Initialize empty individuals
for i in range(POPULATION_SIZE):
self.population.append(Phenotype(alleles))
def readFromFile(self):
newAlleles = []
berlin = open("berlin52.txt")
for line in berlin:
coords = line.strip('\n\r').split(" ")
xyCoords = coords[1:]
xyCoords = [coord for coord in xyCoords if coord] #remove empty strings from odd whitespacing
if xyCoords:
x = int(float(xyCoords[0]))
y = int(float(xyCoords[1]))
finalCoords = [x, y]
newAlleles.append(finalCoords)
PHENOTYPE_LENGTH = len(newAlleles)
return newAlleles
if __name__ == '__main__':
world = World()