# Update the mutation variance so that it varies linearly from g_max_mutation_sigma to # g_min_mutation_sigma g_current_sigma -= (g_max_mutation_sigma - g_min_mutation_sigma) / (g_iterations - 1) # Make sure the mutation variance is up-to-date gaussian_mutation.setMutationVariance(g_current_sigma) # Optimisation and visualisation optimiser = EvolutionaryAlgorithm(test_problem, g_number_of_individuals) # Set the selection operator #optimiser.setSelectionOperator(TournamentSelection(2)); #optimiser.setSelectionOperator(RouletteWheel()); optimiser.setSelectionOperator(RankSelection()) # Create the genetic operators elitism = ElitismOperator(0.1) new_blood = NewBloodOperator(0.1) gaussian_mutation = GaussianMutationOperator(0.1, 0.2) blend_cross_over = BlendCrossoverOperator(0.6, gaussian_mutation) # Add the genetic operators to the EA optimiser.addGeneticOperator(new_blood) optimiser.addGeneticOperator(gaussian_mutation) optimiser.addGeneticOperator(blend_cross_over) optimiser.addGeneticOperator(elitism) test_problem.number_of_evaluation = 0 optimiser.plotAnimation(g_iterations, visualisationCallback)
# Default tournament size tournament_size = 2 # The tournament size is always two for dual if args.selection[0] == "dual": tournament_size = 2 # Update the tournament size if needed elif not isinstance(args.tournament_size, NoneType): if isinstance(args.tournament_size, int): tournament_size = args.tournament_size else: tournament_size = args.tournament_size[0] # Set the selection operator if args.selection[0] == "dual" or args.selection[0] == "tournament": optimiser.setSelectionOperator(TournamentSelection(tournament_size)) elif args.selection[0] == "ranking": optimiser.setSelectionOperator(RankSelection()) elif args.selection[0] == "roulette": optimiser.setSelectionOperator(RouletteWheelSelection()) else: raise ValueError( 'Invalid selection operator "%s". Choose "threshold", "tournament" or "dual".' % (args.selection[0])) # Create the genetic operators gaussian_mutation = GaussianMutationOperator( 0.3, args.initial_mutation_variance[0]) blend_cross_over = BlendCrossoverOperator(0.6, gaussian_mutation) # Add the genetic operators to the EA
#optimiser.setSelectionOperator(RouletteWheelSelection()); #optimiser.setSelectionOperator(RankSelection()); # Set the selection operator selection_operator = None if args.selection[0] == "dual" or args.selection[0] == "tournament": selection_operator = tournament_selection elif args.selection[0] == "threshold": selection_operator = ThresholdSelection(0, tournament_selection, 10) else: raise ValueError( 'Invalid selection operator "%s". Choose "threshold", "tournament" or "dual".' % (args.selection[0])) optimiser.setSelectionOperator(selection_operator) # Create the genetic operators new_blood = NewBloodOperator(args.initial_new_blood_probability[0]) gaussian_mutation = GaussianMutationOperator( 1.0 - args.initial_new_blood_probability[0], args.initial_mutation_variance[0]) # Add the genetic operators to the EA optimiser.addGeneticOperator(new_blood) optimiser.addGeneticOperator(gaussian_mutation) # Show the visualisation if args.visualisation: fig, ax = plt.subplots(7, 2) global_fitness_function.plot(fig, ax, 0, number_of_generation)
optimiser = EvolutionaryAlgorithm(local_fitness, g_number_of_individuals, global_fitness) optimiser.full_name = "Fly algorithm" optimiser.short_name = "FA" g_max_mutation_sigma = 0.1 g_min_mutation_sigma = 0.01 g_current_sigma = g_max_mutation_sigma # Set the selection operator tournament_selection = TournamentSelection(2) threshold_selection = ThresholdSelection(0.0, tournament_selection, round(0.25 * g_number_of_individuals)) optimiser.setSelectionOperator(threshold_selection) #optimiser.setSelectionOperator(tournament_selection); #optimiser.setSelectionOperator(RouletteWheel()); #optimiser.setSelectionOperator(RankSelection()); # Create the genetic operators new_blood = NewBloodOperator(0.5) gaussian_mutation = GaussianMutationOperator(0.5, 0.2) # Add the genetic operators to the EA optimiser.addGeneticOperator(new_blood) optimiser.addGeneticOperator(gaussian_mutation) g_iterations = round(max_iterations / g_number_of_individuals) for i in range(g_iterations):