R/addLoggingWrapper.R
addLoggingWrapper.Rd
Often it is desired and useful to store the optimization path, i.e., the evaluated function values and/or the parameters. Not all optimization algorithms offer such a trace. This wrapper makes a smoof function handle x/y-values itself.
addLoggingWrapper(fn, logg.x = FALSE, logg.y = TRUE, size = 100L)
[smoof_function
]
Smoof function.
[logical(1)
]
Should x-values be logged?
Default is FALSE
.
[logical(1)
]
Should objective values be logged?
Default is TRUE
.
[integer(1)
]
Initial size of the internal data structures used for logging.
Default is 100. I.e., there is space reserved for 100 function evaluations.
In case of an overflow (i.e., more function evaluations than space reserved)
the data structures are re-initialized by adding space for another size
evaluations.
This comes handy if you know the number of function evaluations (or at least
an upper bound thereof) a-priori and may serve to reduce the time complextity
of logging values.
[smoof_logging_function
]
Logging values, in particular logging x-values, will substantially slow down the evaluation of the function.
# We first build the smoof function and apply the logging wrapper to it
fn = makeSphereFunction(dimensions = 2L)
fn = addLoggingWrapper(fn, logg.x = TRUE)
# We now apply an optimization algorithm to it and the logging wrapper keeps
# track of the evaluated points.
res = optim(fn, par = c(1, 1), method = "Nelder-Mead")
# Extract the logged values
log.res = getLoggedValues(fn)
print(log.res$pars)
#> x1 x2
#> 1 1.000000e+00 1.000000e+00
#> 2 1.100000e+00 1.000000e+00
#> 3 1.000000e+00 1.100000e+00
#> 4 9.000000e-01 1.100000e+00
#> 5 9.500000e-01 1.075000e+00
#> 6 9.000000e-01 1.000000e+00
#> 7 8.500000e-01 9.500000e-01
#> 8 9.500000e-01 8.500000e-01
#> 9 9.375000e-01 9.125000e-01
#> 10 8.000000e-01 8.000000e-01
#> 11 7.000000e-01 7.000000e-01
#> 12 8.000000e-01 6.000000e-01
#> 13 8.125000e-01 6.875000e-01
#> 14 5.500000e-01 4.500000e-01
#> 15 3.500000e-01 2.500000e-01
#> 16 2.500000e-01 3.500000e-01
#> 17 3.875000e-01 4.125000e-01
#> 18 -1.000000e-01 -1.000000e-01
#> 19 -5.000000e-01 -5.000000e-01
#> 20 -2.000000e-01 -8.881784e-16
#> 21 -6.250000e-02 6.250000e-02
#> 22 -4.125000e-01 -3.875000e-01
#> 23 8.437500e-02 1.656250e-01
#> 24 -2.468750e-01 -2.031250e-01
#> 25 1.562500e-03 7.343750e-02
#> 26 3.906250e-02 2.359375e-01
#> 27 -6.523438e-02 -1.601563e-02
#> 28 -1.171875e-03 -5.078125e-03
#> 29 2.949219e-02 -3.886719e-02
#> 30 -6.796875e-02 -9.453125e-02
#> 31 -1.582031e-02 3.144531e-02
#> 32 4.824219e-02 4.238281e-02
#> 33 1.987305e-02 2.778320e-02
#> 34 3.452148e-02 -8.740234e-03
#> 35 -3.234863e-03 2.139893e-02
#> 36 -2.427979e-02 -1.146240e-02
#> 37 -1.324158e-02 -1.651001e-03
#> 38 -1.117859e-02 -2.812805e-02
#> 39 -5.220795e-03 9.017181e-03
#> 40 6.848907e-03 5.590057e-03
#> 41 1.826286e-03 3.779793e-03
#> 42 5.875206e-03 -1.031551e-02
#> 43 -2.446795e-03 4.184008e-03
#> 44 5.513668e-04 1.304193e-02
#> 45 -7.410645e-04 -5.481124e-04
#> 46 3.532016e-03 -9.523273e-04
#> 47 2.037314e-03 3.317565e-04
#> 48 -5.300373e-04 -3.996149e-03
#> 49 5.904362e-05 -2.052163e-03
#> 50 -2.719335e-03 -2.932032e-03
#> 51 8.481516e-04 -4.841907e-04
#> 52 4.804339e-05 1.019860e-03
#> 53 5.079345e-05 2.518544e-04
#> 54 -1.538423e-03 1.879327e-04
#> 55 2.515080e-04 -3.161599e-04
#> 56 1.043366e-03 4.838069e-04
#> 57 -2.949569e-04 -2.901326e-04
#> 58 5.972584e-04 2.258271e-04
#> 59 -7.190309e-05 -1.611427e-04
#> 60 -2.726176e-04 4.068715e-04
#> 61 1.204766e-04 -1.354020e-04
#> 62 -2.219950e-06 -5.483990e-04
#> 63 3.754010e-05 5.179101e-05
print(log.res$obj.vals)
#> [1] 2.000000e+00 2.210000e+00 2.210000e+00 2.020000e+00 2.058125e+00
#> [6] 1.810000e+00 1.625000e+00 1.625000e+00 1.711562e+00 1.280000e+00
#> [11] 9.800000e-01 1.000000e+00 1.132812e+00 5.050000e-01 1.850000e-01
#> [16] 1.850000e-01 3.203125e-01 2.000000e-02 5.000000e-01 4.000000e-02
#> [21] 7.812500e-03 3.203125e-01 3.455078e-02 1.022070e-01 5.395508e-03
#> [26] 5.719238e-02 4.512024e-03 2.716064e-05 2.380447e-03 1.355591e-02
#> [31] 1.239090e-03 4.123611e-03 1.166844e-03 1.268125e-03 4.683784e-04
#> [36] 7.208946e-04 1.780652e-04 9.161481e-04 1.085663e-04 7.815627e-05
#> [41] 1.762216e-05 1.409279e-04 2.349272e-05 1.703958e-04 8.496039e-07
#> [46] 1.338207e-05 4.260709e-06 1.625014e-05 4.214860e-06 1.599159e-05
#> [51] 9.538017e-07 1.042423e-06 6.601059e-08 2.402063e-06 1.632133e-07
#> [56] 1.322682e-06 1.711765e-07 4.077154e-07 3.113701e-08 2.398648e-07
#> [61] 3.284831e-08 3.007464e-07 4.091568e-09
log.res = getLoggedValues(fn, compact = TRUE)
print(log.res)
#> x1 x2 y1
#> 1 1.000000e+00 1.000000e+00 2.000000e+00
#> 2 1.100000e+00 1.000000e+00 2.210000e+00
#> 3 1.000000e+00 1.100000e+00 2.210000e+00
#> 4 9.000000e-01 1.100000e+00 2.020000e+00
#> 5 9.500000e-01 1.075000e+00 2.058125e+00
#> 6 9.000000e-01 1.000000e+00 1.810000e+00
#> 7 8.500000e-01 9.500000e-01 1.625000e+00
#> 8 9.500000e-01 8.500000e-01 1.625000e+00
#> 9 9.375000e-01 9.125000e-01 1.711562e+00
#> 10 8.000000e-01 8.000000e-01 1.280000e+00
#> 11 7.000000e-01 7.000000e-01 9.800000e-01
#> 12 8.000000e-01 6.000000e-01 1.000000e+00
#> 13 8.125000e-01 6.875000e-01 1.132812e+00
#> 14 5.500000e-01 4.500000e-01 5.050000e-01
#> 15 3.500000e-01 2.500000e-01 1.850000e-01
#> 16 2.500000e-01 3.500000e-01 1.850000e-01
#> 17 3.875000e-01 4.125000e-01 3.203125e-01
#> 18 -1.000000e-01 -1.000000e-01 2.000000e-02
#> 19 -5.000000e-01 -5.000000e-01 5.000000e-01
#> 20 -2.000000e-01 -8.881784e-16 4.000000e-02
#> 21 -6.250000e-02 6.250000e-02 7.812500e-03
#> 22 -4.125000e-01 -3.875000e-01 3.203125e-01
#> 23 8.437500e-02 1.656250e-01 3.455078e-02
#> 24 -2.468750e-01 -2.031250e-01 1.022070e-01
#> 25 1.562500e-03 7.343750e-02 5.395508e-03
#> 26 3.906250e-02 2.359375e-01 5.719238e-02
#> 27 -6.523438e-02 -1.601563e-02 4.512024e-03
#> 28 -1.171875e-03 -5.078125e-03 2.716064e-05
#> 29 2.949219e-02 -3.886719e-02 2.380447e-03
#> 30 -6.796875e-02 -9.453125e-02 1.355591e-02
#> 31 -1.582031e-02 3.144531e-02 1.239090e-03
#> 32 4.824219e-02 4.238281e-02 4.123611e-03
#> 33 1.987305e-02 2.778320e-02 1.166844e-03
#> 34 3.452148e-02 -8.740234e-03 1.268125e-03
#> 35 -3.234863e-03 2.139893e-02 4.683784e-04
#> 36 -2.427979e-02 -1.146240e-02 7.208946e-04
#> 37 -1.324158e-02 -1.651001e-03 1.780652e-04
#> 38 -1.117859e-02 -2.812805e-02 9.161481e-04
#> 39 -5.220795e-03 9.017181e-03 1.085663e-04
#> 40 6.848907e-03 5.590057e-03 7.815627e-05
#> 41 1.826286e-03 3.779793e-03 1.762216e-05
#> 42 5.875206e-03 -1.031551e-02 1.409279e-04
#> 43 -2.446795e-03 4.184008e-03 2.349272e-05
#> 44 5.513668e-04 1.304193e-02 1.703958e-04
#> 45 -7.410645e-04 -5.481124e-04 8.496039e-07
#> 46 3.532016e-03 -9.523273e-04 1.338207e-05
#> 47 2.037314e-03 3.317565e-04 4.260709e-06
#> 48 -5.300373e-04 -3.996149e-03 1.625014e-05
#> 49 5.904362e-05 -2.052163e-03 4.214860e-06
#> 50 -2.719335e-03 -2.932032e-03 1.599159e-05
#> 51 8.481516e-04 -4.841907e-04 9.538017e-07
#> 52 4.804339e-05 1.019860e-03 1.042423e-06
#> 53 5.079345e-05 2.518544e-04 6.601059e-08
#> 54 -1.538423e-03 1.879327e-04 2.402063e-06
#> 55 2.515080e-04 -3.161599e-04 1.632133e-07
#> 56 1.043366e-03 4.838069e-04 1.322682e-06
#> 57 -2.949569e-04 -2.901326e-04 1.711765e-07
#> 58 5.972584e-04 2.258271e-04 4.077154e-07
#> 59 -7.190309e-05 -1.611427e-04 3.113701e-08
#> 60 -2.726176e-04 4.068715e-04 2.398648e-07
#> 61 1.204766e-04 -1.354020e-04 3.284831e-08
#> 62 -2.219950e-06 -5.483990e-04 3.007464e-07
#> 63 3.754010e-05 5.179101e-05 4.091568e-09