PYGAD如何调试内置父选择方法

问题描述 投票:0回答:1

我正在使用 PYGAD GA 模块编写遗传编程,我想使用锦标赛选择来进行父匹配,我不知道如何使用 print() 来调试父选择方法,这是我的代码:

import numpy as np
import pygad


def fitness_func(ga_instance, solution, solution_idx):
    return sum(solution)

population = np.array([[9, 9, 9], [6, 6, 6], [7, 7, 7], [8, 8, 8],[1,1,1],[2,2,2],[3,3,3],[0,0,0],[4,4,4],[5,5,5]])


ga_instance = pygad.GA(initial_population=population,
                       parent_selection_type="tournament",
                       K_tournament = 3)

# Calculate the fitness of the initial population
fitness = ga_instance.cal_pop_fitness()

我想看看下一代选择的父母,如何打印出来以便调试内置的父母选择方法,非常感谢

python machine-learning artificial-intelligence genetic-programming pygad
1个回答
0
投票

有2种方法:

  1. 使用
    on_parents()
    参数。它被分配了一个接收所选父级的回调函数/方法。

def on_parents(ga_instance, selected_parents):
    print("on_parents()")

ga_instance = pygad.GA(...,
                       on_parents=on_parents,
                       ...)
  1. 使用
    last_generation_parents
    实例属性。您可以使用
    pygad.GA
    类的实例从任何地方获取此属性的值。
© www.soinside.com 2019 - 2024. All rights reserved.