def exec(self, p):
     # 初始化基因,基因包含染色体和染色体的适应度
     self.init_population()
     self.gbest = self.find_best()
     lb = self.particles.pop()
     best_score = calculate_fitness(lb.solution, self.cloudlets, self.vms)
     global_best_score = calculate_fitness(self.gbest.solution,
                                           self.cloudlets, self.vms)
     results = []
     # 迭代过程 仅包含速度和位置的更新
     for t in range(self.times):
         for p in self.particles:
             if p is self.gbest:
                 continue
             if p is lb:
                 continue
             self.update_velocity(p, lb, self.gbest)
             self.update_position(p)
             score = calculate_fitness(p.solution, self.cloudlets, self.vms)
             if score > best_score:
                 best_score = score
                 lb = p
             if score > global_best_score:
                 global_best_score = score
                 self.gbest = p
         tempGbest = self.getSeries(self.gbest.solution)
         if tempGbest.fitness > global_best_score:
             global_best_score = tempGbest.fitness
             self.gbest = tempGbest
         results.append(global_best_score)
         if t % 20 == 0:
             print("CPSO iter: ", t, " / ", self.times, ", 适应度: ",
                   global_best_score)
     # return gb.solution
     return results
示例#2
0
    def scheduler_main(self):
        results = [0 for _ in range(self.times)]
        fitness = 0

        for it in range(self.times):
            best_time = 0

            for ant_id in range(self.population_number):
                topo_jobs = self.gen_topo_jobs()
                fitness = calculate_fitness(topo_jobs, self.cloudlets,
                                            self.vms)
                if fitness > best_time:
                    self.best_topo = topo_jobs
                    best_time = fitness
            assert self.best_topo is not None
            self.update_topo()
            results[it] = best_time
            if it % 20 == 0:
                print("ACO iter: ", it, " / ", self.times, ", 适应度: ", fitness)
        plt.plot(range(self.times), results)
        plt.xlabel("迭代次数")
        plt.ylabel("适应度")
        plt.rcParams['font.sans-serif'] = ['KaiTi']  # 指定默认字体
        plt.rcParams['axes.unicode_minus'] = False  # 解决保存图像是负号'-'显示为方块的问题
        plt.title("蚁群算法求解云任务调度负载均衡问题")
        # plt.savefig('img/ACScheduler-1.1_0.9-popu100-iter200.png', dpi=300,
        #             format='png')  # bbox_inches="tight"解决X轴时间两个字不被保存的问题
        plt.show()
        return results
 def mutation(self):
     for i in range(self.population_number):
         point = np.random.randint(0, self.cloudlet_num)
         if np.random.rand() < self.mp:
             self.genes[i].solution[point] = np.random.randint(
                 0, self.machine_number)
             self.genes[i].fitness = calculate_fitness(
                 self.genes[i].solution, self.cloudlets, self.vms)
 def init_population(self):
     for i in range(self.population_number):
         gene = Gene()
         gene.solution = np.random.randint(0, self.machine_number,
                                           self.cloudlet_num)
         gene.fitness = calculate_fitness(gene.solution, self.cloudlets,
                                          self.vms)
         self.genes.append(gene)
 def find_best(self):
     best_score = 0
     best_particle = None
     for p in self.particles:
         score = calculate_fitness(p.solution, self.cloudlets, self.vms)
         if score > best_score:
             best_score = score
             best_particle = p
     return best_particle
示例#6
0
 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 crossover(self, g1, g2):
     for i in range(self.population_number):
         if np.random.rand() < self.cp:
             point = np.random.randint(0, self.cloudlet_num)
             new_gene = Gene()
             new_gene.solution = np.hstack(
                 (g1.solution[0:point], g2.solution[point:]))
             new_gene.fitness = calculate_fitness(new_gene.solution,
                                                  self.cloudlets, self.vms)
             self.genes[i] = new_gene
示例#8
0
 def exec(self):
     # 初始化基因,基因包含染色体和染色体的适应度
     self.init_population()
     self.gbest = self.find_best()
     lb = self.particles.pop()
     best_score = calculate_fitness(lb.solution, self.cloudlets, self.vms)
     global_best_score = calculate_fitness(self.gbest.solution,
                                           self.cloudlets, self.vms)
     results = []
     # 迭代过程 仅包含速度和位置的更新
     for t in range(self.times):
         # 升序排序
         self.particles.sort(key=lambda x: x.fitness)
         ind = 0
         for p in self.particles:
             if p is self.gbest:
                 continue
             if p is lb:
                 continue
             self.update_velocity(p, lb, self.gbest)
             self.update_position(p)
             if ind > self.population_number / 3 or ind < 2 / 3 * self.population_number:
                 self.update_position_copy1(p, lb, 2)
             else:
                 self.update_position_copy1(p, self.gbest, 1)
             ind += 1
             score = calculate_fitness(p.solution, self.cloudlets, self.vms)
             if score > best_score:
                 best_score = score
                 lb = p
             if score > global_best_score:
                 global_best_score = score
                 self.gbest = p
         tempGbest = self.getSeries(self.gbest.solution)
         if tempGbest.fitness > global_best_score:
             global_best_score = tempGbest.fitness
             self.gbest = tempGbest
         results.append(global_best_score)
         if t % 20 == 0:
             print("CRPSO iter: ", t, " / ", self.times, ", 适应度: ",
                   global_best_score)
     # print(gb.solution)
     return results
    def sa_main(self):
        self.best_way = np.random.randint(0, self.machine_number,
                                          self.cloudlet_num)
        # temp_solution = [0] * self.times
        # temp_value = [0] * self.times

        results = list()
        for t in range(self.times):
            if t % 20 == 0:
                print("SA Iter, ", t, "/", self.times, ", 适应度:", self.best_bl)

            # for j in range(self.Lk):
            for j in range(self.population_number):
                self.best_bl = calculate_fitness(self.best_way, self.cloudlets,
                                                 self.vms)
                way1 = self.gen_new_way(self.best_way)
                way1 = self.gen_new_way_change(way1)
                # print("新解的适应度:", bl1)
                bl1 = calculate_fitness(way1, self.cloudlets, self.vms)
                if bl1 > self.best_bl:
                    self.best_way = way1
                    # if bl1 > temp_value[j]:
                    #     temp_solution[j] = way1
                    #     temp_value[j] = bl1
                else:
                    p = np.exp((bl1 - self.best_bl) / self.T)
                    if np.random.rand() < p:
                        self.best_way = way1
            results.append(self.best_bl)
            # results.append(temp_value[t])
            self.T = self.alpha * self.T
        #     print("最佳适应度", self.best_bl)
        #     print("最佳路径", self.best_way)
        # print("最佳适应度:", self.best_bl)
        # print("策略:", self.best_way)

        return results
 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