示例#1
0
    def run(self):
        """ Execute the algorithm. """
        start_computing_time = time.time()

        self.swarm = self.create_initial_swarm()
        for i in range(self.swarm_size):
            self.problem.evaluate(self.swarm[i])
            if self.swarm[i].objective < self.best_solution.objective:
                self.best_solution = self.swarm[i]

        self.records = []

        for iter in range(self.max_iterations):
            a = 2 - iter * (
                (2) / self.max_iterations)  # a decreases linearly fro 2 to 0

            for i in range(self.swarm_size):
                coord = self.wolves_coords[i]
                neighbors = find_neighborhood(self.neighbor_structure,
                                              self.grid_catalog, coord)
                alpha_wolf, beta_wolf, delta_wolf = self.select_best_three_neigbors(
                    neighbors)

                r1 = np.random.random((3, self.problem.number_of_variables))
                r2 = np.random.random((3, self.problem.number_of_variables))

                A1 = 2 * a * r1[0] - a
                C1 = 2 * r2[0]
                D_alpha = np.abs(C1 * alpha_wolf.variables -
                                 self.swarm[i].variables)
                X1 = alpha_wolf.variables - A1 * D_alpha

                A2 = 2 * a * r1[1] - a
                C2 = 2 * r2[1]
                D_beta = np.abs(C2 * beta_wolf.variables -
                                self.swarm[i].variables)
                X2 = beta_wolf.variables - A2 * D_beta

                A3 = 2 * a * r1[2] - a
                C3 = 2 * r2[2]
                D_delta = np.abs(C3 * delta_wolf.variables -
                                 self.swarm[i].variables)
                X3 = delta_wolf.variables - A3 * D_delta

                new_pos = (X1 + X2 + X3) / 3
                new_pos = np.clip(new_pos, self.problem.lower_bound,
                                  self.problem.upper_bound)
                new_wolf = FloatSolution(self.problem.number_of_variables)
                new_wolf.variables = new_pos
                self.problem.evaluate(new_wolf)

                # greedy
                if new_wolf.objective < self.swarm[i].objective:
                    self.swarm[i] = new_wolf
                    if new_wolf.objective < self.best_solution.objective:
                        self.best_solution = new_wolf

            self.records.append(self.best_solution.objective)

        self.total_computing_time = time.time() - start_computing_time
示例#2
0
文件: BBO.py 项目: zzw95/SOIO
    def run(self):
        """ Execute the algorithm. """
        self.records = []
        start_computing_time = time.time()

        self.habitats = [
            self.problem.create_solution() for _ in range(self.habitats_size)
        ]
        for habitat in self.habitats:
            self.problem.evaluate(habitat)
        self.habitats.sort(key=lambda x: x.objective)
        self.best_solution = self.habitats[0]
        assert self.habitats[0].objective < self.habitats[-1].objective

        self.immigrate_rates = []
        self.emigrate_rates = []
        for i in range(self.habitats_size):
            mu = (self.habitats_size + 1 - i) / (self.habitats_size + 1)
            self.emigrate_rates.append(mu)  #decreasing
            self.immigrate_rates.append(1 - mu)  #increasing
        self.mut_prob = 0.01
        self.keep = 2

        for _ in range(self.max_iterations):
            # Migration
            new_habitats = []
            new_habitats.append(self.habitats[0])
            for hi in range(1, self.habitats_size):
                new_habitat = FloatSolution(self.problem.number_of_variables)
                for vi in range(self.problem.number_of_variables):
                    if random.random() < self.immigrate_rates[hi]:
                        # Roulette Wheel Selection
                        rhi = self.roulette_wheel_selection()
                        new_habitat.variables[vi] = self.habitats[
                            rhi].variables[vi]
                    else:
                        new_habitat.variables[vi] = self.habitats[
                            hi].variables[vi]
                new_habitats.append(new_habitat)

            # Mutation
            for hi in range(self.habitats_size):
                for vi in range(self.problem.number_of_variables):
                    if random.random() < self.mut_prob:
                        new_habitats[hi].variables[vi] = self.problem.lower_bound[vi] + \
                                               (self.problem.upper_bound[vi]-self.problem.lower_bound[vi])*random.random()

            for habitat in new_habitats:
                self.problem.evaluate(habitat)

            new_habitats.sort(key=lambda x: x.objective)
            new_habitats[self.habitats_size -
                         self.keep:] = self.habitats[:self.keep]
            self.habitats = sorted(new_habitats, key=lambda x: x.objective)
            assert len(self.habitats) == self.habitats_size

            self.best_solution = self.habitats[0]
            self.records.append(self.best_solution.objective)

        self.total_computing_time = time.time() - start_computing_time
示例#3
0
文件: CWOA.py 项目: zzw95/SOIO
    def select_best_neigbor(self, neighbors):
        leader = FloatSolution(self.problem.number_of_variables)
        leader.objective = float("inf")
        for i in neighbors:
            if self.swarm[i].objective < leader.objective:
                leader = self.swarm[i]

        return leader
示例#4
0
文件: GWO.py 项目: zzw95/SOIO
    def __init__(self,
                 problem: FloatProblem,
                 swarm_size: int,
                 max_nfes: int):

        self.problem = problem
        self.swarm_size = swarm_size
        self.max_iterations = int(max_nfes/swarm_size)

        self.alpha_wolf = FloatSolution(self.problem.number_of_variables)
        self.alpha_wolf.objective = float("inf")
        self.beta_wolf = FloatSolution(self.problem.number_of_variables)
        self.beta_wolf.objective = float("inf")
        self.delta_wolf = FloatSolution(self.problem.number_of_variables)
        self.delta_wolf.objective = float("inf")
示例#5
0
    def __init__(
        self,
        problem: FloatProblem,
        max_nfes: int,
        grid_shape=[20, 20],
        neighbor_structure=CA_C21,
    ):

        self.problem = problem
        self.swarm_size = grid_shape[0] * grid_shape[1]
        self.max_nfes = max_nfes

        self.max_iterations = int(self.max_nfes /
                                  (grid_shape[0] * grid_shape[1]))

        self.neighbor_structure = neighbor_structure
        self.grid_catalog = np.random.permutation(
            self.swarm_size).reshape(grid_shape)

        self.wolves_coords = [None] * self.swarm_size
        for i in range(grid_shape[0]):
            for j in range(grid_shape[1]):
                self.wolves_coords[self.grid_catalog[i][j]] = [i, j]

        self.best_solution = FloatSolution(self.problem.number_of_variables)
        self.best_solution.objective = float("inf")
示例#6
0
    def run(self):
        """ Execute the algorithm. """
        start_computing_time = time.time()

        self.population = self.create_initial_population()
        for ind in self.population:
            self.problem.evaluate(ind)
            if ind.objective < self.best_solution.objective:
                self.best_solution = ind
        self.records = []

        for iter in range(self.max_iterations):
            for i in range(self.population_size):
                a = int(random.random()*self.population_size)
                while i==a:
                    a = int(random.random() * self.population_size)
                b = int(random.random() * self.population_size)
                while i==b or a==b:
                    b = int(random.random() * self.population_size)
                c = int(random.random() * self.population_size)
                while i==c or a==c or b==c:
                    c = int(random.random() * self.population_size)
                new_solution = FloatSolution(self.problem.number_of_variables)
                for j in range(self.problem.number_of_variables):
                    if random.random() < self.CR:
                        v = self.population[a].variables[j] + self.F * (self.population[b].variables[j] - self.population[c].variables[j])
                        new_solution.variables[j] = min(max(v, self.problem.lower_bound[j]), self.problem.upper_bound[j])
                    else:
                        new_solution.variables[j] = self.population[i].variables[j]
                rj = int(random.random()*self.problem.number_of_variables)
                new_solution.variables[rj] = self.population[a].variables[rj] + self.F * (self.population[b].variables[rj] - self.population[c].variables[rj])
                self.problem.evaluate(new_solution)

                if new_solution.objective < self.population[i].objective:
                    self.population[i] = new_solution
                    if new_solution.objective < self.best_solution.objective:
                        self.best_solution = new_solution

            self.records.append(self.best_solution.objective)

        self.total_computing_time = time.time() - start_computing_time
示例#7
0
文件: GWO.py 项目: zzw95/SOIO
    def run(self):
        """ Execute the algorithm. """
        start_computing_time = time.time()

        self.swarm = self.create_initial_swarm()
        self.evaluate(self.swarm)
        self.records = []

        for iter in range(self.max_iterations):
            a = 2 - iter * 2 / self.max_iterations  # a decreases linearly fro 2 to 0
            r1 = np.random.random((3, self.swarm_size, self.problem.number_of_variables))
            r2 = np.random.random((3, self.swarm_size, self.problem.number_of_variables))
            A1 = 2 * a * r1[0] - a
            C1 = 2 * r2[0]
            A2 = 2 * a * r1[1] - a
            C2 = 2 * r2[1]
            A3 = 2 * a * r1[2] - a
            C3 = 2 * r2[2]

            for i in range(self.swarm_size):
                wolf = self.swarm[i]
                D_alpha = np.abs(C1[i] * self.alpha_wolf.variables - wolf.variables)
                X1 = self.alpha_wolf.variables - A1[i] * D_alpha

                D_beta = np.abs(C2[i] * self.beta_wolf.variables - wolf.variables)
                X2 = self.beta_wolf.variables - A2[i] * D_beta

                D_delta = np.abs(C3[i] * self.delta_wolf.variables - wolf.variables)
                X3 = self.delta_wolf.variables - A3[i] * D_delta

                new_pos = (X1 + X2 + X3) / 3
                new_pos = np.clip(new_pos, self.problem.lower_bound, self.problem.upper_bound)

                new_wolf = FloatSolution(self.problem.number_of_variables)
                new_wolf.variables = new_pos
                self.swarm[i] = new_wolf

            self.swarm = self.evaluate(self.swarm)
            self.records.append(self.alpha_wolf.objective)

        self.total_computing_time = time.time() - start_computing_time
示例#8
0
文件: HS.py 项目: zzw95/SOIO
    def run(self):
        """ Execute the algorithm. """
        start_computing_time = time.time()
        self.HM = [self.problem.create_solution() for _ in range(self.HMS)]
        for s in self.HM:
            self.problem.evaluate(s)
        worst_harmony_idx = self.get_worst_harmony_idx()
        terminate = False
        while True:
            if self.max_nfes < self.problem.nfes:
                terminate = True
                break
            new_harmony = FloatSolution(self.problem.number_of_variables)
            for iv in range(self.problem.number_of_variables):
                if random.random() < self.HMCR:
                    pitch = self.HM[int(random.random() *
                                        self.HMS)].variables[iv]
                    if random.random() < self.PAR:
                        pitch = pitch + (2 * random.random() - 1) * self.bw
                        if pitch > self.problem.upper_bound[iv]:
                            pitch = self.problem.upper_bound[iv]
                        elif pitch < self.problem.lower_bound[iv]:
                            pitch = self.problem.lower_bound[iv]
                    new_harmony.variables[iv] = pitch
                else:
                    new_harmony.variables[iv] = random.uniform(
                        self.problem.lower_bound[iv] * 1.0,
                        self.problem.upper_bound[iv] * 1.0)
            self.problem.evaluate(new_harmony)
            if new_harmony.objective < self.HM[worst_harmony_idx].objective:
                self.HM[worst_harmony_idx] = new_harmony
                worst_harmony_idx = self.get_worst_harmony_idx()

        self.best_solution = self.HM[0]
        for harmony in self.HM:
            if harmony.objective < self.best_solution.objective:
                self.best_solution = harmony

        self.total_computing_time = time.time() - start_computing_time
示例#9
0
    def select_best_three_neigbors(self, neighbors):
        alpha_wolf = FloatSolution(self.problem.number_of_variables)
        alpha_wolf.objective = float("inf")
        beta_wolf = FloatSolution(self.problem.number_of_variables)
        beta_wolf.objective = float("inf")
        delta_wolf = FloatSolution(self.problem.number_of_variables)
        delta_wolf.objective = float("inf")
        for i in neighbors:
            if self.swarm[i].objective < alpha_wolf.objective:
                delta_wolf = beta_wolf
                beta_wolf = alpha_wolf
                alpha_wolf = self.swarm[i]

            elif self.swarm[i].objective < beta_wolf.objective:
                delta_wolf = beta_wolf
                beta_wolf = self.swarm[i]

            elif self.swarm[i].objective < delta_wolf.objective:
                delta_wolf = self.swarm[i]
        return alpha_wolf, beta_wolf, delta_wolf
示例#10
0
文件: ICA.py 项目: zzw95/SOIO
    def __init__(self, problem: FloatProblem, population_size: int,
                 imperialists_size: int, max_nfes: int):

        self.problem = problem
        self.population_size = population_size
        self.imperialists_size = imperialists_size
        self.colonies_size = population_size - imperialists_size
        self.max_infes = max_nfes

        self.beta = 2
        self.gamma = math.pi / 4
        self.epsilon = 0.1

        self.best_solution = FloatSolution(self.problem.number_of_variables)
        self.best_solution.objective = float("inf")
示例#11
0
    def __init__(self,
                 problem: FloatProblem,
                 population_size: int,
                 max_nfes: int,
                 CR:float = 0.5, # [0,1] crossover rate
                 F: float = 0.1, #[0,2}
                 ):

        self.problem = problem
        self.population_size = population_size
        self.max_iterations = int(max_nfes/population_size)
        self.F = F
        self.CR = CR

        self.best_solution = FloatSolution(self.problem.number_of_variables)
        self.best_solution.objective = float("inf")
示例#12
0
    def __init__(
            self,
            problem: FloatProblem,
            max_nfes: int,
            population_size: int = 30,
            archive_size=0,
            p=0.05,  # [5%, 20%]
            c=0.1  #[1/20, 1/5]
    ):

        self.problem = problem
        self.population_size = population_size
        self.max_iterations = int(max_nfes / population_size)
        self.archive_size = archive_size
        self.p = p
        self.c = c

        self.best_solution = FloatSolution(self.problem.number_of_variables)
        self.best_solution.objective = float("inf")
示例#13
0
文件: CWOA.py 项目: zzw95/SOIO
    def run(self):
        """ Execute the algorithm. """
        start_computing_time = time.time()

        self.swarm = self.create_initial_swarm()
        for i in range(self.swarm_size):
            self.problem.evaluate(self.swarm[i])
            if self.swarm[i].objective < self.best_solution.objective:
                self.best_solution = self.swarm[i]

        self.records = []

        for iter in range(self.max_iterations):
            a = 2 - iter * (
                (2) / self.max_iterations)  # a decreases linearly fro 2 to 0
            a2 = -1 + iter * ((-1) / self.max_iterations
                              )  # a2 linearly decreases from -1 to -2

            for i in range(self.swarm_size):
                coord = self.whales_coords[i]
                neighbors = find_neighborhood(self.neighbor_structure,
                                              self.grid_catalog, coord)
                leader = self.select_best_neigbor(neighbors)

                r1 = random.random()
                r2 = random.random()
                A = 2 * a * r1 - a
                C = 2 * r2
                b = 1
                l = (a2 - 1) * random.random() + 1
                p = random.random()

                new_whale = FloatSolution(self.problem.number_of_variables)

                for j in range(0, self.problem.number_of_variables):

                    if p < 0.5:
                        if abs(A) >= 1:
                            # rand_leader_index = random.randint(0, self.swarm_size-1)
                            rand_leader_index = random.choice(neighbors)
                            X_rand = self.swarm[rand_leader_index].variables
                            D_X_rand = abs(C * X_rand[j] -
                                           self.swarm[i].variables[j])
                            new_whale.variables[j] = X_rand[j] - A * D_X_rand

                        elif abs(A) < 1:
                            D_Leader = abs(C * leader.variables[j] -
                                           self.swarm[i].variables[j])
                            new_whale.variables[
                                j] = leader.variables[j] - A * D_Leader

                    elif p >= 0.5:
                        distance2Leader = abs(leader.variables[j] -
                                              self.swarm[i].variables[j])
                        new_whale.variables[j] = distance2Leader * math.exp(
                            b * l) * math.cos(
                                l * 2 * math.pi) + leader.variables[j]

                new_whale.variables = np.clip(new_whale.variables,
                                              self.problem.lower_bound,
                                              self.problem.upper_bound)
                self.problem.evaluate(new_whale)
                # greedy
                if new_whale.objective < self.swarm[i].objective:
                    self.swarm[i] = new_whale
                    if new_whale.objective < self.best_solution.objective:
                        self.best_solution = new_whale

            self.records.append(self.best_solution.objective)

        self.total_computing_time = time.time() - start_computing_time
示例#14
0
    def run(self):
        """ Execute the algorithm. """
        start_computing_time = time.time()

        self.population = self.create_initial_population()
        for ind in self.population:
            self.problem.evaluate(ind)

        top_population = sorted(
            self.population,
            key=lambda x: x.objective)[:int(self.population_size * self.p)]
        archive = []
        mu_CR = 0.5
        mu_F = 0.5

        self.best_solution = top_population[0]
        self.records = []

        for iter in range(self.max_iterations):
            SCR = []  # successful crossover probabilities
            SF = []  # successful mutation factors
            for i in range(self.population_size):
                CR_i = random.normalvariate(mu_F, 0.1)
                F_i = cauchy.rvs(loc=mu_F, scale=0.1, size=1)[0]
                Xp_best = top_population[int(random.random() *
                                             len(top_population))]

                a = int(random.random() * self.population_size)
                while i == a:
                    a = int(random.random() * self.population_size)
                b = int(random.random() *
                        (self.population_size + len(archive)))
                while i == b or a == b:
                    b = int(random.random() * self.population_size +
                            len(archive))
                Xi = self.population[i]
                Xa = self.population[a]
                Xb = self.population[
                    b] if b < self.population_size else archive[
                        b - self.population_size]

                new_solution = FloatSolution(self.problem.number_of_variables)
                for j in range(self.problem.number_of_variables):
                    if random.random() < CR_i:
                        v = Xi.variables[j] + F_i * (
                            Xp_best.variables[j] - Xi.variables[j]) + F_i * (
                                Xa.variables[j] - Xb.variables[j])
                        #new_solution.variables[j] = min(max(v, self.problem.lower_bound[j]), self.problem.upper_bound[j])
                        if v < self.problem.lower_bound[j]:
                            v = (self.problem.lower_bound[j] +
                                 Xi.variables[j]) / 2
                        if v > self.problem.upper_bound[j]:
                            v = (self.problem.upper_bound[j] +
                                 Xi.variables[j]) / 2
                        new_solution.variables[j] = v
                    else:
                        new_solution.variables[j] = Xi.variables[j]
                rj = int(random.random() * self.problem.number_of_variables)
                v = Xi.variables[rj] + F_i * (
                    Xp_best.variables[rj] - Xi.variables[rj]) + F_i * (
                        Xa.variables[rj] - Xb.variables[rj])
                if v < self.problem.lower_bound[rj]:
                    v = (self.problem.lower_bound[rj] + Xi.variables[rj]) / 2
                if v > self.problem.upper_bound[rj]:
                    v = (self.problem.upper_bound[rj] + Xi.variables[rj]) / 2
                new_solution.variables[rj] = v
                self.problem.evaluate(new_solution)

                if new_solution.objective < self.population[i].objective:
                    if self.archive_size > 0:
                        if len(archive) < self.archive_size:
                            archive.append(self.population[i])
                        else:
                            archive[int(
                                random.random() *
                                self.archive_size)] = self.population[i]
                    self.population[i] = new_solution
                    SCR.append(CR_i)
                    SF.append(F_i)

            if len(SCR) > 0:
                mu_CR = (1 - self.c) * mu_CR + self.c * np.mean(SCR)
                mu_F = (1 - self.c) * mu_F + self.c * np.sum(np.power(
                    SF, 2)) / (np.sum(SF) + 1E-10)

            top_population = sorted(
                self.population,
                key=lambda x: x.objective)[:int(self.population_size * self.p)]
            self.best_solution = top_population[0]
            self.records.append(self.best_solution.objective)

        self.total_computing_time = time.time() - start_computing_time
示例#15
0
 def create_solution(self) -> FloatSolution:
     new_solution = FloatSolution(self.number_of_variables)
     new_solution.variables = np.random.uniform(self.lower_bound,
                                                self.upper_bound)
     return new_solution
示例#16
0
 def evaluate(self, solution: FloatSolution):
     self.nfes += 1
     solution.objective = self.compute(solution.variables)