import random from deap import creator, base, tools from evaluate_func import fitness def creating(): if "FitnessMin" not in creator.__dict__: creator.create("FitnessMin", base.Fitness, weights=(-1.0,)) if "Individual" not in creator.__dict__: creator.create("Individual", list, fitness=creator.FitnessMin) toolbox = base.Toolbox() # ============================== # 基因注册(修改后) # ============================== toolbox.register("n_max_trial", random.randint, 50, 200) toolbox.register("prf_size", random.uniform, 0.05, 0.95) toolbox.register("prf_conn", random.uniform, 0.05, 0.95) toolbox.register("cap_limit_prob_type", random.randint, 0, 1) toolbox.register("cap_limit_level", random.randint, 10, 50) toolbox.register("diff_new_conn", random.uniform, 0.05, 0.95) toolbox.register("netw_prf_n", random.randint, 3, 15) toolbox.register("s_r", random.uniform, 0.05, 0.4) toolbox.register("S_r", random.uniform, 0.8, 2.5) toolbox.register("x", random.uniform, 0.1, 0.9) toolbox.register("k", random.uniform, 0.2, 4.0) toolbox.register("production_increase_ratio", random.uniform, 0.3, 3.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