那求函数最大值如何更改呢
时间: 2024-03-24 20:41:54 浏览: 24
如果要求函数的最大值,只需要将适应度函数中的 return f(x1, x2) 改为 return -f(x1, x2) 即可。这样,在选择个体时,最大的适应度将变为最小的适应度,从而达到了求函数最大值的效果。修改后的代码如下:
```python
import random
# 定义函数
def f(x1, x2):
return x1**2 + x2**2
# 定义约束条件函数
def constraints(x1, x2):
return x1 + x2 - 2 >= 0
# 定义遗传算法参数
POP_SIZE = 50
GENERATIONS = 100
MUTATION_RATE = 0.1
# 定义变量范围
x1_range = (-10, 10)
x2_range = (-5, 5)
# 初始化种群
def init_population():
population = []
for i in range(POP_SIZE):
x1 = random.uniform(*x1_range)
x2 = random.uniform(*x2_range)
population.append((x1, x2))
return population
# 计算适应度
def fitness(individual):
x1, x2 = individual
if constraints(x1, x2):
return -f(x1, x2) # 修改为求最大值
else:
return float('-inf')
# 选择
def selection(population):
return random.sample(population, 2)
# 交叉
def crossover(individual1, individual2):
x1 = (individual1[0] + individual2[0]) / 2
x2 = (individual1[1] + individual2[1]) / 2
return (x1, x2)
# 变异
def mutation(individual):
x1 = individual[0]
x2 = individual[1]
if random.random() < MUTATION_RATE:
x1 = random.uniform(*x1_range)
if random.random() < MUTATION_RATE:
x2 = random.uniform(*x2_range)
return (x1, x2)
# 主函数
def main():
# 初始化种群
population = init_population()
# 迭代
for i in range(GENERATIONS):
# 选择
parents = selection(population)
# 交叉
offspring = crossover(parents[0], parents[1])
# 变异
offspring = mutation(offspring)
# 计算适应度
offspring_fitness = fitness(offspring)
# 选择替换
population.remove(min(population, key=fitness)) # 选择适应度最小的个体进行替换
if constraints(offspring[0], offspring[1]):
population.append(offspring)
# 输出每一代的最优解
best_individual = max(population, key=fitness) # 修改为选择适应度最大的个体
print(f'Generation {i}: x1={best_individual[0]}, x2={best_individual[1]}, f={-fitness(best_individual)}') # 修改为输出适应度的相反数
# 输出最终最优解
best_individual = max(population, key=fitness) # 修改为选择适应度最大的个体
print(f'Best solution: x1={best_individual[0]}, x2={best_individual[1]}, f={-fitness(best_individual)}') # 修改为输出适应度的相反数
if __name__ == '__main__':
main()
```
在这个示例中,我们只需要修改适应度函数中的返回值以及选择个体时使用的函数即可。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)