Package 'QGA'

Title: Quantum Genetic Algorithm
Description: Function that implements the Quantum Genetic Algorithm, first proposed by Han and Kim in 2000. This is an R implementation of the 'python' application developed by Lahoz-Beltra (<https://github.com/ResearchCodesHub/QuantumGeneticAlgorithms>). Each optimization problem is represented as a maximization one, where each solution is a sequence of (qu)bits. Following the quantum paradigm, these qubits are in a superposition state: when measuring them, they collapse in a 0 or 1 state. After measurement, the fitness of the solution is calculated as in usual genetic algorithms. The evolution at each iteration is oriented by the application of two quantum gates to the amplitudes of the qubits: (1) a rotation gate (always); (2) a Pauli-X gate (optionally). The rotation is based on the theta angle values: higher values allow a quicker evolution, and lower values avoid local maxima. The Pauli-X gate is equivalent to the classical mutation operator and determines the swap between alfa and beta amplitudes of a given qubit. The package has been developed in such a way as to permit a complete separation between the engine, and the particular problem subject to combinatorial optimization.
Authors: Giulio Barcaroli [aut, cre]
Maintainer: Giulio Barcaroli <[email protected]>
License: GPL (>= 2)
Version: 1.1
Built: 2026-05-31 08:29:32 UTC
Source: https://github.com/barcaroli/qga

Help Index


Quantum Genetic Algorithm

Description

Runs a Quantum Genetic Algorithm (QGA) to optimize a user-defined fitness function over discrete decision variables. Internally it initializes a quantum population, iterates measurement, rotation, and optional mutation, repairs invalid genes, evaluates fitness in parallel when possible, and tracks best/average fitness across generations.

Usage

QGA(
  popsize = 20,
  generation_max = 200,
  nvalues_sol,
  Genome,
  thetainit = 3.1415926535 * 0.05,
  thetaend = 3.1415926535 * 0.025,
  pop_mutation_rate_init = NULL,
  pop_mutation_rate_end = NULL,
  mutation_rate_init = NULL,
  mutation_rate_end = NULL,
  mutation_flag = TRUE,
  plotting = TRUE,
  verbose = TRUE,
  progress = TRUE,
  eval_fitness,
  eval_func_inputs,
  stop_limit = NULL,
  stop_iters = NULL
)

Arguments

popsize

Integer. Population size.

generation_max

Integer. Maximum number of generations to run.

nvalues_sol

Integer. Number of allowed discrete values per gene (1..nvalues_sol).

Genome

Integer. Number of genes (decision variables).

thetainit

Numeric (radians). Initial rotation step for the update rule.

thetaend

Numeric (radians). Final rotation step (linearly decayed to this value).

pop_mutation_rate_init

Numeric in [0,1]. Initial per-individual mutation probability (default '1/(popsize+1)' when 'mutation_flag').

pop_mutation_rate_end

Numeric in [0,1]. Final per-individual mutation probability (default '1/(popsize+1)' when 'mutation_flag').

mutation_rate_init

Numeric in [0,1]. Initial per-bit mutation probability (default '1/(Genome+1)' when 'mutation_flag').

mutation_rate_end

Numeric in [0,1]. Final per-bit mutation probability (default '1/(Genome+1)' when 'mutation_flag').

mutation_flag

Logical. Whether to apply the mutation operator.

plotting

Logical. If TRUE, plots fitness history over generations.

verbose

Logical. If TRUE, prints per-generation summary to console.

progress

Logical. If TRUE, shows a text progress bar.

eval_fitness

Function. User-provided function with signature 'function(solution_integers, eval_func_inputs)' returning a numeric fitness value.

eval_func_inputs

Any. Second argument passed through to 'eval_fitness'.

stop_limit

Numeric. Early-stop threshold; stops when best fitness >= 'stop_limit'.

stop_iters

Integer or NULL. If set, stops when there is no improvement over 'stop_iters' generations.

Details

Solutions are encoded as bitstrings and decoded to integer values in the range 1..'nvalues_sol' for each of the 'Genome' genes. The user must supply a fitness function 'eval_fitness(solution_integers, eval_func_inputs)' that returns a numeric score to maximize.

Value

A list with two elements: - 'best_solution_integers': Integer vector of length 'Genome' with values in 1..'nvalues_sol'. - 'fitness_history_df': Data frame with columns 'generation', 'fitness_average', 'fitness_best'.

See Also

'rotation()', 'mutation()', 'measure()', 'repair()'

Examples

# Minimal example (toy fitness: sum of gene values)
set.seed(1)
f <- function(sol, data) sum(sol)

# This call is illustrative; tune popsize/generation_max for real problems
# out <- QGA(popsize = 10, generation_max = 5,
#            nvalues_sol = 4, Genome = 3,
#            eval_fitness = f, eval_func_inputs = NULL,
#            plotting = FALSE, verbose = FALSE, progress = FALSE)