def generateMap(self): self.ant = Ants(8, 6) #ants class for x in range(Globals.mapwidth): for y in range(Globals.mapheight): if (y <= Globals.mapheight / 2): if randint(0, 10) > 8: self.tiles[x][y] = choice(self.foliageTiles) else: self.tiles[x][y] = choice(self.groundTiles) else: self.tiles[x][y] = choice( self.undergroundTiles) #underground map return View(self.tiles[:, :]) #tiles[every x, every y]
def RunACO(self): import Ants antList = [] for _ in range(self._antNumber): antList.append( Ants.Ants(graph=self._graph, alpha=self._alpha, beta=self._beta)) for _ in range(self._iterNumber): # print _ for ant in antList: ant.MoveToNextEdge() for edge in self._graph._edge: edge._pheromone = self._roh * edge._pheromone delta = 0.0 for ant in antList: if ant.IsPassEdge(edge._startPoint, edge._endPoint): delta += self._H / ant._value edge._pheromone += delta sum_phe = map(lambda x: x._pheromone, self._graph._edge) # print sum_phe sum_phe = sum(sum_phe) # print sum_phe for edge in self._graph._edge: edge._pheromone /= sum_phe # sum_phe = map(lambda x: x._pheromone, self._graph._edge) # print sum_phe for ant in antList: ant.ResetAnt(graph=self._graph, alpha=self._alpha, beta=self._beta) # print antList best = antList[0] for i in range(1, len(antList)): if antList[i]._value >= best._value: best = antList[i] print best._value print best._path
import matplotlib.pyplot as plt from numpy import * from City import * from Ants import * import math from random import randint import Globals import copy as cp from operator import itemgetter, attrgetter allCities = [City(0, 0) for i in range(Globals.MAX_CITIES)] allAnts = [Ants(0, 0, 0, 0, 0, 0) for i in range(Globals.MAX_ANTS)] rankedAnts = [Ants(0, 0, 0, 0, 0, 0) for i in range(Globals.MAX_ANTS)] distance = [[0.0 for i in range(Globals.MAX_CITIES)] for j in range(Globals.MAX_CITIES)] phermones = [[0.0 for i in range(Globals.MAX_CITIES)] for j in range(Globals.MAX_CITIES)] bestAnt = 0 global bestTour bestTour = Globals.MAX_TOUR def calculateDistance(x2, x1, y2, y1): return math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) def getDistance(): for i in range(Globals.MAX_CITIES):
def initializeAnts(self): self.ants = [None]*self.numAnts for i in range(self.numAnts): self.ants[i] = Ants(self.numCities)