Learn R Programming

metaheuristicOpt (version 2.0.0)

GA: Optimization using Genetic Algorithm

Description

This is the internal function that implements Genetic Algorithm. It is used to solve continuous optimization tasks. Users do not need to call it directly, but just use metaOpt.

Usage

GA(FUN, optimType = "MIN", numVar, numPopulation = 40, maxIter = 500,
  rangeVar, Pm = 0.1, Pc = 0.8)

Arguments

FUN

an objective function or cost function,

optimType

a string value that represent the type of optimization. There are two option for this arguments: "MIN" and "MAX". The default value is "MIN", which the function will do minimization. Otherwise, you can use "MAX" for maximization problem. The default value is "MIN".

numVar

a positive integer to determine the number variables.

numPopulation

a positive integer to determine the number populations. The default value is 40.

maxIter

a positive integer to determine the maximum number of iterations. The default value is 500.

rangeVar

a matrix (\(2 \times n\)) containing the range of variables, where \(n\) is the number of variables, and first and second rows are the lower bound (minimum) and upper bound (maximum) values, respectively. If all variable have equal upper bound, you can define rangeVar as matrix (\(2 \times 1\)).

Pm

a positive integer to determine mutation probability. The default value is 0.1.

Pc

a positive integer to determine crossover probability. The default value is 0.8.

Value

Vector [v1, v2, ..., vn] where n is number variable and vn is value of n-th variable.

Details

Genetic algorithms (GA) were invented by John Holland in the 1960 and were developed by Holland and his students and colleagues at the University of Michigan in the 1960 and the 1970. GA are commonly used to generate high-quality solutions to optimization and search problems by relying on bio-inspired operators such as mutation, crossover and selection.

In order to find the optimal solution, the algorithm follow the following steps.

  • Initialization: Initialize the first population randomly, calculate the fitness and save the best fitness as bestPopulation.

  • Selection: Select set of individual parent for doing crossover. Number of parent determined by the crossover probability which defined by user. In this work, we use method called Roulette Whell Selection.

  • Crossover: Doing crossover between two parent from Selection step. This step done by selecting two point randomly and switching the values between them.

  • Mutation : All individu in population have a chance to mutate. When mutation occurs, we generate the random values to replace the old one.

  • Calculate the fitness of each individual and update bestPopulation.

  • Check termination criteria, if termination criterion is satisfied, return the bestPopulation as the optimal solution for given problem. Otherwise, back to Selection steps.

References

Holland, J. H. 1975. Adaptation in Natural and Artificial Systems. University of Michigan Press. (Second edition: MIT Press, 1992.)

Melanie Mitchell. 1998. An Introduction to Genetic Algorithms. MIT Press, Cambridge, MA, USA.

See Also

metaOpt

Examples

Run this code
# NOT RUN {
##################################
## Optimizing the sphere function

# define sphere function as objective function
sphere <- function(x){
    return(sum(x^2))
}

## Define parameter
Pm <- 0.1
Pc <- 0.8
numVar <- 5
rangeVar <- matrix(c(-10,10), nrow=2)

## calculate the optimum solution using Genetic Algorithm
resultGA <- GA(sphere, optimType="MIN", numVar, numPopulation=20,
                 maxIter=100, rangeVar, Pm, Pc)

## calculate the optimum value using sphere function
optimum.value <- sphere(resultGA)

# }

Run the code above in your browser using DataLab