scikit-opt
scikit-opt copied to clipboard
Trying to optimize parameters with another genetic algo error
import numpy as np
def schaffer(p):
x1, x2, x3 = p
x = np.square(x1) + np.square(x2)
return -(2*x1+x2+x3)
def second_level(z):
z1 =z[0]
z2=z[1]
omg = GA(func=schaffer, n_dim=3, size_pop=z1, max_iter=z2, lb=[-5, -5, -5], ub=[5,5,5], precision=1e-2)
best_x, best_y = omg.run()
result=float(best_y[0])
print(result)
return result
from sko.GA import GA
ga = GA(func=second_level, n_dim=2, size_pop=10, max_iter=20, lb=[10, 10], ub=[20, 20], precision=1e-2)
best_x, best_y= ga.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)
gives File "mtrand.pyx", line 741, in numpy.random.mtrand.RandomState.randint File "_bounded_integers.pyx", line 1362, in numpy.random._bounded_integers._rand_int32 TypeError: 'numpy.float64' object cannot be interpreted as an integer no matter what i try to do.
I want to make this recursive up to level n(ga3 to optimize level 2 and ga4 to optimze ga4 and so on). Please help
import numpy as np
def schaffer(p):
x1, x2, x3 = p
x = np.square(x1) + np.square(x2)
return -(2 * x1 + x2 + x3)
def second_level(z):
z1 = int(z[0])
z2 = int(z[1])
omg = GA(func=schaffer, n_dim=3, size_pop=z1, max_iter=z2, lb=[-5, -5, -5], ub=[5, 5, 5], precision=1e-2)
best_x, best_y = omg.run()
result = float(best_y[0])
print(result)
return result
from sko.GA import GA
ga = GA(func=second_level, n_dim=2, size_pop=10, max_iter=20, lb=[10, 10], ub=[20, 20], precision=[2, 1])
best_x, best_y = ga.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)
Thanks!