我正在进行一项比较差分进化和粒子群优化性能的研究,我需要能够将种群点添加到3D图上。我已经审核了Adding points to 3d plot in r,但该解决方案在我的案例中似乎不起作用,我无法弄清楚原因。我正在采用众所周知的G2功能并使用persp3D(),如下所示:
G2 <- function(x) {
if (x[1] >= 0 & x[1] <= 10 & x[2] >= 0 & x[2] <= 10 &
x[1] * x[2] >= 0.75 & x[1] + x[2] <= 15) {
s <- cos(x[1]) ^ 4 + cos(x[2]) ^ 4
p <- 2 * cos(x[1]) ^ 2 * cos(x[2]) ^ 2
r <- sqrt(x[1] ^ 2 + 2 * x[2] ^ 2)
f <- -abs((s - p) / r)
} else {
f <- 0
}
return(f)
}
x = y = seq(0, 10, .1)
data = data.frame(matrix(nrow = length(x), ncol = length(y)))
names(data) = y
rownames(data) = x
#Fill in data.frame with G2 values
for (i in 1:length(x)) {
for (j in 1:length(y)) {
z = G2(c(x[i], y[j]))
data[i,j] = z
}
}
zlim = -range(data)
z = as.matrix(data)
par(mfrow = c(1, 1))
windows()
library(plot3D)
persp3D(z = -z, xlab = "x1", bty = "bl2",
ylab = "x2", zlab = "G2", clab = "depth of G2",
expand = 0.5, d = 2, phi = 20, theta = 30, resfac = 2,
image = TRUE,
contour = list(col = "grey", side = c("zmin", "z")),
zlim = zlim, colkey = list(side = 1, length = 0.5))
所有这一切都运作良好,我得到了我正在寻找的情节。但是,当我尝试添加点时,如上面引用的解决方案中所建议的那样,我的3D绘图将重新生成为只有一个红点的图:
points3D(1, 1, G2(c(1,1)), col = "red", size = 30)
如何将此点添加到现有的persp3D()图中?另外,我可以控制点的添加位置(对于等高线图或3D图像有意义吗?我的意图是随着算法的发展添加群体,并创建GIF以便于可视化,但我不想偏离点,所以我通过尝试将这一点添加到情节来简化。感谢您的帮助!