Я пытаюсь реализовать алгоритм гравитационного поиска, но не могу приблизиться к тем же результатам, что и результаты автора. Это моя реализация:
class GSA(MultiAgent):
def __init__(self, objf, population_size, dim, lb, ub, max_i, g0=100, alpha=20, eps=0.0001):
super().__init__(objf=objf, population_size=population_size, dim=dim, ub=ub, lb=lb, max_i=max_i)
self.g0 = g0
self.alpha = alpha
self.eps = eps
self.v = np.zeros((self.population_size, self.dim))
self.gworst = None
self.run()
def update_gworst(self):
idx = np.argmax(self.fitness)
if self.gworst is None or self.fitness[idx] > self.gworst['fitness']:
self.gworst = {'fitness': np.copy(self.fitness[idx]), 'position': np.copy(self.population[idx])}
def massCalculation(self):
m = (self.fitness - self.gworst['fitness'])/(self.gbest['fitness'] - self.gworst['fitness']) + self.eps #equation 15
m = m/np.sum(m) #equation 16
return m
def g_field(self, m, i):
g = self.g0 * np.exp(-self.alpha * i / self.max_i) # equation 28
subts = self.population - self.population[:, np.newaxis, :]
r = np.sqrt(np.einsum('ijk,ijk->ij', subts, subts))
m_ij = m * m[:, np.newaxis]
rnd = np.random.uniform(size=(self.population_size, self.population_size, 1))
e = g * (rnd * m_ij[:, :, np.newaxis] * subts) / (r[:, :, np.newaxis] + self.eps) #equation 7
e = np.sum(e, 1)
a = np.divide(e, m[:, np.newaxis]) #equation 10
return a
def update_position(self, a):
self.v = np.random.uniform(size=(self.population_size, self.dim)) * self.v + a #equation 11
self.population = self.population + self.v #equation 12
self.population = np.clip(self.population, self.lb, self.ub)
def run(self):
i = 0
while i < self.max_i:
self.update_fitness() #get fitness of population
self.update_gbest() #get gbest
self.plot(i)
#print(self.fitness), print('\n')
print(self.gbest['fitness'])
self.update_gworst()
m = self.massCalculation()
a = self.g_field(m, i)
self.update_position(a)
i += 1
в чем проблема? спасибо за вашу помощь