forked from victcebesp/UndergroundRouteGeneticAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
44 lines (36 loc) · 1.87 KB
/
Main.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from generateRoute.IndividualsGenerator import IndividualsGenerator
from generateRoute.TransferManager import TransferManager
from geneticalAlgorithm.operators.Mutation import Mutator
from getData.DataReader import DataReader
from geneticalAlgorithm.Population import Population
from geneticalAlgorithm.operators.Selection import selection
from geneticalAlgorithm.operators.Crossover import crossover
from geneticalAlgorithm.operators.Evaluation import Evaluator
from configuration.initialConfiguration import originStationId, destinyStationId, initialHour, initialMinute
from configuration.hiperparameters import numberOfIterations
generator = IndividualsGenerator(DataReader('data/Datos_AGs_Buscardor_de_Rutas_v0.xlsx'))
population = Population(originStationId, destinyStationId, generator)
evaluator = Evaluator(generator.journeys, TransferManager((initialHour * 60 + initialMinute) % 24))
mutator = Mutator(generator)
evaluator.evaluate(population.individuals)
def printPopulation(index):
global individual, route, gen
print("GENERACIÓN", index + 1)
for individual in population.individuals:
route = ""
for gen in individual.chromosome:
route += str(gen.stationId) + "[" + gen.line + "] "
print(route, "-> fitnessValue:", individual.fitnessValue)
print("\n")
for i in range(0, numberOfIterations):
printPopulation(i)
pairs = selection(population)
offsprings = crossover(pairs)
mutatedIndividuals = mutator.mutate(offsprings)
evaluator.evaluate(mutatedIndividuals)
population.update(mutatedIndividuals)
print("La mejor ruta encontrada entre", originStationId, "y", destinyStationId, "es:")
bestIndividual = sorted(population.individuals, key=lambda x: x.fitnessValue, reverse=True)[0]
for gen in bestIndividual.chromosome:
route += str(gen.stationId) + "[" + gen.line + "] "
print(route, "->", bestIndividual.fitnessValue)