def crossover(self, crossover_rate):
        temp_population = []
        new_population = []
        for i in range(len(self.population)):
            temp_population.append(self.selection())
        for i in range(0, len(temp_population) - 1, 2):
            if uniform(0, 1) < crossover_rate:
                crossover_point = randint(1, self.n_genes)
                temp1 = temp_population[i].chromo_numb[:crossover_point]
                temp2 = temp_population[i + 1].chromo_numb[crossover_point:]
                son_chromo_numb1 = temp1 + temp2
                temp1 = temp_population[i + 1].chromo_numb[:crossover_point]
                temp2 = temp_population[i].chromo_numb[crossover_point:]
                son_chromo_numb2 = temp1 + temp2

                son_chromo1 = Chromosome(self.n_genes)
                son_chromo1.generate(son_chromo_numb1)
                son_chromo1.numbToExpr(self.max_loops, self.grammar)
                son_chromo1.calculateFitness(self.data_train)
                son_chromo2 = Chromosome(self.n_genes)
                son_chromo2.generate(son_chromo_numb2)
                son_chromo2.numbToExpr(self.max_loops, self.grammar)
                son_chromo2.calculateFitness(self.data_train)
                new_population.append(son_chromo1)
                new_population.append(son_chromo2)
            else:
                new_population.append(temp_population[i])
                new_population.append(temp_population[i + 1])
        if len(temp_population) % 2 == 1:
            new_population.append(temp_population[-1])
        self.population = new_population.copy()
 def generate(self):
     while len(self.population) != self.pop_size:
         chromo = Chromosome(self.n_genes)
         chromo.generate()
         chromo.numbToExpr(self.max_loops, self.grammar)
         chromo.calculateFitness(self.data_train)
         #if chromo.fitness != float('infinity') and len(chromo.chromo_expr)>1:
         self.population.append(chromo)