コード例 #1
0
    def updateFitnessAndLSPbest(self, LOCAL_SEARCH, LS_MAX_TIMES):
        print("updateFitnessAndLSPbest")
        have_new_gbest = False
        if self.REASSIGN:
            self.renewExemplars()
        for p in range(self._number_of_particles):
            # print("update pbest...")
            is_better1 = aggregation_value(self.particles[p].fitness, self.z, self.particles[p].lam) - aggregation_value(self.particles[p].personal_fitness, self.z, self.particles[p].lam)
            if (is_better1 >= 0):
                if (is_better1 > 0):
                    new_position = np.array(self.particles[p].position)
                    new_fitness = self.getProblem().fitness(new_position)
                    self.particles[p].setPersonalPositions(new_position)
                    self.particles[p].setPersonalFitness(new_fitness)
                elif (is_better1 == 0) and (LOCAL_SEARCH):
                    self.TOTAL_LS_CALL = self.TOTAL_LS_CALL + 1
                    LS_FOUND = False
                    for i in range(LS_MAX_TIMES):
                        current_position = np.array(self.particles[p].position)
                        current_fitness = self.particles[p].fitness
                        new_position = np.array(self.localsearch.mutate(current_position))
                        new_fitness = self.getProblem().fitness(new_position)
                        is_better0 = aggregation_value(current_fitness, self.z, self.particles[p].lam) - aggregation_value(new_fitness,
                                                                                                        self.z, self.particles[p].lam)
                        if is_better0 > 0.0:
                            self.COUNT_LS_FOUND_PBEST = self.COUNT_LS_FOUND_PBEST + 1
                            LS_FOUND = True
                            self.particles[p].setPositions(new_position)
                            self.particles[p].setFitness(new_fitness)
                            self.particles[p].setPersonalPositions(new_position)
                            self.particles[p].setPersonalFitness(new_fitness)
            # update reference point z
            update_z(self.z, self.particles[p].fitness)
            # print("update gbest...")
            # update archive
            if (len(self.archive)) < self.archive_size:
                self.archive.append(copy.deepcopy(self.particles[p]))
                have_new_gbest = True
                self.refresh_gap_count[p] = 0
            else:
                if (update_archive(self.particles[p], self.archive)):
                    have_new_gbest = True
                    self.refresh_gap_count[p] = 0
                else:
                    self.refresh_gap_count[p] = self.refresh_gap_count[p] + 1
                    if (self.refresh_gap_count[p] == self.REFRESH_GAP):
                        self.REASSIGN = True
                        self.refresh_gap_count[p] = 0
            self._gbest = copy.deepcopy(self.archive[0])

        return have_new_gbest
コード例 #2
0
 def renewExemplar(self, p):
     par = self.getParticle(p)
     for d in range(par.size):
         rnd = np.random.uniform(0, 1)
         exempl = p
         if rnd >= self._Pc[p]:
             exempl = p
         else:
             p1 = int(np.round(np.random.uniform(0, 1) * self._number_of_particles))
             attemp = 0
             satify = False
             while satify == False and attemp < self._number_of_particles:
                 p1 = int(np.round(np.random.uniform(0, 1) * (self._number_of_particles - 1)))
                 attemp = attemp + 1
                 satify = ((p1 != p)) and (self.getParticle(p1).size > d)
             if satify == False:
                 p1 = p
             p2 = int(np.round(np.random.uniform(0, 1) * self._number_of_particles))
             attemp = 0
             satify = False
             while satify == False and attemp < self._number_of_particles:
                 p2 = int(np.round(np.random.uniform(0, 1) * (self._number_of_particles - 1)))
                 attemp = attemp + 1
                 satify = ((p2 != p)) and (self.getParticle(p2).size > d)
             if satify == False:
                 p2 = p
             if (aggregation_value(self.getParticle(p1).fitness, self.z,self.getParticle(p1).lam) > aggregation_value(self.getParticle(p2).fitness, self.z,self.getParticle(p2).lam)):
                 exempl = p1
             else:
                 exempl = p2
         self.getParticle(p).setExemplar(d, exempl)
     return True
コード例 #3
0
 def rank(self, index):
     swarmFitness = []
     for p in (self.particles):
         print(p.lam)
         a=aggregation_value(p.fitness,self.z,p.lam )
         swarmFitness.append(a)
     return int(pd.Series(swarmFitness).rank(method='max')[index])
コード例 #4
0
 def reinit(self):  # find the size that has the best avg pbest fitness
     print("reinit")
     sizeOfDivision = int(self._size_division)
     size = np.zeros(sizeOfDivision)
     avg_fit = np.zeros(sizeOfDivision)
     divSize = int(self._number_of_particles / sizeOfDivision)
     for i in range(int(self._size_division)):
         start_particle_idx = i * divSize
         size[i] = self.getParticle(start_particle_idx).size
         for j in range(self._div_numOfParticles):
             index = start_particle_idx + j
             p = self.getParticle(index)
             avg_fit[i] = avg_fit[i] + aggregation_value(p.personal_fitness,self.z,p.lam)
         avg_fit[i] = avg_fit[i] / divSize
     best_idx = 0
     best_avg_fit = 0.0
     # best avg fit to update max length
     for i in range(int(self._size_division)):
         if best_avg_fit <= avg_fit[i]:
             best_avg_fit = avg_fit[i]
             best_idx = i
     self._best_size = self._gbest.size
     if self._max_size != self._best_size:
         self._max_size = self._max_size - self._chunk_size
         self.ResizeParticles(size)
     else:
         print("Best size unchanged")
コード例 #5
0
    def assign_lam(self):

        lamSet = []
        for i in range(10):
            lam = np.random.uniform(0.1, 1, 2)
            lamSet.append(lam)
        lamMul = 0
        index = 0
        print(lamSet)
        for p in range(self._number_of_particles):
            index = 0
            for i in range(len(lamSet)):
                new_lamMul = aggregation_value(self.particles[p].fitness, self.z, lamSet[i])
                if (new_lamMul > lamMul):
                    lamMul = new_lamMul
                    index = i
            lam = np.array(lamSet[index])
            self.particles[p].setLambda(lam)
            del lamSet[index]

        return False