我正在使用python中的基因AI包来测试遗传算法(base.py)。
我想要我自己的健身功能所以我写了
def my_fitness(chromosome):
fitness = mean_absolute_percentage_error(chromosome, [0.5 0.5 0.5 0.5])
return fitness然后按照文档编写了以下代码:
from geneal.genetic_algorithms import ContinuousGenAlgSolver
from geneal.applications.fitness_functions.continuous import fitness_functions_continuous
solver = ContinuousGenAlgSolver(
n_genes=4,
fitness_function=my_fitness(chromosome),
pop_size=10,
max_gen=200,
mutation_rate=0.1,
selection_rate=0.6,
selection_strategy="roulette_wheel",
problem_type=float, # Defines the possible values as float numbers
variables_limits=(-10, 10) # Defines the limits of all variables between -10 and 10.
# Alternatively one can pass an array of tuples defining the limits
# for each variable: [(-10, 10), (0, 5), (0, 5), (-20, 20)]
)
solver.solve()我还不清楚如何使用我自己的健身功能。获得染色体未定义的错误(显然!)如何在这个包中使用自己的健身功能。请出示。
发布于 2021-10-03 09:41:57
健身功能有两个要求:
这个函数的内部工作方式由您来决定。然后,在初始化期间将其传递给对象,例如:
from geneal.genetic_algorithms import ContinuousGenAlgSolver
from geneal.applications.fitness_functions.continuous import fitness_functions_continuous
solver = ContinuousGenAlgSolver(
n_genes=4,
fitness_function=my_fitness,
pop_size=10,
max_gen=200,
mutation_rate=0.1,
selection_rate=0.6,
selection_strategy="roulette_wheel",
problem_type=float, # Defines the possible values as float numbers
variables_limits=(-10, 10) # Defines the limits of all variables between -10 and 10.
# Alternatively one can pass an array of tuples defining the limits
# for each variable: [(-10, 10), (0, 5), (0, 5), (-20, 20)]
)我建议您查看包中提供的示例,以便更好地了解如何定义自定义适应度函数:示例
https://stackoverflow.com/questions/69381860
复制相似问题