Пример #1
0
def fitness_from_points(points):
    '''Takes some points, builds a map and returns the fitness value
    '''
    mymap, _ = create_map(mp.feature_resolution, mp.domain)
    mymap, _ = niche_compete(points, mymap, mp.domain)
    fitness = np.nansum(mymap.fitness.flatten())
    return (fitness)
Пример #2
0
def create_prediction_map(
    model,
    observed,
    UCB=False,
    means=True,
    *args,
):
    '''create_prediction_map creates a prediction map by using the map made 
    from all the current best points and then using map-elites to illuminate it
    using the current acquisition function.

    Example: prediction_map = create_prediction_map( gp , points)

    Input:
        model    - [ GPy model ]                - the current posterior GP
        observed - [ n*[ [ x ],[ y ],[ f ] ] ]  - all evaluated points
        *args    - extra args

    Output:
    prediction_map  -   [ Map Class ]   - The best map with current data.

    Code Author: Paul Kent 
    Warwick University
    email: [email protected]
    Oct 2020; Last revision: 14-Oct-2020 
    '''
    fdims = len(mp.feature_resolution)
    mins = mp.domain.feat_mins
    maxs = mp.domain.feat_maxs
    xdims = len(mp.example)
    #Make acquisition
    acq_fun = build_pytorch_acq_fun(model, UCBflag=UCB, meansflag=means)
    #seed map with precise evaluations
    prediction_map, _ = create_map(mp.feature_resolution, mp.domain)
    prediction_map, _ = niche_compete(points=observed,
                                      map=prediction_map,
                                      domain=mp.domain)
    prediction_map = map_elites(mp.domain,
                                init_map=prediction_map,
                                feat_fun=mp.feature_fun,
                                fit_fun=acq_fun,
                                plot=False,
                                me_params=mp.PM_params)
    return (prediction_map)
Пример #3
0
def evaluate_points(points):
    truemap , _ = create_map( mp.feature_resolution , mp.domain )
    truemap , _ = niche_compete(points,truemap,mp.domain)
    fitness = np.nansum(truemap.fitness.flatten())
    print(fitness)
    return(truemap , fitness)
Пример #4
0
def map_elites(domain,
               init_map=None,
               fit_fun=mp.domain.fit_fun,
               feat_fun=mp.domain.feat_fun,
               experiment=False,
               number_evals=mp.n_gens,
               plot=False,
               verbose=False):

    num_cores = multiprocessing.cpu_count()
    pool = multiprocessing.Pool(num_cores)

    def sample(n,
               sample_map=None,
               init=False,
               fitness_fun=mp.domain.fit_fun,
               feature_fun=mp.domain.feat_fun,
               plot=True,
               sample_pool=pool):
        '''Sample performs the calls to the objective and feature functions and
        returns them in the correct format for Map-Elites 
        '''
        if sample_map == None:
            sample_map = mp.map
        population = np.array(
            create_children(batchsize=10**4,
                            map=sample_map,
                            initialise=init,
                            plot=plot))
        fitness = np.array(parallel_eval(fitness_fun, population, sample_pool))
        #print(fitness)
        behaviour = []
        # for pop in population:
        #     behaviour.append(eval( 'feature_fun' )( pop ))
        behaviour = np.array(
            parallel_eval(feature_fun, population, sample_pool))
        # behaviour = np.array( behaviour )
        sampled_points = [[population[i], fitness[i], behaviour[i]]
                          for i in range(n)]
        return (sampled_points)


#######################################################################
################# Initialising ########################################
#######################################################################

    if init_map == None:
        experiment = True
        if mp.map is None:
            mp.map, edges = create_map(mp.feature_resolution, mp.example)

    mymap = init_map

    init_n = mp.n_children  # init_n : initial population size

    sampled_points = sample(n=init_n,
                            sample_map=mymap,
                            init=True,
                            fitness_fun=fit_fun,
                            plot=plot,
                            sample_pool=pool)

    mymap, improvement_percentage = niche_compete(sampled_points, mymap)

    #######################################################################
    ################### Map-Elites #######################################
    #######################################################################
    generation = 1
    terminate = False
    popsize = mp.n_children

    while terminate != True:
        if experiment:
            mymap = None
        new_samples = sample(n=popsize,
                             sample_map=mymap,
                             fitness_fun=fit_fun,
                             plot=plot)

        sampled_points.extend(new_samples)
        mymap, improvement_percentage = niche_compete(points=new_samples,
                                                      map=mymap)
        if verbose:
            print('generation ', generation, ' accepted ',
                  improvement_percentage, ' % of points')
        generation += 1
        if generation > number_evals:
            terminate = True

    sampled = [s[0] for s in sampled_points]
    # x1 = [x[0] for x in sampled]
    # x2 = [x[1] for x in sampled]
    # plt.scatter(x1,x2)
    # plt.show()

    if experiment:
        return ()

    return (mymap)