gpt4 book ai didi

python - 使用整数的鸭嘴兽优化

转载 作者:太空宇宙 更新时间:2023-11-03 14:50:54 25 4
gpt4 key购买 nike

我想使用具有 2 个目标、3 个变量且没有约束的整数(不是 float )对 Platypus 执行多目标优化,我需要最大化目标值。我这样定义它:

problem = Problem(3, 2)
problem.directions[:] = Problem.MAXIMIZE
problem.types[:] = [Integer(-50, 50), Integer(-50, 50), Integer(-50, 50)]

algorithm = NSGAII(problem)
algorithm.run(10000)

for solution in algorithm.result:
print solution

但我一直得到这样的结果:

Solution[[False, True, False, True, False, True, True],[False, True, False, True, False, True, False],[True, True, True, False, True, False, True]|-12.2,629.8|0]
Solution[[False, True, False, True, False, True, True],[True, True, False, True, False, True, False],[True, False, True, False, True, True, False]|-28.0,1240.0|0]

你能帮帮我吗?

提前致谢。

最佳答案

试试这个:

from platypus import Problem, Integer, NSGAII

def my_function(x):
""" Some objective function"""
return -x[0] ** 2 - x[2] ** 2 # we expect the result x[0] = 0, x[1] = whatever, and x[2] = 0

problem = Problem(3, 1) # define 3 inputs and 1 objective (and no constraints)
problem.directions[:] = Problem.MAXIMIZE
int1 = Integer(-50, 50)
int2 = Integer(-50, 50)
int3 = Integer(-50, 50)
problem.types[:] = [int1, int2, int3]
problem.function = my_function
algorithm = NSGAII(problem)
algorithm.run(10000)

# grab the variables (note: we are just taking the ones in the location result[0])
first_variable = algorithm.result[0].variables[0]
second_variable = algorithm.result[0].variables[1]
third_variable = algorithm.result[0].variables[2]

print(int1.decode(first_variable))
print(int2.decode(second_variable))
print(int3.decode(third_variable))

基本上,在这种情况下,由于所有输入的范围都相同(int1、int2 和 int3 具有相同的范围),我们也可以完成“int1.decode(second_variable)”等,但我将其保留为通用如果您想将每个整数的范围更改为不同的值,请在此处。

关于python - 使用整数的鸭嘴兽优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45900813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com