Python Forum
missing 1 required positional argument:
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
missing 1 required positional argument:
#1

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
        ...
Reply
#2
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...
Reply
#3
change line 4 to read:
population = ga.initPopulation(50)
Reply
#4
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?
Reply
#5
don't do this:
    population = genetic_algorithm.GeneticAlgorithm.initPopulation(50)
    ga.evalPopulation(population)
do this:
    population = ga.initPopulation(50)
Reply
#6
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
Reply
#7
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.
Reply
#8
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error: _vhstack_dispatcher() takes 1 positional argument but 9 were given alexfrol86 3 5,798 May-09-2022, 12:49 PM
Last Post: deanhystad
  What is positional argument self? Frankduc 22 5,671 Mar-06-2022, 01:18 AM
Last Post: Frankduc
  TypeError: missing a required argument: 'y' gible 0 2,894 Dec-15-2021, 02:21 AM
Last Post: gible
  positional argument: 'self' mcmxl22 8 3,268 Dec-13-2021, 10:11 PM
Last Post: deanhystad
  TypeError: missing 3 required positional arguments: wardancer84 9 10,822 Aug-19-2021, 04:27 PM
Last Post: deanhystad
  TypeError: run_oracle_job() missing 1 required positional argument: 'connection_strin python_student 1 1,961 Aug-06-2021, 08:05 PM
Last Post: SheeppOSU
  TypeError: max_value() missing 2 required positional arguments: 'alpha' and 'beta' Anldra12 2 4,198 May-15-2021, 04:15 PM
Last Post: Anldra12
  TypeError: sum() missing 1 required positional argument: 'num2' Insen 3 5,451 Jan-06-2021, 04:25 PM
Last Post: Insen
  Missing 1 required position argument: 'failure' while starting thread. BradLivingstone 3 4,082 Jun-19-2020, 02:31 PM
Last Post: stullis
  TypeError: forward() missing 1 required positional argument: 'x' sveto4ka 4 12,244 Jun-17-2020, 07:25 PM
Last Post: sveto4ka

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020