def __init__(self): self._service = Service() pygame.init() logo = pygame.image.load("Resources/logo32x32.png") pygame.display.set_icon(logo) pygame.display.set_caption("GA") screen = pygame.display.set_mode( (SQUARE_WIDTH * WIDTH, SQUARE_HEIGHT * HEIGHT)) screen.fill(WHITE) running = True path = [] detectedPositions = [] while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONDOWN: if pygame.mouse.get_pressed()[pygame.BUTTON_RIGHT - 1] == 1: y, x = pygame.mouse.get_pos() dronePosition = (x // SQUARE_HEIGHT, y // SQUARE_WIDTH) try: self._service.setDronePosition(dronePosition) print("Starting...") data, path, detectedPositions, averages, bests = self._service.solver( ) for info, value in data.items(): print(f"{info}: {value}") fig, (ax1, ax2) = plt.subplots(1, 2) fig.suptitle("Averages -- Bests") ax1.plot(range(1, len(averages) + 1), averages, 'o', color='black') ax2.plot(range(1, len(bests) + 1), bests, 'o', color='black') except Exception as exc: print(exc) image = self._service.mapWithPath( path, self._service.mapWithPath(detectedPositions), color=RED) screen.blit(self._service.mapWithDrone(image), (0, 0)) pygame.display.flip() plt.show() pygame.quit()
class UI: def __init__(self): self.service = Service() # self.service.loadEnvironment("Resources/test2.map") def run(self): pygame.init() logo = pygame.image.load("Resources/logo32x32.png") pygame.display.set_icon(logo) pygame.display.set_caption("Resources/drone exploration") screen = pygame.display.set_mode((800, 400)) screen.fill(WHITE) screen.blit(self.service.environmentImage, (0, 0)) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == KEYDOWN: if pygame.key.get_pressed()[K_LEFT]: self.service.slow_down() if pygame.key.get_pressed()[K_RIGHT]: self.service.speed_up() self.service.moveDFS() screen.blit(self.service.detectedMapImage, (400, 0)) pygame.display.flip() pygame.quit()
def __init__(self): self._service = Service() pygame.init() logo = pygame.image.load("Resources/logo32x32.png") pygame.display.set_icon(logo) pygame.display.set_caption("Path in simple environment") screen = pygame.display.set_mode( (SQUARE_WIDTH * WIDTH, SQUARE_HEIGHT * HEIGHT)) screen.fill(WHITE) running = True finalPosition = None dronePosition = None printExecutionTimes = False while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: printExecutionTimes = True if pygame.key.get_pressed()[pygame.K_t]: self._service.showTabu = not self._service.showTabu if pygame.key.get_pressed()[pygame.K_s]: self._service.showSimulatedAnnealing = not self._service.showSimulatedAnnealing if pygame.key.get_pressed()[pygame.K_c]: self._service.computeSimulatedAnnealing = not self._service.computeSimulatedAnnealing if pygame.key.get_pressed()[pygame.K_g]: self._service.showGreedyAndAStar = not self._service.showGreedyAndAStar if event.type == pygame.MOUSEBUTTONDOWN: printExecutionTimes = True if pygame.mouse.get_pressed()[pygame.BUTTON_LEFT - 1] == 1: y, x = pygame.mouse.get_pos() finalPosition = (x // SQUARE_HEIGHT, y // SQUARE_WIDTH) if pygame.mouse.get_pressed()[pygame.BUTTON_RIGHT - 1] == 1: y, x = pygame.mouse.get_pos() dronePosition = (x // SQUARE_HEIGHT, y // SQUARE_WIDTH) if dronePosition is not None: self._service.setDronePosition(dronePosition) screen.blit(self._service.mapWithDrone(), (0, 0)) if finalPosition is not None: screen.blit(self._service.mapWithPath(finalPosition), (0, 0)) if printExecutionTimes: for strategy, ( execution_time, length) in self._service.executionTimes.items(): print( f"{strategy.ljust(10)}: {execution_time} - {length}" ) print() printExecutionTimes = False pygame.display.flip() # pygame.display.flip() # sleep(2) pygame.quit()
def __init__(self): self._service = Service() self._sleepTime = 0.5 pygame.init() logo = pygame.image.load("Resources/logo32x32.png") pygame.display.set_icon(logo) pygame.display.set_caption("GA") screen = pygame.display.set_mode( (SQUARE_WIDTH * WIDTH, SQUARE_HEIGHT * HEIGHT)) screen.fill(WHITE) running = True path = [] detectedPositions = [] pathIndex = None while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONDOWN: if pygame.mouse.get_pressed()[pygame.BUTTON_RIGHT - 1] == 1: y, x = pygame.mouse.get_pos() dronePosition = (x // SQUARE_HEIGHT, y // SQUARE_WIDTH) try: self._service.setDronePosition(dronePosition) print("Starting...") data, path, detectedPositions, averages, bests = self._service.solver( ) for info, value in data.items(): print(f"{info}: {value}") fig, (ax1, ax2) = plt.subplots(1, 2) fig.suptitle("Averages -- Bests") ax1.plot(range(1, len(averages) + 1), averages, 'o', color='black') ax2.plot(range(1, len(bests) + 1), bests, 'o', color='black') except Exception as exc: print(exc) if event.type == pygame.KEYDOWN: if pygame.key.get_pressed()[pygame.K_SPACE]: pathIndex = 0 if pygame.key.get_pressed( )[pygame. K_s] and not pygame.key.get_pressed()[pygame.K_LCTRL]: (fitnessAvg, fitnessStd), (detectedPositionsAvg, detectedPositionsStd) = \ self._service.computeStats("fitness"), self._service.computeStats("detectedPositions") print( f"Fitness average: {fitnessAvg}, detected positions average: {detectedPositionsAvg}" ) print( f"Fitness std: {fitnessStd}, detected positions std: {detectedPositionsStd}" ) if pygame.key.get_pressed()[pygame.K_LEFT]: self.slow_down() if pygame.key.get_pressed()[pygame.K_RIGHT]: self.speed_up() if pygame.key.get_pressed( )[pygame.K_l] and pygame.key.get_pressed()[pygame.K_LCTRL]: try: path = self._service.loadPath( "Resources/Paths/" + input("Path to load: ") + ".path") detectedPositions = self._service.detectedPositions( path) self._service.setDronePosition(path[0]) except Exception as exc: print(exc) if pygame.key.get_pressed( )[pygame.K_s] and pygame.key.get_pressed()[pygame.K_LCTRL]: try: self._service.savePath( path, "Resources/Paths/" + input("Path to save: ") + ".path") except Exception as exc: print(exc) if pathIndex is not None: if pathIndex == len(path): pathIndex = None else: self._service.setDronePosition(path[pathIndex], self._sleepTime) pathIndex += 1 image = self._service.mapWithPath( path, self._service.mapWithPath(detectedPositions), color=RED) image = self._service.mapWithPath(path[-1:], image, color=PURPLE) screen.blit(self._service.mapWithDrone(image), (0, 0)) pygame.display.flip() plt.show() pygame.quit()
def __init__(self): self.service = Service()