0

new to stackoverflow and new to Python. I appreciate all help given! I'm trying to call the constructor for a class and hand it a populated list. However, when the object is created the list is empty. The debugger shows the list is populated

    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)

And here is the first bit of the Genetics class:

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
    ...

any ideas?

  • Can you please share the constructor as well? – GodMan Mar 16 '13 at 07:46
  • How could `pop` _possibly_ be a `list` right after you write `pop = Population(manager, True)`? It's obviously got to be a `Population` instance. Meanwhile, `pop = Genetics.evolvePop(pop, manager)` replaces `pop` with a new `Population` object, which of course also isn't a `list`. As for what is _is_… well, you haven't shown us the definition of `Population`, what attributes it has, which one(s) you expect to have which value(s), etc. – abarnert Mar 16 '13 at 07:46
  • More simply: Please give us an [SSCCE](http://sscce.org): some complete (but stripped down as far as possible while still demonstrating the problem) runnable code, together with any input, expected output, and actual output. – abarnert Mar 16 '13 at 07:52
  • can you show us what you get and what you expect? – User Mar 16 '13 at 10:58

0 Answers0