Multiprocessing set class attribute values in differential evolution

24 Views Asked by At

I am doing optimization using differential_evolution. I use a class where I want to update the attribute values after each iteration, and use the attribute values from the last iteration to do some calculation.

import numpy as np
from scipy.optimize import differential_evolution

class Model:
    def __init__(self, data):
        self.data = data
        self.p1 = np.array([])
        self.p2 = np.array([])
    
    def post(self, x):
        pp = x[0] * self.p1 / self.p2
        print("pp:", pp)

    def likelihood(params):
        ....
        p1 = ...
        p2 = ...
        self.p1 = p1
        self.p2 = p2
        return -ll


model=Model(data)
results = differential_evolution(model.likelihood, seed=np.random.seed(0),workers=10)
model.post(results.x)

And I get

pp: []

workers=10 enables multiprocessing which (I guess) cannot set a class's attribute values.

How can I get p1 and p2 from the last iteration?

0

There are 0 best solutions below