import random from deap import creator, base, tools from evaluate_func import fitness def creating(): if "FitnessMax" not in creator.__dict__: creator.create("FitnessMax", base.Fitness, weights=(1.0,)) if "Individual" not in creator.__dict__: creator.create("Individual", list, fitness=creator.FitnessMax) toolbox = base.Toolbox() # 基因注册 toolbox.register("n_max_trial", random.randint, 1, 60) toolbox.register("prf_size", random.uniform, 0.0, 1.0) toolbox.register("prf_conn", random.uniform, 0.0, 1.0) toolbox.register("cap_limit_prob_type", random.randint, 0, 1) toolbox.register("cap_limit_level", random.randint, 5, 80) toolbox.register("diff_new_conn", random.uniform, 0.0, 1.0) toolbox.register("netw_prf_n", random.randint, 1, 20) toolbox.register("s_r", random.uniform, 0.01, 0.5) toolbox.register("S_r", random.uniform, 0.5, 1.0) toolbox.register("x", random.uniform, 0.0, 1) toolbox.register("k", random.uniform, 0.1, 5.0) toolbox.register("production_increase_ratio", random.uniform, 1, 5.0) # 个体与种群注册 toolbox.register( "individual", tools.initCycle, creator.Individual, ( toolbox.n_max_trial, toolbox.prf_size, toolbox.prf_conn, toolbox.cap_limit_prob_type, toolbox.cap_limit_level, toolbox.diff_new_conn, toolbox.netw_prf_n, toolbox.s_r, toolbox.S_r, toolbox.x, toolbox.k, toolbox.production_increase_ratio ), n=1 ) toolbox.register("population", tools.initRepeat, list, toolbox.individual) # 遗传算子 toolbox.register("evaluate", fitness) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutShuffleIndexes, indpb=0.1) toolbox.register("select", tools.selTournament, tournsize=3) return toolbox