gagolews / lmlcr

Lightweight Machine Learning Classics with R (Book Draft)
https://lmlcr.gagolewski.com/
Other
16 stars 3 forks source link

Get rid of Ch.8, add an exercise with DEoptim and BFGS + k-means on unbalance #42

Closed gagolews closed 3 years ago

gagolews commented 4 years ago

Storn R, Price K (1997). “Differential Evolution – A Simple and Efficient Heuristic for Global Optimization over Continuous Spaces.”Journal of Global Optimization,11(4), 341–359.

Mullen K, Ardia D, Gil D, Windover D, Cline J (2011). “DEoptim: An R Package for GlobalOptimization by Differential Evolution.”Journal of Statistical Software,40(6), 1–26. URLhttp://www.jstatsoft.org/v40/i06/

X <- as.matrix(read.csv("datasets/sipu_unbalance.csv",
    header=FALSE, sep=" ", comment.char="#",
    stringsAsFactors=FALSE))
X <- X/10000-30 # a more user-friendly scale
K <- 8
p <- 2

library("FNN")
get_fitness <- function(mu, X) {
    # For each point in X,
    # get the index of the closest point in mu:
    memb <- FNN::get.knnx(mu, X, 1)$nn.index

    # compute the sum of squared distances
    # between each point and its closes cluster centre:
    sum((X-mu[memb,])^2)
}

library("DEoptim")
obj <- function(mu) {
            get_fitness(matrix(mu, nrow=K), X)
        }
res <- DEoptim(fn=obj,
        lower=rep(apply(X, 2, min), each=K),
        upper=rep(apply(X, 2, max), each=K)
        #control=list(itermax=1000)
        )

mu_res <- matrix(res$optim$bestmem, nrow=K)
plot(X)
points(mu_res, col=2,cex=3)
get_fitness(mu_res, X)
km <- kmeans(X, mu_res)
get_fitness(km$centers, X)

cntr <- matrix(ncol=2, byrow=TRUE, c( # initial guess
   -15,   5,
   -12,   10,
   -10,   5,
    15,   0,
    15,   10,
    20,   5,
    25,   0,
    25,   10))
km <- kmeans(X, cntr)
get_fitness(km$centers, X)

km <- kmeans(X, K, nstart=10)
get_fitness(km$centers, X)
gagolews commented 4 years ago

the framework is there in ch.8

gagolews commented 4 years ago

you can also show that hydroPSO and SANN don't give too good solutions

gagolews commented 3 years ago

(the to-do list has moved)