Title: | Machine Coded Genetic Algorithms for Real-Valued Optimization Problems |
---|---|
Description: | Machine coded genetic algorithm (MCGA) is a fast tool for real-valued optimization problems. It uses the byte representation of variables rather than real-values. It performs the classical crossover operations (uniform) on these byte representations. Mutation operator is also similar to classical mutation operator, which is to say, it changes a randomly selected byte value of a chromosome by +1 or -1 with probability 1/2. In MCGAs there is no need for encoding-decoding process and the classical operators are directly applicable on real-values. It is fast and can handle a wide range of a search space with high precision. Using a 256-unary alphabet is the main disadvantage of this algorithm but a moderate size population is convenient for many problems. Package also includes multi_mcga function for multi objective optimization problems. This function sorts the chromosomes using their ranks calculated from the non-dominated sorting algorithm. |
Authors: | Mehmet Hakan Satman |
Maintainer: | Mehmet Hakan Satman <[email protected]> |
License: | GPL (>= 2) |
Version: | 3.0.7 |
Built: | 2024-11-12 04:10:14 UTC |
Source: | https://github.com/jbytecode/mcga |
Machine coded genetic algorithm (MCGA) is a fast tool for real-valued optimization problems. It uses the byte representation of variables rather than real-values. It performs the classical crossover operations (uniform) on these byte representations. Mutation operator is also similar to classical mutation operator, which is to say, it changes a randomly selected byte value of a chromosome by +1 or -1 with probability 1/2. In MCGAs there is no need for encoding-decoding process and the classical operators are directly applicable on real-values. It is fast and can handle a wide range of a search space with high precision. Using a 256-unary alphabet is the main disadvantage of this algorithm but a moderate size population is convenient for many problems.
Mehmet Hakan Satman
Maintainer: Mehmet Hakan Satman <[email protected]>
## Not run: # A sample optimization problem # Min f(xi) = (x1-7)^2 + (x2-77)^2 + (x3-777)^2 + (x4-7777)^2 + (x5-77777)^2 # The range of xi is unknown. The solution is # x1 = 7 # x2 = 77 # x3 = 777 # x4 = 7777 # x5 = 77777 # Min f(xi) = 0 require("mcga") f<-function(x){ return ((x[1]-7)^2 + (x[2]-77)^2 +(x[3]-777)^2 +(x[4]-7777)^2 +(x[5]-77777)^2) } m <- mcga( popsize=200, chsize=5, minval=0.0, maxval=999999999.9, maxiter=2500, crossprob=1.0, mutateprob=0.01, evalFunc=f) cat("Best chromosome:\n") print(m$population[1,]) cat("Cost: ",m$costs[1],"\n") ## End(Not run)
## Not run: # A sample optimization problem # Min f(xi) = (x1-7)^2 + (x2-77)^2 + (x3-777)^2 + (x4-7777)^2 + (x5-77777)^2 # The range of xi is unknown. The solution is # x1 = 7 # x2 = 77 # x3 = 777 # x4 = 7777 # x5 = 77777 # Min f(xi) = 0 require("mcga") f<-function(x){ return ((x[1]-7)^2 + (x[2]-77)^2 +(x[3]-777)^2 +(x[4]-7777)^2 +(x[5]-77777)^2) } m <- mcga( popsize=200, chsize=5, minval=0.0, maxval=999999999.9, maxiter=2500, crossprob=1.0, mutateprob=0.01, evalFunc=f) cat("Best chromosome:\n") print(m$population[1,]) cat("Cost: ",m$costs[1],"\n") ## End(Not run)
This function is not called directly but is given as a parameter in GA::ga
function.
In GA::ga
, if the parameter crossover=
is set to arithmetic_crossover
than
the arithmetic crossover operator is applied in the genetic search. arithmetic_crossover
generates offspring using the weighted mean of parents' genes. Weights are drawn randomly.
arithmetic_crossover(object, parents, ...)
arithmetic_crossover(object, parents, ...)
object |
A |
parents |
Indices of the selected parents |
... |
Additional arguments to be passed to the function |
List of two generated offspring
Mehmet Hakan Satman - [email protected]
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- ga(type="real-valued", fitness = f, popSize = 100, maxiter = 100, min = rep(-50,5), max = rep(50,5), crossover = arithmetic_crossover) print(myga@solution)
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- ga(type="real-valued", fitness = f, popSize = 100, maxiter = 100, min = rep(-50,5), max = rep(50,5), crossover = arithmetic_crossover) print(myga@solution)
This function is not called directly but is given as a parameter in GA::ga
function.
In GA::ga
, if the parameter crossover=
is set to blx_crossover
than
the blx crossover operator is applied in the genetic search.
blx_crossover(object, parents, ...)
blx_crossover(object, parents, ...)
object |
A |
parents |
Indices of the selected parents |
... |
Additional arguments to be passed to the function |
List of two generated offspring
Mehmet Hakan Satman - [email protected]
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- ga(type="real-valued", fitness = f, popSize = 100, maxiter = 100, min = rep(-50,5), max = rep(50,5), crossover = blx_crossover) print(myga@solution)
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- ga(type="real-valued", fitness = f, popSize = 100, maxiter = 100, min = rep(-50,5), max = rep(50,5), crossover = blx_crossover) print(myga@solution)
This function is not called directly but is given as a parameter in GA::ga
function.
In GA::ga
, if the parameter crossover=
is set to byte_crossover
than
the byte-coded crossover operator is applied in the genetic search. In mcga2
function, the hard-coded
crossover parameter is set to byte_crossover by definition. byte_crossover
function simply takes two double vectors
(parents) and combines the bytes of doubles using a Uniform distribution with parameters 0 and 1.
byte_crossover(object, parents, ...)
byte_crossover(object, parents, ...)
object |
A |
parents |
Indices of the selected parents |
... |
Additional arguments to be passed to the function |
List of two generated offspring
Mehmet Hakan Satman - [email protected]
M.H.Satman (2013), Machine Coded Genetic Algorithms for Real Parameter Optimization Problems, Gazi University Journal of Science, Vol 26, No 1, pp. 85-95
mcga2
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- GA::ga(type="real-valued", fitness = f, popSize = 100, maxiter = 200, lower = rep(-50,5), upper = rep(50,5), crossover = byte_crossover, mutation = byte_mutation) print(myga@solution)
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- GA::ga(type="real-valued", fitness = f, popSize = 100, maxiter = 200, lower = rep(-50,5), upper = rep(50,5), crossover = byte_crossover, mutation = byte_mutation) print(myga@solution)
This function is not called directly but is given as a parameter in GA::ga
function.
In GA::ga
, if the parameter crossover=
is set to byte_crossover_1p
than
the byte-coded one-point crossover operator is applied in the genetic search. In mcga2
function, the hard-coded
crossover parameter is set to byte_crossover by definition. byte_crossover_1p
function simply takes two double vectors
(parents) and combines the bytes of doubles using given cut-point.
byte_crossover_1p(object, parents, ...)
byte_crossover_1p(object, parents, ...)
object |
A |
parents |
Indices of the selected parents |
... |
Additional arguments to be passed to the function |
List of two generated offspring
Mehmet Hakan Satman - [email protected]
M.H.Satman (2013), Machine Coded Genetic Algorithms for Real Parameter Optimization Problems, Gazi University Journal of Science, Vol 26, No 1, pp. 85-95
mcga2
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- GA::ga(type="real-valued", fitness = f, popSize = 100, maxiter = 200, min = rep(-50,5), max = rep(50,5), crossover = byte_crossover_1p, mutation = byte_mutation) print(myga@solution)
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- GA::ga(type="real-valued", fitness = f, popSize = 100, maxiter = 200, min = rep(-50,5), max = rep(50,5), crossover = byte_crossover_1p, mutation = byte_mutation) print(myga@solution)
This function is not called directly but is given as a parameter in GA::ga
function.
In GA::ga
, if the parameter crossover=
is set to byte_crossover_2p
than
the byte-coded two-point crossover operator is applied in the genetic search. In mcga2
function, the hard-coded
crossover parameter is set to byte_crossover by definition. byte_crossover_2p
function simply takes two double vectors
(parents) and combines the bytes of doubles using given cutpoint1 and cutpoint2.
byte_crossover_2p(object, parents, ...)
byte_crossover_2p(object, parents, ...)
object |
A |
parents |
Indices of the selected parents |
... |
Additional arguments to be passed to the function |
List of two generated offspring
Mehmet Hakan Satman - [email protected]
M.H.Satman (2013), Machine Coded Genetic Algorithms for Real Parameter Optimization Problems, Gazi University Journal of Science, Vol 26, No 1, pp. 85-95
mcga2
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- GA::ga(type="real-valued", fitness = f, popSize = 100, maxiter = 200, min = rep(-50,5), max = rep(50,5), crossover = byte_crossover_2p, mutation = byte_mutation) print(myga@solution)
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- GA::ga(type="real-valued", fitness = f, popSize = 100, maxiter = 200, min = rep(-50,5), max = rep(50,5), crossover = byte_crossover_2p, mutation = byte_mutation) print(myga@solution)
This function is not called directly but is given as a parameter in GA::ga
function.
In GA::ga
, if the parameter mutation=
is set to byte_mutation
than
the byte-coded mutation operator is applied in the genetic search. In mcga2
function, the hard-coded
mutation parameter is set to byte_mutation by definition. Byte-mutation function simply takes an double vector and
changes bytes of this values by +1 or -1 using the pre-determined mutation probabilty.
byte_mutation(object, parent, ...)
byte_mutation(object, parent, ...)
object |
A |
parent |
Index of the candidate solution of the current population |
... |
Additional arguments to be passed to the function |
Mutated double vector
Mehmet Hakan Satman - [email protected]
M.H.Satman (2013), Machine Coded Genetic Algorithms for Real Parameter Optimization Problems, Gazi University Journal of Science, Vol 26, No 1, pp. 85-95
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- GA::ga(type="real-valued", fitness = f, popSize = 100, maxiter = 200, min = rep(-50,5), max = rep(50,5), crossover = byte_crossover, mutation = byte_mutation) print(myga@solution)
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- GA::ga(type="real-valued", fitness = f, popSize = 100, maxiter = 200, min = rep(-50,5), max = rep(50,5), crossover = byte_crossover, mutation = byte_mutation) print(myga@solution)
This function is not called directly but is given as a parameter in GA::ga
function.
In GA::ga
, if the parameter mutation=
is set to byte_mutation_dynamic
than
the byte-coded mutation operator is applied in the genetic search. In mcga2
function, the hard-coded
mutation parameter is set to byte_mutation by definition. Byte-mutation function simply takes an double vector and
changes bytes of this values by +1 or -1 using the dynamically decreased and pre-determined mutation probabilty.
byte_mutation_dynamic(object, parent, ...)
byte_mutation_dynamic(object, parent, ...)
object |
A |
parent |
Index of the candidate solution of the current population |
... |
Additional arguments to be passed to the function |
Mutated double vector
Mehmet Hakan Satman - [email protected]
M.H.Satman (2013), Machine Coded Genetic Algorithms for Real Parameter Optimization Problems, Gazi University Journal of Science, Vol 26, No 1, pp. 85-95
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- GA::ga(type="real-valued", fitness = f, popSize = 100, maxiter = 200, min = rep(-50,5), max = rep(50,5), crossover = byte_crossover, mutation = byte_mutation_dynamic, pmutation = 0.10) print(myga@solution)
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- GA::ga(type="real-valued", fitness = f, popSize = 100, maxiter = 200, min = rep(-50,5), max = rep(50,5), crossover = byte_crossover, mutation = byte_mutation_dynamic, pmutation = 0.10) print(myga@solution)
This function is not called directly but is given as a parameter in GA::ga
function.
In GA::ga
, if the parameter mutation=
is set to byte_mutation_random
than
the byte-coded mutation operator is applied in the genetic search. In mcga2
function, the hard-coded
mutation parameter is set to byte_mutation by definition. This function simply takes an double vector and
changes bytes randomly in the range of [0,255] using the pre-determined mutation probabilty.
byte_mutation_random(object, parent, ...)
byte_mutation_random(object, parent, ...)
object |
A |
parent |
Index of the candidate solution of the current population |
... |
Additional arguments to be passed to the function |
Mutated double vector
Mehmet Hakan Satman - [email protected]
M.H.Satman (2013), Machine Coded Genetic Algorithms for Real Parameter Optimization Problems, Gazi University Journal of Science, Vol 26, No 1, pp. 85-95
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- GA::ga(type="real-valued", fitness = f, popSize = 100, maxiter = 200, min = rep(-50,5), max = rep(50,5), crossover = byte_crossover, mutation = byte_mutation_random, pmutation = 0.20) print(myga@solution)
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- GA::ga(type="real-valued", fitness = f, popSize = 100, maxiter = 200, min = rep(-50,5), max = rep(50,5), crossover = byte_crossover, mutation = byte_mutation_random, pmutation = 0.20) print(myga@solution)
This function is not called directly but is given as a parameter in GA::ga
function.
In GA::ga
, if the parameter mutation=
is set to byte_mutation_random_dynamic
than
the byte-coded mutation operator with dynamic probabilities is applied in the genetic search. In mcga2
function, the hard-coded
mutation parameter is set to byte_mutation by definition. This function simply takes an double vector and
changes bytes randomly in the range of [0,255] using the descrasing values of pre-determined mutation probabilty by generations.
byte_mutation_random_dynamic(object, parent, ...)
byte_mutation_random_dynamic(object, parent, ...)
object |
A |
parent |
Index of the candidate solution of the current population |
... |
Additional arguments to be passed to the function |
Mutated double vector
Mehmet Hakan Satman - [email protected]
M.H.Satman (2013), Machine Coded Genetic Algorithms for Real Parameter Optimization Problems, Gazi University Journal of Science, Vol 26, No 1, pp. 85-95
f <- function(x){ return(-sum( (x-5)^2 ) ) } # Increase popSize and maxiter for more precise solutions myga <- GA::ga(type="real-valued", fitness = f, popSize = 100, maxiter = 200, min = rep(-50,5), max = rep(50,5), crossover = byte_crossover, mutation = byte_mutation_random_dynamic, pmutation = 0.20) print(myga@solution)
f <- function(x){ return(-sum( (x-5)^2 ) ) } # Increase popSize and maxiter for more precise solutions myga <- GA::ga(type="real-valued", fitness = f, popSize = 100, maxiter = 200, min = rep(-50,5), max = rep(50,5), crossover = byte_crossover, mutation = byte_mutation_random_dynamic, pmutation = 0.20) print(myga@solution)
This function is a C++ wrapper for mutating byte representation of a given candidate solution
ByteCodeMutation(bytes1, pmutation)
ByteCodeMutation(bytes1, pmutation)
bytes1 |
A vector of bytes of a candidate solution |
pmutation |
Probability of mutation |
Byte vector of mutated solution
Mehmet Hakan Satman - [email protected]
ByteCodeMutationUsingDoubles
set.seed(1246) print(pi) bytes <- DoubleToBytes(pi) mutated.bytes <- ByteCodeMutation(bytes, 0.10) new.var <- BytesToDouble(mutated.bytes) print(new.var)
set.seed(1246) print(pi) bytes <- DoubleToBytes(pi) mutated.bytes <- ByteCodeMutation(bytes, 0.10) new.var <- BytesToDouble(mutated.bytes) print(new.var)
This function is a C++ wrapper for mutating byte representation of a given candidate solution
ByteCodeMutationUsingDoubles(d, pmutation)
ByteCodeMutationUsingDoubles(d, pmutation)
d |
A vector of doubles |
pmutation |
Probability of mutation |
Double vector of mutated solution
Mehmet Hakan Satman - [email protected]
ByteCodeMutation
set.seed(1246) print(pi) print(exp(1)) new.var <- ByteCodeMutationUsingDoubles(c(pi, exp(1)), 0.10) print(new.var)
set.seed(1246) print(pi) print(exp(1)) new.var <- ByteCodeMutationUsingDoubles(c(pi, exp(1)), 0.10) print(new.var)
This function is a C++ wrapper for mutating byte representation of a given candidate solution. This mutation operator randomly changes a byte in the range of [0,255].
ByteCodeMutationUsingDoublesRandom(d, pmutation)
ByteCodeMutationUsingDoublesRandom(d, pmutation)
d |
A vector of doubles |
pmutation |
Probability of mutation |
Double vector of mutated solution
Mehmet Hakan Satman - [email protected]
ByteCodeMutation
set.seed(1246) print(pi) print(exp(1)) new.var <- ByteCodeMutationUsingDoublesRandom(c(pi, exp(1)), 0.10) print(new.var)
set.seed(1246) print(pi) print(exp(1)) new.var <- ByteCodeMutationUsingDoublesRandom(c(pi, exp(1)), 0.10) print(new.var)
sizeof(double)
bytes to a double valueThis function converts sizeof(double)
bytes to a double typed value
BytesToDouble(x)
BytesToDouble(x)
x |
A vector of bytes (unsigned chars in C++) |
Corresponding double
typed value for a given vector of bytes
Mehmet Hakan Satman - [email protected]
DoubleVectorToBytes
DoubleToBytes
ByteVectorToDoubles
print(BytesToDouble(DoubleToBytes(56.43)))
print(BytesToDouble(DoubleToBytes(56.43)))
sizeof(double)
bytes to a vector of p double valuesThis function converts a byte vector to a vector of doubles
ByteVectorToDoubles(b)
ByteVectorToDoubles(b)
b |
A vector of bytes (unsigned chars in C++) |
Corresponding vector of double
typed values for a given vector of bytes
Mehmet Hakan Satman - [email protected]
DoubleVectorToBytes
BytesToDouble
ByteVectorToDoubles
a <- DoubleVectorToBytes(c(56.54, 89.7666, 98.565)) b <- ByteVectorToDoubles(a) print(b)
a <- DoubleVectorToBytes(c(56.54, 89.7666, 98.565)) b <- ByteVectorToDoubles(a) print(b)
double
typed variableThis function returns a vector of byte values with the length of sizeof(double)
for a given double
typed value
DoubleToBytes(x)
DoubleToBytes(x)
x |
A double typed value |
A vector of byte values with the length of sizeof(double)
for a given double
typed value
Mehmet Hakan Satman - [email protected]
DoubleVectorToBytes
BytesToDouble
ByteVectorToDoubles
print(DoubleToBytes(56.43))
print(DoubleToBytes(56.43))
double
typed variablesThis function returns a vector of byte values for a given vector of double
typed values
DoubleVectorToBytes(d)
DoubleVectorToBytes(d)
d |
A vector of double typed values |
returns a vector of byte values for a given vector of double
typed values
Mehmet Hakan Satman - [email protected]
DoubleToBytes
BytesToDouble
ByteVectorToDoubles
print(DoubleVectorToBytes(c(56.54, 89.7666, 98.565)))
print(DoubleVectorToBytes(c(56.54, 89.7666, 98.565)))
Byte based crossover and mutation operators can generate variables out of bounds of the decision variables. This function controls if variables are between their lower and upper bounds and if not, draws random numbers between these ranges. This function directly modifies the argument doubles
and does not return a value.
EnsureBounds(doubles, mins, maxs)
EnsureBounds(doubles, mins, maxs)
doubles |
A vector of doubles |
mins |
A vector of lower bounds of decision variables |
maxs |
A vector of upper bounds of decision variables |
Function directly modifies the argument doubles
and does not return a result.
Mehmet Hakan Satman - [email protected]
byte_crossover
byte_mutation
mcga2
set.seed(1234) x <- runif(10) print(x) # [1] 0.113703411 0.622299405 0.609274733 0.623379442 0.860915384 0.640310605 # [7] 0.009495756 0.232550506 0.666083758 0.514251141 EnsureBounds(x, mins=rep(0,10), maxs=rep(0.2,10)) print(x) # [1] 0.113703411 0.138718258 0.108994967 0.056546717 0.184686697 0.058463168 # [7] 0.009495756 0.167459126 0.057244657 0.053364156
set.seed(1234) x <- runif(10) print(x) # [1] 0.113703411 0.622299405 0.609274733 0.623379442 0.860915384 0.640310605 # [7] 0.009495756 0.232550506 0.666083758 0.514251141 EnsureBounds(x, mins=rep(0,10), maxs=rep(0.2,10)) print(x) # [1] 0.113703411 0.138718258 0.108994967 0.056546717 0.184686697 0.058463168 # [7] 0.009495756 0.167459126 0.057244657 0.053364156
This function is not called directly but is given as a parameter in GA::ga
function.
In GA::ga
, if the parameter crossover=
is set to flat_crossover
than
the flat crossover operator is applied in the genetic search. flat_crossover
draws a random number between parents' genes and returns a pair of generated offspring
flat_crossover(object, parents, ...)
flat_crossover(object, parents, ...)
object |
A |
parents |
Indices of the selected parents |
... |
Additional arguments to be passed to the function |
List of two generated offspring
Mehmet Hakan Satman - [email protected]
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- ga(type="real-valued", fitness = f, popSize = 100, maxiter = 100, min = rep(-50,5), max = rep(50,5), crossover = flat_crossover) print(myga@solution)
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- ga(type="real-valued", fitness = f, popSize = 100, maxiter = 100, min = rep(-50,5), max = rep(50,5), crossover = flat_crossover) print(myga@solution)
This function is not called directly but is given as a parameter in GA::ga
function.
In GA::ga
, if the parameter crossover=
is set to linear_crossover
than
the linear crossover operator is applied in the genetic search. linear_crossover
generates three offspring and performs a selection mechanism to determine best two of them.
linear_crossover(object, parents, ...)
linear_crossover(object, parents, ...)
object |
A |
parents |
Indices of the selected parents |
... |
Additional arguments to be passed to the function |
List of two generated offspring
Mehmet Hakan Satman - [email protected]
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- ga(type="real-valued", fitness = f, popSize = 100, maxiter = 100, min = rep(-50,5), max = rep(50,5), crossover = linear_crossover) print(myga@solution)
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- ga(type="real-valued", fitness = f, popSize = 100, maxiter = 100, min = rep(-50,5), max = rep(50,5), crossover = linear_crossover) print(myga@solution)
double
typed variableMaximum value of a double
typed variable
MaxDouble()
MaxDouble()
Returns maximum value of a double
typed variable in C++ compiler
Mehmet Hakan Satman - [email protected]
print(MaxDouble())
print(MaxDouble())
Machine coded genetic algorithm (MCGA) is a fast tool for real-valued optimization problems. It uses the byte representation of variables rather than real-values. It performs the classical crossover operations (uniform) on these byte representations. Mutation operator is also similar to classical mutation operator, which is to say, it changes a randomly selected byte value of a chromosome by +1 or -1 with probability 1/2. In MCGAs there is no need for encoding-decoding process and the classical operators are directly applicable on real-values. It is fast and can handle a wide range of a search space with high precision. Using a 256-unary alphabet is the main disadvantage of this algorithm but a moderate size population is convenient for many problems.
mcga(popsize, chsize, crossprob = 1.0, mutateprob = 0.01, elitism = 1, minval, maxval, maxiter = 10, evalFunc)
mcga(popsize, chsize, crossprob = 1.0, mutateprob = 0.01, elitism = 1, minval, maxval, maxiter = 10, evalFunc)
popsize |
Number of chromosomes. |
chsize |
Number of parameters. |
crossprob |
Crossover probability. By default it is 1.0 |
mutateprob |
Mutation probability. By default it is 0.01 |
elitism |
Number of best chromosomes to be copied directly into next generation. By default it is 1 |
minval |
The lower bound of the randomized initial population. This is not a constraint for parameters. |
maxval |
The upper bound of the randomized initial population. This is not a constraint for parameters. |
maxiter |
The maximum number of generations. By default it is 10 |
evalFunc |
An R function. By default, each problem is a minimization. |
population |
Sorted population resulted after generations |
costs |
Cost values for each chromosomes in the resulted population |
Mehmet Hakan Satman - [email protected]
M.H.Satman (2013), Machine Coded Genetic Algorithms for Real Parameter Optimization Problems, Gazi University Journal of Science, Vol 26, No 1, pp. 85-95
# A sample optimization problem # Min f(xi) = (x1-7)^2 + (x2-77)^2 + (x3-777)^2 + (x4-7777)^2 + (x5-77777)^2 # The range of xi is unknown. The solution is # x1 = 7 # x2 = 77 # x3 = 777 # x4 = 7777 # x5 = 77777 # Min f(xi) = 0 require("mcga") f<-function(x){ return ((x[1]-7)^2 + (x[2]-77)^2 +(x[3]-777)^2 +(x[4]-7777)^2 +(x[5]-77777)^2) } m <- mcga( popsize=200, chsize=5, minval=0.0, maxval=999999999.9, maxiter=2500, crossprob=1.0, mutateprob=0.01, evalFunc=f) cat("Best chromosome:\n") print(m$population[1,]) cat("Cost: ",m$costs[1],"\n")
# A sample optimization problem # Min f(xi) = (x1-7)^2 + (x2-77)^2 + (x3-777)^2 + (x4-7777)^2 + (x5-77777)^2 # The range of xi is unknown. The solution is # x1 = 7 # x2 = 77 # x3 = 777 # x4 = 7777 # x5 = 77777 # Min f(xi) = 0 require("mcga") f<-function(x){ return ((x[1]-7)^2 + (x[2]-77)^2 +(x[3]-777)^2 +(x[4]-7777)^2 +(x[5]-77777)^2) } m <- mcga( popsize=200, chsize=5, minval=0.0, maxval=999999999.9, maxiter=2500, crossprob=1.0, mutateprob=0.01, evalFunc=f) cat("Best chromosome:\n") print(m$population[1,]) cat("Cost: ",m$costs[1],"\n")
mcga2
is the improvement version of the standard mcga function as it is based on the GA::ga
function. The
byte_crossover
and the byte_mutation
operators are the main reproduction operators and these operators uses the byte
representations of parents in the computer memory.
mcga2(fitness, ..., min, max, population = gaControl("real-valued")$population, selection = gaControl("real-valued")$selection, crossover = byte_crossover, mutation = byte_mutation, popSize = 50, pcrossover = 0.8, pmutation = 0.1, elitism = base::max(1, round(popSize * 0.05)), maxiter = 100, run = maxiter, maxFitness = Inf, names = NULL, parallel = FALSE, monitor = gaMonitor, seed = NULL)
mcga2(fitness, ..., min, max, population = gaControl("real-valued")$population, selection = gaControl("real-valued")$selection, crossover = byte_crossover, mutation = byte_mutation, popSize = 50, pcrossover = 0.8, pmutation = 0.1, elitism = base::max(1, round(popSize * 0.05)), maxiter = 100, run = maxiter, maxFitness = Inf, names = NULL, parallel = FALSE, monitor = gaMonitor, seed = NULL)
fitness |
The goal function to be maximized |
... |
Additional arguments to be passed to the fitness function |
min |
Vector of lower bounds of variables |
max |
Vector of upper bounds of variables |
population |
Initial population. It is |
selection |
Selection operator. It is |
crossover |
Crossover operator. It is |
mutation |
Mutation operator. It is |
popSize |
Population size. It is 50 by default |
pcrossover |
Probability of crossover. It is 0.8 by default |
pmutation |
Probability of mutation. It is 0.1 by default |
elitism |
Number of elitist solutions. It is |
maxiter |
Maximum number of generations. It is 100 by default |
run |
The genetic search is stopped if the best solution has not any improvements in last |
maxFitness |
Upper bound of the fitness function. By default it is Inf |
names |
Vector of names of the variables. By default it is |
parallel |
If TRUE, fitness calculations are performed parallel. It is FALSE by default |
monitor |
The monitoring function for printing some information about the current state of the genetic search. It is |
seed |
The seed for random number generating. It is |
Returns an object of class ga-class
Mehmet Hakan Satman - [email protected]
M.H.Satman (2013), Machine Coded Genetic Algorithms for Real Parameter Optimization Problems, Gazi University Journal of Science, Vol 26, No 1, pp. 85-95
Luca Scrucca (2013). GA: A Package for Genetic Algorithms in R. Journal of Statistical Software, 53(4), 1-37.
GA::ga
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- mcga2(fitness = f, popSize = 100, maxiter = 300, min = rep(-50,5), max = rep(50,5)) print(myga@solution)
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- mcga2(fitness = f, popSize = 100, maxiter = 300, min = rep(-50,5), max = rep(50,5)) print(myga@solution)
Machine coded genetic algorithm (MCGA) is a fast tool for real-valued optimization problems. It uses the byte representation of variables rather than real-values. It performs the classical crossover operations (uniform) on these byte representations. Mutation operator is also similar to classical mutation operator, which is to say, it changes a randomly selected byte value of a chromosome by +1 or -1 with probability 1/2. In MCGAs there is no need for encoding-decoding process and the classical operators are directly applicable on real-values. It is fast and can handle a wide range of a search space with high precision. Using a 256-unary alphabet is the main disadvantage of this algorithm but a moderate size population is convenient for many problems.
This function performs multi objective optimization using the same logic underlying the mcga. Chromosomes are sorted by their objective values using a non-dominated sorting algorithm.
multi_mcga(popsize, chsize, crossprob = 1.0, mutateprob = 0.01, elitism = 1, minval, maxval, maxiter = 10, numfunc, evalFunc)
multi_mcga(popsize, chsize, crossprob = 1.0, mutateprob = 0.01, elitism = 1, minval, maxval, maxiter = 10, numfunc, evalFunc)
popsize |
Number of chromosomes. |
chsize |
Number of parameters. |
crossprob |
Crossover probability. By default it is 1.0 |
mutateprob |
Mutation probability. By default it is 0.01 |
elitism |
Number of best chromosomes to be copied directly into next generation. By default it is 1 |
minval |
The lower bound of the randomized initial population. This is not a constraint for parameters. |
maxval |
The upper bound of the randomized initial population. This is not a constraint for parameters. |
maxiter |
The maximum number of generations. By default it is 10. |
numfunc |
Number of objective functions. |
evalFunc |
An R function. By default, each problem is a minimization. This function must return a cost vector with dimension of numfunc. Each element of this vector points to the corresponding function to optimize. |
population |
Sorted population resulted after generations |
costs |
Cost values for each chromosomes in the resulted population |
ranks |
Calculated ranks using a non-dominated sorting for each chromosome |
Mehmet Hakan Satman - [email protected]
Deb, K. (2000). An efficient constraint handling method for genetic algorithms. Computer methods in applied mechanics and engineering, 186(2), 311-338.
## Not run: # We have two objective functions. f1<-function(x){ return(sin(x)) } f2<-function(x){ return(sin(2*x)) } # This function returns a vector of cost functions for a given x sent from mcga f<-function(x){ return ( c( f1(x), f2(x)) ) } # main loop m<-multi_mcga(popsize=200, chsize=1, minval= 0, elitism=2, maxval= 2.0 * pi, maxiter=1000, crossprob=1.0, mutateprob=0.01, evalFunc=f, numfunc=2) # Points show best five solutions. curve(f1, 0, 2*pi) curve(f2, 0, 2*pi, add=TRUE) p <- m$population[1:5,] points(p, f1(p)) points(p, f2(p)) ## End(Not run)
## Not run: # We have two objective functions. f1<-function(x){ return(sin(x)) } f2<-function(x){ return(sin(2*x)) } # This function returns a vector of cost functions for a given x sent from mcga f<-function(x){ return ( c( f1(x), f2(x)) ) } # main loop m<-multi_mcga(popsize=200, chsize=1, minval= 0, elitism=2, maxval= 2.0 * pi, maxiter=1000, crossprob=1.0, mutateprob=0.01, evalFunc=f, numfunc=2) # Points show best five solutions. curve(f1, 0, 2*pi) curve(f2, 0, 2*pi, add=TRUE) p <- m$population[1:5,] points(p, f1(p)) points(p, f2(p)) ## End(Not run)
This function is a C++ wrapper for crossing-over of two byte vectors of candidate solutions
OnePointCrossOver(bytes1, bytes2, cutpoint)
OnePointCrossOver(bytes1, bytes2, cutpoint)
bytes1 |
A vector of bytes of the first parent |
bytes2 |
A vector of bytes of the second parent |
cutpoint |
Cut-point for the single point crossing-over |
List of two byte vectors of offspring
Mehmet Hakan Satman - [email protected]
UniformCrossOver
UniformCrossOverOnDoublesUsingBytes
b1 <- DoubleVectorToBytes(c(56.54, 89.7666, 98.565)) b2 <- DoubleVectorToBytes(c(79.76, 56.4443, 34.22121)) result <- OnePointCrossOver(b1,b2, round(runif(1,1,SizeOfDouble() * 3))) print(ByteVectorToDoubles(result[[1]])) print(ByteVectorToDoubles(result[[2]]))
b1 <- DoubleVectorToBytes(c(56.54, 89.7666, 98.565)) b2 <- DoubleVectorToBytes(c(79.76, 56.4443, 34.22121)) result <- OnePointCrossOver(b1,b2, round(runif(1,1,SizeOfDouble() * 3))) print(ByteVectorToDoubles(result[[1]])) print(ByteVectorToDoubles(result[[2]]))
This function is a C++ wrapper for crossing-over of two double vectors of candidate solutions using their byte representations
OnePointCrossOverOnDoublesUsingBytes(d1, d2, cutpoint)
OnePointCrossOverOnDoublesUsingBytes(d1, d2, cutpoint)
d1 |
A vector of doubles of the first parent |
d2 |
A vector of doubles of the second parent |
cutpoint |
An integer between 1 and chromosome length for crossover cutting |
List of two double vectors of offspring
Mehmet Hakan Satman - [email protected]
OnePointCrossOver
UniformCrossOverOnDoublesUsingBytes
d1 <- runif(3) d2 <- runif(3) cutp <- sample(1:(length(d1)*SizeOfDouble()), 1)[1] offspring <- OnePointCrossOverOnDoublesUsingBytes(d1,d2, cutp) print("Parents:") print(d1) print(d2) print("Offspring:") print(offspring[[1]]) print(offspring[[2]])
d1 <- runif(3) d2 <- runif(3) cutp <- sample(1:(length(d1)*SizeOfDouble()), 1)[1] offspring <- OnePointCrossOverOnDoublesUsingBytes(d1,d2, cutp) print("Parents:") print(d1) print(d2) print("Offspring:") print(offspring[[1]]) print(offspring[[2]])
This function is not called directly but is given as a parameter in GA::ga
function.
In GA::ga
, if the parameter crossover=
is set to sbx_crossover
than
the sbx crossover operator is applied in the genetic search. sbx_crossover mimics the classical single-point crossover operator
in binary genetic algorithms.
sbx_crossover(object, parents, ...)
sbx_crossover(object, parents, ...)
object |
A |
parents |
Indices of the selected parents |
... |
Additional arguments to be passed to the function |
List of two generated offspring
Mehmet Hakan Satman - [email protected]
Deb, Kalyanmoy, and Ram Bhushan Agrawal. "Simulated binary crossover for continuous search space." Complex systems 9.2 (1995): 115-148.
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- ga(type="real-valued", fitness = f, popSize = 100, maxiter = 100, min = rep(-50,5), max = rep(50,5), crossover = sbx_crossover) print(myga@solution)
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- ga(type="real-valued", fitness = f, popSize = 100, maxiter = 100, min = rep(-50,5), max = rep(50,5), crossover = sbx_crossover) print(myga@solution)
double
typed variableByte-length of a double
typed variable in computer memory
SizeOfDouble()
SizeOfDouble()
Returns the byte-length of a double
typed variable in computer memory
Mehmet Hakan Satman - [email protected]
print(SizeOfDouble())
print(SizeOfDouble())
int
typed variableByte-length of a int
typed variable in computer memory
SizeOfInt()
SizeOfInt()
Returns the byte-length of a int
typed variable in computer memory
Mehmet Hakan Satman - [email protected]
print(SizeOfInt())
print(SizeOfInt())
long
typed variableByte-length of a long
typed variable in computer memory
SizeOfLong()
SizeOfLong()
Returns the byte-length of a long
typed variable in computer memory
Mehmet Hakan Satman - [email protected]
print(SizeOfLong())
print(SizeOfLong())
This function is a C++ wrapper for crossing-over of two byte vectors of candidate solutions
TwoPointCrossOver(bytes1, bytes2, cutpoint1, cutpoint2)
TwoPointCrossOver(bytes1, bytes2, cutpoint1, cutpoint2)
bytes1 |
A vector of bytes of the first parent |
bytes2 |
A vector of bytes of the second parent |
cutpoint1 |
First cut-point for the single point crossing-over |
cutpoint2 |
Second cut-point for the single point crossing-over |
List of two byte vectors of offspring
Mehmet Hakan Satman - [email protected]
OnePointCrossOver
OnePointCrossOverOnDoublesUsingBytes
UniformCrossOverOnDoublesUsingBytes
b1 <- DoubleVectorToBytes(c(56.54, 89.7666, 98.565)) b2 <- DoubleVectorToBytes(c(79.76, 56.4443, 34.22121)) cutpoints <- sort(sample(1:(length(b1)*SizeOfDouble()), 2, replace = FALSE)) result <- TwoPointCrossOver(b1,b2, cutpoints[1], cutpoints[2]) print(ByteVectorToDoubles(result[[1]])) print(ByteVectorToDoubles(result[[2]]))
b1 <- DoubleVectorToBytes(c(56.54, 89.7666, 98.565)) b2 <- DoubleVectorToBytes(c(79.76, 56.4443, 34.22121)) cutpoints <- sort(sample(1:(length(b1)*SizeOfDouble()), 2, replace = FALSE)) result <- TwoPointCrossOver(b1,b2, cutpoints[1], cutpoints[2]) print(ByteVectorToDoubles(result[[1]])) print(ByteVectorToDoubles(result[[2]]))
This function is a C++ wrapper for crossing-over of two double vectors of candidate solutions using their byte representations
TwoPointCrossOverOnDoublesUsingBytes(d1, d2, cutpoint1, cutpoint2)
TwoPointCrossOverOnDoublesUsingBytes(d1, d2, cutpoint1, cutpoint2)
d1 |
A vector of doubles of the first parent |
d2 |
A vector of doubles of the second parent |
cutpoint1 |
An integer between 1 and chromosome length for crossover cutting |
cutpoint2 |
An integer between cutpoint1 and chromosome length for crossover cutting |
List of two double vectors of offspring
Mehmet Hakan Satman - [email protected]
TwoPointCrossOver
OnePointCrossOver
UniformCrossOver
OnePointCrossOverOnDoublesUsingBytes
d1 <- runif(3) d2 <- runif(3) cutpoints <- sort(sample(1:(length(d1)*SizeOfDouble()), 2, replace = FALSE)) offspring <- TwoPointCrossOverOnDoublesUsingBytes(d1,d2,cutpoints[1], cutpoints[2]) print("Parents:") print(d1) print(d2) print("Offspring:") print(offspring[[1]]) print(offspring[[2]])
d1 <- runif(3) d2 <- runif(3) cutpoints <- sort(sample(1:(length(d1)*SizeOfDouble()), 2, replace = FALSE)) offspring <- TwoPointCrossOverOnDoublesUsingBytes(d1,d2,cutpoints[1], cutpoints[2]) print("Parents:") print(d1) print(d2) print("Offspring:") print(offspring[[1]]) print(offspring[[2]])
This function is not called directly but is given as a parameter in GA::ga
function.
In GA::ga
, if the parameter crossover=
is set to unfair_average_crossover
than
the unfair average crossover operator is applied in the genetic search.
unfair_average_crossover(object, parents, ...)
unfair_average_crossover(object, parents, ...)
object |
A |
parents |
Indices of the selected parents |
... |
Additional arguments to be passed to the function |
List of two generated offspring
Mehmet Hakan Satman - [email protected]
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- ga(type="real-valued", fitness = f, popSize = 100, maxiter = 100, min = rep(-50,5), max = rep(50,5), crossover = unfair_average_crossover) print(myga@solution)
f <- function(x){ return(-sum( (x-5)^2 ) ) } myga <- ga(type="real-valued", fitness = f, popSize = 100, maxiter = 100, min = rep(-50,5), max = rep(50,5), crossover = unfair_average_crossover) print(myga@solution)
This function is a C++ wrapper for crossing-over of two byte vectors of candidate solutions
UniformCrossOver(bytes1, bytes2)
UniformCrossOver(bytes1, bytes2)
bytes1 |
A vector of bytes of the first parent |
bytes2 |
A vector of bytes of the second parent |
List of two byte vectors of offspring
Mehmet Hakan Satman - [email protected]
OnePointCrossOver
UniformCrossOverOnDoublesUsingBytes
b1 <- DoubleVectorToBytes(c(56.54, 89.7666, 98.565)) b2 <- DoubleVectorToBytes(c(79.76, 56.4443, 34.22121)) result <- UniformCrossOver(b1,b2) print(ByteVectorToDoubles(result[[1]])) print(ByteVectorToDoubles(result[[2]]))
b1 <- DoubleVectorToBytes(c(56.54, 89.7666, 98.565)) b2 <- DoubleVectorToBytes(c(79.76, 56.4443, 34.22121)) result <- UniformCrossOver(b1,b2) print(ByteVectorToDoubles(result[[1]])) print(ByteVectorToDoubles(result[[2]]))
This function is a C++ wrapper for crossing-over of two double vectors of candidate solutions using their byte representations
UniformCrossOverOnDoublesUsingBytes(d1, d2)
UniformCrossOverOnDoublesUsingBytes(d1, d2)
d1 |
A vector of doubles of the first parent |
d2 |
A vector of doubles of the second parent |
List of two double vectors of offspring
Mehmet Hakan Satman - [email protected]
OnePointCrossOver
OnePointCrossOverOnDoublesUsingBytes
d1 <- runif(3) d2 <- runif(3) offspring <- UniformCrossOverOnDoublesUsingBytes(d1,d2) print("Parents:") print(d1) print(d2) print("Offspring:") print(offspring[[1]]) print(offspring[[2]])
d1 <- runif(3) d2 <- runif(3) offspring <- UniformCrossOverOnDoublesUsingBytes(d1,d2) print("Parents:") print(d1) print(d2) print("Offspring:") print(offspring[[1]]) print(offspring[[2]])