# Parallel example (change to cores to your own requirements in the example below)
# Always load snowfall manually as it might fail if loaded by the program on request
library(Rsolnp)
library(snowfall)
gofn = function(dat, n)
{
x = dat[1:n]
y = dat[(n+1):(2*n)]
z = dat[(2*n+1):(3*n)]
ii = matrix(1:n, ncol = n, nrow = n, byrow = TRUE)
jj = matrix(1:n, ncol = n, nrow = n)
ij = which(ii<jj, arr.ind = TRUE)
i = ij[,1]
j = ij[,2]
# Coulomb potential
potential = sum(1.0/sqrt((x[i]-x[j])^2 + (y[i]-y[j])^2 + (z[i]-z[j])^2))
potential
}
goeqfn = function(dat, n)
{
x = dat[1:n]
y = dat[(n+1):(2*n)]
z = dat[(2*n+1):(3*n)]
apply(cbind(x^2, y^2, z^2), 1, "sum")
}
n = 25
LB = rep(-1, 3*n)
UB = rep(1, 3*n)
eqB = rep(1, n)
sp = startpars(pars = NULL, fixed = NULL, fun = gofn , eqfun = goeqfn , eqB = eqB, ineqfun = NULL, ineqLB = NULL,
ineqUB = NULL, LB = LB, UB = UB, distr = rep(1, length(LB)), distr.opt = list(),
n.sim = 2000, parallel = TRUE, parallel.control = list(pkg = c("snowfall"), cores = 2),
rseed = 100, bestN = 15, eval.type = 2, n = 25)
# the last column is the value of the evaluated function (here it is the barrier function since eval.type = 2)
print(round(apply(sp, 2, "mean"), 3))
# remember to remove the last column
ans = solnp(pars=sp[1,-76],fun = gofn , eqfun = goeqfn , eqB = eqB, ineqfun = NULL, ineqLB = NULL,
ineqUB = NULL, LB = LB, UB = UB, n = 25)
# should get a value of around 243.8162
Run the code above in your browser using DataLab