def __init__(self, screen, font): '''Init game state, player score, game count, etc...''' self.screen = screen self.font = font self.gfx = gfx.Gfx(screen, font) self.state = menu.Menu(self) self.choose_minigame() self.difficulty = 0 self.players = [player.Player(), player.Player()] self.active_player = 0 self.second_turn = False
def __init__(self, ann, grid_seed=1, num_time_steps=60, should_visualize=False): self.agent = agent.Agent() self.grid = grid.Grid(seed=grid_seed) self.agent.set_ann(ann) self.agent.set_grid(self.grid) self.gfx = gfx.Gfx() if should_visualize else None self.num_time_steps = num_time_steps
def __init__(self, border, font, outputs): '''Init game state, player score, game count, etc...''' self.border = border self.screen = border.subsurface( Rect((Game.BORDER_SIZE, Game.BORDER_SIZE), (Game.SCREEN_WIDTH - 2 * Game.BORDER_SIZE, Game.SCREEN_HEIGHT - 2 * Game.BORDER_SIZE))) self.font = font self.gfx = gfx.Gfx(self.screen, font) self.state = menu.Menu(self) self.outputs = outputs self.init()
def __init__(self): self.gfx = gfx.Gfx() object_collection.ObjectCollection.all_boids = [] for i in range(100): object_collection.ObjectCollection.all_boids.append(boid.Boid()) object_collection.ObjectCollection.all_predators = [] for i in range(0): object_collection.ObjectCollection.all_predators.append( predator.Predator()) self.run()
def test_rendering(self): w = world.World() x, y, width = 1, 0, 2 w.set_item(x, y, width) agent_x = 2 w.set_agent(agent_x) g = gfx.Gfx(fps=2) g.draw(w) w.agent.move(-1) w.move_item_down() g.draw(w) w.agent.move(-2) w.move_item_down() g.draw(w) w.agent.move(-3) w.move_item_down() g.draw(w) w.agent.move(-4) w.move_item_down() g.draw(w) g.draw(w)
with open('best_individual.json') as best_agent_weights: weights = json.load(best_agent_weights) if args.scenario == 'pull': genotype_class = BeerTrackerPullGenotype elif args.scenario == 'wall': genotype_class = BeerTrackerWallGenotype else: genotype_class = BeerTrackerGenotype nn = Rnn(num_input_nodes=genotype_class.num_input_nodes, num_hidden_nodes=genotype_class.num_hidden_nodes, num_output_nodes=genotype_class.num_output_nodes) nn.set_weights(weights) g = gfx.Gfx() g.fps = 8 for i in range(args.num_scenarios): seed = i + ((997 * args.generation) if args.mode == 'dynamic' else 0) print '---', 'seed', seed, '---' bt = BeerTracker(nn=nn, seed=seed, scenario=args.scenario) bt.gfx = g bt.run() print bt.world.agent.num_small_misses, 'small miss(es)' print bt.world.agent.num_large_misses, 'large miss(es)' print bt.world.agent.num_partial_captures, 'partial capture(s)' print bt.world.agent.num_small_captures, 'small capture(s)' print bt.world.agent.num_large_captures, 'large capture(s)' if args.scenario == 'pull': print bt.world.agent.num_good_pulls, 'good pulls'
def __init__(self): arg_parser = argparse.ArgumentParser() arg_parser.add_argument('--scenario', dest='scenario', type=str, choices=['standard', 'pull', 'wall'], required=False, default="standard") arg_parser.add_argument('--mode', dest='mode', type=str, choices=['static', 'dynamic'], required=False, default="dynamic") arg_parser.add_argument( '--num-scenarios', dest='num_scenarios', help='Number of scenarios per agent per generation', type=int, required=False, default=3) arg_parser.add_argument('--adult-selection-method', dest='adult_selection_method', type=str, choices=[ 'full_generational_replacement', 'over_production', 'generational_mixing' ], required=False, default="over_production") arg_parser.add_argument('--parent-selection-method', dest='parent_selection_method', type=str, choices=[ 'fitness_proportionate', 'sigma_scaling', 'boltzmann_selection', 'tournament_selection' ], required=False, default="tournament_selection") arg_parser.add_argument('--adult-pool-size', dest='adult_pool_size', help='Max number of adults in the adult pool', type=int, required=False, default=50) arg_parser.add_argument('-p', '--population-size', dest='population_size', help='Number of individuals in a population', type=int, required=False, default=100) arg_parser.add_argument('-g', '--num-generations', dest='num_generations', help='Number of generations', type=int, required=False, default=190) arg_parser.add_argument('--num-runs', dest='num_runs', help='Number of runs', type=int, required=False, default=1) arg_parser.add_argument( '--crossover-rate', dest='crossover_rate', help= 'Probability of sexual reproduction (two parents) instead of asexual reproduction (one parent)', type=float, required=False, default=0.5) arg_parser.add_argument( '--mutation-rate', dest='mutation_rate', help='Probability of gene mutation in new genotypes', type=float, required=False, default=0.5) arg_parser.add_argument( '--silent', nargs='?', dest='silent', help='Add this flag for the program to be less verbose', const=True, required=False, default=False) arg_parser.add_argument('--visualize-every', dest='visualize_every', type=int, required=False, default=-1) self.args = arg_parser.parse_args() if self.args.adult_pool_size < 1 or self.args.adult_pool_size > self.args.population_size: raise Exception( 'adult_pool_size must be a positive integer that is not greater than population_size' ) if self.args.crossover_rate < 0.0 or self.args.crossover_rate > 1.0: raise Exception('crossover_rate must be between 0.0 and 1.0') if self.args.mutation_rate < 0.0 or self.args.mutation_rate > 1.0: raise Exception('mutation_rate must be between 0.0 and 1.0') if self.args.num_scenarios < 1: raise Exception('num_scenarios must be at least 1') if self.args.adult_selection_method == 'generational_mixing' and self.args.mode == 'dynamic': raise Exception( 'Generational mixing and dynamic mode do not work well together' ) ga.BeerTrackerProblem.parse_args() self.problem_class = ga.BeerTrackerProblem self.problem_class.dynamic_mode = self.args.mode == 'dynamic' self.problem_class.num_scenarios = self.args.num_scenarios self.problem_class.scenario = self.args.scenario if self.args.scenario == 'pull': self.genotype_class = ga.BeerTrackerPullGenotype elif self.args.scenario == 'wall': self.genotype_class = ga.BeerTrackerWallGenotype else: self.genotype_class = ga.BeerTrackerGenotype self.individual_class = ga.BeerTrackerIndividual self.individual_class.genotype_class = self.genotype_class if self.args.visualize_every >= 1: import gfx self.beer_tracker_gfx = gfx.Gfx() self.beer_tracker_gfx.fps = 8 logs = [] for i in range(self.args.num_runs): population = self.run() logs.append(population.log) with open('logs.json', 'w') as log_file: json.dump(logs, log_file)
def __init__(self): arg_parser = argparse.ArgumentParser() arg_parser.add_argument( '-g', '--num-generations', dest='num_generations', type=int, required=False, default=100 ) arg_parser.add_argument( '-p', '--population_size', dest='population_size', type=int, required=False, default=50 ) arg_parser.add_argument( '-s', '--seed', dest='seed', type=int, required=False, default=1 ) arg_parser.add_argument( '-v', '--visualize', nargs='?', dest='visualize', help='Visualize the best neural network in each generation', const=True, required=False, default=False ) arg_parser.add_argument( '--visualize-every', dest='visualize_every', type=int, required=False, default=1 ) arg_parser.add_argument( '--num-scenarios', dest='num_scenarios', type=int, required=False, default=5 ) arg_parser.add_argument( '--mode', dest='mode', type=str, choices=['static', 'dynamic'], required=False, default="dynamic" ) arg_parser.add_argument( '--allow-clones', nargs='?', dest='allow_clones', help="""Allow clones or nearly identical genomes to exist simultaneously in the population. This is useful for non-deterministic environments, as the same individual will get more than one chance to prove himself, also there will be more chances the same individual to mutate in different ways. The drawback is greatly increased time for reproduction. If you want to search quickly, yet less efficient, leave this to true.""", const=True, required=False, default=False ) arg_parser.add_argument( '--add-neuron-prob', dest='add_neuron_probability', type=float, help='MutateAddNeuronProb: Probability for a baby to be mutated with the' ' Add-Neuron mutation', required=False, default=0.03 ) arg_parser.add_argument( '--add-link-prob', dest='add_link_probability', type=float, help='MutateAddLinkProb: Probability for a baby to be mutated with the' ' Add-Link mutation', required=False, default=0.03 ) arg_parser.add_argument( '--rem-link-prob', dest='remove_link_probability', type=float, help='MutateRemLinkProb: Probability for a baby to be mutated with the' ' Remove-Link mutation', required=False, default=0.03 ) arg_parser.add_argument( '--rem-simple-neuron-prob', dest='remove_simple_neuron_probability', type=float, help='MutateRemSimpleNeuronProb: Probability for a baby that a simple neuron' ' will be replaced with a link', required=False, default=0.03 ) arg_parser.add_argument( '--fs-neat', nargs='?', dest='fs_neat', help='Use FS-NEAT', const=True, required=False, default=False ) self.args = arg_parser.parse_args() if self.args.visualize: import gfx self.beer_tracker_gfx = gfx.Gfx() self.beer_tracker_gfx.fps = 8 self.log = [] self.run() with open('logs.json', 'w') as log_file: json.dump([self.log], log_file)