/
plot_model.R
292 lines (269 loc) · 9.89 KB
/
plot_model.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Categorical model generation
#
# @description
#
# Rebuild the probabilities of the sampling models used in irace to generate
# configurations in each iteration.
#
# @template arg_irace_results
#
# @param param_name
# String, parameter to be included in the plot (example: param_name = "algorithm"))
#
# @return data frame with columns "iteration", "elite", "parameter", "value", "prob"
#
# @examples
# NULL
# FIXME: Add examples
getCategoricalModel <- function(irace_results, param_name)
{
if (!(irace_results$parameters$types[[param_name]] %in% c("c"))) {
stop("Error: Parameter is not categorical\n")
}
iterations <- length(irace_results$allElites)
domain <- irace_results$parameters$domain[[param_name]]
n_val <- length(irace_results$parameters$domain[[param_name]])
prob <- rep((1 / n_val), n_val)
# Get elite data by iteration
all_elites <- list()
for (i in 1:iterations){
all_elites[[i]] <- irace_results$allConfigurations[irace_results$allElites[[i]], c(".ID.", ".PARENT.",param_name) ]
}
total_iterations <- floor(2 + log2(irace_results$parameters$nbVariable))
X <- NULL
models <- list()
for (i in 1:(iterations-1)) {
models[[i]] <- list()
total_iterations <- max(total_iterations, i+1)
for (elite in 1:length(irace_results$allElites[[i]])) {
cid <- all_elites[[i]][elite, ".ID."]
parent <- all_elites[[i]][elite, ".PARENT."]
if (i==1) {
cprob <- prob
} else {
if (as.character(cid) %in% names(models[[i-1]]))
cprob <- models[[i-1]][[as.character(cid)]]
else
cprob <- models[[i-1]][[as.character(parent)]]
}
cprob <- cprob * (1 - (i / total_iterations))
index <- which (domain == all_elites[[i]][elite, param_name])
cprob[index] <- (cprob[index] + (i / total_iterations))
if (irace_results$scenario$elitist) {
cprob <- cprob / sum(cprob)
probmax <- 0.2^(1 / irace_results$parameters$nbVariable)
cprob <- pmin(cprob, probmax)
}
# Normalize probabilities.
cprob <- cprob / sum(cprob)
models[[i]][[as.character(cid)]] <- cprob
for (v in 1:length(domain))
X <- rbind(X, cbind(i, elite, param_name, domain[v], as.character(cprob[v])))
}
}
X <- as.data.frame(X, stringsAsFactors=FALSE)
colnames(X) <-c("iteration", "elite", "parameter", "value", "prob")
X[, "prob"] <- as.numeric(X[, "prob"])
return(X)
}
# Numerical model generation
#
# @description
#
# Rebuild the sampling distribution parameters of the models used by irace to sampling configurations during the configuration process.
#
# @template arg_irace_results
#
# @param param_name
# String, parameter to be included in the plot (example: param_name = "algorithm"))
#
# @return data frame with columns "iteration", "elite", "parameter", "mean", "sd"
#
# @examples
# NULL
# FIXME: Add examples.
getNumericalModel <- function(irace_results, param_name)
{
if (irace_results$parameters$types[[param_name]] %not_in% c("i", "r", "i,log", "r,log")) {
stop("Parameter is not numerical")
}
iterations <- length(irace_results$allElites)
domain <- irace_results$parameters$domain
n_par <- irace_results$parameters$nbVariable
# Get elite data by iteration
all_elites <- list()
for (i in seq_len(iterations)){
all_elites[[i]] <- irace_results$allConfigurations[irace_results$allElites[[i]], param_name]
}
# Get initial model standard deviation
s <- (domain[[param_name]][2] - domain[[param_name]][1])/2
X <- NULL
for (i in 1:(iterations-1)) {
# Get not elite configurations executed in an iteration
it_conf <- unique(irace_results$experimentLog[irace_results$experimentLog[,"iteration"] == (i+1), "configuration"])
new_it_conf <- it_conf[!(it_conf %in% irace_results$allElites[[i]])]
n_conf <- length(new_it_conf)
# Generate updated standard deviation (numerical params)
s <- s * (1/n_conf)^(1/n_par)
for (elite in 1:length(irace_results$allElites[[i]])){
par_mean <- all_elites[[i]][elite]
X <- rbind(X, cbind(i, elite, param_name, par_mean, s))
}
}
X <- as.data.frame(X)
colnames(X) <-c("iteration", "elite", "parameter", "mean", "sd")
X[,"sd"] <- as.numeric(as.character(X[,"sd"]))
X[,"mean"] <- as.numeric(as.character(X[,"mean"]))
rownames(X) <- NULL
return(X)
}
# Plot a categorical model
#
# @description
#
# The `plotCategoricalModel` function creates a stacked bar plot showing
# the sampling probabilities of the parameter values for the elite
# configurations in the iterations of the configuration process.
#
# @param model_data
# String, data frame obtained from the `getCategoricalModel` function
#
# @param domain
# String Vector, domain of the parameter whose model will be plotted
#
# @return bar plot
# FIXME: examples!
plotCategoricalModel <- function(model_data, domain)
{
value <- prob <- elite <- NULL
model_data$elite <- factor(model_data$elite)
p <- ggplot(model_data, aes(fill=value, y=prob, x=elite, group=value)) +
geom_bar(position="stack", stat="identity") +
ggplot2::scale_fill_viridis_d() +
facet_grid(~ iteration, scales = "free", space = "free")
p <- p + labs(x = "Elite configurations", y = "Probability") +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_text(vjust = 4),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
p
}
# Plot a categorical model
#
# @description
#
# Creates a sampling distributions plot of the numerical parameters for the
# elite configurations of an iteration.
#
# This plot shows de density function of the truncated normal distributions
# associated to each parameter for each elite configuration.
#
# @param iteration
# Numeric, iteration that should considered in the plot
#
# @param model_data
# String, data frame obtained from the `getNumericalModel` function
#
# @param domain
# Numeric vector, domain of the parameter whose model will be plotted
#
# @param xlabel_iteration
# Numeric, iteration in which the x axis labels should be included
#
# @return sampling distribution plot
# FIXME: examples!
plotNumericalModel <- function(iteration, model_data, domain, xlabel_iteration)
{
model_data <- model_data[model_data[,"iteration"] == iteration, ]
model_data[,"elite"] <- as.factor(model_data[,"elite"])
x <- seq(from=domain[1], to=domain[2], length.out = 100)
# create plot
p <- ggplot(as.data.frame(x, ncol=1), aes(x=x))
el <- unique(as.character(model_data[,"elite"]))
col <- viridisLite::viridis(length(el))
for (i in 1:length(el)) {
mm <- model_data[model_data[,"elite"] == el[i], "mean"]
ss <- model_data[model_data[,"elite"] == el[i], "sd"]
if(is.na(mm)) next
p <- p + ggplot2::stat_function(fun = truncnorm::dtruncnorm,
geom = "area",
args=list(mean = mm,
sd = ss,
a = domain[1],
b = domain[2]),
color = col[i],
fill = col[i],
xlim = domain,
size = 0.5,
alpha = 0.3)
}
if (xlabel_iteration==iteration){
p <- p + ggplot2::ylab(as.character(iteration+1)) +
theme(axis.title.x = element_blank(),
axis.title.y = element_text(vjust = 0),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
} else {
p <- p + theme(axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y = element_text(vjust = 0),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
p <- p + labs(y = as.character(iteration+1))
}
p <- p + ggplot2::xlim(domain[1], domain[2])
return(p)
}
#' Plot the sampling models used by irace
#'
#' @description
#'
#' Display the sampling models from which irace generated parameter values for
#' new configurations during the configurations process.
#'
#' For categorical parameters a stacked bar plot is created. This plot shows
#' the sampling probabilities of the parameter values for the elite
#' configurations in the iterations of the configuration process.
#'
#' For numerical parameters a sampling distributions plot of the
#' numerical parameters for the elite configurations of an iteration.
#' This plot shows de density function of the truncated normal distributions
#' associated to each parameter for each elite configuration on each iteration.
#'
#' @template arg_irace_results
#'
#' @param param_name
#' String, parameter to be included in the plot, e.g., `param_name = "algorithm"`
#'
#' @template arg_filename
#'
#' @return sampling model plot
#'
#' @examples
#' iraceResults <- read_logfile(system.file(package="irace", "exdata",
#' "irace-acotsp.Rdata", mustWork = TRUE))
#' plot_model(iraceResults, param_name="algorithm")
#' \donttest{
#' plot_model(iraceResults, param_name="alpha")
#' }
#' @export
plot_model <- function(irace_results, param_name, filename=NULL)
{
check_unknown_param_names(param_name, irace_results$parameters$names)
iterations <- length(irace_results$allElites)
if (irace_results$parameters$types[param_name] %in% c("c", "o")) {
X <- getCategoricalModel(irace_results, param_name)
q <- plotCategoricalModel(model_data=X, domain=irace_results$parameters$domain[[param_name]])
} else {
X <- getNumericalModel(irace_results, param_name)
p <- lapply((iterations-1):1, plotNumericalModel, model_data=X,
domain=irace_results$parameters$domain[[param_name]],
xlabel_iteration=1)
q <- do.call("grid.arrange", c(p, ncol = 1, left="Iterations"))
}
if (!is.null(filename))
ggsave(filename, plot = q)
q
}