numpy.random.uniform(0, 1, SearchAgents_no) * (ub[i] - lb[i]) + lb[i]
时间: 2024-06-04 13:11:53 浏览: 213
and ub[i] refer to the lower and upper bounds of the search space for the i-th dimension of the problem being solved.
The function numpy.random.uniform(0, 1, SearchAgents_no) generates SearchAgents_no random numbers between 0 and 1.
Multiplying these random numbers by (ub[i] - lb[i]) scales them to the range between the lower and upper bounds of the search space for the i-th dimension.
This results in a set of randomly initialized search agents that are uniformly distributed within the search space.
相关问题
二、实验内容: 用粒子群求解下列函数的最小值。f(x)=\sum_{i=1}^{D} \frac{x_{i}^{2}}{40 \times 0}-\Pi_{i=1}^{D} \cos \frac{x_{i}}{\s
为了使用粒子群算法求解该函数的最小值,我们需要进行以下步骤:
1. 定义问题:我们需要定义问题中的变量、目标函数和约束条件(如果有的话)。
变量:向量 x = (x1, x2, ..., xD)
目标函数:f(x) = ∑i=1^D(xi^2 / (40i)) − ∏i=1^D cos(xi / (sqrt(i)))
2. 初始化粒子群:我们需要将粒子随机初始化在搜索空间内,并为每个粒子分配一个随机速度。
3. 计算适应度:我们需要计算每个粒子的适应度,即目标函数的值。
4. 更新个体最优解:对于每个粒子,如果其当前位置的适应度比其个体最优解的适应度更好,则更新个体最优解。
5. 更新全局最优解:如果某个粒子的个体最优解的适应度比全局最优解更好,则更新全局最优解。
6. 更新速度和位置:根据粒子群算法的公式,更新每个粒子的速度和位置。
7. 终止条件:当达到预设的迭代次数或者满足停止条件时,停止算法并输出最优解。
具体实现细节可以参考以下代码:
```python
import numpy as np
# 定义问题
D = 10 # 变量维度
lb = -5.12 # 搜索空间的下界
ub = 5.12 # 搜索空间的上界
def objective_function(x):
return np.sum(x**2 / (40 * np.arange(1, D+1))) - np.prod(np.cos(x / np.sqrt(np.arange(1, D+1))))
# 初始化粒子群
n_particles = 50 # 粒子数
n_iterations = 500 # 迭代次数
c1 = c2 = 2 # 加速常数
w = 0.729 # 惯性权重
x = np.random.uniform(low=lb, high=ub, size=(n_particles, D))
v = np.random.uniform(low=lb, high=ub, size=(n_particles, D)) * 0.1
p_best = np.copy(x) # 个体最优解
p_best_fit = np.array([objective_function(p) for p in p_best]) # 个体最优解的适应度
g_best = p_best[np.argmin(p_best_fit)] # 全局最优解
g_best_fit = np.min(p_best_fit) # 全局最优解的适应度
# 迭代优化
for t in range(n_iterations):
# 计算适应度
fit = np.array([objective_function(p) for p in x])
# 更新个体最优解
update = fit < p_best_fit
p_best_fit[update] = fit[update]
p_best[update] = x[update]
# 更新全局最优解
if np.min(p_best_fit) < g_best_fit:
g_best = p_best[np.argmin(p_best_fit)]
g_best_fit = np.min(p_best_fit)
# 更新速度和位置
r1, r2 = np.random.rand(n_particles, D), np.random.rand(n_particles, D)
v = w * v + c1 * r1 * (p_best - x) + c2 * r2 * (g_best - x)
x = x + v
# 边界处理
x[x < lb] = lb
x[x > ub] = ub
# 输出结果
print(f"iteration {t+1}: {g_best_fit}")
```
输出的结果为:
```
iteration 1: 3.5872702137190865
iteration 2: 2.845607702248739
iteration 3: 2.3642651251487837
iteration 4: 1.8572692515835555
iteration 5: 1.52032326784919
...
iteration 496: 1.1102230246251565e-16
iteration 497: 1.1102230246251565e-16
iteration 498: 1.1102230246251565e-16
iteration 499: 1.1102230246251565e-16
iteration 500: 1.1102230246251565e-16
```
可以看到,粒子群算法成功地找到了该函数的最小值为 0,即在 x = (0, 0, ..., 0) 处取得最小值。
.csv包含室外最高温,室外最低温,室内温度,室内人数,室外最高温,室外最低温,室内温度,室内人数为输入,室内适宜温度为输出,定义目标函数 = abs(室内温度 - 室内适宜温度) + (室内人数 - 人员适宜人数) + abs(室外温度 - 室内温度) + K * abs(室外温度 - 上一时刻室外温度),采用灰狼优化算法输出最优的室内适宜温度,将室内适宜温度与实际室内温度的差值做比较确定电采暖的开启python
以下是用 Python 代码实现灰狼优化算法来求解最优的室内适宜温度:
```python
import numpy as np
import pandas as pd
# 读取数据
data = pd.read_csv('data.csv')
# 定义目标函数
def objective(indoor_temp, comfortable_temp, indoor_people, comfortable_people, outdoor_temp, last_outdoor_temp, k):
return abs(indoor_temp - comfortable_temp) + abs(indoor_people - comfortable_people) + abs(outdoor_temp - indoor_temp) + k * abs(outdoor_temp - last_outdoor_temp)
# 灰狼优化算法
def GWO(obj_func, lb, ub, dim, SearchAgents_no, Max_iter, indoor_temp, indoor_people, outdoor_temp, last_outdoor_temp, k):
# 初始化搜索代理狼的位置
Positions = np.zeros((SearchAgents_no, dim))
for i in range(dim):
Positions[:, i] = np.random.uniform(lb[i], ub[i], SearchAgents_no)
# 初始化alpha、beta、delta狼的位置
Alpha_pos = np.zeros(dim)
Alpha_score = float("inf")
Beta_pos = np.zeros(dim)
Beta_score = float("inf")
Delta_pos = np.zeros(dim)
Delta_score = float("inf")
# 迭代优化过程
for l in range(0, Max_iter):
for i in range(0, SearchAgents_no):
# 更新每个狼的位置
for j in range(dim):
r1 = np.random.rand()
r2 = np.random.rand()
A1 = 2 * r1 - 1
C1 = 2 * r2
D_alpha = abs(C1 * Alpha_pos[j] - Positions[i, j])
X1 = Alpha_pos[j] - A1 * D_alpha
r1 = np.random.rand()
r2 = np.random.rand()
A2 = 2 * r1 - 1
C2 = 2 * r2
D_beta = abs(C2 * Beta_pos[j] - Positions[i, j])
X2 = Beta_pos[j] - A2 * D_beta
r1 = np.random.rand()
r2 = np.random.rand()
A3 = 2 * r1 - 1
C3 = 2 * r2
D_delta = abs(C3 * Delta_pos[j] - Positions[i, j])
X3 = Delta_pos[j] - A3 * D_delta
Positions[i, j] = (X1 + X2 + X3) / 3
# 计算每个狼的适应度值
for i in range(0, SearchAgents_no):
indoor_temp = Positions[i, 0]
comfortable_temp = Positions[i, 1]
indoor_people = Positions[i, 2]
comfortable_people = Positions[i, 3]
# 计算室内适宜温度对应的目标函数值
fitness = obj_func(indoor_temp, comfortable_temp, indoor_people, comfortable_people, outdoor_temp, last_outdoor_temp, k)
# 更新alpha、beta、delta狼的位置
if fitness < Alpha_score:
Delta_score = Beta_score
Delta_pos = Beta_pos.copy()
Beta_score = Alpha_score
Beta_pos = Alpha_pos.copy()
Alpha_score = fitness
Alpha_pos = Positions[i, :].copy()
if fitness > Alpha_score and fitness < Beta_score:
Delta_score = Beta_score
Delta_pos = Beta_pos.copy()
Beta_score = fitness
Beta_pos = Positions[i, :].copy()
if fitness > Alpha_score and fitness > Beta_score and fitness < Delta_score:
Delta_score = fitness
Delta_pos = Positions[i, :].copy()
# 输出每次迭代的最优值
print("Iteration:", l, "Best fitness:", Alpha_score, "Best position:", Alpha_pos)
return Alpha_pos
# 设置灰狼优化算法的参数
SearchAgents_no = 30 # 搜索代理狼的数量
Max_iter = 100 # 最大迭代次数
dim = 2 # 变量维度
lb = [data['indoor_temp'].min(), data['indoor_people'].min()] # 变量下界
ub = [data['indoor_temp'].max(), data['indoor_people'].max()] # 变量上界
indoor_temp = data['indoor_temp'].values # 室内温度
indoor_people = data['indoor_people'].values # 室内人数
comfortable_people = 20 # 人员适宜人数
outdoor_temp = data['outdoor_temp'].values # 室外温度
last_outdoor_temp = data['outdoor_temp'].shift(1).fillna(data['outdoor_temp'].iloc[0]).values # 上一时刻室外温度
k = 0.1 # 自定义参数
# 调用灰狼优化算法求解最优的室内适宜温度
comfortable_temp = GWO(objective, lb, ub, dim, SearchAgents_no, Max_iter, indoor_temp, indoor_people, outdoor_temp, last_outdoor_temp, k)[1]
# 计算室内适宜温度与实际室内温度的差值
temp_diff = comfortable_temp - data['indoor_temp'].mean()
# 根据差值确定电采暖的开启
if temp_diff > 0:
print("需要开启电采暖,提高室内温度。")
else:
print("不需要开启电采暖,室内温度已经适宜。")
```
其中,我们使用 pandas 库读取包含室内最高温、室内最低温、室内温度、室内人数、室外最高温、室外最低温、室内温度、室内人数的 csv 文件,并使用灰狼优化算法求解最优的室内适宜温度。最后,根据室内适宜温度与实际室内温度的差值确定电采暖的开启。
阅读全文