1. import random
  2. random.seed(5)
  3. POPULATION_SIZE = 10
  4. alleles = []
  5. PHENOTYPE_LENGTH = len(alleles)
  6.  
  7.  
  8. class Phenotype:
  9.     alleles = []
  10.     length = PHENOTYPE_LENGTH
  11.     chromosome = []
  12.     fitness = 0
  13.        
  14.     def __init__(self, alleles = []):
  15.         self.length = len(alleles)
  16.         self.alleles = alleles
  17.         self.initialBuild()
  18.        
  19.     def initialBuild(self):
  20.         alleleSet = self.alleles
  21.         for i in range (self.length):
  22.             #random.seed(i)
  23.             value = random.sample(alleleSet, 1)
  24.             alleleSet.remove(value[0])
  25.             self.chromosome.append(value[0])
  26.         print "Chromosome = ", self.chromosome
  27.        
  28.        
  29. class World:
  30.     population = []
  31.     def __init__(self):
  32.         alleles = self.readFromFile()
  33.         population = self.buildPopulation(alleles)
  34.        
  35.     def buildPopulation(self, alleles):
  36.         #Initialize empty individuals
  37.         for i in range(POPULATION_SIZE):
  38.             self.population.append(Phenotype(alleles))
  39.            
  40.        
  41.     def readFromFile(self):
  42.         newAlleles = []
  43.         berlin = open("berlin52.txt")
  44.         for line in berlin:
  45.             coords = line.strip('\n\r').split(" ")
  46.             xyCoords = coords[1:]
  47.             xyCoords = [coord for coord in xyCoords if coord] #remove empty strings from odd whitespacing
  48.             if xyCoords:
  49.                 x = int(float(xyCoords[0]))
  50.                 y = int(float(xyCoords[1]))
  51.                 finalCoords = [x, y]
  52.                 newAlleles.append(finalCoords)
  53.         PHENOTYPE_LENGTH = len(newAlleles)
  54.         return newAlleles
  55.                
  56. if __name__ == '__main__':
  57.     world = World()