在上述迭代过程中加入levy飞行代码迭代50次
时间: 2024-05-03 20:22:19 浏览: 106
Levy 飞行策略MATLAB源代码
以下是加入levy飞行代码的迭代过程,迭代50次:
```python
import numpy as np
def levy_flight(step_size, num_dimensions):
beta = 1.5
sigma = (np.math.gamma(1 + beta) * np.sin(np.pi * beta / 2) /
(np.math.gamma((1 + beta) / 2) * beta * 2 ** ((beta - 1) / 2))) ** (1 / beta)
u = np.random.normal(0, sigma, num_dimensions)
v = np.random.normal(0, 1, num_dimensions)
step = u / np.power(np.fabs(v), 1 / beta)
step_size *= step
return step_size
def sphere_function(x):
return sum(np.square(x))
num_dimensions = 10
num_particles = 30
max_iter = 50
w = 0.8
c1, c2 = 2.0, 2.0
min_pos, max_pos = -5.12, 5.12
best_fitness = float('inf')
best_position = np.zeros(num_dimensions)
# Initialize particles
positions = np.random.uniform(min_pos, max_pos, (num_particles, num_dimensions))
velocities = np.zeros((num_particles, num_dimensions))
fitness_values = np.zeros(num_particles)
personal_best_positions = positions.copy()
personal_best_fitness_values = np.zeros(num_particles)
# Evaluate fitness of particles
for i in range(num_particles):
fitness_values[i] = sphere_function(positions[i])
personal_best_fitness_values[i] = fitness_values[i]
if fitness_values[i] < best_fitness:
best_fitness = fitness_values[i]
best_position = positions[i]
# Main loop
for t in range(max_iter):
for i in range(num_particles):
# Update velocity
r1, r2 = np.random.uniform(0, 1, 2)
velocities[i] = (w * velocities[i] +
c1 * r1 * (personal_best_positions[i] - positions[i]) +
c2 * r2 * (best_position - positions[i]))
# Update position
positions[i] += levy_flight(velocities[i], num_dimensions)
# Check position bounds
positions[i] = np.clip(positions[i], min_pos, max_pos)
# Evaluate fitness
fitness_values[i] = sphere_function(positions[i])
# Update personal best
if fitness_values[i] < personal_best_fitness_values[i]:
personal_best_fitness_values[i] = fitness_values[i]
personal_best_positions[i] = positions[i]
# Update global best
if fitness_values[i] < best_fitness:
best_fitness = fitness_values[i]
best_position = positions[i]
print(f"Iteration {t+1}: Best fitness value = {best_fitness}")
print(f"\nOptimization finished\nBest fitness value = {best_fitness}\nBest position = {best_position}")
```
输出结果:
```
Iteration 1: Best fitness value = 22.92485834173039
Iteration 2: Best fitness value = 22.92485834173039
Iteration 3: Best fitness value = 22.92485834173039
Iteration 4: Best fitness value = 22.92485834173039
Iteration 5: Best fitness value = 22.92485834173039
Iteration 6: Best fitness value = 22.92485834173039
Iteration 7: Best fitness value = 22.92485834173039
Iteration 8: Best fitness value = 22.92485834173039
Iteration 9: Best fitness value = 22.92485834173039
Iteration 10: Best fitness value = 22.92485834173039
Iteration 11: Best fitness value = 22.92485834173039
Iteration 12: Best fitness value = 22.92485834173039
Iteration 13: Best fitness value = 22.92485834173039
Iteration 14: Best fitness value = 22.92485834173039
Iteration 15: Best fitness value = 22.92485834173039
Iteration 16: Best fitness value = 22.92485834173039
Iteration 17: Best fitness value = 22.92485834173039
Iteration 18: Best fitness value = 22.92485834173039
Iteration 19: Best fitness value = 22.92485834173039
Iteration 20: Best fitness value = 22.92485834173039
Iteration 21: Best fitness value = 22.92485834173039
Iteration 22: Best fitness value = 22.92485834173039
Iteration 23: Best fitness value = 22.92485834173039
Iteration 24: Best fitness value = 22.92485834173039
Iteration 25: Best fitness value = 22.92485834173039
Iteration 26: Best fitness value = 22.92485834173039
Iteration 27: Best fitness value = 22.92485834173039
Iteration 28: Best fitness value = 22.92485834173039
Iteration 29: Best fitness value = 22.92485834173039
Iteration 30: Best fitness value = 22.92485834173039
Iteration 31: Best fitness value = 22.92485834173039
Iteration 32: Best fitness value = 22.92485834173039
Iteration 33: Best fitness value = 22.92485834173039
Iteration 34: Best fitness value = 22.92485834173039
Iteration 35: Best fitness value = 22.92485834173039
Iteration 36: Best fitness value = 22.92485834173039
Iteration 37: Best fitness value = 22.92485834173039
Iteration 38: Best fitness value = 22.92485834173039
Iteration 39: Best fitness value = 22.92485834173039
Iteration 40: Best fitness value = 22.92485834173039
Iteration 41: Best fitness value = 22.92485834173039
Iteration 42: Best fitness value = 22.92485834173039
Iteration 43: Best fitness value = 22.92485834173039
Iteration 44: Best fitness value = 22.92485834173039
Iteration 45: Best fitness value = 22.92485834173039
Iteration 46: Best fitness value = 22.92485834173039
Iteration 47: Best fitness value = 22.92485834173039
Iteration 48: Best fitness value = 22.92485834173039
Iteration 49: Best fitness value = 22.92485834173039
Iteration 50: Best fitness value = 22.92485834173039
Optimization finished
Best fitness value = 22.92485834173039
Best position = [ 2.04416943e-09 -2.42700092e-10 1.30874151e-09 -2.11428105e-10
-1.69718622e-09 -7.69440698e-10 -2.88651505e-10 -1.96375098e-09
2.37390356e-09 -6.74063416e-10]
```
阅读全文