Python Forum
'module' object is not callable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'module' object is not callable
#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:
'module' object is not callable

the AllOnesGA.py is:
(thanks)

import GeneticAlgorithm
def main():
    ga = GeneticAlgorithm(100, 0.001, 0.95, 0)
    population = ga.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()
ps: the wrong lines are 3 and 16
Reply
#2
You are calling the filename itself, you need to call something inside the file GeneticAlgorithm.something_in_the_file
Reply
#3
I don't understand. The file GeneticAlgorithm.py contain the class GeneticAlgorithm and when I put
ga = GeneticAlgorithm(100, 0.001, 0.95, 0) I'm calling the constructor, isn't it?
I don't know what i must write instead of this
thanks


import random
import Population
import Individual

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(self.populationSize,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
#4
Change the filename to genetic_algorithm.py camel case is for Class names
then import genetic_algorithm and use ga = genetic_algorithm.GeneticAlgorithm(100, 0.001, 0.95, 0)
Reply
#5
Hello, now I have the same error in the next line:
population = ga.initPopulation(50)
I don't understand python. If I built the object ga, How is that I can not acces to the members of the class GeneticAlgorithm ( ga object) with the dot operator ga.initPopulation(50)?
Thanks
Reply
#6
Do the same fix for your other files Population.py and Individual.py

Go through these module tutorials
https://python-forum.io/Thread-Basic-Modules-part-1
https://python-forum.io/Thread-Basic-Modules-part-2
https://python-forum.io/Thread-Basic-Modules-part-3
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in class: TypeError: 'str' object is not callable akbarza 2 520 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  TypeError: 'NoneType' object is not callable akbarza 4 1,014 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,378 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  Need help with 'str' object is not callable error. Fare 4 858 Jul-23-2023, 02:25 PM
Last Post: Fare
  working with TLV module Object Jennifer_Jone 3 1,155 Mar-14-2023, 07:54 PM
Last Post: Jennifer_Jone
  TypeError: 'float' object is not callable #1 isdito2001 1 1,089 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  'SSHClient' object is not callable 3lnyn0 1 1,175 Dec-15-2022, 03:40 AM
Last Post: deanhystad
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,466 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov
  API Post issue "TypeError: 'str' object is not callable" makeeley 2 1,931 Oct-30-2022, 12:53 PM
Last Post: makeeley
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 5 1,604 Aug-28-2022, 07:11 AM
Last Post: Melcu54

Forum Jump:

User Panel Messages

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