Python遗传算法使用Pygad库

发布于 2025-01-21 10:10:42 字数 433 浏览 0 评论 0原文

我有一个我想最大化其价值的函数。

但是我不太了解如何使用小瓜库。我在某些站点上看到的是,他们始终使用默认函数,例如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

I have a function that I want to maximize its value.

But I don't know much how to use PyGad library. What I see on some sites is that they always use default functions like w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + 6wx6. Where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7).
And there they write the code without write the function just like :

function_inputs = [4,-2,3.5,5,-11,-4.7]  # Function inputs.
desired_output = 44  # Function output.

And add the other parameters

So what if we want to use a polynomial function like w1^3.x1 + w2^2.x2 + w3.x3 + w4.x4 + w5.x5 + w6.x6?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你拒绝所有暧昧 2025-01-28 10:10:42

这很简单。只需在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

It is simple. Just write your function in Python.

(w1**3)*(x1) + (w2**2)*(x2) + (w3)*(x3) + (w4)*(x4) + (w5)*(x5) + (w6)*(x6)

For the complete example, please use the next fitness function instead of the function used in this example.

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

Thanks for using PyGAD!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文