[Returnanalytics-commits] r2548 - pkg/PortfolioAnalytics/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Jul 12 00:49:08 CEST 2013
Author: rossbennett34
Date: 2013-07-12 00:49:08 +0200 (Fri, 12 Jul 2013)
New Revision: 2548
Modified:
pkg/PortfolioAnalytics/R/optimize.portfolio.R
Log:
adding GenSA method to optimize.portfolio_v2
Modified: pkg/PortfolioAnalytics/R/optimize.portfolio.R
===================================================================
--- pkg/PortfolioAnalytics/R/optimize.portfolio.R 2013-07-11 22:44:31 UTC (rev 2547)
+++ pkg/PortfolioAnalytics/R/optimize.portfolio.R 2013-07-11 22:49:08 UTC (rev 2548)
@@ -807,6 +807,50 @@
} ## end case for pso
+ ## case if method=GenSA---Generalized Simulated Annealing
+ if(optimize_method=="GenSA"){
+ stopifnot("package:GenSA" %in% search() || require("GenSA",quietly = TRUE) )
+ if(hasArg(maxit)) maxit=match.call(expand.dots=TRUE)$maxit else maxit=N*50
+ controlGenSA <- list(maxit = 5000, threshold.stop = NULL, temp = 5230,
+ visiting.param = 2.62, acceptance.param = -5, max.time = NULL,
+ nb.stop.improvement = 1e+06, smooth = TRUE, max.call = 1e+07,
+ verbose = FALSE)
+ GenSAcargs <- names(controlGenSA)
+
+ if( is.list(dotargs) ){
+ pm <- pmatch(names(dotargs), GenSAcargs, nomatch = 0L)
+ names(dotargs[pm > 0L]) <- GenSAcargs[pm]
+ controlGenSA$maxit <- maxit
+ controlGenSA[pm] <- dotargs[pm > 0L]
+ if(hasArg(trace) && try(trace==TRUE,silent=TRUE)) controlGenSA$verbose <- TRUE
+ }
+
+ upper <- constraints$max
+ lower <- constraints$min
+
+ minw = try(GenSA( par = rep(1/N, N), lower = lower[1:N] , upper = upper[1:N], control = controlGenSA,
+ fn = constrained_objective_v2 , R=R, portfolio=portfolio)) # add ,silent=TRUE here?
+
+ if(inherits(minw,"try-error")) { minw=NULL }
+ if(is.null(minw)){
+ message(paste("Optimizer was unable to find a solution for target"))
+ return (paste("Optimizer was unable to find a solution for target"))
+ }
+
+ weights <- as.vector(minw$par)
+ weights <- normalize_weights(weights)
+ names(weights) <- colnames(R)
+
+ out = list(weights=weights,
+ objective_measures=constrained_objective_v2(w=weights, R=R, portfolio=portfolio, trace=TRUE)$objective_measures,
+ out=minw$value,
+ call=call)
+ if (isTRUE(trace)){
+ out$GenSAoutput=minw
+ }
+
+ } ## end case for GenSA
+
# Prepare for final object to return
end_t <- Sys.time()
# print(c("elapsed time:",round(end_t-start_t,2),":diff:",round(diff,2), ":stats: ", round(out$stats,4), ":targets:",out$targets))
More information about the Returnanalytics-commits
mailing list