0

stackoverflow 的新手和 Python 的新手。我感谢所有的帮助!我正在尝试调用一个类的构造函数并将其交给一个填充列表。但是,创建对象时,列表为空。调试器显示列表已填充

    manager = PathManager()

    # create cities
    city1  = City(60, 200)
    manager.addCity(city1)

    city2  = City(180, 200)
    manager.addCity(city2)

    city3  = City(80, 180)
    manager.addCity(city3)

    city4  = City(140, 180)
    manager.addCity(city4)

    city5  = City(20, 160)
    manager.addCity(city5)

    city6  = City(100, 160)
    manager.addCity(city6)

    city7  = City(200, 160)
    manager.addCity(city7)

    city8  = City(140, 140)
    manager.addCity(city8)

    city9  = City(40, 120)
    manager.addCity(city9)

    city10 = City(100, 120)
    manager.addCity(city10)

    pop = Population(manager, True)
    # debugger shows pop is a list containing expected elements

    # call the classmethod      
    pop = Genetics.evolvePop(pop, manager)

这是遗传学类的第一部分:

class Genetics:

# default values
global elitism, tournSize, mutRate
mutRate = 0.015
tournSize = 5
elitism = True

# evolve the population one generation
@classmethod
#@fixme -- pop is empty... why?
def evolvePop(cls, pop, manager):
    newPop = Population(manager, False)

    # testing
    #cls.tournamentSelection(pop, manager).displayCities())

    elitismOffset = 0
    if elitism:
        newPop.paths.insert(0, pop.getFittestPath())
        # remove old end
        if len(newPop.paths) > 10:
            newPop.paths.pop()
        elitismOffset = 1

    # crossover the population
    for index in range(elitismOffset, 10):
        #newPath = cls.crossover(cls.tournamentSelection(pop, manager), cls.tournamentSelection(pop, manager), manager)
        pathOne = cls.tournamentSelection(pop, manager); print('path one')
        pathOne.displayCities()
        pathTwo = cls.tournamentSelection(pop, manager); print('path two')
        pathTwo.displayCities()
        newPath = cls.crossover(pathOne, pathTwo, manager)
        newPop.storePath(index, newPath)
    # mutate the population for randomness
    #print(len(newPop))
    #for index in range(elitismOffset, len(newPop)):
        #cls.mutate(newPop.paths[index])

    return newPop
    ...

有任何想法吗?

4

0 回答 0