def init_population(self): for _ in range(self.population_number): size = self.cloudlet_num p = DParticle(size) p.solution = np.random.randint(0, self.machine_number, size=self.cloudlet_num) p.velocity = -5 + 10 * np.random.rand(self.cloudlet_num) p.fitness = calculate_fitness(p.solution, self.cloudlets, self.vms) self.particles.append(p)
def update_velocity(self, p: DParticle, lb: DParticle, gb: DParticle): E1 = np.array([np.random.random() for _ in range(self.cloudlet_num) ]) # [0.1, 0.2, 0.002, 0.4, ...] E2 = np.array([np.random.random() for _ in range(self.cloudlet_num)]) v1 = np.array(gb.solution) - np.array(p.solution) v2 = np.array(lb.solution) - np.array(p.solution) velocity = self.c1 * E1 * v1 + self.c2 * E2 * v2 velocity = np.clip(velocity, -self.vmax, self.vmax) p.velocity = p.velocity * self.w + velocity
def getSeries(self, v0): tempSet = [[0] * self.cloudlet_num] * 30 tempSet[0] = [x / self.machine_number for x in v0] for i in range(1, 30): tempSet[i] = [x for x in logistic_function(tempSet[i - 1], 3.5)] for i in range(30): tempSet[i] = [int(self.machine_number * x) for x in tempSet[i]] # print(tempSet) tempFit = [] for i in range(30): tempFit.append( calculate_fitness(tempSet[i], self.cloudlets, self.vms)) best_score = 0 best_solution = None for i in range(self.chaosNum): if tempFit[i] > best_score: best_score = tempFit[i] best_solution = tempSet[i] res = DParticle(self.cloudlet_num) res.velocity = self.gbest.velocity res.solution = best_solution res.fitness = best_score return res
def update_position(self, p: DParticle): p.solution = np.abs(p.solution + p.velocity) for t in range(len(self.cloudlets)): if p.solution[t] > self.machine_number: p.solution[t] = np.ceil(p.solution[t]) % self.machine_number p.solution = list(map(int, p.solution))