예제 #1
0
def getDistance(route) -> int:
    distance = 0
    for i in range(len(route)):
        if i == len(route) - 1:
            distance += cm.getDistance(route[i], route[0])
        else:
            distance += cm.getDistance(route[i], route[i + 1])

    return distance
예제 #2
0
    def getDistance(self, fresh=False) -> int:
        if self.distance == 0 or fresh:
            for i in range(len(self.tour)):
                if i == len(self.tour) - 1:
                    self.distance += cm.getDistance(self.tour[i], self.tour[0])
                else:
                    self.distance += cm.getDistance(self.tour[i],
                                                    self.tour[i + 1])

        return self.distance
예제 #3
0
    def __init__(self):

        self.startCity: int = randrange(cm.getLength())
        self.possibleCities = [i for i in range(cm.getLength())]

        self.route = []
        self.distanceTravelled = 0.0
        self.currentCity = self.startCity

        self.route.append(self.startCity)
        self.possibleCities.remove(self.currentCity)
예제 #4
0
    def selectCity(self):

        nearestCity = self.possibleCities[0]
        nearestCityDistance = cm.getDistance(self.currentCity, nearestCity)

        for city in self.possibleCities:

            distanceFromCity = cm.getDistance(self.currentCity, city)
            if distanceFromCity < nearestCityDistance:
                nearestCity = city
                nearestCityDistance = distanceFromCity

        return nearestCity
예제 #5
0
    def __init__(self, pheromone_map, alpha, beta):

        self.start_city = randrange(cm.getLength())
        self.possible_cities = [i for i in range(cm.getLength())]

        self.route = []
        self.distance_travelled = 0.0
        self.current_city = self.start_city

        self.alpha = alpha
        self.beta = beta
        self.pheromone_map = pheromone_map

        self.route.append(self.start_city)
        self.possible_cities.remove(self.current_city)
예제 #6
0
    def __init__(self, colony_size, n_best, alpha, beta,
                 pheromone_evaporation_coefficient, pheromone_constant,
                 iterations):

        # Initializing the List of Ants
        self.ant_count = colony_size
        self.ants = []

        # Initializing Pheromone
        self.tour_size = cm.getLength()
        self.pheromone_map = [[0.0] * self.tour_size
                              for _ in range(self.tour_size)]
        self.ant_updated_pheromone_map = [[0.0] * self.tour_size
                                          for _ in range(self.tour_size)]

        # Other Constants
        self.alpha = alpha
        self.beta = beta
        self.n_best = n_best
        self.pheromone_evaporation_coefficient = pheromone_evaporation_coefficient
        self.pheromone_constant = pheromone_constant
        self.iterations = iterations

        # For Solution
        self.shortest_distance = None
        self.shortest_path = None
예제 #7
0
    def __init__(self):

        self.route = []

        # Initializing Random Tour
        for i in range(cm.getLength()):
            self.route.append(i)
        shuffle(self.route)
예제 #8
0
    def __init__(self, beta, initial_temperature):

        self.beta = beta
        self.temperature = initial_temperature
        self.route = []

        # Initializing Random Tour
        for i in range(cm.getLength()):
            self.route.append(i)
        shuffle(self.route)
예제 #9
0
    def findSolution(self):

        # Until Possible Locations is not Empty
        while self.possibleCities:
            nextCity = self.selectCity()

            # Update Route
            self.route.append(nextCity)
            self.possibleCities.remove(nextCity)

            # Update Distance
            self.distanceTravelled += cm.getDistance(self.currentCity,
                                                     nextCity)

            # Update Current Location
            self.currentCity = nextCity

        self.distanceTravelled += cm.getDistance(self.currentCity,
                                                 self.startCity)
예제 #10
0
    def __init__(self) -> None:
        # Holds a candidate Solution
        self.tour: List[int] = []

        # Caching
        self.fitness: float = 0
        self.distance: int = 0

        # Initializing Random Tour
        for i in range(cm.getLength()):
            self.tour.append(i)
        shuffle(self.tour)
예제 #11
0
def loadDataset(datasetName):
    cm.clear()

    # Creating the Random Problem
    if (datasetName[0:6] == 'Random'):
        for _ in range(int(datasetName[7:])):
            cm.addCity(City())

    # Loading the given Dataset
    else:
        with open('Data/' + datasetName + '.txt') as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=' ')
            for row in csv_reader:
                cm.addCity(City(float(row[0]), float(row[1])))

    # Pre-calculating Distances
    cm.calculateDistances()
예제 #12
0
    def selectCity(self):

        city_attraction = []
        total_attraction = 0.0

        for city in self.possible_cities:
            pheromone = self.pheromone_map[self.current_city][city]
            distance = cm.getDistance(self.current_city, city)

            attraction = (pheromone**self.alpha) * ((1 / distance)**self.beta)
            city_attraction.append(attraction)
            total_attraction += attraction

        if total_attraction == 0:
            total_attraction = 1

        return choices(population=self.possible_cities,
                       weights=[
                           attraction / total_attraction
                           for attraction in city_attraction
                       ])[0]
예제 #13
0
 def partialProblem(self, clusterNumber):
     path = []
     for i in range(cm.getLength()):
         if (self.clusteredCities[i] == clusterNumber):
             path.append(i)
     return path