遗传算法与矮人

发布于 2025-02-07 23:31:20 字数 268 浏览 3 评论 0原文

我正在尝试使用Pygad制作一种遗传算法,以找到最大化此功能的最佳值:z =(3*(x^2 -y)^2 +(100 -x)^2) +(y - x),x和y [-4,4]之间。

有人可以给我发送与此功能类似的示例吗?我发现的示例使用了一个已经给出的输入的函数:y = f(w1:w6)= w1x1 + w2x2 + w3x3 + w4x4 + w4x4 + w5x5 + 6wx6

我不知道我怎么能使用两个变量的函数获取每个人的健身价值

I'm trying to make a genetic algorithm with PyGAD to find the best value that maximize this function: Z = (3*(x^2 - y)^2 + (100 - x)^2) + (y- x), x and y between [-4, 4].

Could someone send me an example of something similar to this function? The example I found uses a function with inputs that have already been given: y = f(w1:w6) = w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + 6wx6

I don't know how i can get the fitness value to each individual using the function with two variables

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

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

发布评论

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

评论(1

墨落成白 2025-02-14 23:31:20

这是一个解决您的问题的小枪代码。

import pygad

def fitness_func(solution, solution_idx):
    x = solution[0]
    y = solution[1]
    Z = -(100*(x**2 - y)**2 + (1 - x)**2)
    return Z

last_fitness = 0
def on_generation(ga_instance):
    global last_fitness
    print("Generation = {generation}".format(generation=ga_instance.generations_completed))
    print("Fitness    = {fitness}".format(fitness=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]))
    print("Change     = {change}".format(change=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1] - last_fitness))
    last_fitness = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]

ga_instance = pygad.GA(num_generations=1000,
                       num_parents_mating=5,
                       sol_per_pop=10,
                       num_genes=2,
                       gene_space={"low": -2, "high": 2},
                       mutation_by_replacement=True,
                       fitness_func=fitness_func,
                       on_generation=on_generation)

ga_instance.run()

ga_instance.plot_fitness()

solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness)
print("Solution", solution)
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))

基于您的描述,以下是某些参数的值:

  • num_genes = 2:将其设置为2,因为您只有2个基因。
  • sol_per_pop = 10:设置为10,因为您说您有10个人。
  • gene_space = {“ low”:-2,“高”:2}:它限制了基因不会超出-2:2范围。
  • mutation_by_replacement = true:这确保应用突变后不会超出范围。

您可以根据自己的喜好更改使用的其他参数。例如,您可能通过增加世代数来获得更好的健身(num_generations)。

请确保安装了Pygad并运行&nbsp'脚本。

This is a PyGAD code that solves your problem.

import pygad

def fitness_func(solution, solution_idx):
    x = solution[0]
    y = solution[1]
    Z = -(100*(x**2 - y)**2 + (1 - x)**2)
    return Z

last_fitness = 0
def on_generation(ga_instance):
    global last_fitness
    print("Generation = {generation}".format(generation=ga_instance.generations_completed))
    print("Fitness    = {fitness}".format(fitness=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]))
    print("Change     = {change}".format(change=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1] - last_fitness))
    last_fitness = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]

ga_instance = pygad.GA(num_generations=1000,
                       num_parents_mating=5,
                       sol_per_pop=10,
                       num_genes=2,
                       gene_space={"low": -2, "high": 2},
                       mutation_by_replacement=True,
                       fitness_func=fitness_func,
                       on_generation=on_generation)

ga_instance.run()

ga_instance.plot_fitness()

solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness)
print("Solution", solution)
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))

Based on your description, here are the values of some parameters:

  • num_genes=2: It is set to 2 because you only have 2 genes.
  • sol_per_pop=10: Set to 10 because you said you have 10 individuals.
  • gene_space={"low": -2, "high": 2}: It restricts the genes to not go beyond the -2:2 range.
  • mutation_by_replacement=True: This makes sure the genes do not go beyond the range after mutation is applied.

You can change the other parameters used according to your preference. For example, you may get better fitness by increasing the number of generations (num_generations).

Please make sure that PyGAD is installed and run the script.

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