Python Forum

Full Version: missing 1 required positional argument:
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Hello, I have for files AllOnesGA.py with the main(), GeneticAlgorithm.py,Population.py and Individual.py
My problem is that when I try to run the program with pycharm an error message happens:

in line 4:
population = genetic_algorithm.GeneticAlgorithm.initPopulation(50)
TypeError: initPopulation() missing 1 required positional argument: 'chromosomeLength'
Thanks

de code is:

import genetic_algorithm
def main():
    ga = genetic_algorithm.GeneticAlgorithm(100, 0.001, 0.95, 0)
    population = genetic_algorithm.GeneticAlgorithm.initPopulation(50)
    ga.evalPopulation(population)
    generation = 1
    while ga.isTerminationConditionMet(population) == false:
        print("Best solution", population.getFittest(0).toString())
        population = ga.crossoverPopulation(population)
        population = ga.mutatePopulation(population)
        ga.evalPopulation(population)
        generation += 1
    print("Found solution in ", generation, " generations")
    print("Best solution:", population.getFittest(0).toString())
if __name__ == "__main__":
    main()
and the file genetic_algorithm.py is:
import random
import population_file
import individual_file

class GeneticAlgorithm:
    def __init__(self, populationSize, mutationRate, crossoverRate, elitismCount):
        self.populationSize=populationSize
        self.mutationRate=mutationRate
        self.crossoverRate=crossoverRate
        self.elitismCount=elitismCount
    def initPopulation(self,chromosomeLength):
        population = population_file(self.populationSize, self.chromosomeLength)
        return population
    def calcFitness(self, individual):
        correctGenes = 0
        for geneIndex in range (0,individual.getChomosomeLength()):
            if individual.getGene(geneIndex)==1:
                correctGenes +=1
        fitness= correctGenes/individual.getChomosomeLength()
        individual.setFitness(fitness)
        return fitness
        ...
I think its because you put self.chromosomeLength instead of just chromosomeLength; as you are passing the variable not getting it from the self object...
change line 4 to read:
population = ga.initPopulation(50)
If I write:
population = ga.initPopulation(50) in line 4
the error is:

File "/home/psm/PycharmProjects/AllOnesGA/AllOnesGA.py", line 4, in main
population = ga.initPopulation(50)
File "/home/psm/PycharmProjects/AllOnesGA/genetic_algorithm.py", line 12, in initPopulation
population = population_file(self.populationSize, chromosomeLength)
TypeError: 'module' object is not callable


I think that I must change in the genetic_algorithm
this line:
population = population_file(self.populationSize, chromosomeLength)
for this:
population = population_file.Population(self.populationSize, chromosomeLength)

what do you think?
don't do this:
    population = genetic_algorithm.GeneticAlgorithm.initPopulation(50)
    ga.evalPopulation(population)
do this:
    population = ga.initPopulation(50)
If I do I get the message:

Error:
File "/home/psm/PycharmProjects/AllOnesGA/AllOnesGA.py", line 4, in main population = ga.initPopulation(50) File "/home/psm/PycharmProjects/AllOnesGA/genetic_algorithm.py", line 12, in initPopulation population = population_file(self.populationSize, chromosomeLength) TypeError: 'module' object is not callable
Ok, but that error is different, and inside of the function that you wanted to call, so you are one step closer.
And is on this call:
population = population_file(self.populationSize, self.chromosomeLength)
Please use BBCode tags they are required by forum rules.
population_file is a module.

Look at your imports in genetic_algorithm.py.
You're importing population_file. This is a module. I guess
you want to access a function or class inside this module.