object
genetic_algorithm(Problem,RandomAlgorithm)
Problem- Problem object implementinggenetic_algorithm_problem_protocol.RandomAlgorithm- Random number generator algorithm for thefast_randomlibrary (e.g.xoshiro128pp,xoshiro256ss,well512a, …).
Genetic algorithm meta-heuristic optimizer. Parameterized by a problem object implementing the genetic_algorithm_problem_protocol protocol and by a random number generator algorithm for the fast_random library. The algorithm minimizes the energy (cost) function defined by the problem by default; maximization is supported via options. Custom stop conditions, diversity measures, progress reporting, and selection pressure can be defined by the problem object or configured via options; suitable defaults are used otherwise.
logtalk_load(genetic_algorithm(loader))static, context_switching_calls
Algorithm: A generational genetic algorithm that maintains a population of individuals. Each generation applies selection, crossover, and mutation to produce the next population. Optional elitism preserves the best individuals across generations.
Selection: Parent selection is controlled by the
selection/1option. Supported schemes aretournament(K)(defaulttournament(3)),roulette, andrank. Tournament samples K individuals and keeps the best; roulette selects proportionally to fitness derived from energy; rank selects proportionally to rank after sorting by objective.Crossover and mutation: Both operators are defined by the problem object. Crossover is applied with probability
crossover_rate; mutation is applied independently to each offspring with probabilitymutation_rate. Rates may be held constant or adapted each generation viacrossover_schedule/1andmutation_schedule/1options, or via optional problem hookscrossover_rate/4andmutation_rate/4(hooks take precedence over schedules).Elitism: When
elite_size(N)is greater than zero, the best N individuals of the current population (clamped to the population size) are copied unchanged into the next generation. This preserves the best solutions found so far against disruption by crossover and mutation. Setelite_size(0)to disable elitism.Best individual tracking: The algorithm tracks the best individual found across all generations, not just the final population.
Seed control: The
seed(S)option initializes the random number generator for reproducible runs.Progress reporting: If the problem object defines
progress/5, it is called periodically with the current generation, best individual, best energy, mean population energy, and diversity. A final report is always produced when the loop terminates when updates are enabled.
Public predicates
run/2
Runs the genetic algorithm using default options and returns the best individual found and its energy.
staticrun(BestIndividual,BestEnergy)run(-nonvar,-number) - onerun/3
Runs the genetic algorithm using the given options and returns the best individual found and its energy.
staticrun(BestIndividual,BestEnergy,Options)run(-nonvar,-number,+list(compound)) - one
max_generations(N)option: Maximum number of generations (default:200).
population_size(N)option: Number of individuals in the population; must be at least 2 (default:50).
crossover_rate(P)option: Initial probability of applying crossover to a selected pair of parents (default:0.8). Used as the constant rate when the schedule isconstant, and as the starting value for adaptive schedules.
mutation_rate(P)option: Initial probability of mutating each offspring (default:0.1). Used as the constant rate when the schedule isconstant, and as the starting value for adaptive schedules.
crossover_schedule(Schedule)option: How the crossover rate evolves across generations:constant(default),linear(Initial, Final)interpolates fromInitialtoFinal, orgeometric(Factor)multiplies the rate byFactoreach generation (clamped to[0.0, 1.0]). Overridden when the problem definescrossover_rate/4.
mutation_schedule(Schedule)option: How the mutation rate evolves across generations:constant(default),linear(Initial, Final)interpolates fromInitialtoFinal, orgeometric(Factor)multiplies the rate byFactoreach generation (clamped to[0.0, 1.0]). Overridden when the problem definesmutation_rate/4.
selection(Scheme)option: Parent selection scheme:tournament(K)with positive integer K (default:tournament(3)),roulette, orrank.
elite_size(N)option: Number of best individuals preserved unchanged into the next generation (default:1). Set to0to disable elitism.
objective(Direction)option: Optimization direction:minimize(default) ormaximize.
updates(N)option: Number of progress reports during the run. Set to0to disable. Progress is reported by callingprogress/5on the problem object (default:0).
seed(S)option: Positive integer seed for the random number generator, enabling reproducible runs (default: none).
run/4
Runs the genetic algorithm using the given options, returns the best individual found and its energy, and returns run statistics.
staticrun(BestIndividual,BestEnergy,Statistics,Options)run(-nonvar,-number,-list(compound),+list(compound)) - one
Statistics list: A list of
Key(Value)pairs:generations(N)is the number of generations executed,evaluations(E)is the total number of fitness evaluations,improvements(I)is the number of generations that improved the best energy, andfinal_population_size(S)is the size of the final population.
Protected predicates
(no local declarations; see entity ancestors if any)
Private predicates
(no local declarations; see entity ancestors if any)
Operators
(none)