def run_test_suite(level=0):
    """Run the full test suite.
    This function will raise an exception if at least one test fails.
    Args:
        level(``int``): the test level (higher values run longer tests)
    """
    import pygmo as pg

    # Make test runs deterministic.
    # NOTE: we'll need to place the async/migration tests at the end, so that at
    # least the first N tests are really deterministic.
    pg.set_global_rng_seed(42)

    retval = 0
    suite = _ut.TestLoader().loadTestsFromTestCase(snopt7_test_case)
    suite.addTest(worhp_test_case())

    test_result = _ut.TextTestRunner(verbosity=2).run(suite)

    # Re-seed to random just in case anyone ever uses this function
    # in an interactive session or something.
    import random
    pg.set_global_rng_seed(random.randint(0, 2**30))

    if len(test_result.failures) > 0 or len(test_result.errors) > 0:
        retval = 1
    if retval != 0:
        raise RuntimeError('One or more tests failed.')
Esempio n. 2
0
def set_base_seed(seed):
    if seed is None:
        # Set seed randomly if not explicitly seeded
        seed = random.randint(0, 999999)
    
    os.environ['PYTHONHASHSEED'] = str(1)
    _base_seed = seed
    np.random.seed(seed)
    pg.set_global_rng_seed(seed)
    tf.random.set_seed(seed)
    random.seed(seed)
Esempio n. 3
0
 def cost_function(X):
     cp['CGPPARAMS']['max_back'] = str(int(X[0][0]))
     cp['OPTIMPARAMS']['omega'] = str(X[0][1])
     cp['OPTIMPARAMS']['eta1'] = str(X[0][2])
     cp['OPTIMPARAMS']['eta2'] = str(X[0][3])
     cp['OPTIMPARAMS']['max_vel'] = str(X[0][4])
     cp['OPTIMPARAMS']['variant'] = str(int(X[0][5] + 1))
     cp['OPTIMPARAMS']['neighb_type'] = str(int(X[0][6] + 1))
     pg.set_global_rng_seed(seed=42)
     log = run_benchmark(cp, x, y, funset)
     return fitness_fn(log)
def main(untrained):
    pop = None
    prob = ConvNetSizeProblem(2, untrained=untrained)
    pg.set_global_rng_seed(94874)
    prob = pg.problem(prob)
    algo = pg.algorithm(pg.sga(1, m=0.2, mutation='uniform'))
    pop = pg.population(prob, pop_size)
    algo.set_verbosity(1)
    for g in range(gens):
        f = pop.get_f()
        x = pop.get_x()
        for i in range(len(x)):
            logger.info(str(g) + ';' + str(x[i]) + ';' + str(f[i][0]))
        pop = algo.evolve(pop)
Esempio n. 5
0
def main(untrained):
    with Environment() as e:
        pop = None
        prob = ConvNetSizeProblem(2, distributed=True, untrained=untrained)
        pg.set_global_rng_seed(45714)
        prob = pg.problem(prob)
        algo = pg.algorithm(pg.sga(1, m=0.2, mutation='uniform'))
        pop = pg.population(prob, pop_size)
        algo.set_verbosity(1)
        for g in range(gens):
            f = pop.get_f()
            x = pop.get_x()
            for i in range(len(x)):
                e.log(
                    str(g) + ';' + np.array2string(
                        x[i], precision=2, separator=',', suppress_small=True)
                    + ';' + str(f[i][0]))
            pop = algo.evolve(pop)
Esempio n. 6
0
def run(algos, output_dir, configs_dir, default_ini=None):
    random.seed(42)
    pg.set_global_rng_seed(42)

    np.warnings.filterwarnings('ignore')

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    start = time()

    for bench_id in range(1, 16):

        x_train, y_train, x_test, y_test = get_keijzer_data(random, bench_id)
        print(bench_id, x_test.shape)

        chosen_run = algos[0]

        #        if x_test.shape[0] > 10000:
        #            print('volim si coevoluciu')
        #            chosen_run = algos[1]

        cp = ConfigParser()

        if default_ini:
            cp.read(os.path.join(configs_dir, default_ini))
        else:
            cp.read(os.path.join(configs_dir, f'hp-keijzer{bench_id}.ini'))

        max_trials = 50

        all_logs = []

        for i in range(max_trials):
            print(f'keijzer{bench_id}-{i}')
            log = chosen_run(cp, x_test, y_test, keijzer_fixed_funset)
            all_logs.append(log)

        with open(os.path.join(output_dir, f'keijzer{bench_id}.log'),
                  'wb') as f:
            pickle.dump(all_logs, f)

    print(f'finised, wall time: {time() - start}')
Esempio n. 7
0
def main():
    """
    Generate two-objective networks for a local authority and save them to the path
    specified in config.yml.
    """
    print("Generating two-objective networks...")
    config = get_config()
    lad20cd = lad20nm_to_lad20cd(config["la"])
    save_path = get_two_objs_filepath(config)

    population_groups, _ = get_objectives(config)
    thetas, n_sensors = get_all_optimisation_params(config)
    gen = config["optimisation"]["two_objectives"]["gen"]
    population_size = config["optimisation"]["two_objectives"]["population_size"]
    seed = config["optimisation"]["two_objectives"]["seed"]
    pg.set_global_rng_seed(seed=seed)
    objectives = config["optimisation"]["two_objectives"]["objectives"]
    inputs = get_two_obj_inputs(lad20cd, objectives, population_groups)

    results = make_two_obj_networks(inputs, thetas, n_sensors, gen, population_size)
    with open(save_path, "wb") as f:
        pickle.dump(results, f)
Esempio n. 8
0
    server_forza = Server('forza')
    server_forza.start()
    server_wheel = Server('wheel')
    server_wheel.start()

    # Pausa dell'esecuzione per permettere ai server di avviarsi correttamente
    # prima di procedere con l'esecuzione dei client.
    time.sleep(10)

    # definisco un User Defined Problem come richiesto da pygmo
    p = problem.My_Problem('Parameters/0_times_evolved_parameters')
    pg_prob = pygmo.problem(p)
    #scelta del numero di individui
    individuals = 100
    for seed in seeds:
        fname = 0
        # imposto il seed
        pygmo.set_global_rng_seed(seed)
        for variant in [6,8]: # le due varianti: rand-to-best e best
            population = pygmo.population(pg_prob, individuals)# definisco la popolazione per il problema
            last_pop, algo = evolve_population_DE_algorithm(50, variant, 2, population) # faccio evolvere la popolazione
            uda = algo.extract(pygmo.sade) #estraggo le informazioni dell'algoritmo
            fname += 1
            store_population('100_individuals/{}_times_evolved_population'.format(fname),
                             last_pop)  #salvo la popolazione ogni 50 generazioni
            if uda is None:
                print("ERRORE")
            with open('log_file_{}_generations.txt'.format(fname), 'w') as f: #salvo il file di log
                for line in uda.get_log():
                    f.write('{}\n'.format(line))
Esempio n. 9
0
def main():
    """
    Generate multi-objective networks for a local authority for a range of theta values,
    numbers of sensors, and objectives. Save them in pkl and json format, as well as
    meta data.
    """
    print("Generating multi-objective networks...")
    la = "Gateshead"
    thetas = [500]  # [100, 250, 500]
    n_sensors = [10]  # [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
    gen = 2  # 1000
    population_size = 8  # 200
    seed = 123
    population_groups = {
        "pop_total": {
            "min": 0,
            "max": 90,
            "weight": 1,
            "title": "Total Residents",
        },
        "pop_children": {
            "min": 0,
            "max": 15,
            "weight": 1,
            "title": "Residents Under 16",
        },
        "pop_elderly": {
            "min": 66,
            "max": 90,
            "weight": 1,
            "title": "Residents Over 65",
        },
    }
    workplace_name = "workplace"
    objectives = [
        ["workplace", "pop_elderly", "pop_children", "pop_total"],
    ]
    include_oa_coverage = True

    lad20cd = lad20nm_to_lad20cd(la)
    pg.set_global_rng_seed(seed=seed)
    save_path = Path(
        f"data/networks/{datetime.now().strftime('%Y-%m-%d_%H%M%S')}")
    os.makedirs(save_path)

    with open(Path(save_path, "meta.json"), "w") as f:
        json.dump(
            {
                "la": la,
                "lad20cd": lad20cd,
                "thetas": thetas,
                "n_sensors": n_sensors,
                "gen": gen,
                "population_size": population_size,
                "seed": seed,
                "population_groups": population_groups,
                "workplace_name": workplace_name,
                "objectives": objectives,
            },
            f,
            indent=4,
        )

    make_multi_obj_networks(
        lad20cd,
        population_groups,
        objectives,
        thetas,
        n_sensors,
        gen,
        population_size,
        save_path,
        workplace_name=workplace_name,
        include_oa_coverage=include_oa_coverage,
    )
Esempio n. 10
0
    def optimize(self,
                 niter=500,
                 minimizer_kwargs=None,
                 nmin=1000,
                 kforce=100.,
                 gradient=False,
                 print_fun=None,
                 popsize=50,
                 stepsize=0.05,
                 optimizer="evolution",
                 seed=None):

        self.kforce = kforce

        if type(seed) == type(None):
            seed = np.random.randint(999999)
        else:
            seed = int(seed)
        np.random.seed(seed)
        pygmo.set_global_rng_seed(seed=seed)
        self.set_x0()

        bounds = gist_bounds(self.xmin, self.xmax, Tconst=True)
        min_bounds = bounds.get_bounds_for_minimizer()

        if optimizer == "evolution":
            ### This works , because pygmo makes deepcopies of this object
            ### in order to remain "thread safe" during all following operations
            prob = pygmo.problem(self)
            if self.decomp:
                if (popsize % 4) != 0:
                    popsize = (popsize / 4) * 4
                if popsize < 5:
                    popsize = 8

            pop = pygmo.population(prob=prob, size=popsize)
            if self.decomp:
                ### For NSGA2, popsize must be >4 and also
                ### a multiple of four.
                algo = pygmo.algorithm(pygmo.nsga2(gen=niter))
                #algo = pygmo.algorithm(pygmo.moead(gen=niter))
            else:
                algo = pygmo.algorithm(pygmo.sade(gen=niter))
            if self.verbose:
                algo.set_verbosity(1)
            pop = algo.evolve(pop)

            for x in pop.get_x():
                print_fun(x)
                print_fun.flush()

        elif optimizer == "brute":
            self.anal_grad = False
            self.anal_boundary = False
            N_dim = self._x0.size
            niter_count = np.zeros(self._x0.size, dtype=int)

            for i in range(self._x0.size):
                self._x0[i] = min_bounds[i][0]
                _diff = min_bounds[i][1] - min_bounds[i][0]
                niter_count[i] = int(_diff / stepsize)

            prop = propagator(self._x0, N_dim, stepsize, niter_count)
            stop = False
            _stop = False

            if nmin > 0:
                self.anal_grad = True
                self.anal_boundary = False
                prob = pygmo.problem(self)
                pop = pygmo.population(prob=prob, size=1)
                algo = pygmo.algorithm(pygmo.nlopt("slsqp"))
                algo.maxeval = nmin
                if self.verbose:
                    algo.set_verbosity(1)

            while (not stop):
                if nmin > 0:
                    self.anal_grad = gradient

                    if self.anal_boundary:
                        min_bounds = None
                        bounds = None

                    pop.set_x(0, self._x0)
                    pop = algo.evolve(pop)
                    x = pop.get_x()[0]

                else:
                    x = self._x0

                if print_fun != None:
                    print_fun(x)
                    print_fun.flush()

                ### propagate self._x0
                prop.add()
                if _stop:
                    stop = True
                _stop = prop.are_we_done()

        elif optimizer == "basinhopping":
            prob = pygmo.problem(self)
            pop = pygmo.population(prob=prob, size=popsize)
            algo = pygmo.algorithm(uda=pygmo.mbh(
                pygmo.nlopt("slsqp"), stop=100, perturb=self.steps * 0.1))
            if self.verbose:
                algo.set_verbosity(1)
            pop = algo.evolve(pop)

            for x in pop.get_x():
                print_fun(x)
                print_fun.flush()

        else:
            raise ValueError("Optimizer %s is not known." % optimizer)
Esempio n. 11
0
import pygmo as pg
import numpy as np
import matplotlib.pyplot as plt

n_trials = 2
p_size = 10
n_gens = 50
D = 5

pg.set_global_rng_seed(seed=32)

# The user-defined problem
udp = pg.rastrigin(dim=D)
# The pygmo problem
prob = pg.problem(udp)
shift = np.linspace(np.pi * 4, np.pi * 4, D)
prob1 = pg.translate(prob=prob, translation=shift)

# For a number of generation based algorithms we can use a similar script to run and average over 25 runs.
udas = [
    pg.sade(gen=n_gens, variant=6, variant_adptv=1, memory=False, seed=1234)
]
#pg.de(gen=n_gens, variant=7, F=0.6, CR=0.9, seed=1234),
#pg.pso(gen=n_gens, neighb_type=4, memory=True, seed=1234)]

global_results = []
for uda in udas:
    logs = []
    algo = pg.algorithm(uda)
    results_trial = []
    for i in range(0, n_trials):
Esempio n. 12
0
    print("global results: ", global_results)

    for i, key in enumerate(P):
        P[key] = pop.champion_x[i]
    os.chdir(r"C:\Users\Vincenzo\Documents\GitHub\ComputazioneNaturale\snakeoil2015")
    print "champion_x", pop.champion_x
    print "champion_f", pop.champion_f
    with open("def_param.py", 'a') as outfile:
        outfile.write("\n")
        outfile.write(new_parameters + " = " + str(P))
    with open(new_parameters + ".txt", 'w') as outfile:
        json.dump(P, outfile)
    # We then add details to the plot
    if n_trials < 2:
        plt.legend()
    plt.grid()
    plt.show()


if __name__ == "__main__":
    offset = 0

    n_trials = 5
    n_servers = 3
    population_size = 15
    n_gens = 100
    pg.set_global_rng_seed(seed=27)
    new_parameters = "SADE2_FullFit"
    mySADE(n_trials, n_gens, population_size, new_parameters, n_servers, offset)
Esempio n. 13
0
import numpy as np
import pygmo as pg
import os
import yaml
from datetime import datetime


__dir__ = os.path.dirname(os.path.realpath(__file__)) + "/"
__datadir__ = __dir__ + "../data/opt/"

# Initialise the random seed
pg.set_global_rng_seed(2018)

with open(__dir__ + 'global-optimisation-algorithms.yaml', 'rb') as config:
    go_algorithms = yaml.safe_load(config)


def optimise(func, algo_name="sga", population=100, verbosity=100,
             plot=True, save=True, save_log_x=True, name=None,
             **kwargs):

    # Initialise the problem
    prob = pg.problem(func)

    # Initialise the algorithms
    gen = kwargs.get("gen", 10000)
    if save_log_x:
        kwargs["gen"] = verbosity
        iterations = gen / verbosity
    else:
        iterations = 1