AttributeError:“ float”对象没有属性“ Vn_change”

时间:2019-11-10 11:56:48

标签: python genetic-algorithm

当我运行这段代码

class Population:
    def __init__(self, size):
        self._population = []
        i = 0
        while i < size:
            self._population.append(Particul())
            i += 1

    def get_population(self):
        return self._population


class GeneticAlgorithm:
    @staticmethod
    def evolve(pop):
        return GeneticAlgorithm._mutate_pop(GeneticAlgorithm._crossover_pop(pop))

    @staticmethod
    def _crossover_pop(pop):
        crossover_pop = Population(0)
        for i in range(NUMB_OF_ELITE_PARTICULS):
            crossover_pop.get_population().append(pop.get_population()[i])
        i = NUMB_OF_ELITE_PARTICULS
        while i < POPULATION_SIZE:
            partcul1 = GeneticAlgorithm._select_population(pop).get_population()[0]
            partcul2 = GeneticAlgorithm._select_population(pop).get_population()[0]
            crossover_pop.get_population().append(GeneticAlgorithm._crossover_particuls(partcul1, partcul2))
            i += 1

        return crossover_pop

    @staticmethod
    def _crossover_particuls(particul1, particul2):
        crossover_part = Particul()
        for i in range(SELECTION_SIZE):
            crossover_part = (particul1.get_particul() + particul2.get_particul()) / random.uniform(1.8, 2.5)
        return crossover_part

**对我来说,该方法似乎将pop.get_population()[i]更改为float **         @staticmethod         def _mutate_pop(pop):             对于范围在(NUMB_OF_ELITE_PARTICULS,POPULATION_SIZE)中的我:                 GeneticAlgorithm._mutate_particul(pop.get_population()[i])

    @staticmethod
    def _mutate_particul(particul):
        if random.random() < MUTATION_RATE_VELOCITY:
            if random.random() < 0.5:
                particul.Vp_change()
            else:
                particul.Vn_change()

        if random.random() < MUTATION_RATE_ALPHA:
            if random.random() < 0.5:
                particul.Ap_change()
            else:
                particul.An_change()
        return particul

    @staticmethod
    def _select_population(pop):
        select_population = Population(0)
        i = 0
        while i < SELECTION_SIZE:
            select_population.get_population().append(pop.get_population()[random.randrange(0, POPULATION_SIZE)])
            i += 1
        return select_population

我遇到了一个错误"AttributeError: 'float' object has no attribute 'Vn_change'"

0 个答案:

没有答案