Created
May 25, 2017 00:37
-
-
Save slwu89/c990c1e11fe736ed13fc60e56cc37010 to your computer and use it in GitHub Desktop.
Optimization with constraints via parameter transform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################# | |
# Optimization with constraints via parameter transform | |
# Sean Wu | |
############################################################# | |
# same as qlogis | |
logit <- function(x){ | |
log(x/(1-x)) | |
} | |
# same as plogis | |
invlogit <- function(x){ | |
exp(x)/(1+exp(x)) | |
} | |
# our target function to optimize; it has global minimum at x = 0 for all dimensions | |
griewank <- function(xx){ | |
xx <- invlogit(xx) # constrain all parameters to lie between 0,1 | |
if(any(xx > 1 | xx < 0)){ | |
stop("transformed parameters lie outside [0,1]") | |
} | |
ii <- c(1:length(xx)) | |
sum <- sum(xx^2/4000) | |
prod <- prod(cos(xx/sqrt(ii))) | |
y <- sum - prod + 1 | |
return(y) | |
} | |
optimOut = optim(par = invlogit(c(-5,5)),fn = griewank,method = "Nelder-Mead") | |
invlogit(optimOut$par) # the real global minima is at 0,0; it's very small here, just as we would expect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment