GA-PSO python代码
时间: 2023-07-23 16:06:17 浏览: 237
GA_PSO算法程序代码
下面是一个简单的 GA-PSO 算法的 Python 代码示例:
```python
import numpy as np
import random
class Particle:
def __init__(self, dim, pos_range, vel_range):
self.dim = dim
self.pos = np.random.uniform(*pos_range, size=dim)
self.vel = np.random.uniform(*vel_range, size=dim)
self.best_pos = self.pos.copy()
self.fitness = 0
self.best_fitness = 0
def update_position(self):
self.pos += self.vel
def update_velocity(self, omega, c1, c2, gbest_pos):
r1 = np.random.uniform(size=self.dim)
r2 = np.random.uniform(size=self.dim)
social = c1 * r1 * (gbest_pos - self.pos)
cognitive = c2 * r2 * (self.best_pos - self.pos)
self.vel = omega * self.vel + social + cognitive
class PSO:
def __init__(self, num_particles, dim, pos_range, vel_range, c1, c2, omega):
self.num_particles = num_particles
self.dim = dim
self.pos_range = pos_range
self.vel_range = vel_range
self.c1 = c1
self.c2 = c2
self.omega = omega
self.particles = [Particle(dim, pos_range, vel_range) for i in range(num_particles)]
self.gbest_pos = np.zeros(dim)
self.gbest_fitness = 0
def evaluate_fitness(self, fitness_func):
for particle in self.particles:
particle.fitness = fitness_func(particle.pos)
if particle.fitness > particle.best_fitness:
particle.best_pos = particle.pos.copy()
particle.best_fitness = particle.fitness
if particle.fitness > self.gbest_fitness:
self.gbest_pos = particle.pos.copy()
self.gbest_fitness = particle.fitness
def update_particles(self):
for particle in self.particles:
particle.update_velocity(self.omega, self.c1, self.c2, self.gbest_pos)
particle.update_position()
class GA:
def __init__(self, num_individuals, dim, bounds, mutation_rate, fitness_func):
self.num_individuals = num_individuals
self.dim = dim
self.bounds = bounds
self.mutation_rate = mutation_rate
self.fitness_func = fitness_func
self.individuals = []
for i in range(num_individuals):
individual = np.random.uniform(*bounds, size=dim)
self.individuals.append(individual)
def evaluate_fitness(self):
fitness = []
for individual in self.individuals:
fitness.append(self.fitness_func(individual))
return fitness
def selection(self, fitness):
total_fitness = sum(fitness)
probabilities = [f / total_fitness for f in fitness]
indices = list(range(self.num_individuals))
selected_indices = random.choices(indices, weights=probabilities, k=self.num_individuals)
selected_individuals = [self.individuals[i] for i in selected_indices]
return selected_individuals
def crossover(self, parents):
children = []
for i in range(0, self.num_individuals, 2):
parent1, parent2 = parents[i], parents[i+1]
split_index = random.randint(1, self.dim - 1)
child1 = np.concatenate((parent1[:split_index], parent2[split_index:]))
child2 = np.concatenate((parent2[:split_index], parent1[split_index:]))
children.append(child1)
children.append(child2)
return children
def mutation(self, children):
for i in range(self.num_individuals):
if random.random() < self.mutation_rate:
children[i] = np.random.uniform(*self.bounds, size=self.dim)
return children
class GAPSO:
def __init__(self, num_particles, dim, pos_range, vel_range, c1, c2, omega,
num_individuals, bounds, mutation_rate, fitness_func):
self.num_particles = num_particles
self.dim = dim
self.pos_range = pos_range
self.vel_range = vel_range
self.c1 = c1
self.c2 = c2
self.omega = omega
self.num_individuals = num_individuals
self.bounds = bounds
self.mutation_rate = mutation_rate
self.fitness_func = fitness_func
self.pso = PSO(num_particles, dim, pos_range, vel_range, c1, c2, omega)
self.ga = GA(num_individuals, dim, bounds, mutation_rate, fitness_func)
def run(self, num_iterations):
for i in range(num_iterations):
self.pso.evaluate_fitness(self.fitness_func)
fitness = self.ga.evaluate_fitness()
parents = self.ga.selection(fitness)
children = self.ga.crossover(parents)
children = self.ga.mutation(children)
self.ga.individuals = self.pso.particles + children
self.pso.num_particles = len(self.ga.individuals)
self.pso.particles = self.ga.individuals
self.pso.update_particles()
return self.pso.gbest_pos
```
在上面的代码中,`Particle` 类表示一个粒子,包含位置、速度、适应度、最佳位置和最佳适应度等属性,以及更新位置和速度的方法。`PSO` 类表示一个 PSO 算法,包含多个粒子,以及初始化、评估适应度和更新粒子的方法。`GA` 类表示一个简单的遗传算法,包含多个个体,以及评估适应度、选择、交叉和变异的方法。`GAPSO` 类表示一个 GA-PSO 算法,包含一个 PSO 和一个 GA,以及运行算法的方法。
阅读全文