class Ui_nodeLibraryView(object): def setupUi(self, nodeLibraryView): nodeLibraryView.setObjectName(_fromUtf8("nodeLibraryView")) nodeLibraryView.resize(447, 443) self.verticalLayout = QtGui.QVBoxLayout(nodeLibraryView) self.verticalLayout.setObjectName(_fromUtf8("verticalLayout")) self.horizontalLayout = QtGui.QHBoxLayout() self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) self.btn_reload = QtGui.QPushButton(nodeLibraryView) self.btn_reload.setMinimumSize(QtCore.QSize(60, 20)) self.btn_reload.setMaximumSize(QtCore.QSize(60, 20)) self.btn_reload.setObjectName(_fromUtf8("btn_reload")) self.horizontalLayout.addWidget(self.btn_reload) spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) self.horizontalLayout.addItem(spacerItem) self.horizontalLayout.setStretch(1, 1) self.verticalLayout.addLayout(self.horizontalLayout) self.nodeList = NodeList(nodeLibraryView) self.nodeList.setObjectName(_fromUtf8("nodeList")) self.verticalLayout.addWidget(self.nodeList) self.verticalLayout.setStretch(1, 1) self.retranslateUi(nodeLibraryView) QtCore.QObject.connect(self.btn_reload, QtCore.SIGNAL(_fromUtf8("clicked()")), nodeLibraryView.onReload) QtCore.QMetaObject.connectSlotsByName(nodeLibraryView) def retranslateUi(self, nodeLibraryView): nodeLibraryView.setWindowTitle(QtGui.QApplication.translate("nodeLibraryView", "Form", None, QtGui.QApplication.UnicodeUTF8)) self.btn_reload.setText(QtGui.QApplication.translate("nodeLibraryView", "Reload", None, QtGui.QApplication.UnicodeUTF8))
def setupUi(self, nodeLibraryView): nodeLibraryView.setObjectName(_fromUtf8("nodeLibraryView")) nodeLibraryView.resize(447, 443) self.verticalLayout = QtModule.QVBoxLayout(nodeLibraryView) self.verticalLayout.setSpacing(0) self.verticalLayout.setContentsMargins(2, 2, 2, 0) self.verticalLayout.setObjectName(_fromUtf8("verticalLayout")) self.horizontalLayout = QtModule.QHBoxLayout() self.horizontalLayout.setSpacing(-1) self.horizontalLayout.setContentsMargins(8, -1, 8, -1) self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) self.btn_reload = QtModule.QPushButton(nodeLibraryView) self.btn_reload.setMinimumSize(QtCore.QSize(60, 20)) self.btn_reload.setMaximumSize(QtCore.QSize(60, 20)) self.btn_reload.setObjectName(_fromUtf8("btn_reload")) self.horizontalLayout.addWidget(self.btn_reload) spacerItem = QtModule.QSpacerItem(40, 20, QtModule.QSizePolicy.Expanding, QtModule.QSizePolicy.Minimum) self.horizontalLayout.addItem(spacerItem) self.horizontalLayout.setStretch(1, 1) self.verticalLayout.addLayout(self.horizontalLayout) self.nodeList = NodeList(nodeLibraryView) self.nodeList.setObjectName(_fromUtf8("nodeList")) self.verticalLayout.addWidget(self.nodeList) self.verticalLayout.setStretch(1, 1) self.retranslateUi(nodeLibraryView) if usePyQt4 : QtCore.QObject.connect(self.btn_reload, QtCore.SIGNAL(_fromUtf8("clicked()")), nodeLibraryView.onReload) else : self.btn_reload.clicked.connect( nodeLibraryView.onReload) QtCore.QMetaObject.connectSlotsByName(nodeLibraryView)
def __init__(self): self.__map = Map() self.__map.loadMap("test1.map") self.__mapSurface = self.__map.getMapSurface() self.__drone = Drone(5, 5) self.__nodeList = NodeList(self.__map) self.__placeDroneOnEmptyPosition() self.__pheromoneTable = [[1.0 for _ in range(Constants.NODE_COUNT)] for _ in range(Constants.NODE_COUNT)] self.__distanceTable = self.__nodeList.getDistanceBetweenNodes()
def __computeProbabilityOfChoosingNextNode(self, possibleMoves, alpha, beta, distanceTable, pheromoneTable): currentNodeIndex = self.__path[-1] nextNodeProbability = [0 for _ in range(Constants.NODE_COUNT)] for moveIndex in possibleMoves: distanceToNextNode = distanceTable[currentNodeIndex][moveIndex] if NodeList.isEnergyNode(moveIndex): distanceToNextNode = 5 - distanceToNextNode pheromoneToNextNode = pheromoneTable[currentNodeIndex][moveIndex] probability = ( (45 - distanceToNextNode)**beta) * (pheromoneToNextNode**alpha) nextNodeProbability[moveIndex] = probability return nextNodeProbability
class Service: def __init__(self): self.__map = Map() self.__map.loadMap("test1.map") self.__mapSurface = self.__map.getMapSurface() self.__drone = Drone(5, 5) self.__nodeList = NodeList(self.__map) self.__placeDroneOnEmptyPosition() self.__pheromoneTable = [[1.0 for _ in range(Constants.NODE_COUNT)] for _ in range(Constants.NODE_COUNT)] self.__distanceTable = self.__nodeList.getDistanceBetweenNodes() def __placeDroneOnEmptyPosition(self): crtX, crtY = self.__drone.getX(), self.__drone.getY() while self.__mapSurface[crtX][crtY] != Constants.EMPTY_POSITION: crtX, crtY = random.randint(0, Constants.MAP_HEIGHT - 1), random.randint( 0, Constants.MAP_WIDTH - 1) self.__drone.setX(crtX) self.__drone.setY(crtY) def __selectBestAnt(self, ants): bestAnt = None bestFitness = 0 for ant in ants: if bestFitness < ant.getFitness(): bestFitness = ant.getFitness() bestAnt = ant return bestAnt def __simulateEpoch(self, antCount, alpha, beta, q0, rho): ants = [Ant() for _ in range(antCount)] # move the ants; remove those which don't reach the end for ant in ants: for step in range( Constants.MOVE_COUNT - 1 ): # subtract 1 because we init the ant by placing it on a node, so 1 node is already taken? ant.nextMove(self.__distanceTable, self.__pheromoneTable, q0, alpha, beta) ant.computeFitness(self.__mapSurface, self.__nodeList.getNodeList()) # simulate pheromone evaporation; it has to be done even if all ants die for i in range(Constants.NODE_COUNT): for j in range(Constants.NODE_COUNT): self.__pheromoneTable[i][j] *= (1 - rho) # add the pheromones produced by the last batch of ants for ant in ants: if ant.getFitness() == Constants.TOTAL_EMPTY_POSITIONS: newPheromone = 0.0001 # it's 29 times smaller than 1 / 340, but not quite 0 else: newPheromone = 1.0 / ant.getFitness() currentPath = ant.getPath() for i in range(len(currentPath) - 1): crtNode = currentPath[i] nextNode = currentPath[i + 1] self.__pheromoneTable[crtNode][nextNode] += newPheromone return self.__selectBestAnt(ants) def __updateBestSolution(self, bestSolution): currentSolution = self.__simulateEpoch(Constants.ANT_COUNT, Constants.ALPHA, Constants.BETA, Constants.Q0, Constants.RHO) if currentSolution is None: return bestSolution currentSolutionPathLength = len(currentSolution.getPath()) if bestSolution is None or currentSolutionPathLength > len( bestSolution.getPath()) or (currentSolutionPathLength == len( bestSolution.getPath()) and currentSolution.getFitness() < bestSolution.getFitness()): return currentSolution # new best solution return bestSolution def getSolutionFromPath(self, path): # path is of the form: entry node, energy node, exit node... sensorEnergyPairs = [] for i in range(0, len(path), 3): sensor = self.__nodeList.getNodeList()[path[i]] sensorEnergyPairs.append( ((sensor.getX(), sensor.getY()), path[i + 1] - path[i] - 1)) return sensorEnergyPairs def run(self): bestSolution = None # will be the one with the largest number of visible positions for epoch in range(Constants.EPOCH_COUNT): bestSolution = self.__updateBestSolution(bestSolution) return bestSolution def getMapSurface(self): return self.__map.getMapSurface() def getNodeList(self): return self.__nodeList.getNodeList() def getDroneXCoord(self): return self.__drone.getX() def getDroneYCoord(self): return self.__drone.getY()