Skip to content

Instantly share code, notes, and snippets.

@jakob-r
Last active August 29, 2015 14:27
Show Gist options
  • Save jakob-r/046aaec40f123cdc5016 to your computer and use it in GitHub Desktop.
Save jakob-r/046aaec40f123cdc5016 to your computer and use it in GitHub Desktop.
library(checkmate)
library(mlr)
makePersp3DLayer = function(opt.path.dt, par.set = NULL, learner, y = "y", x = NULL, resolution = 100, nrow.limit = 400) {
x = coalesce(x, getParamIds(par.set))
assertCharacter(x, len = 2)
data = opt.path.dt[, c(x,y), with=FALSE]
if (nrow(opt.path.dt) > nrow.limit) {
data = data[sample(nrow(data), size = nrow.limit),]
}
task.3d = makeRegrTask(id = "3d", data = data , target = y)
new.x = ParamHelpers::generateGridDesign(par.set = par.set, resolution = resolution)
m = train(learner, task.3d) #, subset = sample(nrow(plot.hr), size = 500))
p = predict(m, newdata = new.x)
list(x = new.x,
z = p$data$response,
z2 = p$data$se,
par.set = par.set,
z.name = y,
x.name = x[1],
y.name = x[2],
x.real = data[, x, with = FALSE],
z.real = data[, y, with = FALSE])
}
drawPersp3DLayer = function(persp3D.layer, include.points = FALSE, points.col = "black", ...) {
x = unique(persp3D.layer$x[,1])
y = unique(persp3D.layer$x[,2])
z = persp3D.layer$z
col = plotrix::color.scale(persp3D.layer$z2,c(0,1,1),c(1,1,0),0)
rgl::persp3d(x = x, y = y, z = z, col = col, ...)
if (include.points) {
rgl::points3d(x = persp3D.layer$x.real[[1]], y = persp3D.layer$x.real[[2]], z = persp3D.layer$z.real[[1]], add = TRUE, col = points.col)
}
title3d(xlab = persp3D.layer$x.name, ylab = persp3D.layer$y.name, zlab = persp3D.layer$z.name)
}
library(rgl)
p3dl.low = makePersp3DLayer(res.dt[algo == "smbo.high" & data == "electricity" & fold == 1], par.set, learner)
p3dl.high = makePersp3DLayer(res.dt[algo == "rs.high" & data == "electricity" & fold == 1], par.set, learner)
p3dl.high.time = makePersp3DLayer(res.dt[algo == "rs.high" & data == "electricity" & fold == 1], par.set, y = "exec.time", learner)
open3d(); drawPersp3DLayer(p3dl.low, alpha = 0.3); drawPersp3DLayer(p3dl.high, alpha = 0.3, add = TRUE, include.points = TRUE)
plotMfLayers = function(opt.path.dt, par.set, learner, resolution = 100, ...) {
mf.opt.path.dt = split(opt.path.dt, f = opt.path.dt$.multifid.lvl)
p3dls = lapply(mf.opt.path.dt, makePersp3DLayer, par.set = par.set, learner = learner, resolution = resolution)
rgl::open3d()
for (i in seq_along(p3dls)) {
rgl::drawPersp3DLayer(p3dls[[i]], add = i > 1, points.col = i, ...)
}
}
animateMfPoints = function(opt.path.dt, par.set, x = NULL) {
x = coalesce(x, getParamIds(par.set))
mf.lvl = max(opt.path.dt$.multifid.lvl)
mf.color = RColorBrewer::brewer.pal(n = mf.lvl, name = "Spectral")
legend3d("topright", legend = paste('lvl', seq_len(mf.lvl)), pch = 16, col = mf.color, cex=1, inset=c(0.02))
for (dob in unique(opt.path.dt$dob)) {
sub.op = opt.path.dt[eval(opt.path.dt$dob == dob),]
#if (nrow(sub.op) == 1) catf("Point on level %i x = (%d, %d), y = %s": sub.op$.multifid.lvl[[1]], sub.op[[x[1]]][[1]], sub.op[[x[2]]][[1]], sub.op$y[[1]])
rgl::points3d(x = sub.op[[x[1]]], y = sub.op[[x[2]]], z = sub.op$y, col = mf.color[sub.op$.multifid.lvl], size = 6)
Sys.sleep(0.5)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment