def test_select_mating_pool_SUS(): np.random.seed(11) evo = Evolution( population_size=10, population=np.array(list(range(1, 11))), genotype_size=1, fitness_normalization_mode='FPS', selection_mode='SUS', reproduction_mode='GENETIC_ALGORITHM', evaluation_function=lambda _: [0.3, 0.2, 0.1, 0.1, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05], max_generation=0, elitist_fraction=0.2, mating_fraction=0.8, # in beer this is 1 - elitist_fraction ) evo.run() evo.fitnesses = np.array( [0.3, 0.2, 0.1, 0.1, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05]) # print("Fitnesses: {}".format(evo.fitnesses)) assert evo.n_mating == 8 assert evo.n_elite == 2 assert evo.n_fillup == 0 mating_pool = evo.select_mating_pool() print(mating_pool) assert mating_pool == [1, 1, 1, 2, 3, 4, 6, 8] print('SUCCESS!')
def test_v003(): folder_path = './data/tmp1' utils.make_dir_if_not_exists(folder_path) evo = Evolution( random_seed=utils.random_int(), population_size=4, genotype_size=2, evaluation_function=lambda pop, seeds: np.sum( pop, axis=1), #np.arange(1,0,-1/len(pop)), performance_objective=0.5, #MAX MIN ZERO ABS_MAX fitness_normalization_mode='NONE', selection_mode='UNIFORM', # UNIFORM, SUS, RWS reproduce_from_elite=True, reproduction_mode= 'GENETIC_ALGORITHM', # 'GENETIC_ALGORITHM' 'HILL_CLIMBING' mutation_variance=0.1, elitist_fraction=0.5, mating_fraction=0.5, crossover_probability=0.5, crossover_mode='UNIFORM', max_generation=100, termination_function=None, checkpoint_interval=1, folder_path=folder_path, timeit=True) evo.run() evo.timing.report()
def test_select_mating_pool_RWS(): from collections import Counter performances = np.array([1000, 1000, 300, 200, 200, 100, 50, 50, 50, 50]) np.random.seed(11) evo = Evolution( population_size=10, population=np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), genotype_size=1, fitness_normalization_mode='RANK', selection_mode='RWS', evaluation_function=lambda _: performances, max_generation=0, elitist_fraction=0.2, mating_fraction=0.8, # in beer this is 1 - elitist_fraction ) evo.run() print("Fitnesses: {}".format(evo.fitnesses)) # assert evo.n_elite == 2 # only in genetic algorithm # assert evo.n_fillup == 0 # only in genetic algorithm assert evo.n_mating == evo.population_size mating_counter = Counter() repetitions = 10000 for _ in range(repetitions): mating_pool = evo.select_mating_pool() for e in mating_pool: mating_counter[e] += 1 # we check if the number of time each genome is selected is proportionate to the its performance (and fitness) # not guaranteed to work for fitness_normalization_mode == 'FPS' because of the linear scaling # not easy to test this further (not implemented in beer) for i in range(2, 9): assert mating_counter[i] > mating_counter[i + 1] print(sorted(mating_counter.items(), key=lambda kv: kv[0])) print('SUCCESS!')
def test_fitness_RANK(): performances = np.array([1, 50, 15, 21, 100, 23, 88, 45, 44, 76]) evo = Evolution(population_size=10, genotype_size=0, fitness_normalization_mode='RANK', evaluation_function=lambda _: performances, max_generation=0) evo.run() correct_fitness_rank = sorted([ 0.11000000000000001, 0.10777777777777779, 0.10555555555555556, 0.10333333333333335, 0.10111111111111111, 0.09888888888888889, 0.09666666666666666, 0.09444444444444444, 0.09222222222222222, 0.09 ], reverse=True) assert list(evo.fitnesses) == correct_fitness_rank # print('fitnesses_rank: {}'.format(fitnesses_rank)) print('SUCCESS!')
def time_evolution(): evo = Evolution( random_seed=123, population_size=5000, genotype_size=100, evaluation_function=lambda pop, seeds: [1] * len(pop), fitness_normalization_mode='RANK', selection_mode='RWS', # SUS, RWS reproduction_mode= 'GENETIC_ALGORITHM', # 'GENETIC_ALGORITHM' 'HILL_CLIMBING' mutation_variance=0.1, elitist_fraction=0.1, mating_fraction=0.9, crossover_probability=0.5, crossover_mode='1-POINT', max_generation=100, termination_function=None, checkpoint_interval=1, timeit=True) evo.run() evo.timing.report()
def test_fitness_FPS(): performances = np.array([1, 50, 15, 21, 100, 23, 88, 45, 44, 76]) evo = Evolution(population_size=10, genotype_size=0, fitness_normalization_mode='FPS', evaluation_function=lambda _: performances, max_generation=0) evo.run() correct_fitness_fps = sorted([ 0.09156424581005587, 0.10068901303538176, 0.09417132216014897, 0.09528864059590317, 0.11000000000000001, 0.0956610800744879, 0.10776536312849162, 0.09975791433891994, 0.09957169459962756, 0.10553072625698326 ], reverse=True) assert list(evo.fitnesses) == correct_fitness_fps # print('fitnesses_fps: {}'.format(fitnesses_fps)) print('SUCCESS!')
def test_continuation(): eval_func = lambda pop, rand_seed: RandomState(rand_seed).random(len(pop)) print('Loading evolution from json file') folder_path = './tmp_cont1' utils.make_dir_if_not_exists(folder_path) evo1 = Evolution( random_seed=123, population_size=1000, genotype_size=2, evaluation_function=eval_func, fitness_normalization_mode='RANK', selection_mode='RWS', reproduction_mode= 'GENETIC_ALGORITHM', #'HILL_CLIMBING', 'GENETIC_ALGORITHM' mutation_variance=0.1, elitist_fraction=0.1, mating_fraction=0.9, crossover_probability=0.5, crossover_mode='1-POINT', max_generation=100, folder_path=folder_path, termination_function=None, checkpoint_interval=50) evo1.run() print() new_folder_path = './tmp_cont2' utils.make_dir_if_not_exists(new_folder_path) evo2 = Evolution.load_from_file(os.path.join(folder_path, 'evo_00050.json'), evaluation_function=eval_func, folder_path=new_folder_path, max_generation=110) evo2.run()
evo = Evolution( random_seed=args.seed, population_size=args.popsize, genotype_size=genotype_size, evaluation_function=sim.evaluate, performance_objective=args.perf_obj, fitness_normalization_mode= 'NONE', # 'NONE', FPS', 'RANK', 'SIGMA' -> NO NORMALIZATION selection_mode='UNIFORM', # 'UNIFORM', 'RWS', 'SUS' reproduce_from_elite=True, reproduction_mode= 'GENETIC_ALGORITHM', # 'HILL_CLIMBING', 'GENETIC_ALGORITHM' mutation_variance=0.1, # mutation noice with variance 0.1 elitist_fraction=0.04, # elite fraction of the top 4% solutions mating_fraction=0.96, # the remaining mating fraction crossover_probability=0.1, crossover_mode='UNIFORM', crossover_points=None, #genotype_structure['crossover_points'], folder_path=outdir, max_generation=args.num_gen, termination_function=None, checkpoint_interval=np.ceil(args.num_gen / 100), ) evo.run() if args.entropy_type == 'transfer': # shutdown JVM transfer_entropy.shutdown_JVM() print('Ellapsed time: {}'.format(t.tocvalue()))
def main(raw_args=None): parser = argparse.ArgumentParser( description='Run the Division of Labor Simulation' ) # evolution arguments parser.add_argument('--seed', type=int, default=0, help='Random seed') parser.add_argument('--dir', type=str, default=None, help='Output directory') parser.add_argument('--perf_obj', default='MAX', help='Performance objective') # 'MAX', 'MIN', 'ZERO', 'ABS_MAX' or float value parser.add_argument('--gen_zfill', type=bool, default=False, help='whether to fill geotipes with zeros (True) or random (false - default)') parser.add_argument('--popsize', type=int, default=96, help='Population size') parser.add_argument('--max_gen', type=int, default=10, help='Number of generations') # simulation arguments parser.add_argument('--num_neurons', type=int, default=2, help='Number of neurons in agent') parser.add_argument('--num_dim', type=int, choices=[1, 2], default=1, help='Number of dimensions of the simulation') parser.add_argument('--num_trials', type=int, default=4, help='Number of trials') parser.add_argument('--trial_duration', type=int, default=50, help='Trial duration') parser.add_argument('--num_random_pairings', type=int, default=None, help='None -> agents are alone in the simulation (default). ' '0 -> agents are evolved in pairs: a genotype contains a pair of agents. ' 'n>0 -> each agent will go though a simulation with N other agents (randomly chosen).') parser.add_argument('--switch_agents_motor_control', type=bool, default=False, help= 'when num_agents is 2 this decides whether the two agents switch control of L/R motors ' 'in different trials (switch=True) or not (switch=False) in which case the first agent ' 'always control the left motor and the second the right') parser.add_argument('--exclusive_motors_threshold', type=float, default=None, help='prevent motors to run at the same time') parser.add_argument('--dual_population', type=bool, default=False, help='If to evolve two separate populations, one always controlling the left ' 'motor and the other the right') parser.add_argument('--cores', type=int, default=1, help='Number of cores') # Gather the provided arguements as an array. args = parser.parse_args(raw_args) t = TicToc() t.tic() genotype_structure = gen_structure.DEFAULT_GEN_STRUCTURE(args.num_dim, args.num_neurons) genotype_size = gen_structure.get_genotype_size(genotype_structure) if args.dir is not None: # create default path if it specified dir already exists if os.path.isdir(args.dir): subdir = '{}d_{}n'.format(args.num_dim, args.num_neurons) if args.exclusive_motors_threshold is not None: subdir += '_exc-{}'.format(args.exclusive_motors_threshold) if args.gen_zfill: subdir += '_zfill' if args.num_random_pairings is not None: subdir += '_rp-{}'.format(args.num_random_pairings) if args.switch_agents_motor_control: subdir += '_switch' if args.dual_population: subdir += '_dual' seed_dir = 'seed_{}'.format(str(args.seed).zfill(3)) outdir = os.path.join(args.dir, subdir, seed_dir) else: # use the specified dir if it doesn't exist outdir = args.dir utils.make_dir_if_not_exists_or_replace(outdir) else: outdir = None checkpoint_interval = int(np.ceil(args.max_gen / 10)) sim = Simulation( genotype_structure=genotype_structure, num_dim=args.num_dim, num_trials=args.num_trials, trial_duration=args.trial_duration, # the brain would iterate trial_duration/brain_step_size number of time num_random_pairings=args.num_random_pairings, switch_agents_motor_control=args.switch_agents_motor_control, exclusive_motors_threshold=args.exclusive_motors_threshold, dual_population=args.dual_population, num_cores=args.cores ) if outdir is not None: sim_config_json = os.path.join(outdir, 'simulation.json') sim.save_to_file(sim_config_json) if args.num_random_pairings == 0: genotype_size *= 2 # two agents per genotype num_populations = 2 if args.dual_population else 1 population = None # by default randomly initialized in evolution if args.gen_zfill: # all genotypes initialized with zeros population = np.zeros( (num_populations, args.popsize, genotype_size) ) evo = Evolution( random_seed=args.seed, population=population, num_populations=num_populations, population_size=args.popsize, genotype_size=genotype_size, evaluation_function=sim.evaluate, performance_objective=args.perf_obj, fitness_normalization_mode='FPS', # 'NONE', 'FPS', 'RANK', 'SIGMA' -> NO NORMALIZATION selection_mode='RWS', # 'UNIFORM', 'RWS', 'SUS' reproduce_from_elite=False, reproduction_mode='GENETIC_ALGORITHM', # 'HILL_CLIMBING', 'GENETIC_ALGORITHM' mutation_variance=0.05, # mutation noice with variance 0.1 elitist_fraction=0.05, # elite fraction of the top 4% solutions mating_fraction=0.95, # the remaining mating fraction (consider leaving something for random fill) crossover_probability=0.1, crossover_mode='UNIFORM', crossover_points=None, # genotype_structure['crossover_points'], folder_path=outdir, max_generation=args.max_gen, termination_function=None, checkpoint_interval=checkpoint_interval ) print('Output path: ', outdir) print('n_elite, n_mating, n_filling: ', evo.n_elite, evo.n_mating, evo.n_fillup) evo.run() print('Ellapsed time: {}'.format(t.tocvalue())) return sim, evo