IndexError: list assignment index out of range
Publicado por psosmol (5 intervenciones) el 16/04/2019 20:54:00
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:
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
population_file.py
individual_file.py
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:
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
1
2
3
4
5
6
7
8
9
10
11
12
13
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
........
Gracias
Valora esta pregunta
0