├── .Rbuildignore ├── .editorconfig ├── .github └── workflows │ └── ci.yaml ├── .gitignore ├── COPYRIGHTS ├── ChangeLog ├── DESCRIPTION ├── NAMESPACE ├── R ├── DEoptim.R ├── RcppExports.R ├── methods.R └── zzz.R ├── README.md ├── benchmark.txt ├── cleanup ├── demo ├── 00Index ├── DEoptim.R ├── c++ │ └── compiledFunctions.cpp ├── compiled.R ├── environment.R ├── large.R └── small.R ├── inst └── DEoptim-docs │ ├── CHANGES │ ├── NEWS │ ├── README │ └── THANKS ├── local └── dot.travis.yml ├── man ├── DEoptim-methods.Rd ├── DEoptim.Rd └── DEoptim.control.Rd ├── scripts ├── benchmark.r ├── bigBenchmark.r ├── check.r ├── compBenchmark.r └── profile.r ├── src ├── Makevars ├── RcppExports.cpp ├── deoptim.cpp ├── devol.cpp ├── evaluate.h ├── exampleFunctions.cpp └── permute.cpp ├── tests ├── compTest.R └── rosenbrock.R └── vignettes ├── RcppDE.Rnw ├── RcppDE.bib └── jsslogo.jpg /.Rbuildignore: -------------------------------------------------------------------------------- 1 | benchmark.R 2 | inst/doc/figures/ 3 | inst/scripts/check2.r 4 | inst/doc/jss.bst 5 | inst/doc/jss.cls 6 | ^.*\.Rproj$ 7 | ^\.Rproj\.user$ 8 | \.travis\.yml$ 9 | benchmark.txt 10 | COPYRIGHTS 11 | scripts 12 | .*\.tar\.gz$ 13 | ^\.github 14 | ^travis-tool\.sh$ 15 | ^run\.sh$ 16 | ^travis_wait_.*\.log$ 17 | ^\.editorconfig$ 18 | ^local 19 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | end_of_line = lf 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | # Matches multiple files with brace expansion notation 13 | # 4 space indentation 14 | [*.{c,cpp,h,hpp,R,r}] 15 | indent_style = space 16 | indent_size = 4 17 | 18 | # Tab indentation (no size specified) 19 | [Makefile] 20 | indent_style = tab 21 | 22 | -------------------------------------------------------------------------------- /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | # Run CI for R using https://eddelbuettel.github.io/r-ci/ 2 | 3 | name: ci 4 | 5 | on: 6 | push: 7 | pull_request: 8 | 9 | env: 10 | _R_CHECK_FORCE_SUGGESTS_: "false" 11 | 12 | jobs: 13 | ci: 14 | strategy: 15 | matrix: 16 | include: 17 | #- {os: macOS-latest} 18 | - {os: ubuntu-latest} 19 | 20 | runs-on: ${{ matrix.os }} 21 | 22 | steps: 23 | - name: Checkout 24 | uses: actions/checkout@v4 25 | 26 | - name: Setup 27 | uses: eddelbuettel/github-actions/r-ci@master 28 | 29 | - name: Dependencies 30 | run: ./run.sh install_deps 31 | 32 | - name: Test 33 | run: ./run.sh run_tests 34 | 35 | #- name: Coverage 36 | # if: ${{ matrix.os == 'ubuntu-latest' }} 37 | # run: ./run.sh coverage 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | src/*.o 5 | src/*.so 6 | src/*.dll 7 | local/ 8 | scripts/ 9 | -------------------------------------------------------------------------------- /COPYRIGHTS: -------------------------------------------------------------------------------- 1 | COPYRIGHT STATUS 2 | ---------------- 3 | 4 | This code is 5 | 6 | Copyright (C) 2009 David Ardia and Katharine Mullen 7 | 8 | All code is subject to the GNU General Public License, Version 2. See 9 | the file COPYING for the exact conditions under which you may 10 | redistribute it. 11 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | 2025-03-17 Dirk Eddelbuettel 2 | 3 | * .github/workflows/ci.yaml: Switch to r-ci with implicit bootstrap 4 | 5 | 2025-02-18 Dirk Eddelbuettel 6 | 7 | * DESCRIPTION (Version, Date): Release 0.1.8 8 | 9 | * README.md: Remove ancient TravisCI badge, add r-universe and 10 | dependency badges 11 | 12 | 2024-10-17 Dirk Eddelbuettel 13 | 14 | * DESCRIPTION (Authors@R): Added 15 | 16 | 2024-10-07 Dirk Eddelbuettel 17 | 18 | * DESCRIPTION (Version, Date): Roll micro version 19 | 20 | * src/devol.cpp (devol): Switch three integer vectors from 21 | arma::icolvec to std::vector for ARMA_64BIT_WORD compilation 22 | 23 | 2024-03-01 Dirk Eddelbuettel 24 | 25 | * .github/workflows/ci.yaml (jobs): Update to actions/checkout@v4, 26 | add r-ci-setup actions 27 | 28 | 2023-04-06 Dirk Eddelbuettel 29 | 30 | * DESCRIPTION (Version, Date): Roll micro version 31 | 32 | * src/Makevars: No longer set a C++ standard 33 | 34 | 2022-12-18 Dirk Eddelbuettel 35 | 36 | * DESCRIPTION (Version, Date): Release 0.1.7 37 | 38 | * src/evaluate.h: Move helper function from devol.cpp 39 | * src/devol.cpp: Idem, remove ancient Armadillo define 40 | 41 | * src/evaluate.h: Update function signatures to use NumericVector 42 | * src/RcppExports.cpp: Idem 43 | * R/RcppExport.R: Idem 44 | 45 | * src/exampleFunctions.cpp: Moved out of evaluate.h 46 | 47 | 2022-12-17 Dirk Eddelbuettel 48 | 49 | * src/evaluate.h: Compiled function interface back to consistently 50 | being single argument of parameter vector 51 | * src/devol.cpp: Idem 52 | 53 | * man/DEoptim.Rd: Use https, remove dead link to code.html 54 | * man/DEoptim.control.Rd: Use https 55 | * man/DEoptim-methods.Rd: Idem 56 | 57 | * .editorconfig: Added 58 | 59 | 2022-12-15 Dirk Eddelbuettel 60 | 61 | * DESCRIPTION (Version, Date): Roll minor version 62 | 63 | 2022-12-08 Max Coulter 64 | 65 | * src/devol.cpp: Additions to crossover weights from DEoptim 66 | 67 | 2022-12-02 Dirk Eddelbuettel 68 | 69 | * DESCRIPTION (Version, Date): Roll minor version 70 | 71 | 2022-11-30 Max Coulter 72 | 73 | * src/devol.cpp: Fix to switch statement strategy 6 74 | 75 | 2022-11-14 Dirk Eddelbuettel 76 | 77 | * .github/workflows/ci.yaml (jobs): Update to actions/checkout@v3 78 | 79 | 2021-05-08 Dirk Eddelbuettel 80 | 81 | * DESCRIPTION (URL, BugReports): Added links 82 | 83 | 2021-01-05 Dirk Eddelbuettel 84 | 85 | * .github/workflows/ci.yaml: Add CI runner using r-ci 86 | * README.md: Add new badge 87 | 88 | 2020-07-31 Dirk Eddelbuettel 89 | 90 | * .travis.yml: Update to 'bionic' and R 4.0.* 91 | 92 | 2019-09-24 Dirk Eddelbuettel 93 | 94 | * DESCRIPTION (Version, Date): Roll minor version 95 | 96 | * src/evaluate.h: Use Rcpp::Shield for Rf_lang3() and Rf_eval() 97 | 98 | 2018-06-09 Dirk Eddelbuettel 99 | 100 | * DESCRIPTION (Version, Date): Release 0.1.6 101 | 102 | 2018-06-07 Kevin Ushey 103 | 104 | * src/deoptim.cpp: disable RNG synchronization with dev. Rcpp 105 | * tests/rosenbrock.R: add test 106 | 107 | 2018-03-03 Dirk Eddelbuettel 108 | 109 | * tests/compTest.R: Condition comparison on (optional) DEoptim 110 | package actually being available 111 | 112 | 2017-08-26 Dirk Eddelbuettel 113 | 114 | * NAMESPACE: Use .registration=TRUE 115 | * src/devol.cpp (putFunPtrInXPtr): Use Rcpp::export on test function 116 | * src/RcppExports.cpp: Updated 117 | * R/RcppExports.R: Idem 118 | 119 | * tests/compTest.R (runPair): Update test 120 | 121 | * src/Makevars (PKG_LIBS): Update, added OpenMP flags, use C++11 122 | 123 | * cleanup: Minor edits 124 | 125 | * .travis.yml: Updated to using run.sh from r-travis 126 | 127 | 2016-02-07 Dirk Eddelbuettel 128 | 129 | * src/devol.cpp: Use R:: namespace for scalar rnorm, rcauchy calls 130 | 131 | 2016-02-06 Yi Kang 132 | 133 | * src/devol.cpp: Support JADE randomization for crossover and weight 134 | * R/DEoptim.R: Idem 135 | * R/DEoptim.control.R: Idem 136 | * man/DEoptim.control.Rd: Idem 137 | 138 | 2016-02-03 Dirk Eddelbuettel 139 | 140 | * src/devol.cpp (devol): Use std::abs() 141 | 142 | * man/DEoptim.control.Rd: Updated to mention d_reltol, d_steptol 143 | 144 | 2016-02-02 Dirk Eddelbuettel 145 | 146 | * src/devol.cpp: Revert Jan 21 change for -std=c++14; ensure 147 | ARMA_32BIT_WORD is defined so that Armadillo will always integers we 148 | can interchange with R (whereas C++11/14 would lead to 64 bit) 149 | 150 | 2016-01-25 Yi Kang 151 | 152 | * src/deoptim.cpp: Add d_reltol, d_steptol for convergence criterion, 153 | remove i_check_winner, i_av_winner 154 | * src/devol.cpp: Idem; also switch memcpy() to memmove() 155 | * R/DEoptim.R: Corresponding changes on R side 156 | 157 | 2016-01-22 Dirk Eddelbuettel 158 | 159 | * DESCRIPTION (Version): Release 0.1.5 160 | 161 | 2016-01-21 Dirk Eddelbuettel 162 | 163 | * src/devol.cpp (devol): Adjust for -std=c++14 164 | 165 | 2015-09-05 Dirk Eddelbuettel 166 | 167 | * DESCRIPTION: Release 0.1.4 168 | 169 | * vignettes/RcppDE.Rnw: Fix another typo 170 | 171 | 2015-09-04 Dirk Eddelbuettel 172 | 173 | * src/deoptim.cpp: Use Rcpp Attributes 174 | * src/devol.cpp: Ditto, remove extra RNGScope 175 | * src/permute.cpp: Ditto, remove extra RNGScope 176 | * src/evaluate.h: Ditto, remove extra RNGScope 177 | * src/Makevars: Updated 178 | * R/DEoptim: Reflect Attributes interface 179 | 180 | 2015-08-23 Dirk Eddelbuettel 181 | 182 | * DESCRIPTION: Release 0.1.3 183 | 184 | * vignettes/RcppDE.Rnw: Remove a spurious } in VignetteIndexEntry 185 | file (with thanks to Kurt Hornik for spotting this) 186 | 187 | * man/DEoptim.Rd: Updated URL to Storn's page [R CMD check] 188 | 189 | * DESCRIPTION: Minor edits [R CMD check] 190 | 191 | * demo/c++/: Renamed subdirectory to C++ 192 | * demo/compiled.R: Adjust path to c++/compiledFunctions.cpp 193 | * demo/00Index: Minor edits 194 | 195 | 2014-12-17 Dirk Eddelbuettel 196 | 197 | * DESCRIPTION: Removed Depends: RcppArmadillo 198 | * DESCRIPTION: Changed Depends: Rcpp to Imports: Rcpp 199 | * NAMESPACE: Added imports(Rcpp) 200 | 201 | * demo/small.R: Renamed from SmallBenchmark.R 202 | * demo/large.R: Renamed from LargeBenchmark.R 203 | * demo/compiled.R: Renamed from CompiledBenchmark.R 204 | * cpp/compiledFunctions.R: Moved down one directory 205 | 206 | 2014-05-26 Dirk Eddelbuettel 207 | 208 | * demo/CompiledBenchmark.R: Rewritten to use Rcpp Attributes 209 | * demo/compiledFunctions.cpp: New file, C++ parts of CompiledBench. 210 | 211 | 2013-12-12 Dirk Eddelbuettel 212 | 213 | * DESCRIPTION: Release 0.1.2 214 | 215 | * vignettes/*: Moved files from inst/doc/ 216 | 217 | * .travis.yml: Added for Github / Travis support 218 | * .gitignore: Added for git support 219 | 220 | * R/zzz.R: Startup message given by .onAttach() rather than .onLoad() 221 | 222 | * src/deoptim.cpp (DEoptim): Added a static_cast<> to make g++-4.8 happy 223 | * src/devol.cpp (devol): Idem 224 | 225 | * .Rbuildignore: Exclude a few more development files from tarball 226 | 227 | 2012-04-08 Dirk Eddelbuettel 228 | 229 | * DESCRIPTION: Release 0.1.1 230 | 231 | * tests/compTest.R: With the just-released DEoptim 2.2.0, results are 232 | no longer identical so we commented-out the stopifnot() comparison to 233 | not break any automated tests (as requested by the CRAN maintainers) 234 | 235 | * .Rbuildignore: Added a few more files which R CMD check does not 236 | want to see in the tarball 237 | 238 | 2011-03-07 Dirk Eddelbuettel 239 | 240 | * src/evaluate.h: Also reflect '...' argument from R function we pass 241 | in, with thanks to Josh Ulrich for the one-line patch 242 | 243 | * R/DEoptim.R: No longer pass environment 'env' down 244 | * man/DEoptim.Rd: No longer document now unused 'env' 245 | 246 | * src/deoptim.cpp: Minor tweak to RcppArmadillo object creation 247 | 248 | 2010-12-12 Dirk Eddelbuettel 249 | 250 | * DESCRIPTION: Release 0.1.0 251 | 252 | * inst/doc/RcppDE.Rnw: Finished first version of vignette 253 | 254 | 2010-12-09 Dirk Eddelbuettel 255 | 256 | * demo/environment.R: Added new demo to show environment use 257 | 258 | 2010-11-16 Dirk Eddelbuettel 259 | 260 | * man/*.Rd: Added Author note for RcppDE. 261 | 262 | 2010-11-06 Dirk Eddelbuettel 263 | 264 | * src/evaluate.h: Converted from evaluate.cpp, now contains abstract 265 | base class for evaluation as well as two implementations for the old 266 | standard (using a function and environment from R) as well as new one 267 | using external pointers passing a function pointer to a function with 268 | signature double (*fun)(SEXP); also counts nb of calls inside class 269 | * src/devol.cpp: Adapted to use the new framework 270 | 271 | * demo/CompiledBenchmark: New benchmark demo showcasing new approach 272 | * compBenchmark: New frontend script 273 | 274 | 2010-11-04 Dirk Eddelbuettel 275 | 276 | * src/deoptim.cpp: Pass fn and rho through to devol() 277 | 278 | * R/DEoptim.R: Make 'env' an optional argument, don't redefine fn() 279 | * man/DEoptim.Rd: Document 'env' argument 280 | 281 | 2010-10-31 Dirk Eddelbuettel 282 | 283 | * src/Makevars: add new define for compilation with OpenMP 284 | 285 | * src/devolMP.cpp: New OpenMP-using variants -- with issues as we 286 | cannot simple multithread around the single-threaded R instance 287 | * src/permuteMP.cpp: New OpenMP-using variant -- without actual 288 | speedup as the task in the permutation loop is so simple that the 289 | multithreading overhead eats any parallelisation gains 290 | 291 | * src/devol.cpp: Added #ifdef to disable if OpenMP is used 292 | * src/permute.cpp: Added #ifdef to disable if OpenMP is used 293 | 294 | * openmp.r: Added test script for OpenMP usage 295 | 296 | 2010-10-30 Dirk Eddelbuettel 297 | 298 | * src/deoptim.cpp: Renamed from de4_0.cpp; function renamed DEoptim 299 | * R/DEoptim.R: Call renamed function "DEoptim" 300 | 301 | * src/devol.cpp: Split function devol() off into its own file 302 | * src/permute.cpp: Split function devol() off into its own file 303 | 304 | * profile.r: Simple front-end script used for profiling runs 305 | 306 | * src/evaluate.cpp: Slight performance increase and simplification 307 | 308 | * benchmark.txt: Updated benchmark results, including one on OS X 309 | 310 | 2010-10-30 Dirk Eddelbuettel 311 | 312 | * benchmark.r: Now calls demo/benchmark.r, minor edits 313 | * bigBenchmark.r: Now calls demo/bigBenchmark.r, minor edits 314 | 315 | 2010-10-27 Dirk Eddelbuettel 316 | 317 | * benchmark.txt: Updated benchmark results, including one on OS X 318 | * src/de4_0.cpp: Minor edits 319 | 320 | 2010-10-26 Dirk Eddelbuettel 321 | 322 | * src/de4_0.cpp: 323 | - corrected sortindex error for strat=6 324 | - corrected inner loop when bs=TRUE 325 | 326 | * DESCRIPTION: explicit Depends: on RcppArmadillo (>= 0.2.8) 327 | 328 | 2010-10-19 Dirk Eddelbuettel 329 | 330 | * R/DEoptim.R: 331 | - reduce storepop list by one to match DEoptim behaviour 332 | - set row and column names 333 | 334 | * src/de4_0.cpp: 335 | - Removed a few more unused variables 336 | - moved some variable declaration down to where they are use 337 | - turned one do-while into for(;;) loop 338 | - replaced macros with proper try/catch block 339 | - made a few more variables local 340 | 341 | 2010-10-18 Dirk Eddelbuettel 342 | 343 | * src/de4_0.cpp: 344 | - Switched from NP in rows to cols which with Armadillo's internal 345 | organisation gets a speed up for larger parameter vectors 346 | (starting around n=10) 347 | - d_storepop is now a Rcpp::List, filled directly with matrices 348 | - the strategy if/elseif/else tree is now a switch() 349 | - use static_cast(::unif_rand()) 350 | - use ::unif_rand() to show this is a 'global' R namespace object 351 | * R/DEoptim.R: No longer need to create list of populations by hand 352 | 353 | * src/evaluate.cpp: Simplied using REAL() to get to pointer 354 | * demo/SmallBenchmark.R: Added 355 | * demo/LargeBenchmark.R: Added 356 | 357 | 2010-10-16 Dirk Eddelbuettel 358 | 359 | * src/de4_0.cpp: 360 | - converted to Armadillo use throughout 361 | - initialpop is a matrix (not vector) on entry 362 | - d_pop is a matrix too 363 | - removed a number of 'element copying' loop 364 | 365 | * R/DEoptim.R: changed to send and receive matrices rather than 366 | vectors that need to be recast to matrix 367 | 368 | 2010-10-15 Dirk Eddelbuettel 369 | 370 | * src/de4_0.cpp (permute): temp. urn vector now allocated above and passed 371 | 372 | * src/Makevars: Added, to permit linking against Rcpp 373 | 374 | * src/de4_0.cpp: Converted to C++ from C, added minimal first set of Rcpp 375 | * src/get_element.c: Removed as no longer needed with Rcpp 376 | 377 | * benchmark.txt: Added log of results 378 | 379 | * benchmark.r: Added simple benchmark comparison to DEoptim 380 | 381 | * data/xrrData.rda, data.SMI.rda: Removed in this package as unused 382 | * man/xrrData.Rd, man/SMI.Rd: Removed in this package as unused 383 | 384 | * src/de4_0.c (devol): removed unused variable 'x' 385 | 386 | * cleanup: Added cleanup script 387 | 388 | 2010-10-14 Dirk Eddelbuettel 389 | 390 | * NAMESPACE, DESCRIPTION: New name is RcppDE to differentiate from DEoptim 391 | * Top-level files CHANGES, COPYING, NEWS, README, THANKS unchanged 392 | 393 | * inst/CITATION: Also removed 394 | 395 | * inst/doc/*: Removed DEoptim papers for this variant 396 | 397 | * demo/DEoptim.R: Removed unused parameters 'refresh' + 'digits' 398 | 399 | * R/zzz.R: Switch to 'packageStartupMessage()' which can suppressed 400 | 401 | * Initial version based on SVN revision 27 of DEoptim 402 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: RcppDE 2 | Version: 0.1.8 3 | Date: 2025-02-18 4 | Title: Global Optimization by Differential Evolution in C++ 5 | Authors@R: c(person("Dirk", "Eddelbuettel", role = c("aut", "cre"), email = "edd@debian.org", 6 | comment = c(ORCID = "0000-0001-6419-907X")), 7 | person("David", "Ardia", role = "aut", comment = "Author of DEoptim"), 8 | person("Katharine", "Mullen", role = "aut", comment = "Author of DEoptim"), 9 | person("Brian", "Peterson", role = "aut", comment = "Author of DEoptim"), 10 | person("Joshua", "Ulrich", role = "aut", comment = "Author of DEoptim"), 11 | person("Kris", "Boudt", role = "ctb", comment = "Contributor to DEoptim"), 12 | person("Rainer", "Storn", role = "aut", comment = "Principal author of DE-Engine")) 13 | Description: An efficient C++ based implementation of the 'DEoptim' 14 | function which performs global optimization by differential evolution. 15 | Its creation was motivated by trying to see if the old approximation "easier, 16 | shorter, faster: pick any two" could in fact be extended to achieving all 17 | three goals while moving the code from plain old C to modern C++. The 18 | initial version did in fact do so, but a good part of the gain was due to 19 | an implicit code review which eliminated a few inefficiencies which have 20 | since been eliminated in 'DEoptim'. 21 | License: GPL (>= 2) 22 | Imports: Rcpp 23 | Suggests: inline, DEoptim, lattice 24 | LinkingTo: Rcpp, RcppArmadillo 25 | URL: https://github.com/eddelbuettel/rcppde, https://dirk.eddelbuettel.com/code/rcpp.de.html 26 | BugReports: https://github.com/eddelbuettel/rcppde/issues 27 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | useDynLib("RcppDE", .registration=TRUE) 2 | export("DEoptim", "DEoptim.control") 3 | S3method("plot", "DEoptim") 4 | S3method("summary", "DEoptim") 5 | importFrom("Rcpp", "evalCpp") 6 | importFrom("grDevices", "devAskNewPage") 7 | importFrom("graphics", "abline", "matplot", "par", "plot", "plot.new") 8 | -------------------------------------------------------------------------------- /R/DEoptim.R: -------------------------------------------------------------------------------- 1 | DEoptim.control <- function(VTR = -Inf, strategy = 2, bs = FALSE, NP = 50, 2 | itermax = 200, CR = 0.5, F = 0.8, trace = TRUE, 3 | initialpop = NULL, storepopfrom = itermax + 1, 4 | storepopfreq = 1, p = 0.2, c = 0, 5 | reltol = sqrt(.Machine$double.eps), steptol = itermax) { 6 | if (itermax <= 0) { 7 | warning("'itermax' <= 0; set to default value 200\n", immediate. = TRUE) 8 | itermax <- 200 9 | } 10 | if (NP < 4) { 11 | warning("'NP' < 4; set to default value 50\n", immediate. = TRUE) 12 | NP <- 50 13 | } 14 | if (F < 0 | F > 2) { 15 | warning("'F' not in [0,2]; set to default value 0.8\n", immediate. = TRUE) 16 | F <- 0.8 17 | } 18 | if (CR < 0 | CR > 1) { 19 | warning("'CR' not in [0,1]; set to default value 0.5\n", immediate. = TRUE) 20 | CR <- 0.5 21 | } 22 | if (strategy < 1 | strategy > 6) { 23 | warning("'strategy' not in {1,...,6}; set to default value 2\n", 24 | immediate. = TRUE) 25 | strategy <- 2 26 | } 27 | 28 | bs <- (bs > 0) 29 | 30 | if ( trace < 0 ) { 31 | warning("'trace' cannot be negative; set to 'TRUE'") 32 | trace <- TRUE 33 | } 34 | 35 | storepopfreq <- floor(storepopfreq) 36 | if (storepopfreq > itermax) 37 | storepopfreq <- 1 38 | 39 | if (p <= 0 || p > 1) { 40 | warning("'p' not in (0,1]; set to default value 0.2\n", immediate. = TRUE) 41 | p <- 0.2 42 | } 43 | 44 | if (c < 0 || c > 1) { 45 | warning("'c' not in [0,1]; set to default value 0\n", immediate. = TRUE) 46 | c <- 0 47 | } 48 | 49 | list(VTR = VTR, strategy = strategy, NP = NP, itermax = itermax, CR 50 | = CR, F = F, bs = bs, trace = trace, initialpop = initialpop, 51 | storepopfrom = storepopfrom, storepopfreq = storepopfreq, 52 | p = p, c = c, reltol = reltol, steptol = steptol) 53 | } 54 | 55 | DEoptim <- function(fn, lower, upper, control = DEoptim.control(), ...) { 56 | ##fn1 <- function(par) fn(par, ...) 57 | if (length(lower) != length(upper)) 58 | stop("'lower' and 'upper' are not of same length") 59 | if (!is.vector(lower)) 60 | lower <- as.vector(lower) 61 | if (!is.vector(upper)) 62 | upper <- as.vector(upper) 63 | if (any(lower > upper)) 64 | stop("'lower' > 'upper'") 65 | if (any(lower == "Inf")) 66 | warning("you set a component of 'lower' to 'Inf'. May imply 'NaN' results", immediate. = TRUE) 67 | if (any(lower == "-Inf")) 68 | warning("you set a component of 'lower' to '-Inf'. May imply 'NaN' results", immediate. = TRUE) 69 | if (any(upper == "Inf")) 70 | warning("you set a component of 'upper' to 'Inf'. May imply 'NaN' results", immediate. = TRUE) 71 | if (any(upper == "-Inf")) 72 | warning("you set a component of 'upper' to '-Inf'. May imply 'NaN' results", immediate. = TRUE) 73 | if (!is.null(names(lower))) 74 | nam <- names(lower) 75 | else if (!is.null(names(upper)) & is.null(names(lower))) 76 | nam <- names(upper) 77 | else 78 | nam <- paste("par", 1:length(lower), sep = "") 79 | 80 | env <- list2env(list(...)) 81 | 82 | ctrl <- do.call(DEoptim.control, as.list(control)) 83 | ctrl$npar <- length(lower) 84 | if (ctrl$NP < 4) { 85 | warning("'NP' < 4; set to default value 50\n", immediate. = TRUE) 86 | ctrl$NP <- 50 87 | } 88 | if (ctrl$NP < 10*length(lower)) 89 | warning("For many problems it is best to set 'NP' (in 'control') to be at least ten times the length of the parameter vector. \n", immediate. = TRUE) 90 | if (!is.null(ctrl$initialpop)) { 91 | ctrl$specinitialpop <- TRUE 92 | if(!identical(as.numeric(dim(ctrl$initialpop)), c(ctrl$NP, ctrl$npar))) 93 | stop("Initial population is not a matrix with dim. NP x length(upper).") 94 | } 95 | else { 96 | ctrl$specinitialpop <- FALSE 97 | ctrl$initialpop <- matrix(0,1,1) # dummy matrix 98 | } 99 | ## 100 | ctrl$trace <- as.numeric(ctrl$trace) 101 | ctrl$specinitialpop <- as.numeric(ctrl$specinitialpop) 102 | 103 | outC <- DEoptim_impl(lower, upper, fn, ctrl, env) 104 | ## 105 | if (length(outC$storepop) > 0) { 106 | nstorepop <- floor((outC$iter - ctrl$storepopfrom) / ctrl$storepopfreq) 107 | ## storepop <- list() 108 | ## cnt <- 1 109 | ## for(i in 1:nstorepop) { 110 | ## idx <- cnt:((cnt - 1) + (ctrl$NP * ctrl$npar)) 111 | ## storepop[[i]] <- matrix(outC$storepop[idx], nrow = ctrl$NP, ncol = ctrl$npar, 112 | ## byrow = TRUE) 113 | ## cnt <- cnt + (ctrl$NP * ctrl$npar) 114 | ## dimnames(storepop[[i]]) <- list(1:ctrl$NP, nam) 115 | ## } 116 | storepop <- outC$storepop[1:nstorepop] 117 | for (i in 1:length(storepop)) dimnames(storepop[[i]]) <- list(1:ctrl$NP, nam) 118 | } 119 | else { 120 | storepop = NULL 121 | } 122 | 123 | ## optim 124 | bestmem <- as.numeric(outC$bestmem) 125 | names(bestmem) <- nam 126 | bestval <- as.numeric(outC$bestval) 127 | nfeval <- as.numeric(outC$nfeval) 128 | iter <- as.numeric(outC$iter) 129 | 130 | ## member 131 | names(lower) <- names(upper) <- nam 132 | #bestmemit <- matrix(outC$bestmemit, nrow = iter, ncol = ctrl$npar, byrow = TRUE) 133 | bestmemit <- outC$bestmemit[seq_len(iter), , drop = FALSE] 134 | 135 | dimnames(bestmemit) <- list(1:iter, nam) 136 | bestvalit <- as.numeric(outC$bestvalit[1:iter]) 137 | #pop <- matrix(outC$pop, nrow = ctrl$NP, ncol = ctrl$npar, byrow = TRUE) 138 | pop <- outC$pop 139 | storepop <- as.list(storepop) 140 | 141 | outR <- list(optim = list( 142 | bestmem = bestmem, 143 | bestval = bestval, 144 | nfeval = nfeval, 145 | iter = iter), 146 | member = list( 147 | lower = lower, 148 | upper = upper, 149 | bestmemit = bestmemit, 150 | bestvalit = bestvalit, 151 | pop = pop, 152 | storepop = storepop)) 153 | 154 | attr(outR, "class") <- "DEoptim" 155 | return(outR) 156 | } 157 | 158 | -------------------------------------------------------------------------------- /R/RcppExports.R: -------------------------------------------------------------------------------- 1 | # Generated by using Rcpp::compileAttributes() -> do not edit by hand 2 | # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 3 | 4 | DEoptim_impl <- function(minbound, maxbound, fnS, control, rhoS) { 5 | .Call(`_RcppDE_DEoptim_impl`, minbound, maxbound, fnS, control, rhoS) 6 | } 7 | 8 | putFunPtrInXPtr <- function(fstr) { 9 | .Call(`_RcppDE_putFunPtrInXPtr`, fstr) 10 | } 11 | 12 | -------------------------------------------------------------------------------- /R/methods.R: -------------------------------------------------------------------------------- 1 | 'summary.DEoptim' <- function(object, ...){ 2 | digits <- max(5, getOption('digits') - 2) 3 | 4 | cat("\n***** summary of DEoptim object *****", 5 | "\nbest member : ", round(object$optim$bestmem, digits), 6 | "\nbest value : ", round(object$optim$bestval, digits), 7 | "\nafter : ", round(object$optim$iter), "generations", 8 | "\nfn evaluated : ", round(object$optim$nfeval), "times", 9 | "\n*************************************\n") 10 | 11 | invisible(object) 12 | } 13 | 14 | plot.DEoptim <- function (x, plot.type = c("bestmemit", "bestvalit", 15 | "storepop"), ...) { 16 | z <- x$member 17 | niter <- length(z$bestvalit) 18 | npar <- length(z$lower) 19 | nam <- names(z$lower) 20 | nstorepop <- length(z$storepop) 21 | if (identical(plot.type[1], "bestmemit")) { 22 | plot.new() 23 | par(mfrow = c(min(3, npar), 1)) 24 | for (i in 1:npar) { 25 | if (identical(i%%4, 0)) { 26 | cat("new plot\n") 27 | devAskNewPage(ask = TRUE) 28 | } 29 | plot(1:niter, z$bestmemit[, i], xlim = c(1, niter), 30 | las = 1, xlab = "iteration", ylab = "value", 31 | main = nam[i], ...) 32 | abline(h = c(z$lower[i], z$upper[i]), col = "red") 33 | } 34 | } 35 | else if (identical(plot.type[1], "bestvalit")) { 36 | plot(1:niter, z$bestvalit, xlim = c(1, niter), las = 1, 37 | xlab = "iteration", ylab = "function value", main = 38 | "convergence plot", 39 | ...) 40 | } 41 | else if (identical(plot.type[1], "storepop") && nstorepop > 0) { 42 | plot.new() 43 | par(mfrow = c(min(3, npar), 1)) 44 | for (i in 1:npar) { 45 | if (identical(i%%4, 0)) { 46 | cat("new plot\n") 47 | devAskNewPage(ask = TRUE) 48 | } 49 | tmp <- NULL 50 | for (j in 1:nstorepop) { 51 | tmp <- cbind(tmp, z$storepop[[j]][, i]) 52 | } 53 | matplot(t(tmp), col = "black", pch = 20, las = 1, xlab = 54 | "stored population", ylab = "value", 55 | main = nam[i], ...) 56 | abline(h = c(z$lower[i], z$upper[i]), col = "red") 57 | par(new = FALSE) 58 | } 59 | } 60 | else { 61 | warning("'plot.type' does not correspond to any plotting type", 62 | immediate. = TRUE) 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | .onAttach <- function (lib, pkg) { 2 | packageStartupMessage(paste("Now loading:", 3 | " RcppDE: C++ Implementation of Differential Evolution Optimisation", 4 | " Author: Dirk Eddelbuettel", 5 | "Based on:", 6 | " DEoptim (version 2.0-7): Differential Evolution algorithm in R", 7 | " Authors: David Ardia, Katharine Mullen, Brian Peterson and Joshua Ulrich", 8 | sep="\n")) 9 | } 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ## RcppDE: Rcpp port of Differential Evolution 3 | 4 | [![CI](https://github.com/eddelbuettel/rcppde/workflows/ci/badge.svg)](https://github.com/eddelbuettel/rcppde/actions?query=workflow%3Aci) 5 | [![License](https://img.shields.io/badge/license-GPL%20%28%3E=%202%29-brightgreen.svg?style=flat)](https://www.gnu.org/licenses/gpl-2.0.html) 6 | [![CRAN](https://www.r-pkg.org/badges/version/RcppDE)](https://cran.r-project.org/package=RcppDE) 7 | [![r-universe](https://eddelbuettel.r-universe.dev/badges/RcppDE)](https://eddelbuettel.r-universe.dev/RcppDE) 8 | [![Downloads](https://cranlogs.r-pkg.org/badges/RcppDE?color=brightgreen)](https://www.r-pkg.org/pkg/RcppDE) 9 | [![Dependencies](https://tinyverse.netlify.app/badge/RcppDE)](https://cran.r-project.org/package=RcppDE) 10 | [![Last Commit](https://img.shields.io/github/last-commit/eddelbuettel/rcppde)](https://github.com/eddelbuettel/rcppde) 11 | 12 | ### About 13 | 14 | The package provides global optimization by differential evolution. 15 | 16 | It uses an efficient C++ based implementation of the DEoptim 17 | function which performs global optimization by differential evolution. Its 18 | creation was motivated by trying to see if the old approximation "easier, 19 | shorter, faster: pick any two" could in fact be extended to achieving all 20 | three goals while moving the code from plain old C to modern C++. The 21 | initial version did in fact do so, but a good part of the gain was due to an 22 | implicit code review which eliminated a few inefficiencies which have since 23 | been eliminated in DEoptim. 24 | 25 | ## Author 26 | 27 | Dirk Eddelbuettel extending DEoptim by David Ardia, Katharine Mullen, Brian 28 | Peterson and Joshua Ulrich, which itself is based on DE-Engine by Rainer 29 | Storn. 30 | -------------------------------------------------------------------------------- /benchmark.txt: -------------------------------------------------------------------------------- 1 | # At 2010-10-15 18:38:56.27626 2 | # SVN 2300M 3 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 4 | Rastrigin2 0.040167 0.040000 0.99585 0.414938 5 | Rastrigin5 0.106500 0.105778 0.99322 0.678143 6 | Rastrigin20 0.548167 0.548444 1.00051 -0.050674 7 | Wild2 0.066278 0.065944 0.99497 0.502934 8 | Wild5 0.181667 0.181500 0.99908 0.091743 9 | Wild20 1.037556 1.035389 0.99791 0.208824 10 | Genrose2 0.070056 0.070444 1.00555 -0.555115 11 | Genrose5 0.185389 0.186500 1.00599 -0.599341 12 | Genrose20 0.890278 0.887889 0.99732 0.268331 13 | Genrose50 2.517944 2.515389 0.99899 0.101494 14 | MEANS 0.564400 0.563728 0.99881 0.119104 15 | 16 | 17 | # At 2010-10-15 20:03:17.732317 18 | # SVN 2301 19 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 20 | Rastrigin2 0.040500 0.040389 0.99726 0.274348 21 | Rastrigin5 0.106778 0.106389 0.99636 0.364204 22 | Rastrigin20 0.539667 0.540667 1.00185 -0.185300 23 | Wild2 0.066278 0.066056 0.99665 0.335289 24 | Wild5 0.181778 0.181333 0.99756 0.244499 25 | Wild20 1.042722 1.035333 0.99291 0.708615 26 | Genrose2 0.070333 0.071056 1.01027 -1.026856 27 | Genrose5 0.186111 0.186833 1.00388 -0.388060 28 | Genrose20 0.891778 0.891889 1.00012 -0.012460 29 | Genrose50 2.546500 2.528111 0.99278 0.722124 30 | MEANS 0.567244 0.564806 0.99570 0.429954 31 | 32 | 33 | 34 | # At 2010-10-15 21:01:22.385205 35 | # SVN 2302 36 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 37 | Rastrigin2 0.040667 0.040222 0.98907 1.09290 38 | Rastrigin5 0.106167 0.104833 0.98744 1.25589 39 | Rastrigin20 0.542278 0.548000 1.01055 -1.05522 40 | Wild2 0.066444 0.066000 0.99331 0.66890 41 | Wild5 0.182000 0.180500 0.99176 0.82418 42 | Wild20 1.046444 1.040389 0.99421 0.57868 43 | Genrose2 0.071000 0.070000 0.98592 1.40845 44 | Genrose5 0.187667 0.183944 0.98017 1.98342 45 | Genrose20 0.900278 0.890333 0.98895 1.10460 46 | Genrose50 2.545444 2.598444 1.02082 -2.08215 47 | MEANS 0.568839 0.572267 1.00603 -0.60259 48 | 49 | 50 | 51 | # At 2010-10-16 09:51:11.671913 52 | # SVN 2306M 53 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 54 | Rastrigin2 0.040556 0.039889 0.98356 1.64384 55 | Rastrigin5 0.106167 0.104722 0.98639 1.36054 56 | Rastrigin20 0.540778 0.546333 1.01027 -1.02733 57 | Wild2 0.066722 0.066278 0.99334 0.66611 58 | Wild5 0.182222 0.180778 0.99207 0.79268 59 | Wild20 1.039167 1.043944 1.00460 -0.45977 60 | Genrose2 0.071167 0.069389 0.97502 2.49805 61 | Genrose5 0.186722 0.182944 0.97977 2.02321 62 | Genrose20 0.894722 0.891167 0.99603 0.39739 63 | Genrose50 2.562056 2.593056 1.01210 -1.20997 64 | MEANS 0.569028 0.571850 1.00496 -0.49597 65 | 66 | 67 | 68 | # At 2010-10-16 10:44:44.497777 69 | # SVN 2307:2308 70 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 71 | Rastrigin2 0.039944 0.040500 1.01391 -1.390821 72 | Rastrigin5 0.105278 0.105722 1.00422 -0.422164 73 | Rastrigin20 0.537889 0.538000 1.00021 -0.020657 74 | Wild2 0.065500 0.066500 1.01527 -1.526718 75 | Wild5 0.181222 0.180500 0.99601 0.398529 76 | Wild20 1.036389 1.032278 0.99603 0.396676 77 | Genrose2 0.071222 0.070222 0.98596 1.404056 78 | Genrose5 0.185833 0.183333 0.98655 1.345291 79 | Genrose20 0.887667 0.879889 0.99124 0.876205 80 | Genrose50 2.523611 2.518944 0.99815 0.184920 81 | MEANS 0.563456 0.561589 0.99669 0.331289 82 | 83 | 84 | # At 2010-10-16 12:26:24.650366 85 | # SVN 2311M 86 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 87 | Rastrigin2 0.040389 0.041333 1.02338 -2.33838 88 | Rastrigin5 0.105833 0.107833 1.01890 -1.88976 89 | Rastrigin20 0.543667 0.547778 1.00756 -0.75618 90 | Wild2 0.066667 0.066667 1.00000 0.00000 91 | Wild5 0.181500 0.181222 0.99847 0.15305 92 | Wild20 1.042833 1.039667 0.99696 0.30366 93 | Genrose2 0.070667 0.071778 1.01572 -1.57233 94 | Genrose5 0.186167 0.187222 1.00567 -0.56699 95 | Genrose20 0.894167 0.896722 1.00286 -0.28580 96 | Genrose50 2.527278 2.562778 1.01405 -1.40467 97 | MEANS 0.565917 0.570300 1.00775 -0.77455 98 | 99 | 100 | # At 2010-10-16 14:34:43.987088 101 | # SVN 2313M 102 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 103 | Rastrigin2 0.040389 0.041556 1.0289 -2.88858 104 | Rastrigin5 0.105556 0.108667 1.0295 -2.94737 105 | Rastrigin20 0.543667 0.550444 1.0125 -1.24668 106 | Wild2 0.066222 0.067167 1.0143 -1.42617 107 | Wild5 0.179833 0.182556 1.0151 -1.51375 108 | Wild20 1.035556 1.044944 1.0091 -0.90665 109 | Genrose2 0.071111 0.072056 1.0133 -1.32813 110 | Genrose5 0.186278 0.188667 1.0128 -1.28243 111 | Genrose20 0.890889 0.905389 1.0163 -1.62759 112 | Genrose50 2.530889 2.577167 1.0183 -1.82852 113 | MEANS 0.565039 0.573861 1.0156 -1.56135 114 | 115 | 116 | # At 2010-10-16 14:59:19.219468 117 | # SVN 2313M 118 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 119 | Rastrigin2 0.039889 0.041167 1.0320 -3.20334 120 | Rastrigin5 0.107667 0.107944 1.0026 -0.25800 121 | Rastrigin20 0.535500 0.541944 1.0120 -1.20344 122 | Wild2 0.065944 0.067000 1.0160 -1.60067 123 | Wild5 0.180167 0.183222 1.0170 -1.69596 124 | Wild20 1.031222 1.039500 1.0080 -0.80272 125 | Genrose2 0.069500 0.070722 1.0176 -1.75859 126 | Genrose5 0.184778 0.185444 1.0036 -0.36079 127 | Genrose20 0.884500 0.896111 1.0131 -1.31273 128 | Genrose50 2.503333 2.542278 1.0156 -1.55570 129 | MEANS 0.560250 0.567533 1.0130 -1.3000 130 | 131 | # At 2010-10-16 15:49:18.185113 132 | # SVN 2313:2315 133 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 134 | Rastrigin2 0.040056 0.041611 1.0388 -3.88350 135 | Rastrigin5 0.104778 0.110389 1.0536 -5.35525 136 | Rastrigin20 0.540667 0.545778 1.0095 -0.94533 137 | Wild2 0.065833 0.067556 1.0262 -2.61603 138 | Wild5 0.179167 0.183111 1.0220 -2.20155 139 | Wild20 1.034500 1.046389 1.0115 -1.14924 140 | Genrose2 0.069889 0.071833 1.0278 -2.78219 141 | Genrose5 0.184944 0.188500 1.0192 -1.92250 142 | Genrose20 0.890722 0.900833 1.0114 -1.13516 143 | Genrose50 2.563278 2.576667 1.0052 -0.52233 144 | MEANS 0.567383 0.573267 1.0104 -1.03692 145 | 146 | 147 | # At 2010-10-16 16:02:19.151733 148 | # SVN 2313:2315M 149 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 150 | Rastrigin2 0.040000 0.041722 1.0431 -4.3056 151 | Rastrigin5 0.106833 0.109000 1.0203 -2.0281 152 | Rastrigin20 0.536722 0.548389 1.0217 -2.1737 153 | Wild2 0.066167 0.067444 1.0193 -1.9312 154 | Wild5 0.180556 0.183056 1.0138 -1.3846 155 | Wild20 1.029778 1.045000 1.0148 -1.4782 156 | Genrose2 0.070167 0.072222 1.0293 -2.9295 157 | Genrose5 0.186056 0.187944 1.0102 -1.0152 158 | Genrose20 0.891167 0.910278 1.0214 -2.1445 159 | Genrose50 2.523000 2.570111 1.0187 -1.8673 160 | MEANS 0.563044 0.573517 1.0186 -1.8599 161 | 162 | 163 | # At 2010-10-16 16:09:42.62347 164 | # SVN 2317M 165 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 166 | Rastrigin2 0.040222 0.041444 1.0304 -3.03867 167 | Rastrigin5 0.105389 0.108056 1.0253 -2.53031 168 | Rastrigin20 0.538056 0.553000 1.0278 -2.77749 169 | Wild2 0.066222 0.066833 1.0092 -0.92282 170 | Wild5 0.181722 0.182722 1.0055 -0.55029 171 | Wild20 1.034167 1.039667 1.0053 -0.53183 172 | Genrose2 0.069778 0.071778 1.0287 -2.86624 173 | Genrose5 0.185333 0.186944 1.0087 -0.86930 174 | Genrose20 0.886389 0.897556 1.0126 -1.25979 175 | Genrose50 2.523278 2.563389 1.0159 -1.58964 176 | MEANS 0.563056 0.571139 1.0144 -1.43562 177 | 178 | 179 | # At 2010-10-16 16:26:55.718442 180 | # SVN 2318M 181 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 182 | Rastrigin2 0.040167 0.041000 1.02075 -2.07469 183 | Rastrigin5 0.105333 0.106500 1.01108 -1.10759 184 | Rastrigin20 0.540556 0.540778 1.00041 -0.04111 185 | Wild2 0.066056 0.066444 1.00589 -0.58873 186 | Wild5 0.181111 0.181000 0.99939 0.06135 187 | Wild20 1.038278 1.035222 0.99706 0.29429 188 | Genrose2 0.070389 0.070722 1.00474 -0.47356 189 | Genrose5 0.186778 0.185222 0.99167 0.83284 190 | Genrose20 0.894556 0.896833 1.00255 -0.25463 191 | Genrose50 2.531000 2.548667 1.00698 -0.69801 192 | MEANS 0.565422 0.567239 1.00321 -0.32129 193 | 194 | 195 | # At 2010-10-16 16:41:11.48119 196 | # SVN 2319M 197 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 198 | Rastrigin2 0.040167 0.042056 1.0470 -4.70263 199 | Rastrigin5 0.104889 0.109889 1.0477 -4.76695 200 | Rastrigin20 0.539667 0.548500 1.0164 -1.63681 201 | Wild2 0.065667 0.066944 1.0195 -1.94585 202 | Wild5 0.180333 0.183111 1.0154 -1.54036 203 | Wild20 1.034167 1.040500 1.0061 -0.61241 204 | Genrose2 0.069444 0.071667 1.0320 -3.20000 205 | Genrose5 0.183278 0.186722 1.0188 -1.87936 206 | Genrose20 0.886167 0.897000 1.0122 -1.22249 207 | Genrose50 2.527722 2.571667 1.0174 -1.73850 208 | MEANS 0.563150 0.571806 1.0154 -1.53699 209 | 210 | 211 | # At 2010-10-16 16:47:29.41668 212 | # SVN 2320M 213 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 214 | Rastrigin2 0.039889 0.042667 1.0696 -6.963788 215 | Rastrigin5 0.104833 0.110611 1.0551 -5.511394 216 | Rastrigin20 0.539778 0.555278 1.0287 -2.871552 217 | Wild2 0.066167 0.068222 1.0311 -3.106633 218 | Wild5 0.181444 0.186111 1.0257 -2.571953 219 | Wild20 1.037778 1.048944 1.0108 -1.076017 220 | Genrose2 0.070111 0.072222 1.0301 -3.011094 221 | Genrose5 0.186667 0.189833 1.0170 -1.696429 222 | Genrose20 0.905167 0.905611 1.0005 -0.049101 223 | Genrose50 2.530556 2.589778 1.0234 -2.340285 224 | MEANS 0.566239 0.576928 1.0189 -1.887700 225 | 226 | 227 | # At 2010-10-16 17:03:48.572089 228 | # SVN 2322M 229 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 230 | Rastrigin2 0.040944 0.042111 1.0285 -2.8494 231 | Rastrigin5 0.105444 0.109833 1.0416 -4.1623 232 | Rastrigin20 0.541333 0.559722 1.0340 -3.3970 233 | Wild2 0.067222 0.069167 1.0289 -2.8926 234 | Wild5 0.185389 0.189389 1.0216 -2.1576 235 | Wild20 1.041444 1.063611 1.0213 -2.1285 236 | Genrose2 0.071333 0.072778 1.0202 -2.0249 237 | Genrose5 0.187167 0.190167 1.0160 -1.6028 238 | Genrose20 0.895056 0.915222 1.0225 -2.2531 239 | Genrose50 2.551333 2.636611 1.0334 -3.3425 240 | MEANS 0.568667 0.584861 1.0285 -2.8478 241 | 242 | 243 | # At 2010-10-16 17:32:19.869673 244 | # SVN 2322:2326 245 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 246 | Rastrigin2 0.040333 0.041556 1.0303 -3.03030 247 | Rastrigin5 0.106278 0.109167 1.0272 -2.71824 248 | Rastrigin20 0.538444 0.552167 1.0255 -2.54849 249 | Wild2 0.066111 0.066833 1.0109 -1.09244 250 | Wild5 0.180333 0.182500 1.0120 -1.20148 251 | Wild20 1.038944 1.049222 1.0099 -0.98925 252 | Genrose2 0.070278 0.071500 1.0174 -1.73913 253 | Genrose5 0.184611 0.186500 1.0102 -1.02317 254 | Genrose20 0.898667 0.909056 1.0116 -1.15603 255 | Genrose50 2.523167 2.603778 1.0319 -3.19484 256 | MEANS 0.564717 0.577228 1.0222 -2.21547 257 | 258 | 259 | # At 2010-10-16 18:07:26.014139 260 | # SVN 2327M 261 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 262 | Rastrigin2 0.040556 0.041333 1.0192 -1.91781 263 | Rastrigin5 0.105278 0.108500 1.0306 -3.06069 264 | Rastrigin20 0.538611 0.549111 1.0195 -1.94946 265 | Wild2 0.066611 0.067722 1.0167 -1.66806 266 | Wild5 0.180556 0.184944 1.0243 -2.43077 267 | Wild20 1.035056 1.051333 1.0157 -1.57265 268 | Genrose2 0.070889 0.071333 1.0063 -0.62696 269 | Genrose5 0.185444 0.186833 1.0075 -0.74895 270 | Genrose20 0.900778 0.897722 0.9966 0.33921 271 | Genrose50 2.522278 2.598667 1.0303 -3.02857 272 | MEANS 0.564606 0.575750 1.0197 -1.97385 273 | 274 | 275 | # At 2010-10-17 14:11:35.50636 276 | # SVN 2328M 277 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 278 | Rastrigin2 0.040667 0.041444 1.0191 -1.91257 279 | Rastrigin5 0.105889 0.107833 1.0184 -1.83631 280 | Rastrigin20 0.551611 0.554111 1.0045 -0.45322 281 | Wild2 0.066278 0.067333 1.0159 -1.59262 282 | Wild5 0.179833 0.182944 1.0173 -1.73000 283 | Wild20 1.041667 1.049222 1.0073 -0.72533 284 | Genrose2 0.068667 0.069778 1.0162 -1.61812 285 | Genrose5 0.179111 0.183056 1.0220 -2.20223 286 | Genrose20 0.877722 0.884722 1.0080 -0.79752 287 | Genrose50 2.504111 2.571556 1.0269 -2.69335 288 | MEANS 0.561556 0.571200 1.0172 -1.71745 289 | 290 | 291 | # At 2010-10-17 14:31:23.884337 292 | # SVN 2328M 293 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 294 | Rastrigin2 0.040444 0.042167 1.0426 -4.25824 295 | Rastrigin5 0.106111 0.109389 1.0309 -3.08901 296 | Rastrigin20 0.547722 0.556889 1.0167 -1.67360 297 | Wild2 0.066167 0.067222 1.0160 -1.59530 298 | Wild5 0.179722 0.182222 1.0139 -1.39104 299 | Wild20 1.041222 1.049056 1.0075 -0.75232 300 | Genrose2 0.068278 0.070278 1.0293 -2.92921 301 | Genrose5 0.181056 0.183333 1.0126 -1.25805 302 | Genrose20 0.878833 0.886500 1.0087 -0.87237 303 | Genrose50 2.544944 2.574889 1.0118 -1.17662 304 | MEANS 0.565450 0.572194 1.0119 -1.19276 305 | 306 | 307 | # At 2010-10-17 15:13:45.700745 308 | # SVN 2328M 309 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 310 | Rastrigin2 0.040556 0.041278 1.0178 -1.7808 311 | Rastrigin5 0.106111 0.107778 1.0157 -1.5707 312 | Rastrigin20 0.548167 0.560222 1.0220 -2.1993 313 | Wild2 0.066167 0.067833 1.0252 -2.5189 314 | Wild5 0.179722 0.188000 1.0461 -4.6059 315 | Wild20 1.043611 1.064833 1.0203 -2.0335 316 | Genrose2 0.068389 0.071056 1.0390 -3.8993 317 | Genrose5 0.180333 0.186889 1.0364 -3.6352 318 | Genrose20 0.877667 0.905778 1.0320 -3.2029 319 | Genrose50 2.506000 2.642111 1.0543 -5.4314 320 | MEANS 0.561672 0.583578 1.0390 -3.9001 321 | edd@max:~/svn/rcpp/pkg/RcppDE$ svnversion ; ./benchmark.r 322 | 323 | 324 | # At 2010-10-17 15:18:19.55212 325 | # SVN 2328M 326 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 327 | Rastrigin2 0.040667 0.041500 1.0205 -2.0492 328 | Rastrigin5 0.106333 0.108833 1.0235 -2.3511 329 | Rastrigin20 0.545778 0.560722 1.0274 -2.7382 330 | Wild2 0.066611 0.068389 1.0267 -2.6689 331 | Wild5 0.179167 0.185444 1.0350 -3.5039 332 | Wild20 1.042556 1.070389 1.0267 -2.6697 333 | Genrose2 0.069056 0.071222 1.0314 -3.1376 334 | Genrose5 0.180278 0.187833 1.0419 -4.1911 335 | Genrose20 0.875556 0.905944 1.0347 -3.4708 336 | Genrose50 2.512778 2.640722 1.0509 -5.0918 337 | MEANS 0.561878 0.584100 1.0395 -3.9550 338 | 339 | 340 | # At 2010-10-17 15:24:36.53087 341 | # SVN 2328M 342 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 343 | Rastrigin2 0.040389 0.042111 1.0426 -4.2641 344 | Rastrigin5 0.106778 0.109778 1.0281 -2.8096 345 | Rastrigin20 0.551556 0.568389 1.0305 -3.0520 346 | Wild2 0.066389 0.068111 1.0259 -2.5941 347 | Wild5 0.181167 0.186056 1.0270 -2.6986 348 | Wild20 1.048333 1.066667 1.0175 -1.7488 349 | Genrose2 0.067944 0.070444 1.0368 -3.6795 350 | Genrose5 0.181722 0.185833 1.0226 -2.2623 351 | Genrose20 0.883389 0.899778 1.0186 -1.8552 352 | Genrose50 2.500611 2.630778 1.0521 -5.2054 353 | 354 | 355 | # At 2010-10-17 20:12:05.239237 356 | # SVN 2328M 357 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 358 | Rastrigin2 0.040500 0.042167 1.0412 -4.1152 359 | Rastrigin5 0.106111 0.109667 1.0335 -3.3508 360 | Rastrigin20 0.547056 0.557444 1.0190 -1.8991 361 | Wild2 0.066222 0.068167 1.0294 -2.9362 362 | Wild5 0.180778 0.184556 1.0209 -2.0897 363 | Wild20 1.037444 1.056056 1.0179 -1.7939 364 | Genrose2 0.068111 0.071722 1.0530 -5.3018 365 | Genrose5 0.182389 0.187389 1.0274 -2.7414 366 | Genrose20 0.876778 0.902833 1.0297 -2.9717 367 | Genrose50 2.494500 2.597722 1.0414 -4.1380 368 | MEANS 0.559989 0.577772 1.0318 -3.1757 369 | 370 | 371 | # At 2010-10-18 06:41:15.901748 372 | # SVN 2329M 373 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 374 | Rastrigin2 0.040611 0.041667 1.0260 -2.5992 375 | Rastrigin5 0.105889 0.109722 1.0362 -3.6201 376 | Rastrigin20 0.542000 0.554056 1.0222 -2.2243 377 | Wild2 0.065667 0.067500 1.0279 -2.7919 378 | Wild5 0.179278 0.183722 1.0248 -2.4791 379 | Wild20 1.037944 1.054278 1.0157 -1.5736 380 | Genrose2 0.068167 0.071167 1.0440 -4.4010 381 | Genrose5 0.179944 0.187222 1.0404 -4.0445 382 | Genrose20 0.874944 0.893333 1.0210 -2.1017 383 | Genrose50 2.526500 2.593056 1.0263 -2.6343 384 | MEANS 0.562094 0.575572 1.0240 -2.3978 385 | 386 | 387 | # At 2010-10-18 07:15:49.59145 388 | # SVN 2329M 389 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 390 | Rastrigin2 0.040500 0.041625 1.0278 -2.77778 391 | Rastrigin5 0.106500 0.108625 1.0200 -1.99531 392 | Rastrigin20 0.547000 0.555375 1.0153 -1.53108 393 | Wild2 0.066250 0.067750 1.0226 -2.26415 394 | Wild5 0.182250 0.184750 1.0137 -1.37174 395 | Wild20 1.042250 1.049125 1.0066 -0.65963 396 | Genrose2 0.068875 0.069750 1.0127 -1.27042 397 | Genrose5 0.180875 0.183250 1.0131 -1.31306 398 | Genrose20 0.877500 0.883375 1.0067 -0.66952 399 | Genrose50 2.516750 2.565750 1.0195 -1.94696 400 | MEANS 0.562875 0.570937 1.0143 -1.43238 401 | # At 2010-10-18 07:17:58.781577 402 | 403 | 404 | # At 2010-10-18 07:20:58.739872 405 | # SVN 2329M 406 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 407 | Rastrigin2 0.041000 0.041375 1.0091 -0.91463 408 | Rastrigin5 0.105625 0.108500 1.0272 -2.72189 409 | Rastrigin20 0.546125 0.549625 1.0064 -0.64088 410 | Wild2 0.066125 0.067750 1.0246 -2.45747 411 | Wild5 0.179875 0.183250 1.0188 -1.87630 412 | Wild20 1.037125 1.052000 1.0143 -1.43425 413 | Genrose2 0.068750 0.069625 1.0127 -1.27273 414 | Genrose5 0.179250 0.182375 1.0174 -1.74338 415 | Genrose20 0.878375 0.883000 1.0053 -0.52654 416 | Genrose50 2.494250 2.568750 1.0299 -2.98687 417 | MEANS 0.559650 0.570625 1.0196 -1.96105 418 | # Done 2010-10-18 07:23:07.486604 419 | 420 | 421 | # At 2010-10-18 19:42:43.859417 422 | # SVN 2337M 423 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 424 | Rastrigin5 0.10663 0.10713 1.00469 -0.46893 425 | Rastrigin10 0.23312 0.22825 0.97909 2.09115 426 | Rastrigin20 0.54738 0.53800 0.98287 1.71272 427 | Wild5 0.18075 0.18237 1.00899 -0.89903 428 | Wild10 0.40675 0.41000 1.00799 -0.79902 429 | Wild20 1.04412 1.03588 0.99210 0.79014 430 | Genrose5 0.18100 0.18388 1.01588 -1.58840 431 | Genrose10 0.38137 0.37850 0.99246 0.75385 432 | Genrose20 0.88325 0.87988 0.99618 0.38211 433 | MEANS 0.44049 0.43821 0.99483 0.51711 434 | # Done 2010-10-18 19:44:06.829564 435 | 436 | 437 | # At 2010-10-18 19:49:15.391802 438 | # SVN 2337M 439 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 440 | Rastrigin50 1.770 1.672 0.94463 5.53672 441 | Rastrigin100 4.825 4.467 0.92580 7.41969 442 | Rastrigin200 15.141 13.220 0.87313 12.68741 443 | Wild50 3.763 3.771 1.00213 -0.21260 444 | Wild100 11.466 11.198 0.97663 2.33735 445 | Wild200 38.585 36.861 0.95532 4.46806 446 | Genrose50 2.543 2.523 0.99214 0.78647 447 | Genrose100 6.191 5.891 0.95154 4.84574 448 | Genrose200 16.934 15.448 0.91225 8.77525 449 | MEANS 11.246 10.561 0.93907 6.09279 450 | # Done 2010-10-18 19:52:32.353386 451 | 452 | 453 | At revision 2338. 454 | # At 2010-10-18 19:57:13.544375 455 | # SVN 2338 456 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 457 | Rastrigin5 0.10638 0.10838 1.01880 -1.880141 458 | Rastrigin10 0.23150 0.23187 1.00162 -0.161987 459 | Rastrigin20 0.54600 0.54262 0.99382 0.618132 460 | Wild5 0.18112 0.18362 1.01380 -1.380262 461 | Wild10 0.40663 0.41125 1.01137 -1.137412 462 | Wild20 1.04288 1.03875 0.99604 0.395541 463 | Genrose5 0.18238 0.18300 1.00343 -0.342700 464 | Genrose10 0.37937 0.37913 0.99934 0.065898 465 | Genrose20 0.88337 0.88163 0.99802 0.198104 466 | MEANS 0.43996 0.44003 1.00016 -0.015784 467 | # Done 2010-10-18 19:58:36.626385 468 | 469 | # At 2010-10-18 19:58:37.152972 470 | # SVN 2338 471 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 472 | Rastrigin50 1.744 1.651 0.94667 5.3326 473 | Rastrigin100 4.796 4.433 0.92431 7.5688 474 | Rastrigin200 14.950 13.125 0.87793 12.2074 475 | Wild50 3.753 3.706 0.98748 1.2523 476 | Wild100 11.373 11.083 0.97450 2.5499 477 | Wild200 38.421 36.958 0.96192 3.8078 478 | Genrose50 2.552 2.495 0.97766 2.2335 479 | Genrose100 6.164 5.874 0.95295 4.7047 480 | Genrose200 16.927 15.422 0.91109 8.8911 481 | MEANS 11.187 10.527 0.94107 5.8929 482 | # Done 2010-10-18 20:01:53.266103 483 | 484 | 485 | 486 | # At 2010-10-27 21:51:43.813065 487 | # SVN 2365 488 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 489 | Rastrigin5 0.10888 0.10925 1.00344 -0.34443 490 | Rastrigin10 0.23863 0.23313 0.97695 2.30487 491 | Rastrigin20 0.55100 0.54150 0.98276 1.72414 492 | Wild5 0.18050 0.18113 1.00346 -0.34626 493 | Wild10 0.40800 0.40737 0.99847 0.15319 494 | Wild20 1.04462 1.03375 0.98959 1.04104 495 | Genrose5 0.18888 0.18775 0.99404 0.59563 496 | Genrose10 0.39112 0.38938 0.99553 0.44743 497 | Genrose20 0.90088 0.89375 0.99209 0.79090 498 | MEANS 0.44583 0.44189 0.99115 0.88474 499 | # Done 2010-10-27 21:53:07.604243 500 | # At 2010-10-27 21:53:08.134416 501 | # SVN 2365 502 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 503 | Rastrigin50 1.762 1.669 0.94722 5.2781 504 | Rastrigin100 4.798 4.453 0.92810 7.1905 505 | Rastrigin200 15.455 13.244 0.85694 14.3060 506 | Wild50 3.753 3.706 0.98748 1.2523 507 | Wild100 11.349 11.073 0.97568 2.4319 508 | Wild200 38.223 36.940 0.96643 3.3566 509 | Genrose50 2.552 2.532 0.99216 0.7837 510 | Genrose100 6.188 5.877 0.94974 5.0259 511 | Genrose200 16.828 15.397 0.91496 8.5037 512 | MEANS 11.212 10.543 0.94037 5.9629 513 | # Done 2010-10-27 21:56:24.62256 514 | 515 | 516 | romain@ ~/svn/rcpp/pkg/RcppDE/demo $ Rscript SmallBenchmark.R 517 | 518 | DEoptim package 519 | Differential Evolution algorithm in R 520 | Authors: David Ardia and Katharine Mullen 521 | # At 2010-10-30 09:37:15 522 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 523 | Rastrigin5 0.1173750 0.120375 1.0255591 -2.55591054 524 | Rastrigin10 0.2581250 0.261250 1.0121065 -1.21065375 525 | Rastrigin20 0.6117500 0.606875 0.9920311 0.79689416 526 | Wild5 0.1786250 0.202125 1.1315605 -13.15605318 527 | Wild10 0.4220000 0.398500 0.9443128 5.56872038 528 | Wild20 1.0581250 1.052500 0.9946840 0.53160071 529 | Genrose5 0.2046250 0.205125 1.0024435 -0.24434942 530 | Genrose10 0.4595000 0.461875 1.0051687 -0.51686616 531 | Genrose20 1.1097500 1.110375 1.0005632 -0.05631899 532 | MEANS 0.4910972 0.491000 0.9998020 0.01979694 533 | # Done 2010-10-30 09:38:50 534 | romain@ ~/svn/rcpp/pkg/RcppDE/demo $ 535 | romain@ ~/svn/rcpp/pkg/RcppDE/demo $ Rscript LargeBenchmark.R 536 | 537 | DEoptim package 538 | Differential Evolution algorithm in R 539 | Authors: David Ardia and Katharine Mullen 540 | # At 2010-10-30 09:40:05 541 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 542 | Rastrigin50 1.95600 1.98900 1.0168712 -1.6871166 543 | Rastrigin100 5.25200 4.91900 0.9365956 6.3404417 544 | Rastrigin200 14.20800 12.30900 0.8663429 13.3657095 545 | Wild50 3.42000 3.44800 1.0081871 -0.8187135 546 | Wild100 9.03800 9.18800 1.0165966 -1.6596592 547 | Wild200 25.81700 28.24300 1.0939691 -9.3969090 548 | Genrose50 4.11200 3.70900 0.9019942 9.8005837 549 | Genrose100 8.97200 8.83100 0.9842844 1.5715560 550 | Genrose200 24.69300 26.59400 1.0769854 -7.6985380 551 | MEANS 10.82978 11.02556 1.0180777 -1.8077728 552 | # Done 2010-10-30 09:43:23 553 | 554 | 555 | 556 | # SVN 2373 557 | # At 2010-10-30 21:36:07.477032 558 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 559 | Rastrigin5 0.11025 0.10888 0.98753 1.24717 560 | Rastrigin10 0.23738 0.23213 0.97788 2.21169 561 | Rastrigin20 0.55788 0.54225 0.97199 2.80081 562 | Wild5 0.18500 0.18538 1.00203 -0.20270 563 | Wild10 0.41350 0.41662 1.00756 -0.75574 564 | Wild20 1.05762 1.04425 0.98735 1.26463 565 | Genrose5 0.18950 0.19063 1.00594 -0.59367 566 | Genrose10 0.39175 0.39337 1.00415 -0.41481 567 | Genrose20 0.90400 0.89975 0.99530 0.47013 568 | MEANS 0.44965 0.44592 0.99169 0.83089 569 | # Done 2010-10-30 21:37:32.008925 570 | # SVN 2373 571 | # At 2010-10-30 21:37:32.54323 572 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 573 | Rastrigin50 1.777 1.683 0.94710 5.2898 574 | Rastrigin100 4.889 4.494 0.91921 8.0794 575 | Rastrigin200 15.150 13.275 0.87624 12.3762 576 | Wild50 3.790 3.733 0.98496 1.5040 577 | Wild100 11.478 11.172 0.97334 2.6660 578 | Wild200 38.655 37.179 0.96182 3.8184 579 | Genrose50 2.620 2.551 0.97366 2.6336 580 | Genrose100 6.310 6.024 0.95468 4.5325 581 | Genrose200 17.146 15.628 0.91147 8.8534 582 | MEANS 11.313 10.638 0.94032 5.9677 583 | # Done 2010-10-30 21:40:50.769914 584 | 585 | 586 | # SVN 2385M 587 | # At 2010-11-03 18:42:16.023768 588 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 589 | Rastrigin5 0.11025 0.10938 0.99206 0.79365 590 | Rastrigin10 0.23600 0.23350 0.98941 1.05932 591 | Rastrigin20 0.55275 0.54250 0.98146 1.85436 592 | Wild5 0.18637 0.18450 0.98994 1.00604 593 | Wild10 0.41475 0.41312 0.99608 0.39180 594 | Wild20 1.04475 1.03350 0.98923 1.07681 595 | Genrose5 0.19388 0.19075 0.98388 1.61186 596 | Genrose10 0.39863 0.39137 0.98181 1.81875 597 | Genrose20 0.90387 0.88875 0.98327 1.67335 598 | MEANS 0.44903 0.44304 0.98667 1.33313 599 | # Done 2010-11-03 18:43:40.432154 600 | # SVN 2385M 601 | # At 2010-11-03 18:43:40.973753 602 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 603 | Rastrigin50 1.772 1.658 0.93567 6.4334 604 | Rastrigin100 4.827 4.407 0.91299 8.7011 605 | Rastrigin200 14.830 12.814 0.86406 13.5941 606 | Wild50 3.735 3.649 0.97697 2.3025 607 | Wild100 11.236 10.806 0.96173 3.8270 608 | Wild200 37.449 35.729 0.95407 4.5929 609 | Genrose50 2.643 2.530 0.95725 4.2754 610 | Genrose100 6.380 5.952 0.93292 6.7085 611 | Genrose200 17.192 15.568 0.90554 9.4463 612 | MEANS 11.118 10.346 0.93053 6.9466 613 | # Done 2010-11-03 18:46:54.812966 614 | 615 | 616 | 617 | edd@max:~/svn/rcpp/pkg/RcppDE$ ./benchmark.r 618 | # SVN 2396M 619 | # At 2010-11-04 20:32:53.896701 620 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 621 | Rastrigin5 0.10888 0.10263 0.94259 5.7405 622 | Rastrigin10 0.23963 0.21712 0.90610 9.3897 623 | Rastrigin20 0.55725 0.51137 0.91768 8.2324 624 | Wild5 0.18337 0.17437 0.95092 4.9080 625 | Wild10 0.41400 0.39425 0.95229 4.7705 626 | Wild20 1.03863 0.99650 0.95944 4.0558 627 | Genrose5 0.18950 0.17912 0.94525 5.4749 628 | Genrose10 0.39650 0.37387 0.94294 5.7062 629 | Genrose20 0.90162 0.85362 0.94676 5.3237 630 | MEANS 0.44771 0.42254 0.94379 5.6212 631 | # Done 2010-11-04 20:34:16.168758 632 | edd@max:~/svn/rcpp/pkg/RcppDE$ ./bigBenchmark.r 633 | # SVN 2396M 634 | # At 2010-11-04 20:40:49.146992 635 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp 636 | Rastrigin50 1.776 1.582 0.89077 10.9234 637 | Rastrigin100 4.828 4.248 0.87987 12.0133 638 | Rastrigin200 14.943 12.458 0.83370 16.6299 639 | Wild50 3.715 3.577 0.96285 3.7147 640 | Wild100 11.144 10.677 0.95809 4.1906 641 | Wild200 37.244 35.470 0.95237 4.7632 642 | Genrose50 2.623 2.413 0.91994 8.0061 643 | Genrose100 6.275 5.728 0.91283 8.7171 644 | Genrose200 17.136 15.159 0.88463 11.5371 645 | MEANS 11.076 10.146 0.91601 8.3985 646 | # Done 2010-11-04 20:44:00.820587 647 | 648 | # small benchmark at SVN 2400M 649 | # At 2010-11-06 10:13:32.662977 650 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp netSpeedUp 651 | Rastrigin5 0.10838 0.10125 0.93426 6.5744 1.0704 652 | Rastrigin10 0.23838 0.21612 0.90666 9.3340 1.1029 653 | Rastrigin20 0.55662 0.50513 0.90748 9.2522 1.1020 654 | Wild5 0.18312 0.17475 0.95427 4.5734 1.0479 655 | Wild10 0.41388 0.39725 0.95983 4.0169 1.0419 656 | Wild20 1.04862 1.00287 0.95637 4.3629 1.0456 657 | Genrose5 0.19100 0.18163 0.95092 4.9084 1.0516 658 | Genrose10 0.39875 0.38012 0.95329 4.6708 1.0490 659 | Genrose20 0.90700 0.86225 0.95066 4.9338 1.0519 660 | MEANS 0.44953 0.42460 0.94454 5.5459 1.0587 661 | # Done 2010-11-06 10:14:55.223565 662 | # big benchmark at SVN 2400M 663 | # At 2010-11-06 10:14:55.748166 664 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp netSpeedUp 665 | Rastrigin50 1.758 1.574 0.89534 10.4664 1.1169 666 | Rastrigin100 4.824 4.250 0.88101 11.8988 1.1351 667 | Rastrigin200 14.924 12.472 0.83570 16.4299 1.1966 668 | Wild50 3.694 3.554 0.96210 3.7899 1.0394 669 | Wild100 11.177 10.632 0.95124 4.8761 1.0513 670 | Wild200 37.251 35.340 0.94870 5.1301 1.0541 671 | Genrose50 2.578 2.415 0.93677 6.3227 1.0675 672 | Genrose100 6.250 5.727 0.91632 8.3680 1.0913 673 | Genrose200 17.132 15.197 0.88705 11.2947 1.1273 674 | MEANS 11.065 10.129 0.91538 8.4619 1.0924 675 | # Done 2010-11-06 10:18:07.172263 676 | # compiled benchmark at SVN 2400M 677 | # At 2010-11-06 10:18:09.3587 678 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp netSpeedUp 679 | Rastrigin50 1.775 0.608 0.34254 65.746 2.9194 680 | Rastrigin100 4.758 2.073 0.43569 56.431 2.2952 681 | Rastrigin200 14.516 7.403 0.50999 49.001 1.9608 682 | Wild50 3.747 0.950 0.25354 74.646 3.9442 683 | Wild100 11.203 3.320 0.29635 70.365 3.3744 684 | Wild200 37.258 12.401 0.33284 66.716 3.0044 685 | Genrose50 2.590 0.272 0.10502 89.498 9.5221 686 | Genrose100 6.266 0.680 0.10852 89.148 9.2147 687 | Genrose200 17.060 2.047 0.11999 88.001 8.3341 688 | MEANS 11.019 3.306 0.30002 69.998 3.3331 689 | # Done 2010-11-06 10:20:19.001289 690 | 691 | 692 | # small benchmark at SVN 2406:2407M 693 | # At 2010-11-06 15:38:26.505902 694 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp netSpeedUp 695 | Rastrigin5 0.10887 0.10050 0.92308 7.6923 1.0833 696 | Rastrigin10 0.23713 0.21525 0.90775 9.2251 1.1016 697 | Rastrigin20 0.56412 0.50475 0.89475 10.5251 1.1176 698 | Wild5 0.18225 0.17213 0.94444 5.5556 1.0588 699 | Wild10 0.41025 0.39113 0.95338 4.6618 1.0489 700 | Wild20 1.04112 0.98613 0.94717 5.2827 1.0558 701 | Genrose5 0.18912 0.18275 0.96629 3.3708 1.0349 702 | Genrose10 0.39812 0.37875 0.95133 4.8666 1.0512 703 | Genrose20 0.90400 0.86100 0.95243 4.7566 1.0499 704 | MEANS 0.44833 0.42138 0.93987 6.0130 1.0640 705 | # Done 2010-11-06 15:39:48.679854 706 | # big benchmark at SVN 2406:2408 707 | # At 2010-11-06 15:39:49.20452 708 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp netSpeedUp 709 | Rastrigin50 1.772 1.574 0.88826 11.1738 1.1258 710 | Rastrigin100 4.814 4.240 0.88076 11.9236 1.1354 711 | Rastrigin200 14.811 12.504 0.84424 15.5763 1.1845 712 | Wild50 3.700 3.552 0.96000 4.0000 1.0417 713 | Wild100 11.230 10.616 0.94533 5.4675 1.0578 714 | Wild200 37.241 35.287 0.94753 5.2469 1.0554 715 | Genrose50 2.602 2.413 0.92736 7.2636 1.0783 716 | Genrose100 6.341 5.743 0.90569 9.4307 1.1041 717 | Genrose200 17.091 15.114 0.88433 11.5675 1.1308 718 | MEANS 11.067 10.116 0.91407 8.5932 1.0940 719 | # Done 2010-11-06 15:43:00.538043 720 | # compiled benchmark at SVN 2406:2408M 721 | # At 2010-11-06 15:43:02.721151 722 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp netSpeedUp 723 | Rastrigin50 1.789 0.6090 0.34041 65.959 2.9376 724 | Rastrigin100 4.792 2.0750 0.43301 56.699 2.3094 725 | Rastrigin200 14.498 7.4280 0.51235 48.765 1.9518 726 | Wild50 3.733 0.9520 0.25502 74.498 3.9212 727 | Wild100 11.184 3.3120 0.29614 70.386 3.3768 728 | Wild200 37.145 12.4000 0.33383 66.617 2.9956 729 | Genrose50 2.599 0.2690 0.10350 89.650 9.6617 730 | Genrose100 6.322 0.6780 0.10724 89.276 9.3245 731 | Genrose200 17.064 1.9350 0.11340 88.660 8.8186 732 | MEANS 11.014 3.2953 0.29919 70.081 3.3423 733 | # Done 2010-11-06 15:45:12.225298 734 | 735 | 736 | 737 | # small benchmark at SVN 2419M 738 | # At 2010-11-08 06:42:29.018531 739 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp netSpeedUp 740 | Rastrigin5 0.10912 0.099875 0.91523 8.4765 1.0926 741 | Rastrigin10 0.23738 0.214875 0.90521 9.4787 1.1047 742 | Rastrigin20 0.55587 0.501500 0.90218 9.7819 1.1084 743 | Wild5 0.18288 0.171875 0.93985 6.0150 1.0640 744 | Wild10 0.40912 0.391125 0.95600 4.3996 1.0460 745 | Wild20 1.04513 0.987375 0.94474 5.5257 1.0585 746 | Genrose5 0.18913 0.179250 0.94779 5.2214 1.0551 747 | Genrose10 0.39538 0.374625 0.94752 5.2482 1.0554 748 | Genrose20 0.90050 0.848375 0.94212 5.7885 1.0614 749 | MEANS 0.44717 0.418764 0.93648 6.3517 1.0678 750 | # Done 2010-11-08 06:43:50.88171 751 | # big benchmark at SVN 2419M 752 | # At 2010-11-08 06:43:51.422299 753 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp netSpeedUp 754 | Rastrigin50 1.770 1.575 0.88983 11.0169 1.1238 755 | Rastrigin100 4.794 4.258 0.88819 11.1806 1.1259 756 | Rastrigin200 14.840 12.472 0.84043 15.9569 1.1899 757 | Wild50 3.692 3.558 0.96371 3.6295 1.0377 758 | Wild100 11.127 10.646 0.95677 4.3228 1.0452 759 | Wild200 38.026 35.755 0.94028 5.9722 1.0635 760 | Genrose50 2.587 2.414 0.93313 6.6873 1.0717 761 | Genrose100 6.252 5.739 0.91795 8.2054 1.0894 762 | Genrose200 17.058 15.147 0.88797 11.2030 1.1262 763 | MEANS 11.127 10.174 0.91431 8.5695 1.0937 764 | # Done 2010-11-08 06:47:03.810348 765 | # compiled benchmark at SVN 2419:2421M 766 | # At 2010-11-08 06:48:42.56918 767 | DEoptim RcppDE ratioRcppToBasic pctGainOfRcpp netSpeedUp 768 | Rastrigin50 1.781 0.6090 0.34194 65.806 2.9245 769 | Rastrigin100 4.807 2.0940 0.43561 56.439 2.2956 770 | Rastrigin200 14.572 7.5000 0.51469 48.531 1.9429 771 | Wild50 3.748 0.9500 0.25347 74.653 3.9453 772 | Wild100 11.268 3.3160 0.29428 70.572 3.3981 773 | Wild200 37.225 12.4120 0.33343 66.657 2.9991 774 | Genrose50 2.667 0.2710 0.10161 89.839 9.8413 775 | Genrose100 6.498 0.7190 0.11065 88.935 9.0376 776 | Genrose200 17.471 1.9830 0.11350 88.650 8.8104 777 | MEANS 11.115 3.3171 0.29843 70.157 3.3509 778 | # Done 2010-11-08 06:50:53.195003 779 | -------------------------------------------------------------------------------- /cleanup: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | rm -f src/*.o src/*.so 4 | 5 | find . -name \*~ -exec rm {} \; 6 | 7 | #cd inst/doc && rm -f *.aux *.log *.bbl *.blg *.out *.tex && rm -rf auto/ 8 | -------------------------------------------------------------------------------- /demo/00Index: -------------------------------------------------------------------------------- 1 | DEoptim some examples of the DEoptim function. 2 | environment simple example of using RcppDE with a user-supplied environemtn 3 | small repeated runs of RcppDE vs DEoptim on small vectors 4 | large single runs of RcppDE vs DEoptim on large vectors 5 | compiled single runs of RcppDE vs DEoptim on compiled objective function 6 | -------------------------------------------------------------------------------- /demo/DEoptim.R: -------------------------------------------------------------------------------- 1 | demo.DEoptim <- function(){ 2 | 3 | 'print.comments' <- function(str){ 4 | star <- "**********" 5 | cat(paste("\n",star,"\n",str,"\n",star,"\n",sep="")) 6 | } 7 | 8 | 'wait' <- function(){ 9 | t <- readline("\nPlease 'q' to quit the demo or any other key to continue...\n") 10 | if (t == "q") TRUE else FALSE 11 | } 12 | 13 | 'Rosenbrock' <- function(x){ 14 | x1 <- x[1] 15 | x2 <- x[2] 16 | 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 17 | } 18 | 19 | 'Wild' <- function(x) 20 | 10 * sin(0.3*x) * sin(1.3*x^2) + 21 | 0.00001 * x^4 + 0.2 * x + 80 22 | 23 | 'demo.1' <- function(){ 24 | r <- DEoptim(Rosenbrock, rep(-10,2), rep(10,2)) 25 | summary(r) 26 | } 27 | 28 | 'demo.2' <- function(){ 29 | r <- DEoptim(Rosenbrock, rep(-10,2), rep(10,2), 30 | control = list(NP = 100)) 31 | summary(r) 32 | } 33 | 34 | 'demo.3' <- function(){ 35 | r <- DEoptim(Rosenbrock, rep(-10,2), rep(10,2), 36 | control = list(NP = 50, itermax = 300, F = 1.5, 37 | CR = 0.2)) 38 | summary(r) 39 | plot(r, type = 'b') 40 | } 41 | 42 | 'demo.4' <- function(){ 43 | r <- DEoptim(Wild, lower = -50, upper = 50, 44 | control = list(NP = 50)) 45 | par(mfrow = c(2,1)) 46 | plot(r, type = 'b') 47 | plot(r, plot.type = "bestvalit", type = 'l') 48 | } 49 | 50 | 'demo.5' <- function(){ 51 | r <- DEoptim(Wild, lower = -50, upper = 50, 52 | control = list(NP = 50)) 53 | } 54 | 55 | str.stop <- "end of the demo" 56 | tstr <- "\nRun the optimization process for the 'Rosenbrock'" 57 | tstr <- paste(tstr, "\nBanana function. Search space [-10,10]^2.\n", sep = "") 58 | print.comments(tstr) 59 | print(Rosenbrock) 60 | print(demo.1) 61 | if (wait()) stop(str.stop) else demo.1() 62 | 63 | tstr <- "\nDecrease to 100 the members in the population.\n" 64 | print.comments(tstr) 65 | print(demo.2) 66 | if (wait()) stop(str.stop) else demo.2() 67 | 68 | tstr <- "\nIncrease the number of iterations to 300, and" 69 | tstr <- paste(tstr, "\nmodify crossover and F parameters.\n", sep = "") 70 | tsts <- paste(tstr, "the result") 71 | print.comments(tstr) 72 | print(demo.3) 73 | if (wait()) stop(str.stop) else demo.3() 74 | 75 | tstr <- "\nRun the optimization process for the 'Wild' function." 76 | tstr <- paste(tstr, "\nSearch space [-50,50].\n", sep = "") 77 | print.comments(tstr) 78 | print(Wild) 79 | plot(Wild, -50, 50, n = 1000, 80 | main = "DEoptim minimizing 'Wild function'") 81 | if (wait()) stop(str.stop) else demo.4() 82 | 83 | tstr <- "\nIncrease the number of printed digits" 84 | print.comments(tstr) 85 | if (wait()) stop(str.stop) else demo.5() 86 | 87 | cat("\n",str.stop,"\n") 88 | } 89 | 90 | demo.DEoptim() 91 | 92 | -------------------------------------------------------------------------------- /demo/c++/compiledFunctions.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | // [[Rcpp::interfaces(r, cpp)]] 5 | 6 | // Note that the interface is double fun(SEXP) as it comes from a time 7 | // before Rcpp Attributes. We could rewrite it for NumericVector or 8 | // Armadillo vector arguments but would then have to do so consistenly 9 | // in the RcppDE sources as well where the XPtr gets unwrapped. 10 | 11 | double genrose(SEXP xs) { 12 | Rcpp::NumericVector x(xs); 13 | int n = x.size(); 14 | double sum = 1.0; 15 | for (int i=1; i(new funcPtr(&genrose))); 47 | else if (fstr == "wild") 48 | return(Rcpp::XPtr(new funcPtr(&wild))); 49 | else 50 | return(Rcpp::XPtr(new funcPtr(&rastrigin))); 51 | } 52 | -------------------------------------------------------------------------------- /demo/compiled.R: -------------------------------------------------------------------------------- 1 | 2 | demo.LargeBenchmark <- function() { 3 | 4 | Wild <- function(x) { ## 'Wild' function, global minimum at about -15.81515 5 | sum(10 * sin(0.3 * x) * sin(1.3 * x^2) + 0.00001 * x^4 + 0.2 * x + 80)/length(x) 6 | } 7 | 8 | Rastrigin <- function(x) { 9 | sum(x+2 - 10 * cos(2*pi*x)) + 20 10 | } 11 | 12 | Genrose <- function(x) { ## One generalization of the Rosenbrock banana valley function (n parameters) 13 | n <- length(x) 14 | 1.0 + sum (100 * (x[-n]^2 - x[-1])^2 + (x[-1] - 1)^2) 15 | } 16 | 17 | Rcpp::sourceCpp("c++/compiledFunctions.cpp") 18 | 19 | maxIt <- 250 # not excessive but so that we get some run-time on simple problems 20 | 21 | suppressMessages(library(DEoptim)) # the original, currently 2.0.7 22 | suppressMessages(library(RcppDE)) # the contender 23 | 24 | basicDE <- function(n, maxIt, fun) DEoptim::DEoptim(fn=fun, lower=rep(-25, n), upper=rep(25, n), 25 | control=list(NP=10*n, itermax=maxIt, trace=FALSE))#, bs=TRUE)) 26 | cppDE <- function(n, maxIt, fun) RcppDE::DEoptim(fn=fun, lower=rep(-25, n), upper=rep(25, n), 27 | control=list(NP=10*n, itermax=maxIt, trace=FALSE))#, bs=TRUE)) 28 | 29 | set.seed(42) 30 | valBasic <- basicDE(5, maxIt, function(...) Rastrigin(...)) 31 | set.seed(42) 32 | valCpp <- cppDE(5, maxIt, function(...) Rastrigin(...)) 33 | #stopifnot( all.equal(valBasic, valCpp) ) 34 | 35 | runPair <- function(n, maxIt, fun, funname) { 36 | gc() 37 | set.seed(42) 38 | bt <- system.time(invisible(basicDE(n, maxIt, fun)))[3] 39 | 40 | gc() 41 | set.seed(42) 42 | xptr <- create_xptr(funname) 43 | ct <- system.time(invisible(cppDE(n, maxIt, xptr)))[3] 44 | 45 | return(data.frame(DEoptim=bt, RcppDE=ct)) 46 | } 47 | 48 | cat("# At", format(Sys.time()), "\n") 49 | 50 | reps <- c(50, 100, 200) 51 | 52 | res <- rbind(do.call(rbind, lapply(reps, runPair, maxIt, function(...) Rastrigin(...), "rastrigin")), 53 | do.call(rbind, lapply(reps, runPair, maxIt, function(...) Wild(...), "wild")), 54 | do.call(rbind, lapply(reps, runPair, maxIt, function(...) Genrose(...), "genrose")) 55 | ) 56 | res <- rbind(res, colMeans(res)) 57 | 58 | rownames(res) <- c(paste("Rastrigin", reps, sep=""), 59 | paste("Wild", reps, sep=""), 60 | paste("Genrose", reps, sep=""), 61 | "MEANS") 62 | 63 | res$ratioRcppToBasic <- res[,2]/res[,1] 64 | res$pctGainOfRcpp <- (1-res[,2]/res[,1])*100 65 | res$netSpeedUp <- res[,1]/res[,2] 66 | 67 | print(res) 68 | cat("# Done", format(Sys.time()), "\n") 69 | } 70 | 71 | demo.LargeBenchmark() 72 | -------------------------------------------------------------------------------- /demo/environment.R: -------------------------------------------------------------------------------- 1 | 2 | suppressMessages(library(RcppDE)) 3 | 4 | ## somewhat pathodological example with nuisance parameter mul 5 | Rastrigin <- function(x) { 6 | mul * (sum(x+2 - 10 * cos(2*pi*x)) + 20) 7 | } 8 | 9 | ## create a new environment associated with the function 10 | funenv <- environment(fun=Rastrigin) 11 | assign("mul", 2, envir=funenv) ## set value 12 | 13 | out <- DEoptim(Rastrigin, -25, 25, 14 | control=list(NP=10, trace=0), 15 | env=funenv) 16 | summary(out) 17 | -------------------------------------------------------------------------------- /demo/large.R: -------------------------------------------------------------------------------- 1 | 2 | demo.LargeBenchmark <- function() { 3 | 4 | Wild <- function(x) { ## 'Wild' function, global minimum at about -15.81515 5 | sum(10 * sin(0.3 * x) * sin(1.3 * x^2) + 0.00001 * x^4 + 0.2 * x + 80)/length(x) 6 | } 7 | 8 | Rastrigin <- function(x) { 9 | sum(x+2 - 10 * cos(2*pi*x)) + 20 10 | } 11 | 12 | Genrose <- function(x) { ## One generalization of the Rosenbrock banana valley function (n parameters) 13 | n <- length(x) 14 | 1.0 + sum (100 * (x[-n]^2 - x[-1])^2 + (x[-1] - 1)^2) 15 | } 16 | 17 | maxIt <- 250 # not excessive but so that we get some run-time on simple problems 18 | 19 | suppressMessages(library(DEoptim)) # the original, currently 2.0.7 20 | suppressMessages(library(RcppDE)) # the contender 21 | 22 | basicDE <- function(n, maxIt, fun) DEoptim::DEoptim(fn=fun, lower=rep(-25, n), upper=rep(25, n), 23 | control=list(NP=10*n, itermax=maxIt, trace=FALSE))#, bs=TRUE)) 24 | cppDE <- function(n, maxIt, fun) RcppDE::DEoptim(fn=fun, lower=rep(-25, n), upper=rep(25, n), 25 | control=list(NP=10*n, itermax=maxIt, trace=FALSE))#, bs=TRUE)) 26 | 27 | set.seed(42) 28 | valBasic <- basicDE(5, maxIt, function(...) Rastrigin(...)) 29 | set.seed(42) 30 | valCpp <- cppDE(5, maxIt, function(...) Rastrigin(...)) 31 | stopifnot( all.equal(valBasic, valCpp) ) 32 | 33 | runPair <- function(n, maxIt, fun) { 34 | gc() 35 | set.seed(42) 36 | bt <- system.time(invisible(basicDE(n, maxIt, fun)))[3] 37 | 38 | gc() 39 | set.seed(42) 40 | ct <- system.time(invisible(cppDE(n, maxIt, fun)))[3] 41 | 42 | return(data.frame(DEoptim=bt, RcppDE=ct)) 43 | } 44 | 45 | cat("# At", format(Sys.time()), "\n") 46 | 47 | reps <- c(50, 100, 200) 48 | 49 | res <- rbind(do.call(rbind, lapply(reps, runPair, maxIt, function(...) Rastrigin(...))), 50 | do.call(rbind, lapply(reps, runPair, maxIt, function(...) Wild(...))), 51 | do.call(rbind, lapply(reps, runPair, maxIt, function(...) Genrose(...))) 52 | ) 53 | res <- rbind(res, colMeans(res)) 54 | 55 | rownames(res) <- c(paste("Rastrigin", reps, sep=""), 56 | paste("Wild", reps, sep=""), 57 | paste("Genrose", reps, sep=""), 58 | "MEANS") 59 | 60 | res$ratioRcppToBasic <- res[,2]/res[,1] 61 | res$pctGainOfRcpp <- (1-res[,2]/res[,1])*100 62 | res$netSpeedUp <- res[,1]/res[,2] 63 | 64 | print(res) 65 | cat("# Done", format(Sys.time()), "\n") 66 | } 67 | 68 | demo.LargeBenchmark() 69 | -------------------------------------------------------------------------------- /demo/small.R: -------------------------------------------------------------------------------- 1 | 2 | demo.SmallBenchmark <- function() { 3 | 4 | Wild <- function(x) { ## 'Wild' function, global minimum at about -15.81515 5 | sum(10 * sin(0.3 * x) * sin(1.3 * x^2) + 0.00001 * x^4 + 0.2 * x + 80)/length(x) 6 | } 7 | 8 | Rastrigin <- function(x) { 9 | sum(x+2 - 10 * cos(2*pi*x)) + 20 10 | } 11 | 12 | Genrose <- function(x) { ## One generalization of the Rosenbrock banana valley function (n parameters) 13 | n <- length(x) 14 | 1.0 + sum (100 * (x[-n]^2 - x[-1])^2 + (x[-1] - 1)^2) 15 | } 16 | 17 | maxIt <- 250 # not excessive but so that we get some run-time on simple problems 18 | 19 | suppressMessages(library(DEoptim)) # the original, currently 2.0.7 20 | suppressMessages(library(RcppDE)) # the contender 21 | 22 | basicDE <- function(n, maxIt, fun) DEoptim::DEoptim(fn=fun, lower=rep(-25, n), upper=rep(25, n), 23 | control=list(NP=10*n, itermax=maxIt, trace=FALSE)) 24 | cppDE <- function(n, maxIt, fun) RcppDE::DEoptim(fn=fun, lower=rep(-25, n), upper=rep(25, n), 25 | control=list(NP=10*n, itermax=maxIt, trace=FALSE)) 26 | 27 | set.seed(42) 28 | valBasic <- basicDE(5, maxIt, function(...) Rastrigin(...)) 29 | set.seed(42) 30 | valCpp <- cppDE(5, maxIt, function(...) Rastrigin(...)) 31 | stopifnot( all.equal(valBasic, valCpp) ) 32 | 33 | runPair <- function(n, maxIt, fun) { 34 | 35 | gc() 36 | set.seed(42) 37 | bt <- mean(replicate(10, system.time(invisible(basicDE(n, maxIt, fun)))[3]), trim=0.1) 38 | 39 | gc() 40 | set.seed(42) 41 | ct <- mean(replicate(10, system.time(invisible(cppDE(n, maxIt, fun)))[3]), trim=0.1) 42 | 43 | return(data.frame(DEoptim=bt, RcppDE=ct)) 44 | } 45 | 46 | cat("# At", format(Sys.time()), "\n") 47 | 48 | reps <- c(5, 10, 20) 49 | 50 | res <- rbind(do.call(rbind, lapply(reps, runPair, maxIt, function(...) Rastrigin(...))), 51 | do.call(rbind, lapply(reps, runPair, maxIt, function(...) Wild(...))), 52 | do.call(rbind, lapply(reps, runPair, maxIt, function(...) Genrose(...))) 53 | # runPair(50, maxIt, function(...) Genrose(...)) 54 | # runPair(100, maxIt, function(...) Genrose(...)) 55 | ) 56 | res <- rbind(res, colMeans(res)) 57 | rownames(res) <- c(paste("Rastrigin", reps, sep=""), 58 | paste("Wild", reps, sep=""), 59 | paste("Genrose", reps, sep=""), 60 | "MEANS") 61 | res$ratioRcppToBasic <- res[,2]/res[,1] 62 | res$pctGainOfRcpp <- (1-res[,2]/res[,1])*100 63 | res$netSpeedUp <- res[,1]/res[,2] 64 | 65 | print(res) 66 | cat("# Done", format(Sys.time()), "\n") 67 | } 68 | 69 | demo.SmallBenchmark() 70 | -------------------------------------------------------------------------------- /inst/DEoptim-docs/CHANGES: -------------------------------------------------------------------------------- 1 | version 1.3-0, 2008-12-03 2 | - The function DEoptim has two arguments: 'storepopfrom' and 'storepopfreq', for tracking intermediate 3 | populations; the output contains also the list 'storepop' which belongs to the 'member' list's element. 4 | - Small bug fixed for the number of iterations. 5 | 6 | version 1.2-1, 2007-09-12 7 | - Fix a bug in the optimization procedure. Thanks to Tarmo Leinonen for pointing out this bug 8 | - Add a demo to show how to increase the number of printed digits while performing the optimization 9 | 10 | version 1.2-0, 2007-09-10 11 | - The function DEoptim has the new argument 'initial', which is an initial or starting population. 12 | You can therefore introduce a starting population in the optimization procedure. This can be useful 13 | when the optimization has to be run many times on data sets which differ sligthly. Thanks to 14 | Tarmo Leinonen for this nice suggestion. 15 | 16 | - The function DEoptim outputs now 'pop' in the 'member' list's element. This is the population obtained 17 | at the last iteration which can be used as a starting population in DEoptim via the argument 'initial'. 18 | 19 | - The function stops if any 'NA' or 'NaN' value is returned by the function 'FUN' to be optimized. 20 | 21 | - The function DEoptim does not handle exponential crossover anymore. This is so to simplify and 22 | accelerate the optimization procedure. Thanks to Vladimir Eremeev for pointing out a 23 | bug with the exponential crossover. 24 | 25 | - The function DEoptim outputs a list of lists. This is more natural and in the same spirit 26 | of usual optimization functions. Thanks to Vladimir Eremeev for proposing this change. 27 | 28 | - documentation for the 'digits'. Thanks to Eugene Demidenko for pointing out this. 29 | 30 | version 1.8-0, 2007/02/02 31 | - The documentation of DEoptim has been modified. Corrections of some references. -------------------------------------------------------------------------------- /inst/DEoptim-docs/NEWS: -------------------------------------------------------------------------------- 1 | Changes in version 2.0-7 2 | o Many improvements in the C code, including removal of all global variables 3 | thanks to Joshua Ulrich. 4 | o Thanks to Ralf Tautenhahn and Joshua Ulrich, a bug with bs=TRUE is removed. 5 | o Thanks to Dirk Eddelbuettel several bugs (some possibly causing segfaults) 6 | removed. 7 | o Added DE/current-to-p-best/1 strategy. 8 | o Added ability to only print every "trace" iterations. 9 | Changes in version 2.0-6 10 | o Added dataset documentation now required by R CMD check. 11 | Changes in version 2.0-5 12 | o CITATION file modified, updated references. 13 | o Removed check on length of parameter vector (it was left over from versions 14 | prior to 2.0-3, thanks to Jean-Luc Jannink. 15 | Changes in version 2.0-4 16 | o added check to prevent the C code being called with NP<4, thanks to Joshua 17 | Ulrich. 18 | o fixed bug introduced in version 2.0-3 that made the objective function 19 | values associated with the winning population members incorrect 20 | o added new options checkWinner and avWinner to the 'control' arguement. 21 | 22 | Changes in version 2.0-3 23 | o Re-write of much of the underlying C code. Now dynamically allocate 24 | storage, so can optimize on parameter vectors of arbitrary size, in a 25 | population of arbitrary size. Changed the documentation to reflect the 26 | new lack of limitations. 27 | o Stop with an error right away if a NaN objective function value occurs. 28 | o Default value of CR changed to .9 from .5. 29 | o Added reference and minor changes to documentation. 30 | 31 | Changes in version 2.0-2 32 | o The maximum number of parameters that can be optimized (set 33 | statically in de.h with #define MAXDIM) was changed from 20 to 34 | 200. 35 | o zzz.R file removed and replaced with call to `useDynLib(DEoptim)' in 36 | NAMESPACE 37 | o Brian Peterson pointed out problems occurring when the objective 38 | function returns a NaN value. Error messages are now added to 39 | report when this happens. 40 | o permute patched, see below. Thanks to Hans Werner Borchers for pointing 41 | out that the problem was not fixed in the CRAN version. 42 | 43 | Changes in version 2.0-1 44 | o Soren Macbeth and Joshua Ulrich pointed out and patched bugs in the 45 | function 'permute'. Note that the version 2.0-1 on CRAN does not patch 46 | these bugs correctly. The correction is made in the next version. 47 | 48 | Changes in version 2.0-0 49 | o The R-based implementation of Differential Evolution has been 50 | replaced with a C-based implementation similar to the MS Visual C++ 51 | v5.0 implementation accompanying the book `Differential Evolution - 52 | A Practical Approach to Global Optimization',downloaded from 53 | http://www.icsi.berkeley.edu/~storn/DeWin.zip. 54 | 55 | The new C implementation is significantly faster. 56 | 57 | o The S3 method for plotting has been enhanced. It allows now to plot 58 | the intermediate populations if provided. 59 | 60 | o The package maintainer has been changed to Katharine Mullen, 61 | . 62 | 63 | o A NAMESPACE has been added. 64 | 65 | o Argument FUN for DEoptim is now called fn for compatibility with optim. 66 | 67 | o demo file has been removed 68 | 69 | o CITATION file modified 70 | 71 | 72 | Changes in version 1.3-3 73 | 74 | o CITATION file modified. 75 | 76 | 77 | Changes in Version 1.3-2 78 | 79 | o CITATION file modified. 80 | 81 | 82 | Changes in Version 1.3-1 83 | 84 | o new plotting argument 'storepop' which displays intermediate 85 | population locations. 86 | 87 | 88 | Changes in Version 1.3-0 89 | 90 | o the function 'DEoptim' has two arguments: 'storepopfrom' and 91 | 'storepopfreq', for tracking intermediate populations; the output 92 | contains also the list 'storepop' which belongs to the 'member' 93 | list's element. 94 | 95 | o small bug fixed for the number of iterations. 96 | 97 | 98 | Changes in Version 1.2-1 99 | 100 | o fix a bug in the optimization procedure. 101 | 102 | o add a demo to show how to increase the number of printed digits 103 | while performing the optimization. 104 | 105 | 106 | Changes in Version 1.2-0 107 | 108 | o the function 'DEoptim' has the new argument 'initial', which is an 109 | initial or starting population. You can therefore introduce a 110 | starting population in the optimization procedure. This can be 111 | useful when the optimization has to be run many times on data sets 112 | which differ sligthly. 113 | 114 | o the function 'DEoptim' outputs now 'pop' in the 'member' list's 115 | element. This is the population obtained at the last iteration 116 | which can be used as a starting population in 'DEoptim' via the 117 | argument 'initial'. 118 | 119 | o the function stops if any 'NA' or 'NaN' value is returned by the 120 | function 'FUN' to be optimized. 121 | 122 | o the function DEoptim does not handle exponential crossover 123 | anymore. This is so to simplify and accelerate the optimization 124 | procedure. 125 | 126 | o the function 'DEoptim' outputs a list of lists. This is more 127 | natural and in the same spirit of usual optimization functions. 128 | 129 | o documentation for the 'digits'. -------------------------------------------------------------------------------- /inst/DEoptim-docs/README: -------------------------------------------------------------------------------- 1 | DEoptim is an R implementation of the Differential Evolution 2 | optimization algorithm. It is still under development and is 3 | distributed without warranty. 4 | 5 | Further details can be found at 6 | http://www.icsi.berkeley.edu/~storn/code.html. 7 | 8 | Report bugs or make suggestions to or 9 | . -------------------------------------------------------------------------------- /inst/DEoptim-docs/THANKS: -------------------------------------------------------------------------------- 1 | The authors would like to thank the following people for their help: 2 | 3 | Tarmo Leinonen 4 | Eugene Demidenko 5 | Rainer Storn 6 | Soren Macbeth 7 | Hans Werner Borchers 8 | Enrico Schumann 9 | Jean-Luc Jannink 10 | Dirk Eddelbuettel 11 | Ralf Tautenhahn -------------------------------------------------------------------------------- /local/dot.travis.yml: -------------------------------------------------------------------------------- 1 | # Run Travis CI for R using https://eddelbuettel.github.io/r-travis/ 2 | 3 | language: c 4 | sudo: required 5 | dist: bionic 6 | 7 | jobs: 8 | include: 9 | #- name: r-3.5 10 | # env: R_VERSION="3.5" 11 | - name: r-4.0 12 | env: R_VERSION="4.0" 13 | 14 | before_install: 15 | - curl -OLs https://eddelbuettel.github.io/r-travis/run.sh && chmod 0755 run.sh 16 | - ./run.sh bootstrap 17 | 18 | install: 19 | - ./run.sh install_aptget r-cran-rcpp r-cran-rcpparmadillo r-cran-inline r-cran-deoptim r-cran-lattice 20 | 21 | script: 22 | - ./run.sh run_tests 23 | 24 | after_failure: 25 | - ./run.sh dump_logs 26 | 27 | notifications: 28 | email: 29 | on_success: change 30 | on_failure: change 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /man/DEoptim-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{DEoptim-methods} 2 | \alias{DEoptim-methods} 3 | \alias{plot.DEoptim} 4 | \alias{summary.DEoptim} 5 | \title{DEoptim-methods} 6 | \description{Methods for DEoptim objects.} 7 | \usage{ 8 | \method{summary}{DEoptim}(object, \dots) 9 | \method{plot}{DEoptim}(x, plot.type = c("bestmemit", "bestvalit", "storepop"), \dots) 10 | } 11 | \arguments{ 12 | \item{object}{an object of class \code{DEoptim}; usually, a result 13 | of a call to \code{\link{DEoptim}}.} 14 | \item{x}{an object of class \code{DEoptim}; usually, a result 15 | of a call to \code{\link{DEoptim}}.} 16 | \item{plot.type}{should we plot the best member at each iteration, the best value 17 | at each iteration or the intermediate populations?} 18 | \item{\dots}{further arguments passed to or from other methods.} 19 | } 20 | \details{ 21 | Members of the class \code{DEoptim} have a \code{plot} method that 22 | accepts the argument \code{plot.type}. \code{plot.type = "bestmemit"} results 23 | in a plot of the parameter values that represent the lowest value of the objective function 24 | each generation. \code{plot.type = "bestvalit"} plots the best value of 25 | the objective function each generation. Finally, \code{plot.type = "storepop"} results in a plot of 26 | stored populations (which are only available if these have been saved by 27 | setting the \code{control} argument of \code{DEoptim} appropriately). Storing intermediate populations 28 | allows us to examine the progress of the optimization in detail. 29 | A summary method also exists and returns the best parameter vector, the best value of the objective function, 30 | the number of generations optimization ran, and the number of times the 31 | objective function was evaluated. 32 | } 33 | \note{ 34 | Further details and examples of the \R package \pkg{DEoptim} can be found 35 | in Mullen et al. (2009) and Ardia et al. (2010). 36 | 37 | Please cite the package in publications. % Use \code{citation("DEoptim")}. 38 | } 39 | \author{ 40 | For \pkg{RcppDE}: Dirk Eddelbuettel. 41 | 42 | For \pkg{DEoptim}: 43 | David Ardia, Katharine Mullen \email{katharine.mullen@nist.gov}, 44 | Brian Peterson and Joshua Ulrich. 45 | } 46 | \seealso{ 47 | \code{\link{DEoptim}} and \code{\link{DEoptim.control}}. 48 | } 49 | \references{ 50 | Mullen, K.M., Ardia, D., Gil, D.L, Windover, D., Cline, J. (2009) 51 | \pkg{DEoptim}: An \R Package for Global Optimization by Differential Evolution. 52 | URL \url{https://www.ssrn.com/abstract=1526466} 53 | 54 | Ardia, D., Boudt, K., Carl, P., Mullen, K.M., Peterson, B.G. (2010) 55 | Differential Evolution (\pkg{DEoptim}) for Non-Convex Portfolio Optimization. 56 | URL \url{https://www.ssrn.com/abstract=1584905} 57 | } 58 | \examples{ 59 | ## Rosenbrock Banana function 60 | ## The function has a global minimum f(x) = 0 at the point (0,0). 61 | ## Note that the vector of parameters to be optimized must be the first 62 | ## argument of the objective function passed to DEoptim. 63 | Rosenbrock <- function(x){ 64 | x1 <- x[1] 65 | x2 <- x[2] 66 | 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 67 | } 68 | 69 | lower <- c(-10, -10) 70 | upper <- -lower 71 | 72 | set.seed(1234) 73 | outDEoptim <- DEoptim(Rosenbrock, lower, upper) 74 | 75 | ## print output information 76 | summary(outDEoptim) 77 | 78 | ## plot the best members 79 | plot(outDEoptim, type = 'b') 80 | 81 | ## plot the best values 82 | dev.new() 83 | plot(outDEoptim, plot.type = "bestvalit", type = 'b', col = 'blue') 84 | 85 | ## rerun the optimization, and store intermediate populations 86 | outDEoptim <- DEoptim(Rosenbrock, lower, upper, 87 | DEoptim.control(itermax = 500, 88 | storepopfrom = 1, storepopfreq = 2)) 89 | summary(outDEoptim) 90 | 91 | ## plot intermediate populations 92 | dev.new() 93 | plot(outDEoptim, plot.type = "storepop") 94 | 95 | ## Wild function 96 | Wild <- function(x) 97 | 10 * sin(0.3 * x) * sin(1.3 * x^2) + 98 | 0.00001 * x^4 + 0.2 * x + 80 99 | 100 | outDEoptim = DEoptim(Wild, lower = -50, upper = 50, 101 | DEoptim.control(trace = FALSE, storepopfrom = 50, 102 | storepopfreq = 1)) 103 | 104 | plot(outDEoptim, type = 'b') 105 | 106 | dev.new() 107 | plot(outDEoptim, plot.type = "bestvalit", type = 'b') 108 | 109 | \dontrun{ 110 | ## an example with a normal mixture model: requires package mvtnorm 111 | library(mvtnorm) 112 | 113 | ## neg value of the density function 114 | negPdfMix <- function(x) { 115 | tmp <- 0.5 * dmvnorm(x, c(-3, -3)) + 0.5 * dmvnorm(x, c(3, 3)) 116 | -tmp 117 | } 118 | 119 | ## wrapper plotting function 120 | plotNegPdfMix <- function(x1, x2) 121 | negPdfMix(cbind(x1, x2)) 122 | 123 | ## contour plot of the mixture 124 | x1 <- x2 <- seq(from = -10.0, to = 10.0, by = 0.1) 125 | thexlim <- theylim <- range(x1) 126 | z <- outer(x1, x2, FUN = plotNegPdfMix) 127 | 128 | contour(x1, x2, z, nlevel = 20, las = 1, col = rainbow(20), 129 | xlim = thexlim, ylim = theylim) 130 | 131 | set.seed(1234) 132 | outDEoptim <- DEoptim(negPdfMix, c(-10, -10), c(10, 10), 133 | DEoptim.control(NP = 100, itermax = 100, storepopfrom = 1, 134 | storepopfreq = 5)) 135 | 136 | ## convergence plot 137 | dev.new() 138 | plot(outDEoptim) 139 | 140 | ## the intermediate populations indicate the bi-modality of the function 141 | dev.new() 142 | plot(outDEoptim, plot.type = "storepop") 143 | } 144 | } 145 | \keyword{methods} 146 | -------------------------------------------------------------------------------- /man/DEoptim.Rd: -------------------------------------------------------------------------------- 1 | \name{DEoptim} 2 | \alias{DEoptim} 3 | \title{Differential Evolution Optimization} 4 | \concept{minimization} 5 | \description{ 6 | Performs evolutionary global optimization via the Differential Evolution algorithm. 7 | } 8 | \usage{ 9 | DEoptim(fn, lower, upper, control = DEoptim.control(), ...) 10 | } 11 | \arguments{ 12 | \item{fn}{the function to be optimized (minimized). The function should have as its first 13 | argument the vector of real-valued parameters to optimize, and return a scalar real result. \code{NA} 14 | and \code{NaN} values are not allowed. Note that \code{fn} can also 15 | be an external pointer object encapsulating a C/C++-level function pointer 16 | to a compiled functions which may offer considerable speed improvements.} 17 | \item{lower, upper}{two vectors specifying scalar real lower and upper bounds on each parameter to be optimized, so that the i-th element 18 | of \code{lower} and \code{upper} applied to the i-th parameter. The implementation searches 19 | between \code{lower} and \code{upper} for the global optimum (minimum) of \code{fn}.} 20 | \item{control}{a list of control parameters; see \code{\link{DEoptim.control}}.} 21 | \item{...}{further arguments to be passed to \code{fn}.} 22 | } 23 | \details{ 24 | \code{DEoptim} performs optimization (minimization) of \code{fn}. 25 | 26 | The \code{control} argument is a list; see the help file for 27 | \code{\link{DEoptim.control}} for details. 28 | 29 | The \R implementation of Differential Evolution (DE), \pkg{DEoptim}, was first published on the Comprehensive \R Archive 30 | Network (CRAN) in 2005 by David Ardia. Early versions were written in 31 | pure \R. Since version 2.0-0 (published to CRAN in 2009) the package has relied on an 32 | interface to a C implementation of DE, which is significantly 33 | faster on most problems as compared to the implementation in 34 | pure \R. The C interface is in many respects similar to the MS 35 | Visual C++ v5.0 implementation of the Differential Evolution algorithm 36 | distributed with the book \emph{Differential Evolution -- A Practical 37 | Approach to Global Optimization} by Price, K.V., Storn, R.M., Lampinen 38 | J.A, Springer-Verlag, 2006. Since version 2.0-3 the C 39 | implementation dynamically allocates the memory required to store the population, removing limitations on the 40 | number of members in the population and length of the parameter vectors that may be optimized. 41 | Since becoming publicly available, the package \pkg{DEoptim} has been used by several authors to solve optimization 42 | problems arising in diverse domains; see Mullen et al. (2009) for a review. 43 | 44 | To perform a maximization (instead of minimization) of a given 45 | function, simply define a new function which is the opposite of the 46 | function to maximize and apply \code{DEoptim} to it. 47 | 48 | To integrate additional constraints (than box constraints) on the parameters \code{x} of 49 | \code{fn(x)}, for instance \code{x[1] + x[2]^2 < 2}, integrate the 50 | constraint within the function to optimize, for instance: 51 | \preformatted{ 52 | fn <- function(x)\{ 53 | if (x[1] + x[2]^2 < 2)\{ 54 | r <- Inf 55 | else\{ 56 | ... 57 | \} 58 | return(r) 59 | \} 60 | } 61 | This simplistic strategy usually does not work all that well for gradient-based or Newton-type 62 | methods. It is likely to be alright when the solution is in the interior of the feasible region, but when 63 | the solution is on the boundary, optimization algorithm would have a difficult time converging. Furthermore, when 64 | the solution is on the boundary, this strategy would make the algorithm converge to an inferior solution in the interior. 65 | However, for methods such as DE which are not gradient based, this strategy might not be that bad. 66 | 67 | Note that \code{DEoptim} stops if any \code{NA} or \code{NaN} value is 68 | obtained. You have to redefine your function to handle these values 69 | (for instance, set \code{NA} to \code{Inf} in your objective function). 70 | 71 | It is important to emphasize that the result of \code{DEoptim} is a random variable, 72 | i.e., different results will obtain when the algorithm is run repeatedly with the same 73 | settings. Hence, the user should set the random seed if they want to reproduce the results, e.g., by 74 | setting \code{set.seed(1234)} before the call of \code{DEoptim}. 75 | 76 | \code{DEoptim} relies on repeated evaluation of the objective function 77 | in order to move the population toward a global minimum. Users 78 | interested in making \code{DEoptim} run as fast as possible should 79 | ensure that evaluation of the objective function is as efficient as 80 | possible. Using pure \R code, this may often be accomplished 81 | using vectorization. Writing parts of the objective function in a 82 | lower-level language like C or Fortran may also increase speed. 83 | 84 | Further details and examples of the \R package \pkg{DEoptim} can be found 85 | in Mullen et al. (2009) and Ardia et al. (2010). 86 | 87 | Please cite the package in publications. %Use \code{citation("DEoptim")}. 88 | } 89 | \value{ 90 | The output of the function \code{DEoptim} is a member of the \code{S3} class \code{DEoptim}. More precisely, 91 | this is a list (of length 2) containing the following elements:\cr 92 | 93 | \code{optim}, a list containing the following elements: 94 | \itemize{ 95 | \item \code{bestmem}: the best set of parameters found. 96 | \item \code{bestval}: the value of \code{fn} corresponding to \code{bestmem}. 97 | \item \code{nfeval}: number of function evaluations. 98 | \item \code{iter}: number of procedure iterations. 99 | } 100 | 101 | \code{member}, a list containing the following elements: 102 | \itemize{ 103 | \item \code{lower}: the lower boundary. 104 | \item \code{upper}: the upper boundary. 105 | \item \code{bestvalit}: the best value of \code{fn} at each iteration. 106 | \item \code{bestmemit}: the best member at each iteration. 107 | \item \code{pop}: the population generated at the last iteration. 108 | \item \code{storepop}: a list containing the intermediate populations. 109 | } 110 | 111 | Members of the class \code{DEoptim} have a \code{plot} method that 112 | accepts the argument \code{plot.type}. \code{plot.type = "bestmemit"} results 113 | in a plot of the parameter values that represent the lowest value of the objective function 114 | each generation. \code{plot.type = "bestvalit"} plots the best value of 115 | the objective function each generation. Finally, \code{plot.type = "storepop"} results in a plot of 116 | stored populations (which are only available if these have been saved by 117 | setting the \code{control} argument of \code{DEoptim} appropriately). Storing intermediate populations 118 | allows us to examine the progress of the optimization in detail. 119 | A summary method also exists and returns the best parameter vector, the best value of the objective function, 120 | the number of generations optimization ran, and the number of times the 121 | objective function was evaluated. 122 | } 123 | \note{ 124 | \emph{Differential Evolution} (DE) is a search heuristic introduced by Storn and Price (1997). 125 | Its remarkable performance as a global optimization algorithm on continuous numerical minimization 126 | problems has been extensively explored; see Price et al. (2006). DE belongs to the class of genetic 127 | algorithms which use biology-inspired operations of 128 | crossover, mutation, and selection on a population in order to minimize an objective 129 | function over the course of successive generations (see Mitchell, 1998). As with other evolutionary algorithms, 130 | DE solves optimization problems by evolving a population of candidate solutions using alteration and selection 131 | operators. DE uses floating-point instead of bit-string encoding of population members, and 132 | arithmetic operations instead of logical operations in mutation. DE is particularly well-suited to find the global optimum of a 133 | real-valued function of real-valued parameters, and does not require that the function be 134 | either continuous or differentiable. 135 | 136 | Let \eqn{\mathit{NP}}{NP} denote the number of parameter vectors (members) \eqn{x \in R^d}{x in R^d} in the population. 137 | In order to create the initial generation, \eqn{\mathit{NP}}{NP} guesses for the optimal value 138 | of the parameter vector are made, either using random values between lower and upper 139 | bounds (defined by the user) or using values given by 140 | the user. Each generation involves creation of a new population from 141 | the current population members \eqn{\{ x_i \,|\, i = 1, \ldots, \mathit{NP}\}}{{x_i | i=1,...,NP}}, 142 | where \eqn{i} indexes the vectors that make up the population. 143 | This is accomplished using \emph{differential mutation} of the 144 | population members. An initial mutant parameter vector \eqn{v_i} is 145 | created by choosing three members of the population, \eqn{x_{r_0}}, 146 | \eqn{x_{r_1}} and \eqn{x_{r_2}}, at random. Then \eqn{v_i} is 147 | generated as 148 | 149 | \deqn{v_i \doteq x_{r_0} + \mathit{F} \cdot (x_{r_1} - x_{r_2})}{v_i := x_{r_0} + F * (x_{r_1} - x_{r_2})} 150 | 151 | where \eqn{\mathit{F}}{F} is a positive scale factor, effective values for which are 152 | typically less than one. After the first mutation operation, mutation is 153 | continued until \eqn{d} mutations have been made, with a crossover probability 154 | \eqn{\mathit{CR} \in [0,1]}{CR in [0,1]}. 155 | The crossover probability \eqn{\mathit{CR}}{CR} controls the fraction of the parameter 156 | values that are copied from the mutant. If an element of the trial parameter vector is found to violate the 157 | bounds after mutation and crossover, it is reset in such a way that the bounds are respected (with the 158 | specific protocol depending on the implementation). 159 | Then, the objective function values associated with the children are determined. If a trial 160 | vector has equal or lower objective function value than the previous 161 | vector it replaces the previous vector in the population; 162 | otherwise the previous vector remains. Variations of this scheme have also 163 | been proposed; see Price et al. (2006) and \code{\link{DEoptim.control}}. 164 | 165 | Intuitively, the effect of the scheme is that the shape of the distribution of the population in the 166 | search space is converging with respect to size and direction towards areas with high 167 | fitness. The closer the population gets to the global optimum, the more the distribution 168 | will shrink and therefore reinforce the generation of smaller difference vectors. 169 | 170 | As a general advice regarding the choice of \eqn{\mathit{NP}}{NP}, \eqn{\mathit{F}}{F} and \eqn{\mathit{CR}}{CR}, 171 | Storn et al. (2006) state the following: Set the number 172 | of parents \eqn{\mathit{NP}}{NP} to 10 times the number of parameters, select weighting factor 173 | \eqn{\mathit{F} = 0.8}{F = 0.8} and crossover constant \eqn{\mathit{CR} = 0.9}{CR = 0.9}. Make sure that you initialize your parameter vectors 174 | by exploiting their full numerical range, i.e., if a parameter is allowed to exhibit 175 | values in the range [-100, 100] it is a good idea to pick the initial values from this 176 | range instead of unnecessarily restricting diversity. If you experience misconvergence in 177 | the optimization process you usually have to increase the value for \eqn{\mathit{NP}}{NP}, but often you only have to adjust 178 | \eqn{\mathit{F}}{F} to be a little lower or higher than 0.8. If you increase 179 | \eqn{\mathit{NP}}{NP} and simultaneously lower \eqn{\mathit{F}}{F} a little, convergence is more 180 | likely to occur but generally takes longer, i.e., DE is getting 181 | more robust (there is always a convergence speed/robustness trade-off). 182 | 183 | DE is much more sensitive to the choice of \eqn{\mathit{F}}{F} than it is to 184 | the choice of \eqn{\mathit{CR}}{CR}. \eqn{\mathit{CR}}{CR} is more like a fine tuning element. High 185 | values of \eqn{\mathit{CR}}{CR} like \eqn{\mathit{CR} = 1}{CR = 1} give faster convergence if convergence 186 | occurs. Sometimes, however, you have to go down as much as \eqn{\mathit{CR} = 0}{CR = 0} to 187 | make DE robust enough for a particular problem. For more details on the DE strategy, we refer 188 | the reader to Storn and Price (1997) and Price et al. (2006). 189 | } 190 | \references{ 191 | Storn, R. and Price, K. (1997) 192 | Differential Evolution -- A Simple and Efficient Heuristic for Global Optimization over Continuous Spaces, 193 | \emph{Journal of Global Optimization}, 11:4, 341--359. 194 | 195 | Price, K.V., Storn, R.M., Lampinen J.A. (2006) 196 | \emph{Differential Evolution - A Practical Approach to Global Optimization}. 197 | Berlin Heidelberg: Springer-Verlag. ISBN 3540209506. 198 | 199 | Mitchell, M. (1998) 200 | \emph{An Introduction to Genetic Algorithms}. 201 | The MIT Press. ISBN 0262631857. 202 | 203 | Mullen, K.M., Ardia, D., Gil, D.L, Windover, D., Cline, J. (2009) 204 | \pkg{DEoptim}: An \R Package for Global Optimization by Differential Evolution. 205 | URL \url{https://www.ssrn.com/abstract=1526466} 206 | 207 | Ardia, D., Boudt, K., Carl, P., Mullen, K.M., Peterson, B.G. (2010) 208 | Differential Evolution (\pkg{DEoptim}) for Non-Convex Portfolio Optimization. 209 | URL \url{https://www.ssrn.com/abstract=1584905} 210 | } 211 | \author{ 212 | For \pkg{RcppDE}: Dirk Eddelbuettel. 213 | 214 | For \pkg{DEoptim}: 215 | David Ardia, Katharine Mullen \email{katharine.mullen@nist.gov}, 216 | Brian Peterson and Joshua Ulrich. 217 | } 218 | \seealso{ 219 | \code{\link{DEoptim.control}} for control arguments, 220 | \code{\link{DEoptim-methods}} for methods on \code{DEoptim} objects, 221 | including some examples in plotting the results; 222 | \code{\link{optim}} or \code{\link{constrOptim}} 223 | for alternative optimization algorithms. 224 | } 225 | \examples{ 226 | ## Rosenbrock Banana function 227 | ## The function has a global minimum f(x) = 0 at the point (0,0). 228 | ## Note that the vector of parameters to be optimized must be the first 229 | ## argument of the objective function passed to DEoptim. 230 | Rosenbrock <- function(x){ 231 | x1 <- x[1] 232 | x2 <- x[2] 233 | 100 * (x2 - x1 * x1)^2 + (1 - x1)^2 234 | } 235 | 236 | ## DEoptim searches for minima of the objective function between 237 | ## lower and upper bounds on each parameter to be optimized. Therefore 238 | ## in the call to DEoptim we specify vectors that comprise the 239 | ## lower and upper bounds; these vectors are the same length as the 240 | ## parameter vector. 241 | lower <- c(-10,-10) 242 | upper <- -lower 243 | 244 | ## run DEoptim and set a seed first for replicability 245 | set.seed(1234) 246 | DEoptim(Rosenbrock, lower, upper) 247 | 248 | ## increase the population size 249 | DEoptim(Rosenbrock, lower, upper, DEoptim.control(NP = 100)) 250 | 251 | ## change other settings and store the output 252 | outDEoptim <- DEoptim(Rosenbrock, lower, upper, DEoptim.control(NP = 80, 253 | itermax = 400, F = 1.2, CR = 0.7)) 254 | 255 | ## plot the output 256 | plot(outDEoptim) 257 | 258 | ## 'Wild' function, global minimum at about -15.81515 259 | Wild <- function(x) 260 | 10 * sin(0.3 * x) * sin(1.3 * x^2) + 261 | 0.00001 * x^4 + 0.2 * x + 80 262 | 263 | plot(Wild, -50, 50, n = 1000, main = "'Wild function'") 264 | 265 | outDEoptim <- DEoptim(Wild, lower = -50, upper = 50, 266 | control = DEoptim.control(trace = FALSE)) 267 | 268 | plot(outDEoptim) 269 | 270 | DEoptim(Wild, lower = -50, upper = 50, 271 | control = DEoptim.control(NP = 50)) 272 | } 273 | \keyword{nonlinear} 274 | \keyword{optimize} 275 | -------------------------------------------------------------------------------- /man/DEoptim.control.Rd: -------------------------------------------------------------------------------- 1 | \name{DEoptim.control} 2 | \alias{DEoptim.control} 3 | \title{Control various aspects of the DEoptim implementation} 4 | \description{ 5 | Allow the user to set some characteristics of the 6 | Differential Evolution optimization algorithm implemented 7 | in \code{DEoptim}. 8 | } 9 | \usage{ 10 | DEoptim.control(VTR = -Inf, strategy = 2, bs = FALSE, NP = 50, 11 | itermax = 200, CR = 0.5, F = 0.8, trace = TRUE, 12 | initialpop = NULL, storepopfrom = itermax + 1, 13 | storepopfreq = 1, p = 0.2, c = 0, reltol = sqrt(.Machine$double.eps), 14 | steptol = itermax) 15 | } 16 | \arguments{ 17 | \item{VTR}{the value to be reached. The optimization process 18 | will stop if either the maximum number of iterations \code{itermax} 19 | is reached or the best parameter vector \code{bestmem} has found a value 20 | \code{fn(bestmem) <= VTR}. Default to \code{-Inf}.} 21 | \item{strategy}{defines the Differential Evolution 22 | strategy used in the optimization procedure:\cr 23 | \code{1}: DE / rand / 1 / bin (classical strategy)\cr 24 | \code{2}: DE / local-to-best / 1 / bin (default)\cr 25 | \code{3}: DE / best / 1 / bin with jitter\cr 26 | \code{4}: DE / rand / 1 / bin with per-vector-dither\cr 27 | \code{5}: DE / rand / 1 / bin with per-generation-dither\cr 28 | \code{6}: DE / current-to-p-best / 1\cr 29 | any value not above: variation to DE / rand / 1 / bin: either-or-algorithm. Default 30 | strategy is currently \code{2}. See *Details*. 31 | } 32 | \item{bs}{if \code{FALSE} then every mutant will be tested against a 33 | member in the previous generation, and the best value will proceed 34 | into the next generation (this is standard trial vs. target 35 | selection). If \code{TRUE} then the old generation and \code{NP} 36 | mutants will be sorted by their associated objective function 37 | values, and the best \code{NP} vectors will proceed into the next 38 | generation (best of parent and child selection). Default is 39 | \code{FALSE}.} 40 | \item{NP}{number of population members. Defaults to \code{50}. For 41 | many problems it is best to set 42 | \code{NP} to be at least 10 times the length 43 | of the parameter vector. } 44 | \item{itermax}{the maximum iteration (population generation) allowed. 45 | Default is \code{200}.} 46 | \item{CR}{crossover probability from interval [0,1]. Default 47 | to \code{0.5}.} 48 | \item{F}{step-size from interval [0,2]. Default to \code{0.8}.} 49 | \item{trace}{Printing of progress occurs? Default to \code{TRUE}. If 50 | numeric, progress will be printed every \code{trace} iterations.} 51 | \item{initialpop}{an initial population used as a starting 52 | population in the optimization procedure. May be useful to speed up 53 | the convergence. Default to \code{NULL}. If given, each member of 54 | the initial population should be given as a row of a numeric matrix, so that 55 | \code{initialpop} is a matrix with \code{NP} rows and a number of 56 | columns equal to the length of the parameter vector to be optimized. } 57 | \item{storepopfrom}{from which generation should the following 58 | intermediate populations be stored in memory. Default to 59 | \code{itermax + 1}, i.e., no intermediate population is stored.} 60 | \item{storepopfreq}{the frequency with which populations are stored. 61 | Default to \code{1}, i.e., every intermediate population 62 | is stored.} 63 | \item{p}{when \code{strategy = 6}, the top (100 * p)\% best 64 | solutions are used in the mutation. \code{p} must be defined in 65 | (0,1].} 66 | \item{c}{when \code{c > 0}, crossover probability(CR) and step-size(F) are randomized 67 | at each mutation as an implementation of the JADE algorithm . \code{c} must be defined in 68 | [0,1].} 69 | \item{reltol}{relative convergence tolerance. The algorithm stops if 70 | it is unable to reduce the value by a factor of \code{reltol * (abs(val) + 71 | reltol)} after \code{steptol} steps. Defaults to 72 | \code{sqrt(.Machine$double.eps)}, typically about \code{1e-8}.} 73 | \item{steptol}{see \code{reltol}. Defaults to \code{itermax}.} 74 | } 75 | \value{ 76 | The default value of \code{control} is the return value of 77 | \code{DEoptim.control()}, which is a list (and a member of the \code{S3} class 78 | \code{DEoptim.control}) with the above elements. 79 | } 80 | \details{ 81 | This defines the Differential Evolution 82 | strategy used in the optimization procedure, described below in the 83 | terms used by Price et al. (2006); see also Mullen et al. (2009) for details. 84 | 85 | \itemize{ 86 | \item \code{strategy = 1}: DE / rand / 1 / bin. \cr 87 | This strategy is the classical approach for DE, and is described in \code{\link{DEoptim}}. 88 | 89 | \item \code{strategy = 2}: DE / local-to-best / 1 / bin. \cr 90 | In place of the classical DE mutation the expression 91 | \deqn{ 92 | v_{i,g} = old_{i,g} + (best_{g} - old_{i,g}) + x_{r0,g} + F \cdot (x_{r1,g} - x_{r2,g}) 93 | }{ 94 | v_i,g = old_i,g + (best_g - old_i,g) + x_r0,g + F * (x_r1,g - x_r2,g) 95 | } 96 | is used, where \eqn{old_{i,g}}{old_i,g} and \eqn{best_{g}}{best_g} are the 97 | \eqn{i}-th member and best member, respectively, of the previous population. 98 | This strategy is currently used by default. 99 | 100 | \item \code{strategy = 3}: DE / best / 1 / bin with jitter.\cr 101 | In place of the classical DE mutation the expression 102 | \deqn{ 103 | v_{i,g} = best_{g} + jitter + F \cdot (x_{r1,g} - x_{r2,g}) 104 | }{ 105 | v_i,g = best_g + jitter + F * (x_r1,g - x_r2,g) 106 | } 107 | is used, where \eqn{jitter} is defined as 0.0001 * \code{rand} + F. 108 | 109 | \item \code{strategy = 4}: DE / rand / 1 / bin with per vector dither.\cr 110 | In place of the classical DE mutation the expression 111 | \deqn{ 112 | v_{i,g} = x_{r0,g} + dither \cdot (x_{r1,g} - x_{r2,g}) 113 | }{ 114 | v_i,g = x_r0,g + dither * (x_r1,g - x_r2,g) 115 | } 116 | is used, where \eqn{dither} is calculated as \eqn{F + \code{rand} * (1 - F)}. 117 | 118 | \item \code{strategy = 5}: DE / rand / 1 / bin with per generation dither.\cr 119 | The strategy described for \code{4} is used, but \eqn{dither} 120 | is only determined once per-generation. 121 | 122 | \item any value not above: variation to DE / rand / 1 / bin: either-or algorithm.\cr 123 | In the case that \code{rand} < 0.5, the classical strategy \code{strategy = 1} is used. 124 | Otherwise, the expression 125 | \deqn{ 126 | v_{i,g} = x_{r0,g} + 0.5 \cdot (F + 1) \cdot (x_{r1,g} + x_{r2,g} - 2 \cdot x_{r0,g}) 127 | }{ 128 | v_i,g = x_r0,g + 0.5 * (F + 1) * (x_r1,g + x_r2,g - 2 * x_r0,g) 129 | } 130 | is used. 131 | } 132 | } 133 | \note{ 134 | Further details and examples of the \R package \pkg{DEoptim} can be found 135 | in Mullen et al. (2009) and Ardia et al. (2010). 136 | 137 | Please cite the package in publications. %Use \code{citation("DEoptim")}. 138 | } 139 | \seealso{ 140 | \code{\link{DEoptim}} and \code{\link{DEoptim-methods}}. 141 | } 142 | \references{ 143 | Price, K.V., Storn, R.M., Lampinen J.A. (2006) 144 | \emph{Differential Evolution - A Practical Approach to Global Optimization}. 145 | Berlin Heidelberg: Springer-Verlag. ISBN 3540209506. 146 | 147 | Mullen, K.M., Ardia, D., Gil, D.L, Windover, D., Cline, J. (2009) 148 | \pkg{DEoptim}: An \R Package for Global Optimization by Differential Evolution. 149 | URL \url{https://www.ssrn.com/abstract=1526466} 150 | 151 | Ardia, D., Boudt, K., Carl, P., Mullen, K.M., Peterson, B.G. (2010) 152 | Differential Evolution (\pkg{DEoptim}) for Non-Convex Portfolio Optimization. 153 | URL \url{https://www.ssrn.com/abstract=1584905} 154 | } 155 | \author{ 156 | For \pkg{RcppDE}: Dirk Eddelbuettel. 157 | 158 | For \pkg{DEoptim}: 159 | David Ardia, Katharine Mullen \email{katharine.mullen@nist.gov}, 160 | Brian Peterson and Joshua Ulrich. 161 | } 162 | \examples{ 163 | ## set the population size to 20 164 | DEoptim.control(NP = 20) 165 | 166 | ## set the population size, the number of iterations and don't 167 | ## display the iterations during optimization 168 | DEoptim.control(NP = 20, itermax = 100, trace = FALSE) 169 | } 170 | \keyword{nonlinear} 171 | \keyword{optimize} 172 | 173 | -------------------------------------------------------------------------------- /scripts/benchmark.r: -------------------------------------------------------------------------------- 1 | #!/usr/bin/r -t 2 | 3 | svnver <- system("svnversion", intern=TRUE) 4 | cat("# small benchmark at SVN", svnver, "\n") 5 | source("demo/SmallBenchmark.R") 6 | -------------------------------------------------------------------------------- /scripts/bigBenchmark.r: -------------------------------------------------------------------------------- 1 | #!/usr/bin/r -t 2 | 3 | svnver <- system("svnversion", intern=TRUE) 4 | cat("# big benchmark at SVN", svnver, "\n") 5 | source("demo/LargeBenchmark.R") 6 | -------------------------------------------------------------------------------- /scripts/check.r: -------------------------------------------------------------------------------- 1 | #!/usr/bin/r -t 2 | 3 | Wild <- function(x) { ## 'Wild' function, global minimum at about -15.81515 4 | sum(10 * sin(0.3 * x) * sin(1.3 * x^2) + 0.00001 * x^4 + 0.2 * x + 80)/length(x) 5 | } 6 | 7 | Rastrigin <- function(x) { 8 | sum(x+2 - 10 * cos(2*pi*x)) + 20 9 | } 10 | 11 | Genrose <- function(x) { ## One generalization of the Rosenbrock banana valley function (n parameters) 12 | n <- length(x) 13 | 1.0 + sum (100 * (x[-n]^2 - x[-1])^2 + (x[-1] - 1)^2) 14 | } 15 | 16 | #maxIt <- 100 # not excessive but so that we get some run-time on simple problems 17 | n <- 20 18 | maxIt <- 50 19 | useBS <- TRUE 20 | storeFrom <- maxIt+1 21 | strat <- 6 # TODO fix segfault when strat==6 22 | 23 | suppressMessages(library(DEoptim)) # the original, currently 2.0.7 24 | suppressMessages(library(RcppDE)) # the contender 25 | 26 | ctrl <- DEoptim::DEoptim.control(NP=10*n, 27 | itermax=maxIt, 28 | trace=FALSE, 29 | bs=useBS, 30 | storepopfrom=storeFrom, 31 | strategy=strat) 32 | 33 | basicDE <- function(n, maxIt, fun) DEoptim::DEoptim(fn=fun, lower=rep(-25, n), upper=rep(25, n), control=ctrl) 34 | cppDE <- function(n, maxIt, fun) RcppDE::DEoptim(fn=fun, lower=rep(-25, n), upper=rep(25, n), control=ctrl) 35 | 36 | set.seed(42) 37 | valBasic <- basicDE(n, maxIt, function(...) Rastrigin(...)) 38 | #print(str(valBasic[[2]])) 39 | set.seed(42) 40 | valCpp <- cppDE(n, maxIt, function(...) Rastrigin(...)) 41 | #print(str(valCpp[[2]])) 42 | stopifnot( all.equal(valBasic[[1]], valCpp[[1]]) ) 43 | 44 | cat("# Done ", format(Sys.time()), "\n") 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /scripts/compBenchmark.r: -------------------------------------------------------------------------------- 1 | #!/usr/bin/r -t 2 | 3 | svnver <- system("svnversion", intern=TRUE) 4 | cat("# compiled benchmark at SVN", svnver, "\n") 5 | source("demo/CompiledBenchmark.R") 6 | -------------------------------------------------------------------------------- /scripts/profile.r: -------------------------------------------------------------------------------- 1 | #!/usr/bin/r -t 2 | 3 | suppressMessages(library(RcppDE)) 4 | 5 | Genrose <- function(x) { ## One generalization of the Rosenbrock banana valley function (n parameters) 6 | n <- length(x) 7 | 1.0 + sum (100 * (x[-n]^2 - x[-1])^2 + (x[-1] - 1)^2) 8 | } 9 | 10 | 11 | maxIt <- 2500 # not excessive but so that we get some run-time on simple problems 12 | n <- 100 13 | 14 | RcppDE::DEoptim(fn=Genrose, lower=rep(-25, n), upper=rep(25, n), control=list(NP=10*n, itermax=maxIt, trace=FALSE)) 15 | -------------------------------------------------------------------------------- /src/Makevars: -------------------------------------------------------------------------------- 1 | ## Hey Emacs make this a -*- mode: makefile; -*- file 2 | ## 3 | ## -- default 4 | PKG_LIBS=$(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) $(SHLIB_OPENMP_CXXFLAGS) 5 | ## 6 | ## -- for Google Perftools profiling, add -lprofiler 7 | ## PKG_LIBS+= -lprofiler 8 | ## 9 | 10 | PKG_CXXFLAGS=$(SHLIB_OPENMP_CXXFLAGS) 11 | -------------------------------------------------------------------------------- /src/RcppExports.cpp: -------------------------------------------------------------------------------- 1 | // Generated by using Rcpp::compileAttributes() -> do not edit by hand 2 | // Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 3 | 4 | #include 5 | #include 6 | 7 | using namespace Rcpp; 8 | 9 | #ifdef RCPP_USE_GLOBAL_ROSTREAM 10 | Rcpp::Rostream& Rcpp::Rcout = Rcpp::Rcpp_cout_get(); 11 | Rcpp::Rostream& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get(); 12 | #endif 13 | 14 | // DEoptim_impl 15 | Rcpp::List DEoptim_impl(const arma::colvec& minbound, const arma::colvec& maxbound, SEXP fnS, const Rcpp::List& control, SEXP rhoS); 16 | RcppExport SEXP _RcppDE_DEoptim_impl(SEXP minboundSEXP, SEXP maxboundSEXP, SEXP fnSSEXP, SEXP controlSEXP, SEXP rhoSSEXP) { 17 | BEGIN_RCPP 18 | Rcpp::RObject rcpp_result_gen; 19 | Rcpp::RNGScope rcpp_rngScope_gen; 20 | Rcpp::traits::input_parameter< const arma::colvec& >::type minbound(minboundSEXP); 21 | Rcpp::traits::input_parameter< const arma::colvec& >::type maxbound(maxboundSEXP); 22 | Rcpp::traits::input_parameter< SEXP >::type fnS(fnSSEXP); 23 | Rcpp::traits::input_parameter< const Rcpp::List& >::type control(controlSEXP); 24 | Rcpp::traits::input_parameter< SEXP >::type rhoS(rhoSSEXP); 25 | rcpp_result_gen = Rcpp::wrap(DEoptim_impl(minbound, maxbound, fnS, control, rhoS)); 26 | return rcpp_result_gen; 27 | END_RCPP 28 | } 29 | // putFunPtrInXPtr 30 | SEXP putFunPtrInXPtr(const std::string& fstr); 31 | RcppExport SEXP _RcppDE_putFunPtrInXPtr(SEXP fstrSEXP) { 32 | BEGIN_RCPP 33 | Rcpp::RObject rcpp_result_gen; 34 | Rcpp::RNGScope rcpp_rngScope_gen; 35 | Rcpp::traits::input_parameter< const std::string& >::type fstr(fstrSEXP); 36 | rcpp_result_gen = Rcpp::wrap(putFunPtrInXPtr(fstr)); 37 | return rcpp_result_gen; 38 | END_RCPP 39 | } 40 | 41 | static const R_CallMethodDef CallEntries[] = { 42 | {"_RcppDE_DEoptim_impl", (DL_FUNC) &_RcppDE_DEoptim_impl, 5}, 43 | {"_RcppDE_putFunPtrInXPtr", (DL_FUNC) &_RcppDE_putFunPtrInXPtr, 1}, 44 | {NULL, NULL, 0} 45 | }; 46 | 47 | RcppExport void R_init_RcppDE(DllInfo *dll) { 48 | R_registerRoutines(dll, NULL, CallEntries, NULL, NULL); 49 | R_useDynamicSymbols(dll, FALSE); 50 | } 51 | -------------------------------------------------------------------------------- /src/deoptim.cpp: -------------------------------------------------------------------------------- 1 | // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 2 | // 3 | // Port of DEoptim (2.0.7) to Rcpp/RcppArmadillo/Armadillo 4 | // Copyright (C) 2010 - 2015 Dirk Eddelbuettel 5 | // 6 | // DEoptim is Copyright (C) 2009 David Ardia and Katharine Mullen 7 | // and based on DE-Engine v4.0, Rainer Storn, 2004 8 | // (http://www.icsi.berkeley.edu/~storn/DeWin.zip) 9 | 10 | #include // declarations for both Rcpp and RcppArmadillo offering Armadillo classes 11 | // #include 12 | 13 | void devol(double VTR, double f_weight, double fcross, int i_bs_flag, 14 | const arma::colvec & lower, const arma::colvec & upper, SEXP fcall, SEXP rho, int i_trace, 15 | int i_strategy, int i_D, int i_NP, int i_itermax, 16 | arma::mat & initpopm, int i_storepopfreq, int i_storepopfrom, 17 | int i_specinitialpop, 18 | arma::mat & ta_popP, arma::mat & ta_oldP, arma::mat & ta_newP, arma::colvec & t_bestP, 19 | arma::colvec & ta_popC, arma::colvec & ta_oldC, arma::colvec & ta_newC, double & t_bestC, 20 | arma::colvec & t_bestitP, arma::colvec & t_tmpP, arma::mat & d_pop, Rcpp::List & d_storepop, 21 | arma::mat & d_bestmemit, arma::colvec & d_bestvalit, int & i_iterations, double i_pPct, double d_c, long & l_nfeval, 22 | double d_reltol, int i_steptol); 23 | 24 | // [[Rcpp::export]] 25 | Rcpp::List DEoptim_impl(const arma::colvec & minbound, // user-defined lower bounds 26 | const arma::colvec & maxbound, // user-defined upper bounds 27 | SEXP fnS, // function to be optimized, either R or C++ 28 | const Rcpp::List & control, // parameters 29 | SEXP rhoS) { // optional environment 30 | 31 | #if RCPP_DEV_VERSION >= RcppDevVersion(0,12,17,1) 32 | Rcpp::SuspendRNGSynchronizationScope rngScope; 33 | #endif 34 | 35 | double VTR = Rcpp::as(control["VTR"]); // value to reach 36 | int i_strategy = Rcpp::as(control["strategy"]); // chooses DE-strategy 37 | int i_itermax = Rcpp::as(control["itermax"]); // Maximum number of generations 38 | long l_nfeval = 0; // nb of function evals (NOT passed in) 39 | int i_D = Rcpp::as(control["npar"]); // Dimension of parameter vector 40 | int i_NP = Rcpp::as(control["NP"]); // Number of population members 41 | int i_storepopfrom = Rcpp::as(control["storepopfrom"]) - 1; // When to start storing populations 42 | int i_storepopfreq = Rcpp::as(control["storepopfreq"]); // How often to store populations 43 | int i_specinitialpop = Rcpp::as(control["specinitialpop"]); // User-defined inital population 44 | double f_weight = Rcpp::as(control["F"]); // stepsize 45 | double f_cross = Rcpp::as(control["CR"]); // crossover probability 46 | int i_bs_flag = Rcpp::as(control["bs"]); // Best of parent and child 47 | int i_trace = Rcpp::as(control["trace"]); // Print progress? 48 | double i_pPct = Rcpp::as(control["p"]); // p to define the top 100p% best solutions 49 | double d_c = Rcpp::as(control["c"]); // c as a trigger of the JADE algorithm 50 | double d_reltol = Rcpp::as(control["reltol"]); // tolerance for relative convergence test, default to be sqrt(.Machine$double.eps) 51 | int i_steptol = Rcpp::as(control["steptol"]); // maximum of iteration after relative convergence test is passed, default to be itermax 52 | 53 | // as above, doing it in two steps is faster 54 | Rcpp::NumericMatrix initialpopm = Rcpp::as(control["initialpop"]); 55 | arma::mat initpopm(initialpopm.begin(), initialpopm.rows(), initialpopm.cols(), false); 56 | 57 | arma::mat ta_popP(i_D, i_NP*2); // Data structures for parameter vectors 58 | arma::mat ta_oldP(i_D, i_NP); 59 | arma::mat ta_newP(i_D, i_NP); 60 | arma::colvec t_bestP(i_D); 61 | 62 | arma::colvec ta_popC(i_NP*2); // Data structures for obj. fun. values 63 | arma::colvec ta_oldC(i_NP); 64 | arma::colvec ta_newC(i_NP); 65 | double t_bestC; 66 | 67 | arma::colvec t_bestitP(i_D); 68 | arma::colvec t_tmpP(i_D); 69 | 70 | int i_nstorepop = static_cast(ceil(static_cast((i_itermax - i_storepopfrom) / i_storepopfreq))); 71 | arma::mat d_pop(i_D, i_NP); 72 | Rcpp::List d_storepop(i_nstorepop); 73 | arma::mat d_bestmemit(i_D, i_itermax); 74 | arma::colvec d_bestvalit(i_itermax); 75 | int i_iter = 0; 76 | 77 | // call actual Differential Evolution optimization given the parameters 78 | devol(VTR, f_weight, f_cross, i_bs_flag, minbound, maxbound, fnS, rhoS, i_trace, i_strategy, i_D, i_NP, 79 | i_itermax, initpopm, i_storepopfrom, i_storepopfreq, i_specinitialpop, 80 | ta_popP, ta_oldP, ta_newP, t_bestP, ta_popC, ta_oldC, ta_newC, t_bestC, t_bestitP, t_tmpP, 81 | d_pop, d_storepop, d_bestmemit, d_bestvalit, i_iter, i_pPct, d_c, l_nfeval, 82 | d_reltol, i_steptol); 83 | 84 | return Rcpp::List::create(Rcpp::Named("bestmem") = t_bestP, // and return a named list with results to R 85 | Rcpp::Named("bestval") = t_bestC, 86 | Rcpp::Named("nfeval") = l_nfeval, 87 | Rcpp::Named("iter") = i_iter, 88 | Rcpp::Named("bestmemit") = trans(d_bestmemit), 89 | Rcpp::Named("bestvalit") = d_bestvalit, 90 | Rcpp::Named("pop") = trans(d_pop), 91 | Rcpp::Named("storepop") = d_storepop); 92 | 93 | } 94 | 95 | -------------------------------------------------------------------------------- /src/devol.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Port of DEoptim (2.0.7) by Ardia et al to Rcpp/RcppArmadillo/Armadillo 3 | // Copyright (C) 2010 - 2022 Dirk Eddelbuettel 4 | // 5 | // DEoptim is Copyright (C) 2009 David Ardia and Katharine Mullen 6 | // and based on DE-Engine v4.0, Rainer Storn, 2004 7 | // (http://www.icsi.berkeley.edu/~storn/DeWin.zip) 8 | 9 | #include // declarations for both Rcpp and RcppArmadillo offering Armadillo classes 10 | #include "evaluate.h" // simple function evaluation framework 11 | // #include 12 | 13 | void permute(int ia_urn2[], int i_urn2_depth, int i_NP, int i_avoid, int ia_urntmp[]); 14 | 15 | void devol(double VTR, double f_weight, double f_cross, int i_bs_flag, 16 | const arma::colvec & fa_minbound, const arma::colvec & fa_maxbound, SEXP fcall, SEXP rho, int i_trace, 17 | int i_strategy, int i_D, int i_NP, int i_itermax, arma::mat & initialpopm, 18 | int i_storepopfrom, int i_storepopfreq, int i_specinitialpop, 19 | arma::mat &ta_popP, arma::mat &ta_oldP, arma::mat &ta_newP, arma::colvec & t_bestP, 20 | arma::colvec & ta_popC, arma::colvec & ta_oldC, arma::colvec & ta_newC, double & t_bestC, 21 | arma::colvec & t_bestitP, arma::colvec & t_tmpP, 22 | arma::mat &d_pop, Rcpp::List &d_storepop, arma::mat & d_bestmemit, arma::colvec & d_bestvalit, 23 | int & i_iterations, double i_pPct, double d_c, long & l_nfeval, 24 | double d_reltol, int i_steptol) { 25 | 26 | //ProfilerStart("/tmp/RcppDE.prof"); 27 | Rcpp::DE::EvalBase *ev = NULL; // pointer to abstract base class 28 | if (TYPEOF(fcall) == EXTPTRSXP) { // non-standard mode: we are being passed an external pointer 29 | ev = new Rcpp::DE::EvalCompiled(fcall, rho); // so assign a pointer using external pointer in fcall SEXP 30 | } else { // standard mode: env_ is an env, fcall_ is a function 31 | ev = new Rcpp::DE::EvalStandard(fcall, rho); // so assign R function and environment 32 | } 33 | const int urn_depth = 5; // 4 + one index to avoid 34 | Rcpp::NumericVector par(i_D); // initialize parameter vector to pass to evaluate function 35 | std::vector ia_urn2(urn_depth); // fixed-size vector for urn draws 36 | std::vector ia_urntmp(i_NP); // so that we don't need to re-allocated each time in permute 37 | arma::mat initialpop(i_D, i_NP); 38 | int i_nstorepop = static_cast(ceil(static_cast((i_itermax - i_storepopfrom) / i_storepopfreq))); 39 | int p_NP = round(i_pPct * i_NP); // choose at least two best solutions 40 | p_NP = p_NP < 2 ? 2 : p_NP; 41 | std::vector sortIndex(i_NP); // sorted values of ta_oldC 42 | if (i_strategy == 6) { 43 | for (int i = 0; i < i_NP; i++) 44 | sortIndex[i] = i; 45 | } 46 | 47 | initialpop.zeros(); // initialize initial popuplation 48 | d_bestmemit.zeros(); // initialize best members 49 | d_bestvalit.zeros(); // initialize best values 50 | d_pop.zeros(); // initialize best population 51 | i_nstorepop = (i_nstorepop < 0) ? 0 : i_nstorepop; 52 | 53 | if (i_specinitialpop > 0) { // if initial population provided, initialize with values 54 | initialpop = trans(initialpopm); // transpose as we prefer columns for population members here 55 | } 56 | 57 | for (int i = 0; i < i_NP; i++) { // ------Initialization----------------------------- 58 | if (i_specinitialpop <= 0) { // random initial member 59 | for (int j = 0; j < i_D; j++) { 60 | ta_popP.at(j,i) = fa_minbound[j] + ::unif_rand() * (fa_maxbound[j] - fa_minbound[j]); 61 | } 62 | } else { // or user-specified initial member 63 | ta_popP.col(i) = initialpop.col(i); 64 | } 65 | memmove(REAL(par), ta_popP.colptr(i), Rf_nrows(par) * sizeof(double)); 66 | ta_popC[i] = ev->eval(par); 67 | if (i == 0 || ta_popC[i] <= t_bestC) { 68 | t_bestC = ta_popC[i]; 69 | t_bestP = ta_popP.unsafe_col(i); 70 | } 71 | } 72 | 73 | ta_oldP = ta_popP.cols(0, i_NP-1); // ---assign pointers to current ("old") population--- 74 | ta_oldC = ta_popC.rows(0, i_NP-1); 75 | 76 | int i_iter = 0; // ------Iteration loop-------------------------------------------- 77 | int popcnt = 0; 78 | int i_iter_tol = 0; 79 | 80 | //Trigger JADE algorithm when d_c <> 0 (randomize cross-over and weight coefficient) 81 | //Using user supplied cross-over and weight coefficient as the mean of randomized coefficients 82 | 83 | double goodCR = 0, goodF = 0, goodF2 = 0, meanCR = f_cross, meanF = f_weight;////------------change 84 | int i_goodNP = 0; 85 | 86 | double rand_cross = f_cross; 87 | double rand_weight = f_weight; 88 | 89 | while ((i_iter < i_itermax) && (t_bestC > VTR) && (i_iter_tol <= i_steptol)) { // main loop ==================================== 90 | if (i_iter % i_storepopfreq == 0 && i_iter >= i_storepopfrom) { // store intermediate populations 91 | d_storepop[popcnt++] = Rcpp::wrap( trans(ta_oldP) ); 92 | } // end store pop 93 | 94 | d_bestmemit.col(i_iter) = t_bestP; // store the best member 95 | d_bestvalit[i_iter] = t_bestC; // store the best value 96 | t_bestitP = t_bestP; 97 | i_iter++; // increase iteration counter 98 | 99 | double f_dither = f_weight + ::unif_rand() * (1.0 - f_weight); // ----computer dithering factor -------------- 100 | 101 | if (i_strategy == 6) { // ---DE/current-to-p-best/1 ------------------------------------------ 102 | arma::colvec temp_oldC = ta_oldC; // create copy of ta_oldC to avoid changing it 103 | rsort_with_index( temp_oldC.memptr(), sortIndex.data(), i_NP ); // sort temp_oldC to use sortIndex 104 | } 105 | 106 | for (int i = 0; i < i_NP; i++) { // ----start of loop through ensemble------------------------ 107 | 108 | t_tmpP = ta_oldP.col(i); // t_tmpP is the vector to mutate and eventually select 109 | 110 | permute(ia_urn2.data(), urn_depth, i_NP, i, ia_urntmp.data()); // Pick 4 random and distinct 111 | 112 | //Trigger JADE algorithm when d_c <> 0 (randomize cross-over and weight coefficient) 113 | if(d_c>0){ 114 | rand_cross = R::rnorm(meanCR, 0.1);//changed from f_cross to meanCR 115 | rand_cross = rand_cross > 1.0 ? 1 : rand_cross; 116 | rand_cross = rand_cross < 0.0 ? 0 : rand_cross; 117 | do{ 118 | rand_weight = R::rcauchy(meanF, 0.1);//changed from f_weight to meanF 119 | rand_weight = rand_weight > 1.0 ? 1.0 : rand_weight; 120 | } while (rand_weight <= 0.0); 121 | } 122 | 123 | int k = 0; // loop counter used in all strategies below 124 | 125 | // ===Choice of strategy======================================================= 126 | switch (i_strategy) { 127 | 128 | case 1: { // ---classical strategy DE/rand/1/bin--------------------------------- 129 | int j = static_cast(::unif_rand() * i_D); // random parameter 130 | do { // add fluctuation to random target 131 | t_tmpP[j] = ta_oldP.at(j,ia_urn2[1]) + rand_weight * 132 | (ta_oldP.at(j,ia_urn2[2]) - ta_oldP.at(j,ia_urn2[3])); 133 | j = (j + 1) % i_D; 134 | } while ((::unif_rand() < rand_cross) && (++k < i_D)); 135 | break; 136 | } 137 | case 2: { // ---DE/local-to-best/1/bin------------------------------------------- 138 | int j = static_cast(::unif_rand() * i_D); // random parameter 139 | do { // add fluctuation to random target 140 | t_tmpP[j] = t_tmpP[j] + rand_weight * (t_bestitP[j] - t_tmpP[j]) + rand_weight * 141 | (ta_oldP.at(j,ia_urn2[2]) - ta_oldP.at(j,ia_urn2[3])); 142 | j = (j + 1) % i_D; 143 | } while ((::unif_rand() < rand_cross) && (++k < i_D)); 144 | break; 145 | } 146 | case 3: { // ---DE/best/1/bin with jitter--------------------------------------- 147 | int j = static_cast(::unif_rand() * i_D); // random parameter 148 | do { // add fluctuation to random target 149 | double f_jitter = 0.0001 * ::unif_rand() + rand_weight; 150 | t_tmpP[j] = t_bestitP[j] + f_jitter * (ta_oldP.at(j,ia_urn2[1]) - ta_oldP.at(j,ia_urn2[2])); 151 | j = (j + 1) % i_D; 152 | } while ((::unif_rand() < rand_cross) && (++k < i_D)); 153 | break; 154 | } 155 | case 4: { // ---DE/rand/1/bin with per-vector-dither---------------------------- 156 | int j = static_cast(::unif_rand() * i_D); // random parameter 157 | do { // add fluctuation to random target * 158 | t_tmpP[j] = ta_oldP.at(j,ia_urn2[1]) + (rand_weight + ::unif_rand()*(1.0 - rand_weight)) 159 | * (ta_oldP.at(j,ia_urn2[2]) - ta_oldP.at(j,ia_urn2[3])); 160 | j = (j + 1) % i_D; 161 | } while ((::unif_rand() < rand_cross) && (++k < i_D)); 162 | break; 163 | } 164 | case 5: { // ---DE/rand/1/bin with per-generation-dither------------------------ 165 | int j = static_cast(::unif_rand() * i_D); // random parameter 166 | do { // add fluctuation to random target 167 | t_tmpP[j] = ta_oldP.at(j,ia_urn2[1]) + f_dither 168 | * (ta_oldP.at(j,ia_urn2[2]) - ta_oldP.at(j,ia_urn2[3])); 169 | j = (j + 1) % i_D; 170 | } while ((::unif_rand() < rand_cross) && (++k < i_D)); 171 | break; 172 | } 173 | case 6: { // ---DE/current-to-p-best/1 (JADE)----------------------------------- 174 | int j = static_cast(::unif_rand() * i_D); // random parameter 175 | do { // add fluctuation to random target 176 | int i_pbest = sortIndex[static_cast(::unif_rand() * p_NP)]; // select from [0, 1, 2, ..., (pNP-1)] 177 | t_tmpP[j] = ta_oldP.at(j,i) + rand_weight * (ta_oldP.at(j,i_pbest) - ta_oldP.at(j,i)) + 178 | rand_weight * (ta_oldP.at(j,ia_urn2[1]) - ta_oldP.at(j,ia_urn2[2])); 179 | j = (j + 1) % i_D; 180 | } while ((::unif_rand() < rand_cross) && (++k < i_D)); 181 | break; 182 | } 183 | default: { // ---variation to DE/rand/1/bin: either-or-algorithm------------------ 184 | int j = static_cast(::unif_rand() * i_D); // random parameter 185 | if (::unif_rand() < 0.5) { // differential mutation, Pmu = 0.5 186 | do { // add fluctuation to random target */ 187 | t_tmpP[j] = ta_oldP.at(j,ia_urn2[1]) + rand_weight * (ta_oldP.at(j,ia_urn2[2]) - ta_oldP.at(j,ia_urn2[3])); 188 | j = (j + 1) % i_D; 189 | } while ((::unif_rand() < rand_cross) && (++k < i_D)); 190 | 191 | } else { // recombination with K = 0.5*(F+1) -. F-K-Rule 192 | do { // add fluctuation to random target */ 193 | t_tmpP[j] = ta_oldP.at(j,ia_urn2[1]) + 0.5 * (rand_weight + 1.0) * 194 | (ta_oldP.at(j,ia_urn2[2]) + ta_oldP.at(j,ia_urn2[3]) - 2 * ta_oldP.at(j,ia_urn2[1])); 195 | j = (j + 1) % i_D; 196 | } while ((::unif_rand() < rand_cross) && (++k < i_D)); 197 | } 198 | break; 199 | } 200 | } // end switch (i_strategy) ... 201 | 202 | for (int j = 0; j < i_D; j++) { // boundary constraints, bounce-back method was not enforcing bounds 203 | if (t_tmpP[j] < fa_minbound[j]) { 204 | t_tmpP[j] = fa_minbound[j] + ::unif_rand() * (fa_maxbound[j] - fa_minbound[j]); 205 | } 206 | if (t_tmpP[j] > fa_maxbound[j]) { 207 | t_tmpP[j] = fa_maxbound[j] - ::unif_rand() * (fa_maxbound[j] - fa_minbound[j]); 208 | } 209 | } 210 | 211 | // ------Trial mutation now in t_tmpP----------------- 212 | memmove(REAL(par), t_tmpP.memptr(), Rf_nrows(par) * sizeof(double)); 213 | double t_tmpC = ev->eval(par); // Evaluate mutant in t_tmpP 214 | if (t_tmpC <= ta_oldC[i] || i_bs_flag) { // i_bs_flag means will choose best NP later 215 | ta_newP.col(i) = t_tmpP; // replace target with mutant 216 | ta_newC[i] = t_tmpC; 217 | if (t_tmpC <= t_bestC) { 218 | t_bestP = t_tmpP; 219 | t_bestC = t_tmpC; 220 | } 221 | if (d_c > 0) { /* calculate new goodCR and goodF */ //------------Added in 222 | goodCR += rand_cross / ++i_goodNP; 223 | goodF += rand_weight; 224 | goodF2 += pow(rand_weight,2.0); 225 | } 226 | } else { 227 | ta_newP.col(i) = ta_oldP.col(i); 228 | ta_newC[i] = ta_oldC[i]; 229 | } 230 | } // End mutation loop through pop., ie the "for (i = 0; i < i_NP; i++)" 231 | 232 | if (d_c > 0) { /* calculate new meanCR and meanF */ 233 | meanCR = (1-d_c)*meanCR + d_c*goodCR; 234 | meanF = (1-d_c)*meanF + d_c*goodF2/goodF; 235 | 236 | } 237 | 238 | 239 | if (i_bs_flag) { // examine old and new pop. and take the best NP members into next generation 240 | 241 | ta_popP.cols(0, i_NP-1) = ta_oldP; 242 | ta_popC.rows(0, i_NP-1) = ta_oldC; 243 | 244 | ta_popP.cols(i_NP, 2*i_NP-1) = ta_newP; 245 | ta_popC.rows(i_NP, 2*i_NP-1) = ta_newC; 246 | 247 | int i_len = 2 * i_NP; 248 | int step = i_len, done; // array length 249 | while (step > 1) { 250 | step /= 2; // halve the step size 251 | do { 252 | done = 1; 253 | int bound = i_len - step; 254 | for (int j = 0; j < bound; j++) { 255 | int i = j + step + 1; 256 | if (ta_popC[j] > ta_popC[i-1]) { 257 | ta_popP.swap_cols(j, i-1); 258 | ta_popC.swap_rows(j, i-1); 259 | done = 0; 260 | } // if 261 | } // for 262 | } while (!done); // while 263 | } // while (step > 1) 264 | ta_newP = ta_popP.cols(0, i_NP-1); // now the best NP are in first NP places in gta_pop, use them 265 | ta_newC = ta_popC.rows(0, i_NP-1); 266 | } // i_bs_flag 267 | 268 | ta_oldP = ta_newP; // have selected NP mutants move on to next generation 269 | ta_oldC = ta_newC; 270 | 271 | // print out temporary results 272 | if ( (i_trace > 0) && ((i_iter % i_trace) == 0) ) { 273 | Rprintf("Iteration: %d bestvalit: %f bestmemit:", i_iter, t_bestC); 274 | for (int j = 0; j < i_D; j++) 275 | Rprintf("%12.6f", t_bestP[j]); 276 | Rprintf("\n"); 277 | } 278 | 279 | //check relative convergence 280 | if (std::abs(d_bestvalit[i_iter - 1] - t_bestC) < (d_reltol*(std::abs(d_bestvalit[i_iter - 1]) + d_reltol))) { 281 | i_iter_tol++; 282 | } else { 283 | i_iter_tol = 0; 284 | } 285 | 286 | } // end loop through generations 287 | 288 | d_pop = ta_oldP; 289 | i_iterations = i_iter; 290 | l_nfeval = ev->getNbEvals(); 291 | // ProfilerStop(); 292 | } 293 | -------------------------------------------------------------------------------- /src/evaluate.h: -------------------------------------------------------------------------------- 1 | 2 | // Port of DEoptim (2.0.7) by Ardia et al to Rcpp/RcppArmadillo/Armadillo 3 | // Copyright (C) 2010 - 2022 Dirk Eddelbuettel 4 | // 5 | // DEoptim is Copyright (C) 2009 David Ardia and Katharine Mullen 6 | 7 | #ifndef Rcpp_DE_evaluate_h_ 8 | #define Rcpp_DE_evaluate_h_ 9 | 10 | #include 11 | 12 | namespace Rcpp { 13 | namespace DE { 14 | 15 | class EvalBase { 16 | public: 17 | EvalBase() : neval(0) {}; 18 | virtual double eval(NumericVector par) = 0; 19 | unsigned long getNbEvals() { return neval; } 20 | protected: 21 | unsigned long int neval; 22 | }; 23 | 24 | class EvalStandard : public EvalBase { 25 | public: 26 | EvalStandard(SEXP fcall_, SEXP env_) : fcall(fcall_), env(env_) {} 27 | double eval(NumericVector par) { 28 | neval++; 29 | return defaultfun(par); 30 | } 31 | private: 32 | SEXP fcall, env; 33 | double defaultfun(NumericVector par) { // essentialy same as the old evaluate 34 | Rcpp::Shield fn(::Rf_lang3(fcall, par, R_DotsSymbol)); 35 | Rcpp::Shield sexp_fvec(::Rf_eval(fn, env)); 36 | double f_result = REAL(sexp_fvec)[0]; 37 | if (ISNAN(f_result)) 38 | ::Rf_error("NaN value of objective function! \nPerhaps adjust the bounds."); 39 | return(f_result); 40 | } 41 | }; 42 | 43 | typedef double (*funcPtr)(NumericVector); 44 | 45 | class EvalCompiled : public EvalBase { 46 | public: 47 | EvalCompiled(Rcpp::XPtr xptr, SEXP env_) { 48 | funptr = *(xptr); 49 | env = env_; 50 | }; 51 | EvalCompiled(SEXP xps, SEXP env_) { 52 | Rcpp::XPtr xptr(xps); 53 | funptr = *(xptr); 54 | env = env_; 55 | }; 56 | double eval(NumericVector par) { 57 | neval++; 58 | return funptr(par); //, env); 59 | } 60 | private: 61 | funcPtr funptr; 62 | SEXP env; 63 | }; 64 | 65 | } 66 | 67 | } 68 | 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /src/exampleFunctions.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Port of DEoptim (2.0.7) by Ardia et al to Rcpp/RcppArmadillo/Armadillo 3 | // Copyright (C) 2010 - 2022 Dirk Eddelbuettel 4 | // 5 | // DEoptim is Copyright (C) 2009 David Ardia and Katharine Mullen 6 | 7 | #include 8 | 9 | double genrose(Rcpp::NumericVector x) { // genrose function in C++ 10 | const double a = 1.0; 11 | const double b = 100.0; 12 | int n = x.size(); 13 | double sum = 1.0; 14 | for (int i=1; i(new funcPtr(&genrose))); 46 | else if (fstr == "wild") 47 | return(Rcpp::XPtr(new funcPtr(&wild))); 48 | else 49 | return(Rcpp::XPtr(new funcPtr(&rastrigin))); 50 | } 51 | -------------------------------------------------------------------------------- /src/permute.cpp: -------------------------------------------------------------------------------- 1 | // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- 2 | // 3 | // Port of DEoptim (2.0.7) by Ardia et al to Rcpp/RcppArmadillo/Armadillo 4 | // Copyright (C) 2010 - 2015 Dirk Eddelbuettel 5 | // 6 | // DEoptim is Copyright (C) 2009 David Ardia and Katharine Mullen 7 | // and based on DE-Engine v4.0, Rainer Storn, 2004 8 | // (http://www.icsi.berkeley.edu/~storn/DeWin.zip) 9 | 10 | #include 11 | 12 | // Function : void permute(int ia_urn2[], int i_urn2_depth) 13 | // Author : Rainer Storn (w/bug fixes contributed by DEoptim users) 14 | // Description : Generates i_urn2_depth random indices ex [0, i_NP-1] 15 | // which are all distinct. This is done by using a 16 | // permutation algorithm called the "urn algorithm" 17 | // which goes back to C.L.Robinson. 18 | // Functions : - 19 | // Globals : - 20 | // Parameters : ia_urn2 (O) array containing the random indices 21 | // i_urn2_depth (I) number of random indices (avoided index included) 22 | // i_NP (I) range of indices is [0, i_NP-1] 23 | // i_avoid (I) is the index to avoid and is located in ia_urn2[0]. 24 | // ia_urn1 (I) additional temp vector 25 | // Preconditions : # Make sure that ia_urn2[] has a length of i_urn2_depth. 26 | // # i_urn2_depth must be smaller than i_NP. 27 | // Postconditions : # the index to be avoided is in ia_urn2[0], so fetch the 28 | // indices from ia_urn2[i], i = 1, 2, 3, ..., i_urn2_depth. 29 | // Return Value : - 30 | void permute(int ia_urn2[], int i_urn2_depth, int i_NP, int i_avoid, int ia_urn1[]) { 31 | int k = i_NP; 32 | int i_urn1 = 0; 33 | int i_urn2 = 0; 34 | for (int i = 0; i < i_NP; i++) 35 | ia_urn1[i] = i; /* initialize urn1 */ 36 | 37 | i_urn1 = i_avoid; /* get rid of the index to be avoided and place it in position 0. */ 38 | while (k > i_NP - i_urn2_depth) { /* i_urn2_depth is the amount of indices wanted (must be <= NP) */ 39 | ia_urn2[i_urn2] = ia_urn1[i_urn1]; /* move it into urn2 */ 40 | ia_urn1[i_urn1] = ia_urn1[k-1]; /* move highest index to fill gap */ 41 | k = k - 1; /* reduce number of accessible indices */ 42 | i_urn2 = i_urn2 + 1; /* next position in urn2 */ 43 | i_urn1 = static_cast(::unif_rand() * k); /* choose a random index */ 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /tests/compTest.R: -------------------------------------------------------------------------------- 1 | 2 | Wild <- function(x) { ## 'Wild' function, global minimum at about -15.81515 3 | sum(10 * sin(0.3 * x) * sin(1.3 * x^2) + 0.00001 * x^4 + 0.2 * x + 80)/length(x) 4 | } 5 | 6 | Rastrigin <- function(x) { 7 | sum(x+2 - 10 * cos(2*pi*x)) + 20 8 | } 9 | 10 | Genrose <- function(x, a = 1, b = 100) { ## One generalization of the Rosenbrock banana valley function (n parameters) 11 | n <- length(x) 12 | 1.0 + sum (b * (x[-n]^2 - x[-1])^2 + (x[-1] - a)^2) 13 | } 14 | 15 | maxIt <- 25 # not excessive but so that we get some run-time on simple problems 16 | 17 | haveDEoptim <- requireNamespace("DEoptim", quietly=TRUE) 18 | 19 | suppressMessages({ 20 | if (haveDEoptim) { 21 | library(DEoptim) # the original, currently 2.0.7 22 | } 23 | library(RcppDE) # the contender 24 | }) 25 | 26 | basicDE <- function(n, maxIt, fun, ...) DEoptim::DEoptim(fn=fun, lower=rep(-25, n), upper=rep(25, n), 27 | control=list(NP=10*n, itermax=maxIt, trace=FALSE), 28 | ...)#, bs=TRUE)) 29 | cppDE <- function(n, maxIt, fun, ...) RcppDE::DEoptim(fn=fun, lower=rep(-25, n), upper=rep(25, n), 30 | control=list(NP=10*n, itermax=maxIt, trace=FALSE), 31 | ...)#, bs=TRUE)) 32 | 33 | if (haveDEoptim) { 34 | set.seed(42) 35 | valBasic <- basicDE(5, maxIt, function(...) Rastrigin(...)) 36 | set.seed(42) 37 | valCpp <- cppDE(5, maxIt, function(...) Rastrigin(...)) 38 | ##stopifnot( all.equal(valBasic, valCpp) ) 39 | } 40 | 41 | runPair <- function(n, maxIt, fun, funname, ...) { 42 | gc() 43 | set.seed(42) 44 | bt <- system.time(invisible(ores <- basicDE(n, maxIt, fun, ...)))[3] 45 | 46 | gc() 47 | set.seed(42) 48 | xptr <- RcppDE:::putFunPtrInXPtr(funname) 49 | ct <- system.time(invisible(cres <- cppDE(n, maxIt, xptr, ...)))[3] 50 | 51 | #stopifnot(all.equal(ores, cres)) 52 | 53 | gc() 54 | set.seed(42) 55 | rt <- system.time(invisible(rres <- cppDE(n, maxIt, fun, ...)))[3] 56 | 57 | ##stopifnot(all.equal(ores, rres)) 58 | 59 | return(data.frame(DEoptim=bt, RcppDEc=ct, RcppDEr=rt)) 60 | } 61 | 62 | if (haveDEoptim) { 63 | 64 | cat("# At", format(Sys.time()), "\n") 65 | options(width=120) 66 | 67 | reps <- c(5, 10, 20, 50) 68 | 69 | res <- rbind(do.call(rbind, lapply(reps, runPair, maxIt, function(...) Rastrigin(...), "rastrigin")), 70 | do.call(rbind, lapply(reps, runPair, maxIt, function(...) Wild(...), "wild")), 71 | do.call(rbind, lapply(reps, runPair, maxIt, function(...) Genrose(...), "genrose", a = 1, b = 100)) 72 | ) 73 | res <- rbind(res, colMeans(res)) 74 | 75 | rownames(res) <- c(paste("Rastrigin", reps, sep=""), 76 | paste("Wild", reps, sep=""), 77 | paste("Genrose", reps, sep=""), 78 | "MEANS") 79 | 80 | res$ratioRcppCompToBasic <- res[,2]/res[,1] 81 | res$pctGainOfRcppComp <- (1-res[,2]/res[,1])*100 82 | ##res$netSpeedUpC <- res[,1]/res[,2] 83 | 84 | res$ratioRcppRToBasic <- res[,3]/res[,1] 85 | res$pctGainOfRcppR <- round((1-res[,3]/res[,1])*100, digits=3) 86 | ##res$netSpeedUpR <- res[,1]/res[,3] 87 | 88 | 89 | print(res) 90 | cat("# Done", format(Sys.time()), "\n") 91 | } 92 | -------------------------------------------------------------------------------- /tests/rosenbrock.R: -------------------------------------------------------------------------------- 1 | library(Rcpp) 2 | library(RcppDE) 3 | 4 | set.seed(1234) 5 | 6 | Rosenbrock <- function(x) { 7 | 8 | # briefly, calls to functions generated with Rcpp Attributes 9 | # could break optimizers due to incorrect sync of RNG state 10 | RcppDE:::putFunPtrInXPtr("dummy") 11 | 12 | x1 <- x[1] 13 | x2 <- x[2] 14 | 100 * (x2 - x1 * x1) ^ 2 + (1 - x1) ^ 2 15 | } 16 | 17 | lower <- c(-10, -10) 18 | upper <- -lower 19 | cont <- DEoptim.control(trace = 0) 20 | 21 | fit <- DEoptim(Rosenbrock, lower, upper, control = cont) 22 | 23 | if (packageVersion("Rcpp") >= "0.12.17.1") 24 | stopifnot(all.equal(fit$optim$bestmem, c(par1 = 1, par2 = 1))) 25 | -------------------------------------------------------------------------------- /vignettes/RcppDE.bib: -------------------------------------------------------------------------------- 1 | @String{CRAN = "http://cran.r-project.org/" } 2 | @String{manuals = CRAN # "doc/manuals/" } 3 | @String{RCoreTeam = "{R Development Core Team}" } 4 | @String{RFoundation = "R Foundation for Statistical Computing" } 5 | 6 | @manual{R:exts, 7 | author = RCoreTeam, 8 | organization = RFoundation, 9 | address = {Vienna, Austria}, 10 | year = 2010, 11 | isbn = {3-900051-11-9}, 12 | title = "Writing R Extensions", 13 | url = manuals # "R-exts.html" 14 | } 15 | 16 | @Manual{R:Main, 17 | title = {R: A Language and Environment for Statistical 18 | Computing}, 19 | author = RCoreTeam, 20 | organization = RFoundation, 21 | address = {Vienna, Austria}, 22 | year = 2010, 23 | note = {{ISBN} 3-900051-07-0}, 24 | url = {http://www.R-project.org/}, 25 | } 26 | 27 | @Manual{CRAN:inline, 28 | title = {inline: Inline C, C++, Fortran function calls from 29 | R}, 30 | author = {Oleg Sklyar and Duncan Murdoch and Mike Smith and 31 | Dirk Eddelbuettel and Romain Fran\c{c}ois}, 32 | year = 2010, 33 | note = {R package version 0.3.7}, 34 | url = CRAN # "/package=inline" 35 | } 36 | 37 | @Manual{CRAN:Rcpp, 38 | title = {Rcpp {R/C++} interface package}, 39 | author = {Dirk Eddelbuettel and Romain Fran\c{c}ois}, 40 | year = {2010}, 41 | note = {R package version 0.8.8}, 42 | url = {http://CRAN.R-project.org/package=Rcpp} 43 | } 44 | 45 | @Manual{CRAN:RcppArmadillo, 46 | title = {RcppArmadillo: Rcpp integration for Armadillo 47 | templated linear algebra library}, 48 | author = {Romain Fran\c{c}ois and Dirk Eddelbuettel and 49 | Douglas Bates}, 50 | year = 2010, 51 | note = {R package version 0.2.9}, 52 | url = CRAN # "/package=RcppArmadillo" 53 | } 54 | 55 | @Manual{CRAN:DEoptim, 56 | title = {DEoptim: Global optimization by differential evolution}, 57 | author = {David Ardia and Katharine Mullen and Brian Peterson and Joshua Ulrich}, 58 | year = 2010, 59 | note = {R package version 2.0-7}, 60 | url = CRAN # "/package=DEoptim" 61 | } 62 | 63 | @Unpublished{MullenArdiaEtAl:2009:DEoptim, 64 | author = {Katherine M. Mullen and David Ardia and David L. Gil 65 | and Donald Windover and James Cline}, 66 | title = {{DEoptim}: An 'R' Package for Global Optimization by 67 | Differential Evolution}, 68 | note = {Unpublished Manuscript}, 69 | year = 2009, 70 | url = "http://ssrn.com/abstract=1526466" 71 | } 72 | 73 | @Unpublished{ArdiaBoudtCarlEtAl:2010:DEoptim, 74 | author = {David Ardia and Kris Boudt and Peter Carl and 75 | Katherine M. Mullen and Brian G. Peterson}, 76 | title = {Differential Evolution (DEoptim) for Non-Convex 77 | Portfolio Optimization}, 78 | note = {Unpublished Manuscript}, 79 | year = 2010, 80 | url = "http://ssrn.com/abstract=1584905" 81 | } 82 | 83 | @Book{PriceStornLampinen:2006:DE, 84 | author = {Kenneth V. Price and Rainer M. Storn and Jouni 85 | A. Lampinen}, 86 | title = {Differential Evolution -- A Practical Approach to 87 | Global Optimization}, 88 | publisher = {Springer}, 89 | year = 2006, 90 | address = {Berlin and Heidelberg}, 91 | note = {ISBN 3540209506} 92 | } 93 | 94 | @conference{VesterstromThomsen:2004:Comparative, 95 | title = {A comparative study of differential evolution, 96 | particle swarm optimization, and evolutionary 97 | algorithms on numerical benchmark problems}, 98 | author = {Jacob S. Vesterstrom and Ren\'{e} Thomsen}, 99 | booktitle = {CEC2004: Congress on Evolutionary Computation, 2004}, 100 | volume = {2}, 101 | pages = {1980--1987}, 102 | isbn = {0780385152}, 103 | year = {2004}, 104 | url = "http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=1331139", 105 | organization = {IEEE} 106 | } 107 | 108 | @article{StornPrice:1997:Differential, 109 | title = {Differential evolution--a simple and efficient 110 | heuristic for global optimization over continuous 111 | spaces}, 112 | author = {Rainer M. Storn and Kenneth V. Price}, 113 | journal = {Journal of Global Optimization}, 114 | volume = {11}, 115 | number = {4}, 116 | pages = {341--359}, 117 | issn = {0925-5001}, 118 | year = {1997}, 119 | url = "http://www.springerlink.com/content/x555692233083677/fulltext.pdf", 120 | publisher = {Springer} 121 | } 122 | 123 | 124 | @TechReport{Sanderson:2010:Armadillo, 125 | author = {Conrad Sanderson}, 126 | title = {{Armadillo}: {An} open source {C++} Algebra Library 127 | for Fast Prototyping and Computationally Intensive 128 | Experiments }, 129 | institution = {{NICTA}}, 130 | year = 2010, 131 | url = "http://arma.sf.net" 132 | } 133 | 134 | @article{MullenKrayzmanLevin:2010:Atomic, 135 | author = "Katharine M. Mullen and Victor Krayzman and Igor 136 | Levin", 137 | title = "{Atomic structure analysis at the nanoscale using 138 | the pair distribution function: simulation studies 139 | of simple elemental nanoparticles}", 140 | journal = "Journal of Applied Crystallography", 141 | year = 2010, 142 | volume = 43, 143 | number = 3, 144 | pages = "483--490", 145 | month = "June", 146 | doi = {10.1107/S0021889810008460}, 147 | url = {http://dx.doi.org/10.1107/S0021889810008460}, 148 | } 149 | 150 | @Article{BoernerHigginsKantelhardt:2007:Rainfall, 151 | author = {Jan B\"{o}rner and Steven I. Higgins and Jochen 152 | Kantelhardt and Simon Scheiter}, 153 | title = {Rainfall or price variability: what determines 154 | rangeland management decision? A 155 | simulation-optimization approach to South African 156 | savannas}, 157 | journal = {Agricultural Economics}, 158 | year = 2007, 159 | volume = 37, 160 | number = {2-3}, 161 | pages = {189--200}, 162 | month = {September/November}, 163 | doi = {doi: 10.1111/j.1574-0862.2007.00265.x} 164 | } 165 | 166 | @InProceedings{BoudtPetersonCarl:2008:HFPortfolio, 167 | author = {Kris Boudt and Brian G. Peterson and Peter Carl}, 168 | title = {Hedge fund portfolio selection with modified 169 | expected shortfall}, 170 | booktitle = {Computational Finance and its Applications III}, 171 | year = 2008, 172 | editor = {M. Costantino and M. Larran and C.A. Brebbia}, 173 | volume = 41, 174 | series = {WIT Transactions on Information and Communications 175 | Technologies}, 176 | address = {Southampton, UK}, 177 | month = {May}, 178 | publisher = {WIT Press} 179 | } 180 | 181 | @Manual{CRAN:micEconCES, 182 | title = {micEconCES: Analysis with the Constant Elasticity of Scale (CES) function}, 183 | author = {Arne Henningsen and Geraldine Henningsen}, 184 | year = 2010, 185 | note = {R package version 0.6-8}, 186 | url = CRAN # "/package=micEconCES" 187 | } 188 | 189 | @Manual{CRAN:selectMeta, 190 | title = {selectMeta: Estimation weight function in meta analysis}, 191 | author = {Kaspar Rufibach}, 192 | year = 2010, 193 | note = {R package version 1.0.1}, 194 | url = CRAN # "/package=selectMeta" 195 | } 196 | -------------------------------------------------------------------------------- /vignettes/jsslogo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eddelbuettel/rcppde/cdc0b8286476c8da6b0307ff56b3980f62ce1d7d/vignettes/jsslogo.jpg --------------------------------------------------------------------------------