def demo1_func(p):
'''This function has plenty of local minimum, with strong shocks
global minimum at (0,0) with value 0'''
x1, x2 = p
print(x1, x2)
x = x1 ** 2 + x2 ** 2
return x
def cons1(x):
return [x[0] + x[1] - 10]
cons = cons1
from sko.GA import GA
ga = GA(func=demo1_func, n_dim=2, size_pop=50, max_iter=800, lb=[-1, -1], ub=[8, 10], precision=1e-7, constraint_eq=[cons])
best_x, best_y = ga.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)
结果
best_x: [5.35140186 4.64859812]
best_y: [50.24696633]