单目标函数
import random
def fitness_function(position):
x = position[0]
return -(x ** 2 - 4 * x + 4)
class Particle:
def __init__(self, position):
self.position = position
self.velocity = [random.uniform(-1, 1) for _ in range(len(position))]
self.best_position = position
self.best_fitness = fitness_function(position)
def particle_swarm_optimization(pop_size, num_generations, inertia_weight, cognitive_weight, social_weight, lower_bound,
upper_bound):
particles = [Particle([random.uniform(lower_bound, upper_bound)]) for _ in range(pop_size)]
global_best_position = particles[0].position
global_best_fitness = particles[0].best_fitness
for generation in range(num_generations):
for particle in particles:
for i in range(len(particle.position)):
r1 = random.random()
r2 = random.random()
cognitive_component = cognitive_weight * r1 * (particle.best_position[i] - particle.position[i])
social_component = social_weight * r2 * (global_best_position[i] - particle.position[i])
particle.velocity[i] = inertia_weight * particle.velocity[i] + cognitive_component + social_component
particle.position[i] += particle.velocity[i]
fitness = fitness_function(particle.position)
if fitness > particle.best_fitness:
particle.best_position = particle.position
particle.best_fitness = fitness
if fitness > global_best_fitness:
global_best_position = particle.position
global_best_fitness = fitness
return global_best_position, -global_best_fitness
if __name__ == "__main__":
pop_size = 50
num_generations = 100
inertia_weight = 0.7
cognitive_weight = 1.4
social_weight = 1.4
lower_bound = -10
upper_bound = 10
best_position, best_fitness = particle_swarm_optimization(pop_size, num_generations, inertia_weight,
cognitive_weight, social_weight, lower_bound, upper_bound)
print("Best position:", best_position)
print("Best fitness:", best_fitness)
多目标函数
import random
import time
import numpy as np
def objective_function(x):
return x[0] ** 2 + x[1] ** 2
class Particle:
def __init__(self, bounds):
self.bounds = bounds
self.position = np.array([random.uniform(bounds[i][0], bounds[i][1]) for i in range(len(bounds))])
self.velocity = np.array([0.0 for _ in range(len(bounds))])
self.best_position = self.position.copy()
self.best_value = float('inf')
def update(self, global_best):
w = 0.5
c1 = 1
c2 = 2
v_max = 5
self.velocity = w * self.velocity + c1 * random.random() * (
self.best_position - self.position) + c2 * random.random() * (global_best - self.position)
self.velocity = np.clip(self.velocity, -v_max, v_max)
self.position += self.velocity
self.position = np.clip(self.position, [b[0] for b in self.bounds], [b[1] for b in self.bounds])
current_value = objective_function(self.position)
if current_value < self.best_value:
self.best_position = self.position.copy()
self.best_value = current_value
def particle_swarm_optimization(num_particles, bounds, num_iterations):
particles = [Particle(bounds) for _ in range(num_particles)]
global_best = np.array([float('inf'), float('inf')])
global_best_value = float('inf')
for _ in range(num_iterations):
for particle in particles:
particle.update(global_best)
if particle.best_value < global_best_value:
global_best = particle.best_position.copy()
global_best_value = particle.best_value
return global_best, global_best_value
start_time = time.time()
bounds = [(-10, 10), (-10, 10)]
best_position, best_value = particle_swarm_optimization(1000, bounds, 100)
print(f"最佳位置: {best_position}, 最佳值: {best_value}")
end_time = time.time()
print(f"函数执行时间为: {end_time - start_time} 秒")