diff --git a/ackley_plot_for_handout.r b/ackley_plot_for_handout.r new file mode 100644 index 0000000000000000000000000000000000000000..476cb20bc0d307aa0bbb3ec73e60de8ca5192037 --- /dev/null +++ b/ackley_plot_for_handout.r @@ -0,0 +1,31 @@ +# This only works in two dimensions, but it is enough for the Uebung +# description +ackley <- function(x, y, a, b, c) { + d <- 2 + + sum1 <- x * x + y * y + sum2 <- cos(c * x) + cos(c * y) + + term1 <- -a * exp(-b * sqrt(sum1 / d)) + term2 <- -exp(sum2 / d) + y <- term1 + term2 + a + exp(1) + return(y) +} + +mymain <- function() { + a <- 20; b <- 0.2; c <- 2 * pi; # defaults + x <- seq(from = -4, to = 4, length.out = 200) + z <- outer(X = x, Y = x, FUN = ackley, a, b, c) + + lim <- c(min(x), max(x)) + if (T) { + png(filename = "ackley2d.png", bg = "white") + } + persp(x = x, y = x, z = z, + xlab = "x1", ylab = "x2", zlab = "ackley", + theta = -20, + ticktype = "detailed") + dev.off() +} + +mymain() diff --git a/examples/plot1.r b/examples/plot1.r index bb80a7c94be0b8fbcbd6f2f906b4dc5b6f1149f8..063d7f2d6ae23f1874ed2d20d34bf7a29ed9d279 100644 --- a/examples/plot1.r +++ b/examples/plot1.r @@ -1,12 +1,15 @@ -# Let us see if we can make a 2D histogram +# Make a 2D plot of some example results. +# Do not use default axis scaling. It is determined by the most extreme points +# visited and there are always a few outliers. Set the boundaries manually with +# plotmin and plotmax. input_file <- "ex2.csv" plotmin <- -5 plotmax <- 5 -getxy <- function (fname) { +getxy <- function(fname) { csvnames <- c("step", "fval", "temp", "x1", "x2") df <- read.csv(input_file, header = F, col.names = csvnames) - df <- df[c("x1","x2")] + df <- df[c("x1", "x2")] return(df) } @@ -23,23 +26,23 @@ mymain <- function() { df <- enforce_limits(df) nbins <- 100 - if (F) { - x.bin <- seq(floor(min(df$x1)), ceiling(max(df$x1)), length = nbins) - y.bin <- seq(floor(min(df$x2)), ceiling(max(df$x2)), length = nbins) + if (F) { # This would be default scaling, but it looks awful. + x_bin <- seq(floor(min(df$x1)), ceiling(max(df$x1)), length = nbins) + y_bin <- seq(floor(min(df$x2)), ceiling(max(df$x2)), length = nbins) } else { - x.bin <- seq(-5,5,length = nbins) - y.bin <- seq(-5,5,length = nbins) + x_bin <- seq(-5, 5, length = nbins) + y_bin <- seq(-5, 5, length = nbins) } - freq <- as.data.frame(table(findInterval(df$x1, x.bin),findInterval(df$x2, y.bin))) - freq[,1] <- as.numeric(freq[,1]) - freq[,2] <- as.numeric(freq[,2]) + freq <- as.data.frame(table(findInterval(df$x1, x_bin), findInterval(df$x2, y_bin))) + freq[, 1] <- as.numeric(freq[, 1]) + freq[, 2] <- as.numeric(freq[, 2]) - freq2D <- diag(nbins)*0 - freq2D[cbind(freq[,1], freq[,2])] <- freq[,3] - par(mfrow=c(1,2)) - image(x.bin, y.bin, freq2D, col=topo.colors(max(freq2D))) - contour(x.bin, y.bin, freq2D, add=TRUE, col=rgb(1,1,1,.7)) + freq2D <- diag(nbins) * 0 + freq2D[cbind(freq[, 1], freq[, 2])] <- freq[, 3] + par(mfrow=c(1, 2)) + image(x_bin, y_bin, freq2D, col=topo.colors(max(freq2D))) + contour(x_bin, y_bin, freq2D, add=TRUE, col=rgb(1,1,1,.7)) palette(rainbow(max(freq2D))) cols <- (freq2D[-1,-1] + freq2D[-1,-(nbins-1)] + freq2D[-(nbins-1),-(nbins-1)] + freq2D[-(nbins-1),-1])/4 diff --git a/function.docx b/function.docx deleted file mode 100644 index c7e25b61d7bd7c4dee71d2c9f7e30e9b056deb49..0000000000000000000000000000000000000000 Binary files a/function.docx and /dev/null differ diff --git a/uebung_ackley.docx b/uebung_ackley.docx new file mode 100644 index 0000000000000000000000000000000000000000..1e5f34c36573b5488c3f94708042f931f406acd5 Binary files /dev/null and b/uebung_ackley.docx differ