-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_stopping_condition.R
61 lines (58 loc) · 1.98 KB
/
global_stopping_condition.R
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
#' Factory function for a global stopping condition that stops
#' the computation after given number of metaepochs.
#'
#' @param metaepochs_count - numeric - maximum number of metaepochs
#'
#' @return Function that receives a list of metaepoch snapshots
#' and returns a Boolean value determining whether the computation
#' should be stopped based on how many metaepochs have passed,
#' which can be used as a global stopping condition for the hms function.
#'
#' @export
#'
#' @examples
#' global_stopping_condition <- gsc_metaepochs_count(10)
gsc_metaepochs_count <- function(metaepochs_count) {
function(metaepoch_snapshots) {
length(metaepoch_snapshots) >= metaepochs_count
}
}
#' Factory function for a global stopping condition that stops
#' the computation after fitness function has been evaluated
#' given number of times.
#'
#' @param max_evaluations - numeric - maximum number of fitness function evaluations
#'
#' @return Function that receives a list of metaepoch snapshots
#' and returns a Boolean value determining whether the computation
#' should be stopped based on how many fitness function evaluations
#' have been made, which can be used as a global stopping condition
#' for the hms function.
#'
#' @export
#'
#' @examples
#' global_stopping_condition <- gsc_max_fitness_evaluations(10000)
gsc_max_fitness_evaluations <- function(max_evaluations) {
function(metaepoch_snapshots) {
length(metaepoch_snapshots) > 0 &&
utils::tail(metaepoch_snapshots, n = 1)[[1]]@fitness_evaluations > max_evaluations
}
}
#' Factory function for a global stopping condition that never stops
#' the computation. It results in hms running until there are no more
#' active demes.
#'
#' @return function that always returns \code{FALSE}, which can be used
#' as a global stopping condition for the hms function.
#'
#' @export
#'
#' @examples
#' global_stopping_condition <- gsc_trivial()
gsc_trivial <- function() {
function(metaepoch_snapshots) {
FALSE
}
}
gsc_default <- gsc_metaepochs_count(10)