import random
import numpy as np
import matplotlib.pyplot as plt
# 定义目标函数
def objective_function(x):
return x ** 2 - 10 * np.cos(2 * np.pi * x)
# 定义适应度函数
def fitness_function(x):
return 1.0 / (1.0 + objective_function(x))
# 初始化种群
def initialize_population(pop_size, min_range, max_range):
population = []
for i in range(pop_size):
x = random.uniform(min_range, max_range)
population.append(x)
return population
# 计算猴子的运动距离
def calculate_distance(position, fitness, global_best_position, global_best_fitness):
r1 = random.uniform(0, 1)
r2 = random.uniform(0, 1)
a = 2 * r1 - 1
c = 2 * r2
distance = c * abs(global_best_position - position) + a * fitness
return distance
# 移动猴子位置
def move_monkey(position, distance, min_range, max_range):
new_position = position + distance
if ne
蜘蛛猴优化算法python代码
最新推荐文章于 2024-06-20 17:54:56 发布