'type' object is not subscriptable
Publicado por psosmol (5 intervenciones) el 16/04/2019 14:05:46
Hola, buenas
Tengo cuatro ficheros: AllOnesGA.py con el main(), Genetic_Algorithm.py, Population_file.py and Individual_file.py
Con pycharm me sale un error:
'type' object is not subscriptable
y el genetic_algorithm.py:
y el population_file.py que creo tiene problemas con la sobrecarga del constructor
Gracias
Tengo cuatro ficheros: AllOnesGA.py con el main(), Genetic_Algorithm.py, Population_file.py and Individual_file.py
Con pycharm me sale un error:
'type' object is not subscriptable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import genetic_algorithm
def main():
ga = genetic_algorithm.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()
y el genetic_algorithm.py:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
.......
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import individual_file
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
def getFittest(self,offset):
for i in range (0,population.size()):
for j in range (0, population.size()):
o1= self.population[i]
if self.population[j-1].getFitness()<self.population[j].getFitness():
temp = self.population[j-1]
self.population[j-1]=self.population[j]
self.population[j]=temp
return self.population[offset]
.........
Gracias
Valora esta pregunta


0