def init_Population(self): fun = tf.Test_fun() for i in range(self.pN): for j in range(self.dim): self.X[i][j] = random.uniform(0,1) self.V[i][j] = random.uniform(0, 1) self.pbest[i] = self.X[i] tmp = fun.Sphere_function(self.X[i]) self.p_fit[i] = tmp if(tmp < self.fit): self.fit = tmp self.gbest = self.X[i]
def iterator(self): fitness = [] fun = tf.Test_fun() for t in range(self.max_iter): for i in range(self.pN): temp = fun.Sphere_function(self.X[i]) if (temp < self.p_fit[i]): self.p_fit[i] = temp self.pbest[i] = self.X[i] if (self.p_fit[i] < self.fit): self.gbest = self.X[i] self.fit = self.p_fit[i] for i in range(self.pN): self.V[i] = self.w*self.V[i] + self.c1*self.r1*(self.pbest[i] - self.X[i])+\ self.c2*self.r2*(self.gbest - self.X[i]) self.X[i] = self.V[i] + self.X[i] fitness.append(self.fit) print(self.fit) return fitness
def iterator(self): fitness = [] fun = tf.Test_fun() for t in range(self.max_iter): for i in range(self.pN): temp = fun.Sphere_function(self.X[i]) if(temp < self.p_fit[i]): self.p_fit[i] = temp self.pbest[i] = self.X[i] if(self.p_fit[i] < self.fit): self.gbest = self.X[i] self.fit = self.p_fit[i] for i in range(self.pN): total_c = self.c1 + self.c2 factor = 2/abs(2 - total_c - math.sqrt(total_c*total_c - 4*total_c)) self.V[i] = self.w*self.V[i] + self.c1*self.r1*(self.pbest[i] - self.X[i])+\ self.c2*self.r2*(self.gbest - self.X[i]) self.V[i] = self.V[i]*factor self.X[i] = self.V[i] + self.X[i] fitness.append(self.fit) print(self.fit) return fitness
def iterator(self, judge): fitness = [] fun = tf.Test_fun() for t in range(self.max_iter): if judge == 1: w = self.liner_weight(t) else: w = self.random_weight() for i in range(self.pN): temp = fun.Sphere_function(self.X[i]) if (temp < self.p_fit[i]): self.p_fit[i] = temp self.pbest[i] = self.X[i] if (self.p_fit[i] < self.fit): self.gbest = self.X[i] self.fit = self.p_fit[i] for i in range(self.pN): self.V[i] = w*self.V[i] + self.c1*self.r1*(self.pbest[i] - self.X[i])+\ self.c2*self.r2*(self.gbest - self.X[i]) self.X[i] = self.V[i] + self.X[i] fitness.append(self.fit) print(self.fit) return fitness
def iterator(self): fitness = [] fun = tf.Test_fun() for t in range(self.max_iter): tempX = np.zeros((1, self.dim)) for i in range(len(tempX)): tempX[i] = random.uniform(0, 1) for i in range(self.pN): temp = fun.Sphere_function(self.X[i]) if(temp < self.p_fit[i]): self.p_fit[i] = temp self.pbest[i] = self.X[i] if(self.p_fit[i] < self.fit): self.gbest = self.X[i] self.fit = self.p_fit[i] for i in range(self.pN): self.V[i] = self.w*self.V[i] + self.c1*self.r1*(self.pbest[i] - 2*self.X[i] + tempX)+\ self.c2*self.r2*(self.gbest - 2*self.X[i] + tempX) tempX = self.X[i] self.X[i] = self.V[i] + self.X[i] fitness.append(self.fit) print(self.fit) return fitness