def test_step_3(self): vo_mock = Mock() vo_mock.side_effect = [ fill_step_mock(self.brain_name, np.full((10, 10, 1), 0.17)), fill_step_mock(self.brain_name, np.full((10, 10, 1), 0.23)), fill_step_mock(self.brain_name, np.full((10, 10, 1), 0.02)), fill_step_mock(self.brain_name, np.full((10, 10, 1), 0.11)), fill_step_mock(self.brain_name, np.full((10, 10, 1), 0.90)), fill_step_mock(self.brain_name, np.full((10, 10, 1), 0.14)), fill_step_mock(self.brain_name, np.full((10, 10, 1), 0.10)), fill_step_mock(self.brain_name, np.full((10, 10, 1), 0.19)) ] self.env_mock.step = vo_mock self.env_provider_mock.provide.return_value = self.env_mock cfg = Config('tests/config.json') cfg.set_config_value('OBS_SIZE', 10) cfg.set_config_value('FRAMES_SKIP', 0) cfg.set_config_value('FRAMES_LOOKBACK', 5) cfg.set_config_value('USE_DIFF', True) env = Environment(self.env_provider_mock, cfg) res = env.step(1) self.assertTrue( frames_helper( res["stacked_observation"], [0, 0, 0, 0, 0], True)) res = env.step(1) self.assertTrue( frames_helper( res["stacked_observation"], [0, 0, 0, 0, 0.06], True)) res = env.step(1) self.assertTrue( frames_helper( res["stacked_observation"], [0, 0, 0, 6, -21], True)) res = env.step(1) self.assertTrue( frames_helper( res["stacked_observation"], [0, 0, 6, -21, 9], True)) res = env.step(1) self.assertTrue( frames_helper( res["stacked_observation"], [0, 6, -21, 9, -2], True)) res = env.step(1) self.assertTrue( frames_helper( res["stacked_observation"], [6, -21, 9, -2, 5], True)) res = env.step(1) self.assertTrue( frames_helper( res["stacked_observation"], [-21, 9, -2, 5, -4], True)) res = env.step(1) self.assertTrue( frames_helper( res["stacked_observation"], [9, -2, 5, -4, 9], True))
def test_step_1(self): self.env_mock.step.return_value = fill_step_mock( self.brain_name, np.random.rand(10, 10, 1)) self.env_provider_mock.provide.return_value = self.env_mock cfg = Config('tests/config.json') cfg.set_config_value('OBS_SIZE', 10) cfg.set_config_value('FRAMES_SKIP', 4) cfg.set_config_value('FRAMES_LOOKBACK', 5) cfg.set_config_value('USE_DIFF', False) env = Environment(self.env_provider_mock, cfg) res = env.step(1) self.assertEqual(res["stacked_observation"].shape, (1, 10, 10, 5))
def init_environment(self, pool, end, q_source=None, q_target=None): return [ pool.apply_async(Environment().loop, args=( end, q_source, q_target, )) ]
def __init__( self ): pygame.init() self.running = True self.will_render = True self.display = pygame.display.set_mode( [config.window_width, config.window_height], pygame.HWSURFACE | pygame.DOUBLEBUF ) self.clock = pygame.time.Clock() self.font = pygame.font.SysFont("monospace", 12) self.environment = Environment() self.database = Database() self.steps = 0 self.sim_start = pygame.time.get_ticks() self.display.fill((255,255,255)) pygame.display.flip()
class Main: def __init__( self ): pygame.init() self.running = True self.will_render = True self.display = pygame.display.set_mode( [config.window_width, config.window_height], pygame.HWSURFACE | pygame.DOUBLEBUF ) self.clock = pygame.time.Clock() self.font = pygame.font.SysFont("monospace", 12) self.environment = Environment() self.database = Database() self.steps = 0 self.sim_start = pygame.time.get_ticks() self.display.fill((255,255,255)) pygame.display.flip() def execute( self ): while( self.running ): if not self.will_render: self.clock.tick(60) for event in pygame.event.get(): self.events(event) keys = pygame.key.get_pressed() if keys[pygame.K_SPACE]: self.will_render = False else: self.will_render = True self.update() self.render() self.steps += 1 if self.environment.generations == config.maximum_generations: self.running = False self.cleanup() def events( self, event ): if event.type == pygame.QUIT: self.running = False def update( self ): self.environment.update_all() if self.environment.has_active == False: self.environment.perform_scoring() self.database.save_population( self.environment.members ) self.environment.perform_selection() self.environment.perform_crossover() self.environment.perform_mutation() self.environment.generations += 1 def render( self ): self.display.fill((255,255,255)) for obj in self.environment.get_objects(): obj.draw( self.display ) pygame.draw.rect( self.display, [150,150,150], [config.viewport_width, 0, config.window_width - config.viewport_width, config.viewport_height]) pygame.draw.aaline( self.display, [0,0,0], [config.viewport_width, 0], [config.viewport_width, config.viewport_height], 1) labels = [ self.font.render("Mutation Rate: " + str(config.mutation_rate * 100) + '%', 1, [0,0,0] ), self.font.render("Selection Rate: " + str(config.selection_rate * 100) + '%', 1, [0,0,0] ), self.font.render("Simulation Steps: " + str(self.steps), 1, [0,0,0] ), self.font.render("Generation: " + str(self.environment.generations), 1, [0,0,0] ), self.font.render("Members Alive: " + str(self.environment.remaining), 1, [0,0,0] ), self.font.render("Overall Best:", 1, [0,0,0] ) ] if self.environment.overall_best != None: labels.append(self.font.render("Score : " + str(self.environment.overall_best.score), 1, [0,0,0] )) labels.append(self.font.render("Neurons : " + str(len(self.environment.overall_best.brain.all_neurons())), 1, [0,0,0] )) labels.append(self.font.render("Food : " + str(self.environment.overall_best.food), 1, [0,0,0] )) padding_increment = 10 for ind, label in enumerate(labels): self.display.blit( label, (config.viewport_width + config.panel_padding, padding_increment)) if ind == 4 or ind == 1: padding_increment += 20 else: padding_increment += 10 pygame.display.flip() def cleanup( self ): self.database.graph_optimization() self.database.generate_csv() pygame.quit()
def argparser(): parser = argparse.ArgumentParser(description='Chips and Circuits') parser.add_argument('--netlist', default=1, type=int, choices=[1, 2, 3, 4, 5, 6], help='choose the netlist.') parser.add_argument('--algorithm', choices=[ "astar", "genetic", "hillclimbing", "randomwalk", "hillclimbing_solution" ], help='the algorithm that is used.') parser.add_argument('--astar-complete', default=1, type=int, choices=[0, 1], help='Whether to find a complete solution') parser.add_argument('--genetic-poolSize', default=500, type=int, help='The pool size for genetic algorithm') parser.add_argument('--genetic-parentSize', default=25, type=int, help='The parent size(pair) for genetic algorithm') parser.add_argument('--genetic-generationSize', default=30, type=int, help='The generation size for genetic algorithm') parser.add_argument('--steps', default=50, type=int, help='The steps for hill climber') parser.add_argument( '--amount', default=3, type=int, help='The amount of wires changed in a transformation for hill climber' ) parser.add_argument('--retry', default=3, type=int, help='The allowed max retry in a step in hill climber') parser.add_argument( '--savechip', default=False, type=bool, choices=[0, 1], help='Save the best chip in json file after hill climbing') parser.add_argument('--showchip', default=False, type=bool, choices=[0, 1], help='Show the best chip after hill climbing') parser.add_argument('--result', default=True, type=bool, choices=[0, 1], help='Show the hill climbing process') parser.add_argument('--savechip_name', default="hillclimbing_bestchip.json", type=str, help='The name of the chip json file') parser.add_argument('--showchip_name', default="hillclimbing_bestchip", type=str, help='The name of the chip plot') parser.add_argument('--result_name', default="hillclimbing_result", type=str, help='The name of the plot of hill climbing process') args = parser.parse_args() if args.algorithm != 'astar' and args.astar_complete != True: parser.error( '--astar-complete can only be set when --algorithm=astar.') if args.algorithm != "genetic" and args.genetic_poolSize != 500: parser.error( '--genetic-poolSize can only be set when --algorithm=genetic.') if args.algorithm != 'genetic' and args.genetic_parentSize != 25: parser.error( '--genetic-parentSize can only be set when --algorithm=genetic.') if args.algorithm != 'genetic' and args.genetic_generationSize != 30: parser.error( '--genetic-generationSize can only be set when --algorithm=genetic.' ) env = Environment(args.netlist) steps = args.steps amount = args.amount retry = args.retry savechip = args.savechip showchip = args.showchip result = args.result savechip_name = args.savechip_name showchip_name = args.showchip_name result_name = args.result_name algos = { "astar": AstarSpfa(env).run, "genetic": GeneticAlgorithm(env).run, "hillclimbing": HillClimber(env, steps=steps, amount=amount, retry=retry, save_chip=savechip, show_chip=showchip, show_lineplot=result, chip_filename=savechip_name, chip_plotname=showchip_name, lineplot_filename=result_name).hillclimbing, "randomwalk": HillClimber(env, steps=steps, amount=amount, retry=retry, save_chip=savechip, show_chip=showchip, show_lineplot=result, chip_filename=savechip_name, chip_plotname=showchip_name, lineplot_filename=result_name).randomwalk, "hillclimbing_solution": HillClimber(env, steps=steps, amount=amount, retry=retry, save_chip=savechip, show_chip=showchip, show_lineplot=result, chip_filename=savechip_name, chip_plotname=showchip_name, lineplot_filename=result_name).hillclimbing_solution, } if args.algorithm == "astar": algos[args.algorithm](args.astar_complete) elif args.algorithm == "genetic": algos[args.algorithm](args.genetic_poolSize, args.genetic_parentSize, args.genetic_generationSize) elif args.algorithm == "hillclimbing" or \ args.algorithm == "randomwalk" or \ args.algorithm == "hillclimbing_solution": algos[args.algorithm]() else: parser.print_help()
import csv import os import sys from functions.menu import menu from classes.environment import Environment try: village = int(sys.argv[1]) if village == 1: env = Environment(r"..\data\wijk1_huizen.csv", r"..\data\wijk1_batterijen.csv", 1) elif village == 2: env = Environment(r"..\data\wijk2_huizen.csv", r"..\data\wijk2_batterijen.csv", 2) elif village == 3: env = Environment(r"..\data\wijk3_huizen.csv", r"..\data\wijk3_batterijen.csv", 3) except IndexError: sys.exit( "Please use main.py with a village number. For example: main.py 1") print( "What algorithm would you like to run? Type the number corresponding with the algorithm.\n" ) print( "To make a model of the connection between houses and batteries: \n1: Random \n2: Depth first with branch & bound \n3: Stochastic hillclimber \n4: Simulated Annealing \n5: Evolution\n" ) print( "To make a model that determines the locations of the batteries: \n6: K-means \n7: Stochastic hillclimber on the location of batteries" ) choice = int(input())
from classes.environment import Environment from classes.agent import PolicyGradientAgent from classes.config import Config from classes.unity_env_provider import UnityEnvironmentProvider from utils import * import numpy as np import logging try: experiment_id = get_experiment_id() cfg = Config('config.json') env_provider = UnityEnvironmentProvider(cfg) env = Environment(env_provider, cfg) agent = PolicyGradientAgent(env, cfg, experiment_id) create_folders(experiment_id) agent.learn() except Exception as e: logging.exception("message")
def main_parallel(blind, mode, individuals, numAsteroids, generation2load = None): global SCREEN, BG, GAME_AREA, MUTATION_PROBABILITY currentIndividual = 0 currentGeneration = 0 if blind == True: initDraw() # Init the ships ships = None #if mode == 'genetic': if generation2load is None: print("Inicializando uma população do zero") ships = initializePopulation(individuals, numAsteroids) # # of ships and asteroids in each ship saveGeneration(ships, currentGeneration) else: generation = generation2load[0] individualNum = generation2load[1] ships = loadGeneration(generation) #ships = loaded_data[0] currentGeneration = generation environment = Environment(individuals, ships) numThreads = 8 while True: # Here I have to generate a list so that each position is a arguments bag that each process will pick argumentBag = [] for currentIndividual in range(int(individuals/numThreads)): for i in range(numThreads): individualNum = i + currentIndividual*numThreads ship1 = ships[individualNum] argumentBag.append([ship1, individualNum]) with multiprocessing.Pool(processes=numThreads) as pool: pool.map(MainLooping, argumentBag) pool.close() pool.join() for i in range(individuals): ships[ship_[i][1]] = copy.deepcopy(ship_[i][0]) ship_[:] = [] #print(ships[1].brain) print("REPLICATING..") environment.replicate(MUTATION_PROBABILITY) currentIndividual = 0 restoreScene(ships, numAsteroids) currentGeneration += 1 saveGeneration(ships, currentGeneration) MUTATION_PROBABILITY -= MUTATION_PROBABILITY*0.01 print("Nova MP: " + str(MUTATION_PROBABILITY)) pygame.quit()
def main(blind, mode, individuals, numAsteroids, generation2load = None): global SCREEN, BG, GAME_AREA, METADATA currentIndividual = 0 currentGeneration = 0 drawNeuralNetwork = None if blind == True: initDraw() drawNeuralNetwork = DrawNN([8,16,16,4], METADATA) # Init the ships ships = None #if mode == 'genetic': generation = 0 if generation2load is None: print("Inicializando uma população do zero") ships = initializePopulation(individuals, numAsteroids, drawNeuralNetwork) # # of ships and asteroids in each ship saveGeneration(ships, currentGeneration) else: generation = generation2load[0] individualNum = generation2load[1] ships = loadGeneration(generation, drawNeuralNetwork) #ships = loaded_data[0] currentGeneration = generation currentIndividual = individualNum # But a repeatead IF? Yes, m**********r if blind == True: lines = np.asarray(np.loadtxt("checkpoints/scores.txt", comments="#", delimiter="-", unpack=False)).astype(int) lines = lines[:,0] lines = lines[0:generation+1] PLOT = plotData(lines) #elif mode == "normalGame": # asteroids = initAsteroids(1) # ships = initShips(1, asteroids) environment = Environment(individuals, ships) clock = pygame.time.Clock() running = True random.seed(ships[currentIndividual].seed) cont = 0 while running: ship = ships[currentIndividual] # UPDATE SHIPS EM PARALELO if update(ship) == 'dead': print("O individuo " + str(currentIndividual) + " morreu") currentIndividual += 1 if generation2load is not None: exit() if currentIndividual == individuals: print("REPLICATING..") environment.replicate(MUTATION_PROBABILITY) print(ships) currentIndividual = 0 restoreScene(ships, numAsteroids) currentGeneration += 1 if generation2load is None: saveGeneration(ships, currentGeneration) print("Iniciando a geração " + str(currentGeneration)) # Restore the seed of the new individual just before update him random.seed(ships[currentIndividual].seed) continue if blind == True: draw(ship, drawNeuralNetwork) # Score text SCORE = plotScore(str(ship.shotsHit-1)) METADATA.blit(SCORE, (125,20)) # Score chart PLOT.set_colorkey((0,255,0)) METADATA.blit(PLOT, (10,450)) SCREEN.blit(GAME_AREA, (METADATA_SPACE,0)) SCREEN.blit(METADATA, (0,0)) #pygame.image.save(SCREEN, "/home/davi/asteroids/video/frames_dos_games/1/"+str(cont)+".png") cont += 1 pygame.display.update() SCREEN.blit(BG, (METADATA_SPACE,0)) GAME_AREA.fill((0, 255, 0)) METADATA.fill((0, 0, 0)) keyPressed = readKeys() updatePosition(keyPressed, ships) if keyPressed == False: running = False clock.tick(10000) pygame.quit()