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)) # 检查解向量个数是否等于种群大小 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)
时间: 2024-01-02 09:02:18 浏览: 28
好的,根据您的要求,我已经修改了代码,将每次迭代的轮数也输出,代码如下所示:
```python
rounds = 0
while True:
rounds += 1
# 生成一个新的种群,每个个体是一个解向量
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))
# 检查解向量个数是否等于种群大小
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)
print("Iteration rounds:", rounds)
```
现在,每次迭代时都会输出当前进行对话的轮数。请注意,我将迭代轮数的变量命名为`rounds`,并在每次迭代开始时将其加1。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![apk](https://img-home.csdnimg.cn/images/20210720083646.png)