【发布时间】:2022-01-08 08:58:57
【问题描述】:
我想用 PyGAD 解决工作分配问题,希望得到的结果不仅是一条染色体,而且是整个人群。如[[1,1,0,0][0,0,1,1]]表示4个slots,第一个染色体中的1表示一个人分配给这些slots,另外两个slots根据第二个染色体分配给另一个人.
按照PyGAD网站上的教程(https://pygad.readthedocs.io),我是否正确使用initial_population来生成人口?使用cal_pop_fitness()函数是否正确?我尝试了一些,但仍然不知道。
import pygad
import numpy
sol_per_pop = 2
num_genes = 4
pop_size = (sol_per_pop,num_genes)
new_population = numpy.random.randint(low=0,high=2,size=pop_size)
num_generations = 100
num_parents_mating = 2
def fitness_func(solution,solution_idx):
fitness = ga_instance.cal_pop_fitness(new_population)
#do something
ga_instance = pygad.GA(initial_population = new_population,
num_generations=num_generations,
num_parents_mating=num_parents_mating,
sol_per_pop=sol_per_pop,
num_genes=num_genes,
mutation_type=None,
fitness_func=fitness_func)
ga_instance.run()
【问题讨论】: