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 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]
class MainWindow(QMainWindow): '''Wrapper class for...well, the game? Maybe this needs to be called the game engine then''' def __init__(self): ''' Only initialize critical components(like opengl) here, use start() for anything else ''' QMainWindow.__init__(self) Globals.glwidget = GLWidget(self) self.setCentralWidget(Globals.glwidget) Globals.glwidget.makeCurrent() if Globals.musicOn: print "Using music" Globals.mediaobject = Phonon.MediaObject(self) self.audioOutput = Phonon.AudioOutput(Phonon.MusicCategory, self) Phonon.createPath(Globals.mediaobject, self.audioOutput) self.drawTimer = QTimer() self.drawTimer.timeout.connect(self.drawTimerTimeout) self.drawTimer.start(13) self.map=Map()#map class self.ant=Ants(8,6)#ants class def start(self): if Globals.musicOn: Globals.muspanel = MusPanel(self) #draw map self.map.generateMap() #draw ant self.ant.drawAnt() def drawTimerTimeout(self): Globals.glwidget.updateGL()
def aco(self, file_name, num_ants, iterations, board_dim): datapoints = dh.DataHandler.data_to_points(file_name) file_name = file_name.strip(".csv") aco = Ants.AntFarm(num_ants, datapoints, filename=file_name, max_iterations=iterations, dim=board_dim) df = dh.DataHandler.points_to_dataframe(aco.run()) df = self.dbscan(df, 2, 3) print("scoring {}\n\n".format(file_name)) score = self.evaluate_model(df) with open("{} score.txt".format(file_name), 'w') as results: results.write(str(score)) results.close() print("finished".format(file_name))
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
def __init__(self): ''' Only initialize critical components(like opengl) here, use start() for anything else ''' QMainWindow.__init__(self) Globals.glwidget = GLWidget(self) self.setCentralWidget(Globals.glwidget) Globals.glwidget.makeCurrent() if Globals.musicOn: print "Using music" Globals.mediaobject = Phonon.MediaObject(self) self.audioOutput = Phonon.AudioOutput(Phonon.MusicCategory, self) Phonon.createPath(Globals.mediaobject, self.audioOutput) self.drawTimer = QTimer() self.drawTimer.timeout.connect(self.drawTimerTimeout) self.drawTimer.start(13) self.map=Map()#map class self.ant=Ants(8,6)#ants class
class Map(): ''' Class for generating maps ''' def __init__(self): #Ground tiles self.groundTilesPath = Globals.datadir + 'images/ground/' self.groundTiles = [] #Foliage tiles self.foliageTilesPath = Globals.datadir + 'images/foliage/' self.foliageTiles = [] #Undeground tiles self.undergroundTilesPath = Globals.datadir + 'images/underground/' self.undergroundTiles = [] #Populate list of ground tiles dirList = os.listdir(self.groundTilesPath) for fname in dirList: self.groundTiles.append(Tile(self.groundTilesPath + fname, True)) #Populate list of underground tiles dirList = os.listdir(self.undergroundTilesPath) for fname in dirList: if (fname == "underground1.png"): #underground2.png is for tunnels self.undergroundTiles.append( Tile(self.undergroundTilesPath + fname, True)) #Populate list of foliage tiles dirList = os.listdir(self.foliageTilesPath) for fname in dirList: self.foliageTiles.append(Tile(self.foliageTilesPath + fname, True)) self.tiles = numpy.empty([Globals.mapwidth, Globals.mapheight], dtype=object) #Waiting for mouse move signal Globals.glwidget.mousePress.connect(self.getCoords) 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 update(self): if self.ant.pos != self.ant.newPos: #Globals.view.blackNest() self.ant.move(self.ant.newPos[0] / Globals.pixelsize, self.ant.newPos[1] / Globals.pixelsize) def getCoords(self, button, x, y): ''' On click, move ant ''' if button == 1: self.ant.newPos = [x, y]
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):
class Map(): ''' Class for generating maps ''' def __init__(self): #Ground tiles self.groundTilesPath = Globals.datadir + 'images/ground/' self.groundTiles = [] #Foliage tiles self.foliageTilesPath = Globals.datadir + 'images/foliage/' self.foliageTiles = [] #Undeground tiles self.undergroundTilesPath = Globals.datadir + 'images/underground/' self.undergroundTiles = [] #Populate list of ground tiles dirList = os.listdir(self.groundTilesPath) for fname in dirList: self.groundTiles.append(Tile(self.groundTilesPath + fname, True)) #Populate list of underground tiles dirList = os.listdir(self.undergroundTilesPath) for fname in dirList: if (fname == "underground1.png"):#underground2.png is for tunnels self.undergroundTiles.append(Tile(self.undergroundTilesPath + fname, True)) #Populate list of foliage tiles dirList = os.listdir(self.foliageTilesPath) for fname in dirList: self.foliageTiles.append(Tile(self.foliageTilesPath + fname, True)) self.tiles = numpy.empty([Globals.mapwidth, Globals.mapheight], dtype=object) #Waiting for mouse move signal Globals.glwidget.mousePress.connect(self.getCoords) 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 update(self): if self.ant.pos != self.ant.newPos: #Globals.view.blackNest() self.ant.move(self.ant.newPos[0]/Globals.pixelsize, self.ant.newPos[1]/Globals.pixelsize) def getCoords(self, button, x, y): ''' On click, move ant ''' if button == 1: self.ant.newPos = [x, y]
def initializeAnts(self): self.ants = [None]*self.numAnts for i in range(self.numAnts): self.ants[i] = Ants(self.numCities)