Пример #1
0
 def test_correct_sorting(self):
     es1 = ESFitness(ERT=None, FCE=float('inf'))
     es2 = ESFitness(ERT=None, FCE=float(32))
     es3 = ESFitness(ERT=2, FCE=float('inf'))
     es4 = ESFitness(ERT=3, FCE=float(42))
     self.assertListEqual(sorted([es1, es2, es3, es4]),
                          [es3, es4, es2, es1])
Пример #2
0
    def test_inequality(self):
        es1 = ESFitness(ERT=None, FCE=5)
        es2 = ESFitness(ERT=None, FCE=42)
        self.assertTrue(es1 != es2)

        es1 = ESFitness(ERT=3, FCE=42)
        es2 = ESFitness(ERT=4, FCE=42)
        self.assertTrue(es1 != es2)
Пример #3
0
    def test_create_from_human_radable_values(self):
        es1 = ESFitness(ERT=None, FCE=float('inf'))
        self.assertIsNone(es1.ERT)
        self.assertEqual(es1.FCE, float('inf'))

        es2 = ESFitness(ERT=None, FCE=float(32))
        self.assertIsNone(es2.ERT)
        self.assertEqual(es2.FCE, float(32))

        es3 = ESFitness(ERT=2, FCE=float('inf'))
        self.assertEqual(es3.ERT, 2)
        self.assertEqual(es3.FCE, float('inf'))

        es4 = ESFitness(ERT=3, FCE=float(42))
        self.assertEqual(es4.ERT, 3)
        self.assertEqual(es4.FCE, float(42))
Пример #4
0
def _runExperiments():
    for ndim in Config.experiment_dims:
        for fid in Config.experiment_funcs:

            # Initialize the first individual in the population
            discrete_part = [np.random.randint(len(x[1])) for x in options]
            lamb = int(4 + np.floor(3 * np.log(parameters.n)))
            int_part = [lamb]
            float_part = [
                parameters.mu, parameters.alpha_mu, parameters.c_sigma,
                parameters.damps, parameters.c_c, parameters.c_1,
                parameters.c_mu, 0.2, 0.955, 0.5, 0, 0.3, 0.5, 2
            ]

            population = [
                MixedIntIndividual(len(discrete_part) + len(int_part) +
                                   len(float_part),
                                   num_discrete=len(num_options_per_module),
                                   num_ints=len(int_part))
            ]
            population[0].genotype = np.array(discrete_part + int_part +
                                              float_part)
            population[0].fitness = ESFitness()

            while len(population) < Config.GA_mu:
                population.append(copy(population[0]))

            parameters = Parameters(len(options) + 15,
                                    Config.GA_budget,
                                    mu=Config.GA_mu,
                                    lambda_=Config.GA_lambda)
            parameters.l_bound[len(options):] = np.array(
                [2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]).reshape(15)
            parameters.u_bound[len(options):] = np.array(
                [200, 1, 5, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5]).reshape(15)
            u_bound, l_bound = create_bounds(float_part, 0.3)
            parameters.u_bound[len(options) + 1:] = np.array(u_bound)
            parameters.l_bound[len(options) + 1:] = np.array(l_bound)

            print("Optimizing for function ID {} in {}-dimensional space:".
                  format(fid, ndim))
            x = datetime.now()
            gen_sizes, sigmas, fitness, best = _MIES(n=ndim,
                                                     fitnessFunction=fid,
                                                     budget=Config.GA_budget,
                                                     mu=Config.GA_mu,
                                                     lambda_=Config.GA_lambda,
                                                     parameters=parameters,
                                                     population=population)
            y = datetime.now()

            z = y - x
            np.savez("{}final_GA_results_{}dim_f{}".format(
                non_bbob_datapath, ndim, fid),
                     sigma=sigmas,
                     best_fitness=fitness,
                     best_result=best.genotype,
                     generation_sizes=gen_sizes,
                     time_spent=z)
Пример #5
0
 def test_repr_min_fitnesses(self):
     es = ESFitness(target=1,
                    min_fitnesses=[0, 0, 40, 0, 0],
                    min_indices=[32, 48, 999, 16, 64],
                    num_successful=4)
     self.assertEqual(
         repr(es), 'ESFitness(target=1,min_fitnesses=[0, 0, 40, 0, 0],'
         'min_indices=[32, 48, 999, 16, 64],num_successful=4)')
Пример #6
0
def evaluateCustomizedESs(representations,
                          iids,
                          ndim,
                          fid,
                          budget=None,
                          num_reps=1,
                          storage_file=None):
    """
        Function to evaluate customizedES instances using the BBOB framework. Can be passed one or more representations
        at once, will run them in parallel as much as possible if instructed to do so in Config.

        :param representations: The genotype to be translated into customizedES-ready options.
        :param iids:            The BBOB instance ID's to run the representation on (for statistical significance)
        :param ndim:            The dimensionality to test the BBOB function with
        :param fid:             The BBOB function ID to use in the evaluation
        :param budget:          The allowed number of BBOB function evaluations
        :param num_reps:        Number of times each (ndim, fid, iid) combination has to be repeated
        :param storage_file:    Filename to use when storing fitness information
        :returns:               A list containing one instance of ESFitness representing the fitness of the defined ES
    """

    representations = _ensureListOfLists(representations)
    for rep in representations:
        displayRepresentation(rep)

    budget = Config.ES_budget_factor * ndim if budget is None else budget
    runFunction = partial(runCustomizedES, ndim=ndim, fid=fid, budget=budget)

    num_multiplications = len(iids) * num_reps
    arguments = list(product(representations, iids, range(num_reps)))
    run_data = runParallelFunction(runFunction, arguments)
    for rep in representations:
        reorganiseBBOBOutput(datapath + reprToString(rep) + '/', fid, ndim,
                             iids, num_reps)

    targets, results = zip(*run_data)
    fitness_results = []

    for i, rep in enumerate(representations):

        # Preprocess/unpack results
        _, _, fitnesses, _ = (list(x) for x in zip(
            *results[i * num_multiplications:(i + 1) * num_multiplications]))
        fitnesses = _trimListOfListsByLength(fitnesses)

        # Subtract the target fitness value from all returned fitnesses to only get the absolute distance
        fitnesses = np.subtract(
            np.array(fitnesses),
            np.array(targets[i * num_multiplications:(i + 1) *
                             num_multiplications]).T[:, np.newaxis])
        fitness = ESFitness(fitnesses)
        fitness_results.append(fitness)

        if not isinstance(rep, list):
            rep = rep.tolist()
        _writeResultToFile(rep, fitness, storage_file)

    return fitness_results
Пример #7
0
 def test_create_FCE_from_summary_values(self):
     ERT, FCE, std_dev_ERT, std_dev_FCE = ESFitness._calcFCEandERT(
         min_fitnesses=[1, 2, 42, 3, 4],
         min_indices=[999, 999, 999, 999, 999],
         num_successful=0)
     self.assertIsNone(ERT)
     self.assertAlmostEqual(std_dev_ERT, 0)
     self.assertAlmostEqual(FCE, 10.4)
     self.assertAlmostEqual(std_dev_FCE, 15.83161394173)
Пример #8
0
 def test_create_both_from_summary_values(self):
     ERT, FCE, std_dev_ERT, std_dev_FCE = ESFitness._calcFCEandERT(
         min_fitnesses=[0, 0, 40, 0, 0],
         min_indices=[32, 48, 999, 16, 64],
         num_successful=4)
     self.assertAlmostEqual(ERT, 289.75)
     self.assertAlmostEqual(std_dev_ERT, 383.9335359147)
     self.assertAlmostEqual(FCE, 8)
     self.assertAlmostEqual(std_dev_FCE, 16)
Пример #9
0
 def test_str(self):
     es = ESFitness(target=1,
                    ERT=1234.5678,
                    FCE=123.45678,
                    std_dev_ERT=12.345678,
                    std_dev_FCE=1.2345678)
     self.assertEqual(
         str(es),
         'ERT: 1234.568  (std:     12.3)  |  FCE: 1.23e+02  (std:     1.23)'
     )
Пример #10
0
    def test_create_both_from_original_values(self):
        fitnesses = [
            [9] * 10 + [6] * 10 + [4] * 10 + [3] * 10 + [1] * 10,
            [6] * 10 + [1] * 10 + [3] * 10 + [4] * 20,
            [9] * 10 + [8] * 10 + [7] * 10 + [6] * 10 + [5] * 10,
            [5] * 10 + [3] * 10 + [1] * 30,
            [9] * 10 + [6] * 10 + [4] * 10 + [1] * 10 + [3] * 10,
        ]
        fitnesses = np.array(fitnesses)
        min_fitnesses, min_indices, num_successful = ESFitness._preCalcFCEandERT(
            fitnesses=fitnesses, target=2)
        self.assertListEqual(min_fitnesses, [1, 1, 5, 1, 1])
        self.assertListEqual(min_indices, [40, 10, 50, 20, 30])
        self.assertEqual(num_successful, 4)

        min_fitnesses, min_indices, num_successful = ESFitness._preCalcFCEandERT(
            fitnesses=fitnesses, target=0)
        self.assertListEqual(min_fitnesses, [1, 1, 5, 1, 1])
        self.assertListEqual(min_indices, [50, 50, 50, 50, 50])
        self.assertEqual(num_successful, 0)
Пример #11
0
def performanceLogger(featureObj,
                      EvolutionaryOptimizer,
                      target,
                      PerformanceLogger=None):
    # weird why is this only 14-- ". In its default settings, flacco computes more than 300 different numerical landscape features, distributed across 17 so-called feature sets"

    #features = [ela_distr", "ela_level", "ela_meta", "basic", "disp", "limo", "nbc", "pca",, "ic"]

    features = [
        "cm_angle", "cm_conv", "cm_grad", "ela_distr", "ela_level", "ela_meta",
        "basic", "disp", "limo", "nbc", "pca", "ic"
    ]
    data = {}
    for feature in features:
        try:
            print(feature)
            featureSet = calculate_feature_set(featureObj, feature)
            keys = featureSet.keys()

            for key in keys:
                data[key] = featureSet[key]
        except:
            pass

    print("Es Fitness computation")
    fitness = ESFitness(fitnesses=np.array(
        [EvolutionaryOptimizer.fitness_over_time]),
                        target=target)
    data['FCE'] = fitness.FCE
    data['ERT'] = fitness.ERT

    if PerformanceLogger is None:
        Logger = pd.DataFrame(data, index=[0])
    else:
        Logger = PerformanceLogger.append(data, ignore_index=True)

    return Logger
Пример #12
0
 def test_repr_ERT_FCE(self):
     es = ESFitness(target=1, ERT=2, FCE=3, std_dev_ERT=4, std_dev_FCE=5)
     self.assertEqual(
         repr(es),
         'ESFitness(target=1,ERT=2,FCE=3,std_dev_ERT=4,std_dev_FCE=5)')
Пример #13
0
def _runGA(ndim=5, fid=1, run=1):
    x = datetime.now()

    # Where to store genotype-fitness information
    # storage_file = '{}GA_results_{}dim_f{}.tdat'.format(non_bbob_datapath, ndim, fid)
    storage_file = '{}MIES_results_{}dim_f{}run_{}.tdat'.format(
        non_bbob_datapath, ndim, fid, run)

    # Fitness function to be passed on to the baseAlgorithm
    fitnessFunction = partial(evaluateCustomizedESs,
                              fid=fid,
                              ndim=ndim,
                              iids=range(Config.ES_num_runs),
                              storage_file=storage_file)

    parameters = Parameters(len(options) + 15,
                            Config.GA_budget,
                            mu=Config.GA_mu,
                            lambda_=Config.GA_lambda)
    parameters.l_bound[len(options):] = np.array(
        [2, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]).reshape(15)
    parameters.u_bound[len(options):] = np.array(
        [200, 1, 5, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5]).reshape(15)

    # Initialize the first individual in the population
    discrete_part = [np.random.randint(len(x[1])) for x in options]
    lamb = int(4 + np.floor(3 * np.log(parameters.n)))
    int_part = [lamb]
    float_part = [
        parameters.mu, parameters.alpha_mu, parameters.c_sigma,
        parameters.damps, parameters.c_c, parameters.c_1, parameters.c_mu, 0.2,
        0.955, 0.5, 0, 0.3, 0.5, 2
    ]

    population = [
        MixedIntIndividual(len(discrete_part) + len(int_part) +
                           len(float_part),
                           num_discrete=len(num_options_per_module),
                           num_ints=len(int_part))
    ]
    population[0].genotype = np.array(discrete_part + int_part + float_part)
    population[0].fitness = ESFitness()

    while len(population) < Config.GA_mu:
        population.append(copy(population[0]))

    u_bound, l_bound = create_bounds(float_part, 0.3)
    parameters.u_bound[len(options) + 1:] = np.array(u_bound)
    parameters.l_bound[len(options) + 1:] = np.array(l_bound)

    gen_sizes, sigmas, fitness, best = _MIES(
        n=ndim,
        fitnessFunction=fitnessFunction,
        budget=Config.GA_budget,
        mu=Config.GA_mu,
        lambda_=Config.GA_lambda,
        parameters=parameters,
        population=population)  # This line does all the work!
    y = datetime.now()
    print()
    print("Best Individual:     {}\n"
          "        Fitness:     {}\n"
          "Fitnesses over time: {}".format(best.genotype, best.fitness,
                                           fitness))

    z = _displayDuration(x, y)

    if Config.write_output:
        np.savez("{}final_GA_results_{}dim_f{}_run{}".format(
            non_bbob_datapath, ndim, fid, run),
                 sigma=sigmas,
                 best_fitness=fitness,
                 best_result=best.genotype,
                 generation_sizes=gen_sizes,
                 time_spent=z)