For example
from scipy.optimize import differential_evolution
import numpy as np
def func(parameters, *data):
a,b,c = parameters
x,y = data
result = (a*x**2 + b*x+ c - y)**2
return sum(result)
bounds = [(0.5, 1), (0.5, 1), (0.5, 1)]
x = np.array([1,2,3,4,5])
y = np.array([1,4,9,16,25])
args = (x,y)
parameters = differential_evolution(func, bounds, args=args,seed=np.random.seed(7))
print(parameters.x)
then a, b, c = [0.85699693 0.5 0.5]
Now, if some one want to enforce that c must be greater than a, i.e. c>a for the parameters fitting result. How could I achieve that?
My own solution is to use a conditional return for the defined function, so replace return sum(result)
with return sum(result) if c>a else np.Inf
. This gives me a new set of a, b, c = [0.83454878 0.50031474 0.83487768]
from scipy.optimize import differential_evolution
import numpy as np
def func(parameters, *data):
a,b,c = parameters
x,y = data
result = (a*x**2 + b*x+ c - y)**2
return sum(result) if c>a else np.Inf
bounds = [(0.5, 1), (0.5, 1), (0.5, 1)]
x = np.array([1,2,3,4,5])
y = np.array([1,4,9,16,25])
args = (x,y)
parameters = differential_evolution(func, bounds, args=args,seed=np.random.seed(7))
print(parameters.x)
[0.83454878 0.50031474 0.83487768]