toolbox = base.Toolbox() toolbox.register("evalfit", FitnessFunction) toolbox.register("evalfitsh", FitSharing, fitFunction=toolbox.evalfit, distanceFunction=NormHamming2, sigma=0.2) toolbox.register("attr_bool", random.randint, 0, 1) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=IND_SIZE) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) toolbox.register("select", tools.selStochasticUniversalSampling, fit_attr='fitshare') # toolbox.register("select", tools.selTournament, tournsize=3, fit_attr='fitshare') population = toolbox.population(n=NUM_IND) AllBinSeq = GenBinSeq(IND_SIZE) AllPossibleFits = [FitnessFunction(i)[0] for i in AllBinSeq] NumSequences=[NormBinSeqToNum(i) for i in AllBinSeq] fig, ax = plt.subplots() ax.plot(NumSequences, AllPossibleFits, '-r') scatt = ax.scatter([NormBinSeqToNum(i) for i in population],[FitnessFunction(i)[0] for i in population]) def animate(frame, population=population, toolbox=toolbox): offspring = algorithms.varAnd(population, toolbox, cxpb=0.1, mutpb=0) for ind in offspring: ind.fitshare.values = toolbox.evalfitsh(ind, offspring) population = toolbox.select(offspring, k=len(population)) xdata=np.asarray([NormBinSeqToNum(i) for i in population]) ydata=np.asarray([FitnessFunction(i)[0] for i in population]) scatt.set_array(np.c_(xdata,ydata))
plt.subplot(142) plt.plot([i for i in range(NUM_GEN)],mpr_val) plt.title('generation VS MaxPeakRatio') plt.xlabel('generation') plt.ylabel('MaxPeakRatio') plt.grid(True) plt.subplot(143) num_gen=[i for i in range(NUM_GEN)] plt.plot(num_gen, MaxFitness, 'r', label='MaxFitness') plt.plot(num_gen, AvgFitness, 'g', label='AvgFitness') plt.plot(num_gen, MinFitness, 'b', label='MinFitness') plt.title('generation VS fitness') plt.xlabel('generation') plt.ylabel('Fitness') plt.grid(True) plt.legend() # niches graphical rapresentation plt.subplot(144) AllBinSeq = GenBinSeq(10) AllPossibleFits = [FitnessFunction(i)[0] for i in AllBinSeq] PlotBinSeq(AllBinSeq, AllPossibleFits, 'r') ScatBinFct(population, FitnessFunction) plt.title('fitness VS individuals') plt.xlabel('Individuals') plt.ylabel('Fitness') plt.grid(True) plt.show()
#!/usr/bin/python3 import matplotlib.pyplot as plt from FitFunctions import FnctA, FnctB from utilities import GenBinSeq, PlotBinFct, NormBinSeqToNum # individual size: IND_SIZE = 8 PlotBinFct(GenBinSeq(IND_SIZE), FnctA) PlotBinFct(GenBinSeq(IND_SIZE), FnctB, '--r') plt.show()