示例#1
0
def random_pairs():
    dir = "data/2n_rp-3_shannon-dd_neural_social_coll-edge/seed_001"
    evo_file = os.path.join(dir, "evo_2000.json")
    sim_file = os.path.join(dir, "simulation.json")
    output_file = os.path.join(dir, "perf_dist.json")
    evo = Evolution.load_from_file(evo_file, folder_path=dir)
    sim = Simulation.load_from_file(sim_file)
    sim.num_random_pairings = 0  # we build the pairs dynamically
    pop_size = len(evo.population)
    best_agent = evo.population[0]
    if os.path.exists(output_file):
        perfomances, distances = read_data_from_file(output_file)
    else:
        new_population_pairs = []
        perfomances = []
        distances = []
        for j in range(1, pop_size):
            b = evo.population[j]
            pair = np.concatenate([best_agent, b])
            new_population_pairs.append(pair)
            distances.append(euclidean_distance(best_agent, b))
        for i in tqdm(range(pop_size - 1)):
            perf = sim.run_simulation(new_population_pairs, i)
            perfomances.append(perf)
        write_data_to_file(perfomances, distances, output_file)
    plt.scatter(distances, perfomances)
    plt.show()
def single_paired_agents(input_dir='data'):
    """
    Test whether individually evolved agents can perform the task together
    and calculate their combined neural complexity.
    """
    from dol.simulation import Simulation
    import json
    seed_dir = f'{input_dir}/2n_exc-0.1_zfill/seed_001'
    generation = 5000
    population_idx = 0

    rs = RandomState(1)

    sim_json_filepath = os.path.join(seed_dir, 'simulation.json')
    evo_json_filepath = os.path.join(seed_dir, 'evo_{}.json'.format(generation))

    sim = Simulation.load_from_file(
        sim_json_filepath,
        switch_agents_motor_control=True,  # forcing switch
        num_random_pairings=1  # forcing to play with one another
    )

    evo = Evolution.load_from_file(evo_json_filepath, folder_path=None)

    original_populations = evo.population_unsorted

    best_two_agent_pop = np.array([
        [
            original_populations[0][x] for x in
            evo.population_sorted_indexes[population_idx][:2]
        ]
    ])

    data_record_list = []

    performance, sim_perfs, _ = sim.run_simulation(
        best_two_agent_pop, 0, 0, 0, None,
        data_record_list
    )

    nc = get_sim_agent_complexity(
        sim_perfs, sim, data_record_list,
        agent_index=None,
        sim_idx=None,
        analyze_sensors=True,
        analyze_brain=True,
        analyze_motors=False,
        combined_complexity=False,
        only_part_n1n2=False,
        rs=rs
    )

    print('performance', performance)
    print("Sim agents similarity: ", sim.agents_genotype_distance[0])
    print('nc', nc)
def main_scatter_plot(input_dir='data'):
    """
    From a given seed, look at the last generation,
    and compute the neural complexity for all agents.
    Plot correlation between fitness and complexity.
    """
    seed_dir = f'{input_dir}/2n_exc-0.1_zfill/seed_001'
    generation = 5000
    pop_index = 0

    analyze_sensors = True
    analyze_brain = True
    analyze_motors = False

    combined_complexity = False
    only_part_n1n2 = True

    rs = RandomState(1)

    evo_file = sorted([f for f in os.listdir(seed_dir) if 'evo_' in f])[0]
    evo_json_filepath = os.path.join(seed_dir, evo_file)
    evo = Evolution.load_from_file(evo_json_filepath, folder_path=None)

    pop_size = len(evo.population[0])
    print('pop_size', pop_size)

    perf_data = np.zeros(pop_size)
    nc_data = np.zeros(pop_size)

    for genotype_idx in tqdm(range(pop_size)):
        perf, sim_perfs, evo, sim, data_record_list, sim_idx = run_simulation_from_dir(
            seed_dir, generation, genotype_idx, population_idx=pop_index, quiet=True)

        agent_index = None # agent_index must be None (to get best agents of the two)
        sim_idx = None # sim_idx must be None (to get best sim among randomom pairs)

        nc_avg = get_sim_agent_complexity(
            sim_perfs, sim, data_record_list, agent_index, sim_idx,
            analyze_sensors, analyze_brain, analyze_motors,
            combined_complexity, only_part_n1n2, rs
        )

        perf_data[genotype_idx] = perf
        nc_data[genotype_idx] = nc_avg

    fig = plt.figure(figsize=(10, 6))
    ax = fig.add_subplot(1, 1, 1)
    ax.scatter(perf_data, nc_data)
    plt.xlabel('performance')
    plt.ylabel('nc')
    plt.show()
示例#4
0
def same_pairs():
    dir = "data/2n_shannon-dd_neural_social_coll-edge/seed_001"
    evo_file = os.path.join(dir, "evo_2000.json")
    sim_file = os.path.join(dir, "simulation.json")
    output_file = os.path.join(dir, "perf_dist.json")
    if os.path.exists(output_file):
        perfomances, distances = read_data_from_file(output_file)
    else:
        evo = Evolution.load_from_file(evo_file, folder_path=dir)
        sim = Simulation.load_from_file(sim_file)
        assert sim.num_random_pairings == 0
        pop_size = len(evo.population)
        perfomances = []
        distances = []
        for i in tqdm(range(pop_size)):
            perf = sim.run_simulation(evo.population, i)
            a, b = np.array_split(evo.population[i], 2)
            perfomances.append(perf)
            distances.append(euclidean_distance(a, b))
        write_data_to_file(perfomances, distances, output_file)
    plt.scatter(distances, perfomances)
    plt.show()
示例#5
0
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()
示例#6
0
    args = parser.parse_args()

    dir = args.dir
    evo_files = sorted([f for f in os.listdir(dir) if f.startswith('evo_')])
    assert len(evo_files) > 0, "Can't find evo files in dir {}".format(dir)
    last_generation = evo_files[-1].split('_')[1].split('.')[0]
    file_num_zfill = len(last_generation)
    sim_json_filepath = os.path.join(dir, 'simulation.json')
    evo_json_filepath = os.path.join(dir,
                                     'evo_{}.json'.format(last_generation))

    assert args.max_gen > int(last_generation), \
        "max_gen is <= of the last available generation ({})".format(last_generation)

    sim = Simulation.load_from_file(sim_json_filepath)

    if args.cores > 0:
        sim.cores = args.cores

    evo = Evolution.load_from_file(evo_json_filepath,
                                   evaluation_function=sim.evaluate,
                                   max_generation=args.max_gen)

    t = TicToc()
    t.tic()

    evo.run()

    print('Ellapsed time: {}'.format(t.tocvalue()))
示例#7
0
def run_simulation_from_dir(dir, generation=None, genotype_idx=0, population_idx=0,
                            random_target_seed=None, random_pairing_seed=None, 
                            isolation_idx=None, write_data=False, **kwargs):
    """
    Utitity function to get data from a simulation
    """

    evo_files = sorted([f for f in os.listdir(dir) if f.startswith('evo_')])
    assert len(evo_files) > 0, "Can't find evo files in dir {}".format(dir)
    file_num_zfill = len(evo_files[0].split('_')[1].split('.')[0])
    if generation is None:
        evo_json_filepath = os.path.join(dir, evo_files[-1])
        generation = int(evo_files[-1].split('_')[1].split('.')[0])
    else:
        generation_str = str(generation).zfill(file_num_zfill)
        evo_json_filepath = os.path.join(dir, 'evo_{}.json'.format(generation_str))    
    sim_json_filepath = os.path.join(dir, 'simulation.json')    
    sim = Simulation.load_from_file(sim_json_filepath)
    evo = Evolution.load_from_file(evo_json_filepath, folder_path=dir)

    data_record_list = []

    random_seed = evo.pop_eval_random_seed

    expect_same_results = isolation_idx is None

    # overwriting simulaiton
    if random_target_seed is not None:
        print("Using random target")
        # standard target was initialized in sim.__post_init__
        # so this is going to overwrite it
        sim.init_target(RandomState(random_target_seed))
        expect_same_results = False
    if random_pairing_seed is not None:
        print("Setting random pairing with seed ", random_pairing_seed)
        random_seed = random_pairing_seed
        expect_same_results = False

    original_populations = evo.population_unsorted

    # get the indexes of the populations as they were before being sorted by performance
    # we only need to do this for the first population (index 0)
    original_genotype_idx = evo.population_sorted_indexes[population_idx][genotype_idx]

    performance, sim_perfs, _ = sim.run_simulation(
        original_populations,
        original_genotype_idx,
        random_seed,
        population_idx,
        isolation_idx,
        data_record_list
    )

    performance = sim.normalize_performance(performance)

    verbose = not kwargs.get('quiet', False)

    if verbose:
        if genotype_idx == 0:
            perf_orig = evo.best_performances[generation][population_idx]
            perf_orig = sim.normalize_performance(perf_orig)
            print("Performace original: {}".format(perf_orig))
        print("Performace recomputed: {}".format(performance))
        if expect_same_results:
            diff_perfomance = abs(perf_orig - performance)
            if diff_perfomance > 1e-5:
                print(f'Warning: diff_perfomance: {diff_perfomance}')
            # assert diff_perfomance < 1e-5, f'diff_perfomance: {diff_perfomance}'
        # if performance == perf_orig:
        #     print("Exact!!")

    if write_data:
        for s, data_record in enumerate(data_record_list, 1):
            if len(data_record_list) > 1:
                outdir = os.path.join(dir, 'data', 'sim_{}'.format(s))
            else:
                outdir = os.path.join(dir, 'data')
            utils.make_dir_if_not_exists_or_replace(outdir)
            for k, v in data_record.items():
                if type(v) is dict:
                    # summary
                    outfile = os.path.join(outdir, '{}.json'.format(k))
                    utils.save_json_numpy_data(v, outfile)
                else:
                    outfile = os.path.join(outdir, '{}.json'.format(k))
                    utils.save_json_numpy_data(v, outfile)

    
    if kwargs.get('select_sim', None) is None:
        # select best one
        sim_idx = np.argmax(sim_perfs)
        # if sim.num_random_pairings != None and sim.num_random_pairings > 0:
        if verbose:
            print("Best sim (random pairings)", sim_idx+1)
    else:
        sim_idx = kwargs['select_sim'] - 1  # zero based
        if verbose:
            print("Selecting simulation", sim_idx+1)

    if verbose:
        sim_perf = sim.normalize_performance(sim_perfs[sim_idx])
        print("Performance recomputed (sim): ",  sim_idx+1, sim_perf)
        if sim.num_agents == 2:
            print("Sim agents genotype distance: ", sim.agents_genotype_distance[sim_idx])
        # print agents signatures
        agents_sign = [get_numpy_signature(gt) for gt in data_record_list[sim_idx]['genotypes']]
        print('Agent(s) signature(s):', agents_sign) 


    if kwargs.get('compute_complexity', False):
        from dol.analyze_complexity import get_sim_agent_complexity
        for a in range(sim.num_agents):
            nc = get_sim_agent_complexity(
                sim_perfs, sim, data_record_list,
                agent_index=a,
                sim_idx=sim_idx,
                analyze_sensors=True,
                analyze_brain=True,
                analyze_motors=False,
                combined_complexity=False,
                only_part_n1n2=True,
                rs=RandomState(1)
            )
            print('TSE', a+1, nc)

    return performance, sim_perfs, evo, sim, data_record_list, sim_idx
def test_split_genotypes():
    dir = "data/2n_shannon-dd_neural_social_coll-edge/seed_001"
    evo_file = os.path.join(dir, "evo_2000.json")
    evo = Evolution.load_from_file(evo_file, folder_path=dir)
    get_similarity_matrix(evo.population)
    get_similarity_split(evo.population)
示例#9
0
def run_simulation_from_dir(dir, generation=None, genotype_idx=0, **kwargs):
    ''' 
    utitity function to get data from a simulation
    '''

    random_pos_angle = kwargs.get('random_pos_angle', None)
    entropy_type = kwargs.get('entropy_type', None)
    entropy_target_value = kwargs.get('entropy_target_value', None)
    concatenate = kwargs.get('concatenate', None)
    collision_type = kwargs.get('collision_type', None)
    ghost_index = kwargs.get('ghost_index', None)
    initial_distance = kwargs.get('initial_distance', None)
    isolation = kwargs.get('isolation', None)
    write_data = kwargs.get('write_data', None)

    func_arguments = locals()
    from pyevolver.evolution import Evolution
    evo_files = [f for f in os.listdir(dir) if f.startswith('evo_')]
    assert len(evo_files) > 0, "Can't find evo files in dir {}".format(dir)
    file_num_zfill = len(evo_files[0].split('_')[1].split('.')[0])
    if generation is None:
        # assumes last generation
        evo_files = sorted([f for f in os.listdir(dir) if f.startswith('evo')])
        evo_json_filepath = os.path.join(dir, evo_files[-1])
    else:
        generation = str(generation).zfill(file_num_zfill)
        evo_json_filepath = os.path.join(dir, 'evo_{}.json'.format(generation))
    sim_json_filepath = os.path.join(dir, 'simulation.json')
    sim = Simulation.load_from_file(sim_json_filepath)
    evo = Evolution.load_from_file(evo_json_filepath, folder_path=dir)

    if initial_distance is not None:
        print("Forcing initial distance to: {}".format(initial_distance))
        sim.agents_pair_initial_distance = initial_distance
        sim.set_initial_positions_angles()

    if random_pos_angle:
        print("Randomizing positions and angles")
        random_state = RandomState()
        sim.set_initial_positions_angles(random_state)

    if entropy_type is not None:
        sim.entropy_type = entropy_type
        print("Forcing entropy type: {}".format(sim.entropy_type))

    if entropy_target_value is not None:
        sim.entropy_target_value = entropy_target_value
        print("Forcing entropy target value: {}".format(
            sim.entropy_target_value))

    if concatenate is not None:
        sim.concatenate = concatenate == 'on'
        print("Forcing concatenation: {}".format(sim.concatenate))

    if collision_type is not None:
        sim.collision_type = collision_type
        sim.init_agents_pair()
        print("Forcing collision_type: {}".format(sim.collision_type))

    if isolation is not None:
        sim.isolation = isolation
        print("Forcing isolation to: {}".format(isolation))

    data_record_list = []
    genotype_idx_unsorted = evo.population_sorted_indexes[genotype_idx]
    random_seed = evo.pop_eval_random_seeds[genotype_idx_unsorted]

    if ghost_index is not None:
        assert ghost_index in [0, 1], 'ghost_index must be 0 or 1'
        # get original results without ghost condition and no random
        func_arguments['ghost_index'] = None
        func_arguments['random_pos_angle'] = False
        func_arguments['initial_distance'] = None
        func_arguments['write_data'] = None
        _, _, original_data_record_list = run_simulation_from_dir(
            **func_arguments)
        perf = sim.run_simulation(
            evo.population_unsorted,
            genotype_idx_unsorted,
            random_seed,
            data_record_list,
            ghost_index=ghost_index,
            original_data_record_list=original_data_record_list)
        print(
            "Overall Performance recomputed (non-ghost agent only): {}".format(
                perf))
    else:
        perf = sim.run_simulation(evo.population_unsorted,
                                  genotype_idx_unsorted, random_seed,
                                  data_record_list)
        if genotype_idx == 0:
            original_perfomance = evo.best_performances[-1]
            print("Original Performance: {}".format(original_perfomance))
        print("Overall Performance recomputed: {}".format(perf))

    if write_data:
        for s, data_record in enumerate(data_record_list, 1):
            if len(data_record_list) > 1:
                outdir = os.path.join(dir, 'data', 'sim_{}'.format(s))
            else:
                outdir = os.path.join(dir, 'data')
            utils.make_dir_if_not_exists(outdir)
            for t in range(sim.num_trials):
                for k, v in data_record.items():
                    if v is dict:
                        # summary
                        if t == 0:  # only once
                            outfile = os.path.join(outdir, '{}.json'.format(k))
                            utils.save_json_numpy_data(v, outfile)
                    elif len(v) != sim.num_trials:
                        # genotype/phenotype
                        outfile = os.path.join(outdir, '{}.json'.format(k))
                        utils.save_json_numpy_data(v, outfile)
                    elif len(v[0]) == 2:
                        # data for each agent
                        for a in range(2):
                            outfile = os.path.join(
                                outdir,
                                '{}_{}_{}.json'.format(k, t + 1, a + 1))
                            utils.save_json_numpy_data(v[t][a], outfile)
                    else:
                        # single data for both agent (e.g., distance)
                        outfile = os.path.join(outdir,
                                               '{}_{}.json'.format(k, t + 1))
                        utils.save_json_numpy_data(v[t], outfile)

    return evo, sim, data_record_list