# \donttest{
# NOTE: Examples were excluded from testing
# to reduce package check time.
# Use a preset seed so test values are reproducible.
set.seed(1234)
# Bound-constrained optimization
# Griewank function
#
# -600 <= xi <= 600, i = {1, 2, ..., n}
# The function has a global minimum located at
# x* = (0, 0, ..., 0) with f(x*) = 0. Number of local minima
# for arbitrary n is unknown, but in the two dimensional case
# there are some 500 local minima.
#
# Source:
# Ali, M. Montaz, Khompatraporn, Charoenchai, and
# Zabinsky, Zelda B. (2005).
# A numerical evaluation of several stochastic algorithms
# on selected continuous global optimization test problems.
# Journal of Global Optimization 31, 635-672.
# https://doi.org/10.1007/s10898-004-9972-2
griewank <- function(x) {
1 + crossprod(x)/4000 - prod( cos(x/sqrt(seq_along(x))) )
}
JDEoptim(rep(-600, 10), rep(600, 10), griewank,
tol = 1e-7, trace = TRUE, triter = 50)
# Nonlinear constrained optimization
# 0 <= x1 <= 34, 0 <= x2 <= 17, 100 <= x3 <= 300
# The global optimum is
# (x1, x2, x3; f) = (0, 16.666667, 100; 189.311627).
#
# Source:
# Westerberg, Arthur W., and Shah, Jigar V. (1978).
# Assuring a global optimum by the use of an upper bound
# on the lower (dual) bound.
# Computers and Chemical Engineering 2, 83-92.
# https://doi.org/10.1016/0098-1354(78)80012-X
fcn <-
list(obj = function(x) {
35*x[1]^0.6 + 35*x[2]^0.6
},
eq = 2,
con = function(x) {
x1 <- x[1]; x3 <- x[3]
c(600*x1 - 50*x3 - x1*x3 + 5000,
600*x[2] + 50*x3 - 15000)
})
JDEoptim(c(0, 0, 100), c(34, 17, 300),
fn = fcn$obj, constr = fcn$con, meq = fcn$eq,
tol = 1e-7, trace = TRUE, triter = 50)
# Designing a pressure vessel
# Case A: all variables are treated as continuous
#
# 1.1 <= x1 <= 12.5*, 0.6 <= x2 <= 12.5*,
# 0.0 <= x3 <= 240.0*, 0.0 <= x4 <= 240.0
# Roughly guessed*
# The global optimum is (x1, x2, x3, x4; f) =
# (1.100000, 0.600000, 56.99482, 51.00125; 7019.031).
#
# Source:
# Lampinen, Jouni, and Zelinka, Ivan (1999).
# Mechanical engineering design optimization
# by differential evolution.
# In: David Corne, Marco Dorigo and Fred Glover (Editors),
# New Ideas in Optimization, McGraw-Hill, pp 127-146
pressure_vessel_A <-
list(obj = function(x) {
x1 <- x[1]; x2 <- x[2]; x3 <- x[3]; x4 <- x[4]
0.6224*x1*x3*x4 + 1.7781*x2*x3^2 +
3.1611*x1^2*x4 + 19.84*x1^2*x3
},
con = function(x) {
x1 <- x[1]; x2 <- x[2]; x3 <- x[3]; x4 <- x[4]
c(0.0193*x3 - x1,
0.00954*x3 - x2,
750.0*1728.0 - pi*x3^2*x4 - 4/3*pi*x3^3)
})
JDEoptim(c( 1.1, 0.6, 0.0, 0.0),
c(12.5, 12.5, 240.0, 240.0),
fn = pressure_vessel_A$obj,
constr = pressure_vessel_A$con,
tol = 1e-7, trace = TRUE, triter = 50)
# Mixed integer nonlinear programming
# Designing a pressure vessel
# Case B: solved according to the original problem statements
# steel plate available in thicknesses multiple
# of 0.0625 inch
#
# wall thickness of the
# shell 1.1 [18*0.0625] <= x1 <= 12.5 [200*0.0625]
# heads 0.6 [10*0.0625] <= x2 <= 12.5 [200*0.0625]
# 0.0 <= x3 <= 240.0, 0.0 <= x4 <= 240.0
# The global optimum is (x1, x2, x3, x4; f) =
# (1.125 [18*0.0625], 0.625 [10*0.0625],
# 58.29016, 43.69266; 7197.729).
pressure_vessel_B <-
list(obj = function(x) {
x1 <- floor(x[1])*0.0625
x2 <- floor(x[2])*0.0625
x3 <- x[3]; x4 <- x[4]
0.6224*x1*x3*x4 + 1.7781*x2*x3^2 +
3.1611*x1^2*x4 + 19.84*x1^2*x3
},
con = function(x) {
x1 <- floor(x[1])*0.0625
x2 <- floor(x[2])*0.0625
x3 <- x[3]; x4 <- x[4]
c(0.0193*x3 - x1,
0.00954*x3 - x2,
750.0*1728.0 - pi*x3^2*x4 - 4/3*pi*x3^3)
})
res <- JDEoptim(c( 18, 10, 0.0, 0.0),
c(200+1, 200+1, 240.0, 240.0),
fn = pressure_vessel_B$obj,
constr = pressure_vessel_B$con,
tol = 1e-7, trace = TRUE, triter = 50)
res
# Now convert to integer x1 and x2
c(floor(res$par[1:2]), res$par[3:4])
# }
Run the code above in your browser using DataLab