PyGMO Batch fitness evaluation

1.2k Views Asked by At

My goal is to perform a parameter estimation (model calibration) using PyGmo. My model will be an external "black blox" model (c-code) outputting the objective function J to be minimized (J in this case will be the "Normalized Root Mean Square Error" (NRMSE) between model outputs and measured data. To speed up the optimization (calibration) I would like to run my models/simulations on multiple cores/threads in parallel. Therefore I would like to use a batch fitness evaluator (bfe) in PyGMO. I prepared a minimal example using a simple problem class but using pure python (no external model) and the rosenbrock problem:

#!/usr/bin/env python
# coding: utf-8

import numpy as np
from fmpy import read_model_description, extract, simulate_fmu, freeLibrary
from fmpy.fmi2 import FMU2Slave
import pygmo as pg
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import time

#-------------------------------------------------------

def main():
        # Optimization
        # Define problem
        class my_problem:
                def __init__(self, dim):
                        self.dim = dim
                def fitness(self, x):
                        J = np.zeros((1,))
                        for i in range(len(x) - 1):
                                J[0] += 100.*(x[i + 1]-x[i]**2)**2+(1.-x[i])**2
                        return J
                def get_bounds(self):
                        return (np.full((self.dim,),-5.),np.full((self.dim,),10.))
                def get_name(self):
                        return "My implementation of the Rosenbrock problem"
                def get_extra_info(self):
                        return "\nDimensions: " + str(self.dim)
                def batch_fitness(self, dvs):
                        J = [123] * len(dvs)
                        return J

        prob = pg.problem(my_problem(30))
        print('\n----------------------------------------------')
        print('\nProblem description: \n')
        print(prob)

        #-------------------------------------------------------

        dvs = pg.batch_random_decision_vector(prob, 1)
        print('\n----------------------------------------------')
        print('\nBarch fitness evaluation:')
        print('\ndvs length:' + str(len(dvs)))
        print('\ndvs:')
        print(dvs)
        udbfe = pg.default_bfe()
        b = pg.bfe(udbfe=udbfe)       
        print('\nudbfe:')
        print(udbfe)
        print('\nbfe:')
        print(b)
        fvs = b(prob, dvs)
        print(fvs)

        #-------------------------------------------------------

        pop_size = 50
        gen_size = 1000
        algo = pg.algorithm(pg.sade(gen = gen_size)) # The algorithm (a self-adaptive form of Differential Evolution (sade - jDE variant)
        algo.set_verbosity(int(gen_size/10)) # We set the verbosity to 100 (i.e. each 100 gen there will be a log line)
        print('\n----------------------------------------------')
        print('\nOptimization:')
        start = time.time()
        pop = pg.population(prob, size = pop_size) # The initial population
        pop = algo.evolve(pop) # The actual optimization process
        best_fitness = pop.get_f()[pop.best_idx()] # Getting the best individual in the population
        print('\n----------------------------------------------')
        print('\nResult:')
        print('\nBest fitness: ', best_fitness) # Get the best parameter set
        best_parameterset = pop.get_x()[pop.best_idx()]
        print('\nBest parameter set: ',best_parameterset)
        print('\nTime elapsed for optimization: ', time.time() - start, ' seconds\n')

if __name__ == '__main__':
    main()

When I try to run this code I get the following error:

Exception has occurred: ValueError

function: bfe_check_output_fvs

where: C:\projects\pagmo2\src\detail\bfe_impl.cpp, 103

what: An invalid result was produced by a batch fitness evaluation: the number of produced fitness vectors, 30, differs from the number of input decision vectors, 1

By deleting or commeting out this two lines:

   fvs = b(prob, dvs)
    print(fvs)

the script can be run without errors.

My questions:

  1. How to use the batch fitness evaluation? (I know this is a new capability of PyGMO and they are still working on the documentation...) Can anybody give a minimal example on how to implement this?
  2. Is this the right way to go to speed up my model calibration problem? Or should I use islands and archipelagos? If I got it right, the islands in an archipelago are not communicating to eachother, right? So if one performs e.g. a Particle Swarm Optimization and wants to evaluate several objective function calls simultaneously (in parallel) then the batch fitness evaluator is the right choice?
  3. Do I need to care about archipelagos and islands in this example? What are they exactly meant for? Is it worth running several optimizations but with different initial x (input to objective function) and then to take the best solution? Is this a common approach in optimization with GA's?

I am very knew to the field of optimization and PyGMO, so thx for helping!

1

There are 1 best solutions below

0
On

Is this the right way to go to speed up my model calibration problem? Or should I use islands and archipelagos? If I got it right, the islands in an archipelago are not communicating to eachother, right? So if one performs e.g. a Particle Swarm Optimization and wants to evaluate several objective function calls simultaneously (in parallel) then the batch fitness evaluator is the right choice?

There are 2 modes of parallelization in pagmo, the island model (i.e., coarse-grained parallelization) and the BFE machinery (i.e., fine-grained parallelization).

The island model works on any problem/algorithm combination, and it is based on the idea that multiple optimisations are run in parallel while exchanging information to accelerate the global convergence to a solution.

The BFE machinery, instead, parallelizes a single optimisation, and it requires explicit support in the solver to work. Currently in pagmo only a handful of solvers are able to take advantage of the BFE machinery. The BFE machinery can also be used to parallelise the initialisation of a population of individuals, which can be useful is your fitness function is particularly heavyweight.

Which parallelisation method is best for you depends on the properties of your problem. In my experience, users tend to prefer the BFE machinery (fine-grained parallelisation) if the fitness function is very heavy (e.g., it takes minutes or more to compute), because in such a situation fitness evaluations are so costly that in order to take advantage of the island model one would have to wait too long. The BFE is also in some sense easier to understand because you don't have to delve into the details of archipelagos, topologies, etc. On the other hand, the BFE works only with certain solvers (although we are trying to extend BFE support to other solvers as time goes by).

How to use the batch fitness evaluation? (I know this is a new capability of PyGMO and they are still working on the documentation...) Can anybody give a minimal example on how to implement this?

One way of using the BFE is what you did in your example, i.e., via the implementation of a batch_fitness() method in your problem. However, my suggestion would be to comment out the batch_fitness() method and try using one of the general-purpose batch fitness evaluators provided with pagmo. The easiest thing to do is to just default-construct an instance of the bfe class and then pass it to one of the algorithms that can use the BFE machinery. One such algorithm is nspso:

https://esa.github.io/pygmo2/algorithms.html#pygmo.nspso

So, something like this:

b = pg.bfe() # Construct a default BFE
uda = pg.nspso(gen = gen_size) # Construct the algorithm
uda.set_bfe(b) # Tell the UDA to use the BFE machinery
algo = pg.algorithm(uda) # Construct a pg.algorithm from the UDA
new_pop = algo.evolve(pop) # Evolve the population

This should use multiple processes to evaluate your fitness functions in parallel within the loop of the nspso algorithm.

If you need more help, please come over to our public users/devs chat room, where you should get assistance rather quickly (normally):

https://gitter.im/pagmo2/Lobby