我有一个函数,我想最大限度地利用它的价值。
但我不太知道如何使用PyGad库。我在一些网站上看到的是,它们总是使用默认函数,比如w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + 6wx6
。在哪里(x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7)
。在那里,他们编写代码而不编写函数,就像:
function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs.
desired_output = 44 # Function output.
并添加其他参数
那么如果我们想使用像w1^3.x1 + w2^2.x2 + w3.x3 + w4.x4 + w5.x5 + w6.x6
这样的多项式函数呢?
发布于 2022-04-16 15:27:57
这很简单。只需用Python编写函数即可。
(w1**3)*(x1) + (w2**2)*(x2) + (w3)*(x3) + (w4)*(x4) + (w5)*(x5) + (w6)*(x6)
对于完整的示例,请使用下一个适应度函数,而不是这个示例中使用的函数。
def fitness_func(solution, solution_idx):
w1, w2, w3, w4, w5, w6 = solution
x1, x2, x3, x4, x5, x6 = function_inputs
output = (w1**3)*(x1) + (w2**2)*(x2) + (w3)*(x3) + (w4)*(x4) + (w5)*(x5) + (w6)*(x6)
fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
return fitness
谢谢你使用PyGAD!
https://stackoverflow.com/questions/71881944
复制相似问题