Description Usage Arguments Value Examples
Monitor objects serve for monitoring the optimization process. Each monitor
object expects the parameters before
, step
and after
,
each being a function and expecting opt.state
and ...
as the
only parameters. This way one can access all the variables used within the
evolutionary cycle.
1 |
before |
[ |
step |
[ |
after |
[ |
... |
[ |
[ecr_monitor
]
Monitor object.
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 | # We want to find the minimum of the function f(x) = x sin(2x) on the intervall
# [0, 2pi]. We optimize f here with a simple (10 + 10)
# evolutionary strategy. We overwrite the default console monitor
# (see function setupConsoleMonitor) with an enhanced console monitor :-)
obj.fn = makeSingleObjectiveFunction(
name = "My obj. function",
fn = function(x) x * sin(2 * x),
par.set = makeParamSet(makeNumericParam("x", lower = 0, upper = 2 * pi))
)
# Now we define our enhanced monitoring function
# Monitor functions expect the opt.state (optimization state) and ... (not used
# until now). This way we can access all the variables saved there.
monitorStep = function(opt.state, ...) {
iter = opt.state$iter
best.fitness = opt.state$best.value
if (iter == 1L) {
# manupulate opt.state
opt.state$first.best = best.fitness
}
first.best.fitness = opt.state$first.best
cat(sprintf("Best objective value in iteration %i is %.6f
(overall absolute improvement is: %.6f)\n",
iter, best.fitness, first.best.fitness - best.fitness)
)
}
myFancyConsoleMonitor = makeMonitor(
before = function(opt.state, ...) {
catf("I am starting now buddy!")
},
step = monitorStep,
after = function(opt.state, ...) {
catf("Finished!")
}
)
# We want to solve this with a (10 + 10) evolutionary strategy based on
# the floating point representation of the input vectors with the default
# operators: intermediate recombinator and Gauss mutation
ctrl = setupECRControl(
n.population = 10L,
n.offspring = 10L,
survival.strategy = "plus",
representation = "float",
stopping.conditions = setupTerminators(max.iter = 30L),
monitor = myFancyConsoleMonitor
)
ctrl = setupEvolutionaryOperators(ctrl)
res = doTheEvolution(obj.fn, ctrl)
print(res)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.