请在我提供的代码中修改
时间: 2024-03-09 20:47:59 浏览: 24
根据您提供的代码,可以在while True循环中加入类似于下面的代码来输出每次迭代的结果:
```
iter_count = 0 # 迭代计数器
best_solution = None # 最优解向量
best_value = float('inf') # 最优解对应的目标函数值
while True:
iter_count += 1
solutions = []
# 生成新的种群,并计算目标函数值
# ...
# 更新最优解向量和对应的目标函数值
for sol in solutions:
if sol[1] < best_value:
best_solution = sol[0]
best_value = sol[1]
# 输出当前迭代的结果
print(f"Iteration {iter_count}: best solution = {best_solution}, best value = {best_value}")
# 更新种群
optimizer.tell(solutions)
```
这样,在每次迭代结束后,就会输出当前迭代轮数、种群中最优解向量以及其对应的目标函数值。请将上述代码中的以下部分替换您提供的代码中的相应部分:
```
while True: # 生成一个新的种群,每个个体是一个解向量
solutions = []
for _ in range(optimizer.population_size):
x = optimizer.ask()
x[0] = int(x[0])
x[1] = int(x[1])
if (x[0] == 1 and x[1] in [1, 51]) or (x[0] == 51 and x[1] in [1, 51]) or (x[0] == 26 and x[1] == 26): pass
else:
value = quadratic(x[0], x[1], x[2], x[3])
if (x[0] == 1 and x[1] in [1, 51]) or (x[0] == 51 and x[1] in [1, 51]) or (x[0] == 26 and x[1] == 26):
pass
else:
solutions.append((x, value))
# 更新最优解向量和对应的目标函数值
for sol in solutions:
if sol[1] < best_value:
best_solution = sol[0]
best_value = sol[1]
# 输出当前迭代的结果
print(f"Iteration {iter_count}: best solution = {best_solution}, best value = {best_value}")
iter_count += 1
# 检查解向量个数是否等于种群大小
if len(solutions) != optimizer.population_size:
# 随机生成一些解向量,补足不足的部分
while len(solutions) < optimizer.population_size:
x = [random.randint(Min_pump_zcjj, Max_pump_zcjj),
random.randint(Min_pump_bdljd, Max_pump_bdljd),
random.uniform(Min_pump_bdwz, Max_pump_bdwz),
random.uniform(Min_pump_skhd, Max_pump_skhd)]
value = quadratic(x[0], x[1], x[2], x[3])
solutions.append((x, value))
# 计算每个个体的目标函数值,并存储在solutions列表中
optimizer.tell(solutions)
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)