optim
function.doTheEvolution
. Although this approach is highly flexible
and very readable it requires quite a lot of code. However, in everyday
life R users frequently need to optimize a single-objective R function.
The ecr
function thus provides a more R like interface for single
objective optimization similar to the interface of the optim
function.
ecr(obj.fun, n.dim, lower = NULL, upper = NULL, n.bits, representation, n.population, n.offspring, n.mating.pool = floor(n.population/2), survival.strategy = "plus", n.elite = 0L, vectorized.evaluation = FALSE, custom.constants = list(), logger = NULL, monitor = setupConsoleMonitor(), max.iter = 100L, max.evals = NULL, max.time = NULL, more.args = list(), initial.population = NULL, parent.selector = getDefaultEvolutionaryOperators(representation, "parent.selector"), survival.selector = getDefaultEvolutionaryOperators(representation, "survival.selector"), generator = getDefaultEvolutionaryOperators(representation, "generator"), mutator = getDefaultEvolutionaryOperators(representation, "mutator"), recombinator = getDefaultEvolutionaryOperators(representation, "recombinator"))
function
]
The single-objective target function. Can be any R function which takes a
single vector as input and returns a scalar value describing the vectors
fitness.integer(1)
]
Dimension of the decision space.numeric
]
Vector of minimal values for each parameter of the decision space in case
of float or permutation encoding.numeric
]
Vector of maximal values for each parameter of the decision space in case
of float or permutation encoding.integer(1)
]
Number of bits to use for binary representation.character(1)
]
Genotype representation of the parameters. Available are binary,
float, permutation and custom.integer(1)
]
Number of individuals in the population.integer(1)
]
Number of individuals generated in each generation.integer(1)
]
Number of individuals which can potentially participate in the
generation of offspring.
Default is half of the population size.character(1)
]
Determines the survival strategy used by the EA. Possible are plus for
a classical (mu + lambda) strategy and comma for (mu, lambda).
Default is plus.integer(1)
]
Number of fittest individuals of the current generation that shall be copied to the
next generation without changing. Keep in mind, that the algorithm
does not care about this option if the survival.strategy
is set to 'plus'.
Default is 0.logical(1L)
]
Is the fitness/objective function vectorized? I.e., does the fitness function accept
a list? This allows for faster execution or parallelization by hand.
If TRUE
the following destinction on the type of the objective function is made:
Default is FALSE
.
list
]
Additional constants which should be available to all generators and operators.
Defaults to empty list.function
]
Monitoring object used to log stuff.
Default is NULL
which means no logging at all.
See setupOptPathLoggingMonitor
for ecr's build-in logger.function
]
Monitoring function.
Default is NULL
, i.e. no monitoring.integer(1)
]
Maximal number of iterations. Default ist 100L
.integer(1)
]
Maximal number of iterations/generations. Default is Inf
.integer(1)
]
Time budget in seconds. Default ist Inf
.list
]
Additional arguments passed to objective function.list
]
List of individuals which should be placed in the initial population.
The function will stop with an error message if the number of passed individuals
is larger than control$n.population
. If the number of passed individuals
is lower than control$n.population
, the population will be filled up
by individuals generated by the corresponding generator.
Default is NULL
, i.e., the entire population is generated by the
population generator.ecr_selector
]
Selection operator which implements a procedure to copy individuals from a
given population to the mating pool, i. e., allow them to become parents.ecr_selector
]
Selection operator which implements a procedurce to extract individuals from
a given set, which should survive and set up the next generation.ecr_generator
]
Generator operator of type ecr_generator
for the generation of the initial
population.ecr_mutator
]
Mutation operator of type ecr_mutator
.ecr_recombinator
]
Recombination operator of type ecr_recombinator
.setupECRControl
for building the control object,
makeOptimizationTask
to define an optimization problem and
doTheEvolution
for the main working horse of ecr.
fn = function(x) {
sum(x^2)
}
res = ecr(fn, n.dim = 2L, lower = c(-5, -5), upper = c(5, 5),
representation = "float", n.population = 20L, n.offspring = 10L, max.iter = 30L)
Run the code above in your browser using DataLab