Projekt_AI-Automatyczny_saper/Engine/Fitness.py
2021-06-15 13:12:20 +02:00

38 lines
1.1 KiB
Python

import numpy as np
from Engine.GeneticFinder import GeneticFinder
from Engine.State import State
from Constants import RIGHT
class Fitness:
def __init__(self, route, board, dTree):
self.route = route
self.distance = 0
self.fitness = 0.0
self.board = board
self.dTree = dTree
def routeDistance(self):
if self.distance == 0:
pathDistance = 0
for i in range(0, len(self.route)):
fromPoint = self.route[i]
toPoint = None
if i + 1 < len(self.route):
toPoint = self.route[i + 1]
else:
toPoint = self.route[0]
pathDistance += self.cdistance(fromPoint, toPoint)
self.distance = pathDistance
return self.distance
def routeFitness(self):
if self.fitness == 0:
self.fitness = 1 / float(self.routeDistance())
return self.fitness
def cdistance(self, fromP, toP):
distance = abs(fromP.getX() - toP.getX()) + abs(fromP.getY() - toP.getY())
return distance