0

I need to minimize a function using the scipy implementation of differential evolution. I'd like to exploit parallelism to speed up the computation and I tried setting workers=-1.

I get an error and searching I found that the problem is that the function that I'm trying to minimize is not pickable. I need help to understand how to make it pickable.

The function to minimize works in the following way:

  • A class object has an attribute vector, the observed data.
  • One method of the class takes some parameters and compute an estimate of the vector.
  • The function to minimize compute the mean square error between the vector and the computed estimate.

The pseudocode of the function could be something like that:

def function_to_minimize(self, parameters):
    true_vector = self.true_vector
    estimated_vector = self.estimate_vector(parameters)
    return mse(true_vector, estimated_vector)
user254087
  • 51
  • 5

1 Answers1

0

Something like this should work:

class Objective(object):
    def __init__(self, data):
        self.measured_data = data

    def __call__(self, parameters):
        # need to return a scalar value
        estimated_vector = self.estimate_vector(parameters)
        return np.sum(np.power(self.measured_data - estimated_vector, 2))

    def estimate_vector(parameters):
        # calculate what you expect to happen with the parameters
        pass

you should pass Objective(data) to differential_evolution as the function to minimize. On macOS and Windows that use spawn as the default way of creating new processes this function should be defined in a file that is importable.

Andrew Nelson
  • 460
  • 3
  • 11