Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Overload of constructor
#1
Hello, I have for files AllOnesGA.py with the main(), GeneticAlgorithm.py,Population.py and Individual.py

The cuestion is that pycharm mark as an error the overloading of the constructor
thanks
class Population:
    def __init__(self,populationSize):
        self.populationSize=individual_file[populationSize]
    def __init__(self,populationSize,chromosomeLength):
        self.population = individual_file.Individual[populationSize]
        for individualCount in range (0,populationSize):
            individual = individual_file(chromosomeLength)
            self.population[individualCount]= individual
        ...
what I'm doing wrong

ps:the error is
Error:
File "/home/psm/PycharmProjects/AllOnesGA/genetic_algorithm.py", line 12, in initPopulation population = population_file.Population(self.populationSize, chromosomeLength) File "/home/psm/PycharmProjects/AllOnesGA/population_file.py", line 7, in __init__ self.population = individual_file.Individual[populationSize] TypeError: 'type' object is not subscriptable
Reply
#2
I would say
self.population = individual_file.Individual[populationSize]
should be

self.population = individual_file.Individual(populationSize)
note brackets vs square brackets
Also note you don't need __init__() twice. The second one simply override the first one
And based on all your questions so far I would suggest to take [once again] an introductory tutorial on OOP
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hello, I correct some mistake and now I have this new error

Error:
File "/home/psm/PycharmProjects/AllOnesGA/genetic_algorithm.py", line 12, in initPopulation population = population_file.Population(self.populationSize, chromosomeLength) File "/home/psm/PycharmProjects/AllOnesGA/population_file.py", line 8, in __init__ self.population = individual_file.Individual(populationSize) File "/home/psm/PycharmProjects/AllOnesGA/individual_file.py", line 9, in __init__ self.setGene(gene,1) File "/home/psm/PycharmProjects/AllOnesGA/individual_file.py", line 17, in setGene self.chromosome[offset]= gene IndexError: list assignment index out of range
genetic_algorithm.py

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.Population(self.populationSize, chromosomeLength)
        return population
      ........
population_file.py

import random
class Population:
    def __init__(self,populationSize):
        self.populationSize=individual_file(populationSize)
    def __init__(self,populationSize,chromosomeLength):
        self.population = individual_file.Individual(populationSize)
        for individualCount in range (0,populationSize):
            individual = individual_file.Individual(chromosomeLength)
            self.population[individualCount]= individual
    def getIndividuals(self):
        return self.population
     ........
individual_file.py
import random
class Individual:
    def __init__(self,chromosome):
        self.chromosome=chromosome
    def __init__(self,chromosomeLength):
        self.chromosome=[chromosomeLength]
        for gene in range (chromosomeLength):
            if 0.5 < random.randint(0,1):
                self.setGene(gene,1)
            else:
                self.setGene(gene,0)
    def getChromosome(self):
        return self.chromosome
    def getChromosomeLength(self):
        return self.chromosome.size
    def setGene(self,offset,gene):
        self.chromosome[offset]= gene
    def getGene(self,offset):
        return self.chromosome[offset]
    def setFitness(self,fitness):
        self.fitness(fitness)
    def getFitness(self):
        return self.fitness
   ........
Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 974 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Not including a constructor __init__ in the class definition... bytecrunch 3 12,029 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  syntaxerror when entering a constructor MaartenRo 2 2,007 Aug-03-2020, 02:09 PM
Last Post: MaartenRo
  error in constructor overriding in python3 srm 1 1,824 Jul-18-2019, 12:21 PM
Last Post: ichabod801
  This constructor takes no arguments Friend 2 5,350 Jun-26-2019, 02:54 PM
Last Post: Friend
  class constructor with dataframe UGuntupalli 2 2,342 Jun-11-2019, 10:50 PM
Last Post: UGuntupalli
  How I can overload operator [] ? AlekseyPython 3 3,167 Feb-20-2019, 05:38 AM
Last Post: AlekseyPython
  Constructor Rajesh1978 2 3,222 May-22-2018, 05:18 PM
Last Post: micseydel
  I'm trying to make a constructor without hardcoding all of the values RedSkeleton007 7 4,515 Apr-05-2018, 11:12 AM
Last Post: buran
  Author class, with constructor validations nexusfactor 3 2,894 Oct-12-2017, 11:00 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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