some refactor
This commit is contained in:
parent
c8557f3de6
commit
f6fa3b461b
@ -11,7 +11,7 @@ class GeneticAlgorithm:
|
|||||||
return sorted_by_assess[:max_selected]
|
return sorted_by_assess[:max_selected]
|
||||||
|
|
||||||
def stopCondition(self, i):
|
def stopCondition(self, i):
|
||||||
return i == 100
|
return i == 64
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
population = self.firstPopulation
|
population = self.firstPopulation
|
||||||
@ -29,7 +29,7 @@ class GeneticAlgorithm:
|
|||||||
|
|
||||||
population = newPopulation
|
population = newPopulation
|
||||||
theBestMatch = min(population, key=lambda x: x.fitness)
|
theBestMatch = min(population, key=lambda x: x.fitness)
|
||||||
print("Generation: {} S: {} fitness: {}".format(i, theBestMatch, theBestMatch.fitness))
|
print("Generation: {} S: {} fitness: {}".format(i+1, theBestMatch, theBestMatch.fitness))
|
||||||
|
|
||||||
i += 1
|
i += 1
|
||||||
if self.stopCondition(i):
|
if self.stopCondition(i):
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
from random import randint, sample
|
from random import randint, sample
|
||||||
from math import sqrt
|
from math import sqrt
|
||||||
|
|
||||||
from src.AI.GaTravelingForHerbs.GeneticAlgorithm import GeneticAlgorithm
|
|
||||||
|
|
||||||
START_COORD = [(6, 2)]
|
START_COORD = [(6, 2)]
|
||||||
END_COORD = [(10, 7)]
|
END_COORD = [(10, 7)]
|
||||||
|
|
||||||
|
|
||||||
class Traveling:
|
class Traveling:
|
||||||
def __init__(self, coords):
|
def __init__(self, coords):
|
||||||
self.coords = coords
|
self.coords = coords
|
||||||
@ -21,13 +20,13 @@ class Traveling:
|
|||||||
sum += sqrt((nextCoord[0] - c[0]) ** 2 + (nextCoord[1] - c[1]) ** 2)
|
sum += sqrt((nextCoord[0] - c[0]) ** 2 + (nextCoord[1] - c[1]) ** 2)
|
||||||
return sum
|
return sum
|
||||||
|
|
||||||
def crossover(self, element2: 'Element') -> 'Element':
|
def crossover(self, parentCoords):
|
||||||
childCoords = self.coords[1:int(len(self.coords) / 2)]
|
childCoords = self.coords[1:int(len(self.coords) / 2)]
|
||||||
for coord in element2.coords:
|
for coord in parentCoords.coords:
|
||||||
if coord not in childCoords and coord not in END_COORD + START_COORD:
|
if coord not in childCoords and coord not in END_COORD + START_COORD:
|
||||||
childCoords.append(coord)
|
childCoords.append(coord)
|
||||||
|
|
||||||
if len(childCoords) == len(element2.coords):
|
if len(childCoords) == len(parentCoords.coords):
|
||||||
break
|
break
|
||||||
return Traveling(START_COORD + childCoords + END_COORD)
|
return Traveling(START_COORD + childCoords + END_COORD)
|
||||||
|
|
||||||
@ -39,11 +38,3 @@ class Traveling:
|
|||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return str(self.coords)
|
return str(self.coords)
|
||||||
|
|
||||||
|
|
||||||
# firstGeneration = [Traveling(START_COORD + sample(COORDS, len(COORDS)) + END_COORD) for _ in range(100)]
|
|
||||||
# mutationProbability = float(0.1)
|
|
||||||
#
|
|
||||||
# ga = GeneticAlgorithm(firstGeneration, mutationProbability)
|
|
||||||
# movementList = ga.run()
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user