GAReal: Genetic Algorithm setup

Description Usage Arguments Details Value References Examples

View source: R/GAOptim_real.R

Description

Setup a GAReal object that can be used to perform a real-based optimization.

Usage

1
2
3
4
5
  GAReal(FUN, lb, ub, popSize = 100, mutRate = 0.01,
    cxRate = 0.9, eliteRate = 0.4,
    selection = c("fitness", "uniform"),
    crossover = c("blend", "two.points"),
    mutation = c("noise"))

Arguments

FUN

The fitness function, which should take a vector as argument and return a numeric value (See details).

lb

A numeric vector specifying the lower bounds for the search domain.

ub

A numeric vector specifying the upper bounds for the search domain.

popSize

The population size.

mutRate

The mutation rate, a numeric value between 0 and 1. When implementing a custom mutation function, this value should be one of the parameters (see details and examples).

cxRate

The crossover rate, a numeric value between 0 and 1. This parameter specifies the probability of two individuals effectively exchange DNA during crossover. In case the individuals didn't crossover, the offspring is a exact copy of the parents. When implementing a custom crossover function, this value should be one of the arguments (see details and examples).

eliteRate

A numeric value between 0 and 1. The eliteRate * popSize best-fitted individuals will automatically be selected for the next generation.

selection

The selection operator to be used. You can also implement a custom selection function (see details and examples).

crossover

The crossover operator to be used. You can also implement a custom crossover function (see details and examples).

mutation

The mutation operator to be used. You can also implement a custom mutation function (see details and examples).

Details

This is the function used to configure and fine-tune a real-based optimization. The basic usage requires only the FUN parameter (function to be maximized), together with the lb and ub parameters (lower and upper search domain), all the other parameters have sensible defaults.

The parameters selection, crossover and mutation can also take a custom function as argument, which needs to be in the appropriate format (see the examples). The text below explains the default behaviour for these parameters, which will be usefull if you want to override one or more genetic operators.

Value

An object of class GAReal, which you can pass as an argument to plot or summary. This object is a list with the following accessor functions:

bestFit: Returns a vector with the best fitness achieved in each generation.
meanFit: Returns a vector with the mean fitness achieved in each generation.
bestIndividual: Returns a vector with the best solution found.
evolve(h): This is the function you call to evolve your population.
You also need to specify the number of generations to evolve.
population: Returns the current population matrix.

References

Randy L. Haupt, Sue Ellen Haupt (2004). Practical genetic algorithms - 2nd ed.

Michalewicz, Zbigniew. Genetic Algorithms + Data Structures = Evolution Programs - 3rd ed.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Maximize a trivial 5 variable function
# The function and search-space below will be used for all examples

fitness.FUN = function(x) sum(x)
lb = c(0, 0, 0, 0, 0)
ub = c(10, 10, 10, 10, 10)

ga1 = GAReal(fitness.FUN, lb, ub)
ga1$evolve(200)
plot(ga1)

# A custom selection example
selec.FUN = function(population, fitnessVec, nleft)
{
 # population - The population matrix
 # fitnessVec - The corresponding fitness vector for the population matrix
 # nleft - The number of individuals you should select

 half = as.integer(nleft/2)
 remain = nleft - half
 idxs = 1:nrow(population)

 # pick half using fitness-proportionate
 rowIdxs = sample(idxs, half, replace = TRUE, prob = fitnessVec)
 # pick the other half randomly
 rowIdxs = c(rowIdxs, sample(idxs, remain, replace = TRUE))

 # Just return the nLeft selected row indexes
 return(rowIdxs)
}

ga2 = GAReal(fitness.FUN, lb, ub, selection = selec.FUN)
ga2$evolve(200)
summary(ga2)

# A custom crossover example
crossover.FUN = function(parent1, parent2, prob)
{
 # parent1, parent2 - The individuals to crossover
 # prob - The probability of a crossover happen (cxRate parameter)

 # Respect the cxRate parameter: if DNA is not exchanged, just return the parents
 if (runif(1) > prob)
   return(matrix(c(parent1, parent2), nrow = 2, byrow = TRUE))

 # A simple uniform crossover - just swap the 'genes' with a probability of 0.5
 for (i in 1:length(parent1))
 {
   if (runif(1) > 0.5)
   {
     tempval = parent1[i]
     parent1[i] = parent2[i]
     parent2[i] = tempval
   }
 }
 # You should return a matrix in this format
 return(matrix(c(parent1, parent2), nrow = 2, byrow = TRUE))
}

ga3 = GAReal(fitness.FUN, lb, ub, crossover = crossover.FUN)
ga3$evolve(200)
plot(ga3)

# A custom mutation example
mutation.FUN = function(population, nMut)
{
 # population - The population matrix to apply mutation
 # nMut - The number of mutations you supposed to apply, according to mutRate

 rows = sample(1:nrow(population), nMut, replace = TRUE)
 cols = sample(1:ncol(population), nMut, replace = TRUE)
 noise = (runif(nMut))^2

 # extract the matrix indexes
 ext = matrix(c(rows, cols), nMut, 2)
 population[ext] = noise
 return(population)
}

ga4 = GAReal(fitness.FUN, lb, ub, mutation = mutation.FUN)
ga4$evolve(200)
summary(ga4)

gaoptim documentation built on May 30, 2017, 12:37 a.m.

Search within the gaoptim package
Search all R packages, documentation and source code