12.2 Standard Test Functions for Optimization Algorithms

Some of them are …

# Extended Rosenbrock Function
Rosenbrock = function(x)
{
# Extern: DimX
# DimX must be positive multiple of 2
  f = vector(length=DimX)

  for (i in 1:(DimX/2)) {
    f[2*i-1] = 10 * (x[2*i] - x[2*i-1]^2)
    f[2*i] = 1 - x[2*i-1]
  }
  
  return(sum(f^2))
}

InitRosen = function(n=2)
{
  DimX <<- n
  x0 = vector(length=n+2)
  for (i in 1:((n+2)/3)) {
    x0[3*i-2] = -1
    x0[3*i-1] = 2
    x0[3*i] = 1
  }
  return(x0[1:n])
}

############################################
# Extended Powell Singular Function
Powell = function(x)
{
# Extern: DimX
# DimX must be positive multiple of 4
  f = vector(length=DimX)

  for (i in 1:(DimX/4)) {
    f[4*i-3] = x[4*i-3] + 10*x[4*i-2]
    f[4*i-2] = sqrt(5)*(x[4*i-1] - x[4*i])
    f[4*i-1] = (x[4*i-2] - 2*x[4*i-1])^2
    f[4*i]   = sqrt(10)*(x[4*i-3]-x[4*i])^2
  }

  return(sum(f^2))
}

InitPowell = function(n=4)
{
# n must be positive multiple of 4
# gradient and hessian are singular
  DimX <<- n
  x0 = vector(length=n)
  for (i in 1:(n/4)) {
    x0[4*i-3] = 3
    x0[4*i-2] = -1
    x0[4*i-1] = 0
    x0[4*i]   = 1
  }
  return(x0)
}

############################################
# Trigonometric Function
Trig = function(x)
{
# Extern: DimX
# DimX : any positive integer, typically 10
  f = vector(length=DimX)
  subf = vector(length=DimX)
  
  for (i in 1:DimX) {
    for (j in 1:DimX) subf[j] = cos(x[j]) + i*(1-cos(x[i])) - sin(x[i])
    f[i] = DimX - sum(subf) 
  }
  
  return(sum(f^2))  
}

############################################
# Helical Valley Function
Valley = function(x)
{
  if (x[1] > 0) {
    phi = 1 / (2*pi) * atan(x[2]/x[1])
  } else if (x[1] < 0) {
    phi = 1 / (2*pi) * atan(x[2]/x[1]) + 0.5
  } else {
    phi = 0
  }
  f1 = 10 * (x[3] - 10 * phi)
  f2 = 10 * (sqrt(x[1]^2 + x[2]^2) - 1)
  f3 = x[3]
  return(f1^2 + f2^2 + f3^2)
}

############################################
# Wood Function
Wood = function(x)
{
  f = 100 * (x[1]^2 - x[2])^2 + (1-x[1])^2 + 90*(x[3]^2-x[4])^2 + (1-x[3])^2 + 
      10.1 * ((1-x[2])^2+(1-x[4])^2) + 19.8 * (1-x[2])*(1-x[4])
  return(f^2)
}