Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save smackesey/bfc4870343020f38f138 to your computer and use it in GitHub Desktop.
Save smackesey/bfc4870343020f38f138 to your computer and use it in GitHub Desktop.
#!/usr/bin/env Rscript
library("GenSA")
ntrials = 1
nparams = 22
bounds_vector <- c(
300000, 4000000,
40, 2000,
50, 19000,
150, 60000,
150, 60000,
60, 80000,
60, 80000,
50, 19000,
60, 80000,
90, 34000,
60, 80000,
90, 34000,
90, 34000,
60, 80000,
90, 34000,
60, 80000,
60, 80000,
50, 19000,
50, 19000,
50, 19000,
60, 80000,
60, 80000
)
bounds <- array(bounds_vector, dim=c(2,nparams))
lower <- bounds[1,]
upper <- bounds[2,]
obj_func <- function(x) {
1*(abs(2*x[8] + 2*x[18] - 1*x[19] - 1*x[20]) / (2*x[8] + 2*x[18] + 1*x[19] + 1*x[20])) +
1*(abs(11*x[19] - 6*x[20]) / (11*x[19] + 6*x[20])) +
1*(abs(-6*x[4] + 35*x[5]) / (6*x[4] + 35*x[5])) +
1*(abs(3*x[4] - 9*x[5]) / (3*x[4] + 9*x[5])) +
1*(abs(-17*x[19] + 14*x[20]) / (17*x[19] + 14*x[20])) +
1*(abs(6*x[6] - 1*x[7] - 1*x[9]) / (6*x[6] + 1*x[7] + 1*x[9])) +
1*(abs(-5*x[6] + 1*x[10] + 1*x[11] + 1*x[12] + 1*x[13] + 1*x[14] + 1*x[15] + 1*x[16] + 1*x[17] + 1*x[21] + 1*x[22]) / (5*x[6] + 1*x[10] + 1*x[11] + 1*x[12] + 1*x[13] + 1*x[14] + 1*x[15] + 1*x[16] + 1*x[17] + 1*x[21] + 1*x[22])) +
1*(abs(-7*x[7] - 7*x[9] + 6*x[10]) / (7*x[7] + 7*x[9] + 6*x[10])) +
1*(abs(4*x[10] - 7*x[13]) / (4*x[10] + 7*x[13])) +
1*(abs(-8*x[7] - 8*x[9] + 4*x[11] + 4*x[12] + 4*x[13] + 4*x[16]) / (8*x[7] + 8*x[9] + 4*x[11] + 4*x[12] + 4*x[13] + 4*x[16])) +
1*(abs(-6*x[11] - 6*x[12] - 6*x[13] + 8*x[14] + 8*x[15] - 6*x[16] + 8*x[21]) / (6*x[11] + 6*x[12] + 6*x[13] + 8*x[14] + 8*x[15] + 6*x[16] + 8*x[21])) +
1*(abs(-11*x[14] - 11*x[15] - 11*x[21] + 6*x[22]) / (11*x[14] + 11*x[15] + 11*x[21] + 6*x[22])) +
1*(abs(11*x[17] - 11*x[22]) / (11*x[17] + 11*x[22])) +
1*(abs(169*x[3] - 64*x[8] - 64*x[18]) / (169*x[3] + 64*x[8] + 64*x[18])) +
5*(abs(-16801 + 1*x[10] + 1*x[12] + 1*x[13] + 1*x[15]) / (16801 + 1*x[10] + 1*x[12] + 1*x[13] + 1*x[15])) +
5*(abs(-46734 + 1*x[2] + 1*x[4] + 1*x[5] + 1*x[10] + 1*x[12] + 1*x[13] + 1*x[15]) / (46734 + 1*x[2] + 1*x[4] + 1*x[5] + 1*x[10] + 1*x[12] + 1*x[13] + 1*x[15])) +
5*(abs(-35900 + 1*x[6] + 1*x[7] + 1*x[8] + 1*x[9] + 1*x[10] + 1*x[11] + 1*x[12] + 1*x[13] + 1*x[14] + 1*x[15] + 1*x[16] + 1*x[17] + 1*x[18] + 1*x[19] + 1*x[20] + 1*x[21] + 1*x[22]) / (35900 + 1*x[6] + 1*x[7] + 1*x[8] + 1*x[9] + 1*x[10] + 1*x[11] + 1*x[12] + 1*x[13] + 1*x[14] + 1*x[15] + 1*x[16] + 1*x[17] + 1*x[18] + 1*x[19] + 1*x[20] + 1*x[21] + 1*x[22])) +
5*(abs(-47*x[6] - 47*x[7] - 47*x[9] + 26*x[10] - 47*x[11] + 26*x[12] + 26*x[13] - 47*x[14] + 26*x[15] - 47*x[16] - 47*x[17] - 47*x[21] - 47*x[22]) / (47*x[6] + 47*x[7] + 47*x[9] + 26*x[10] + 47*x[11] + 26*x[12] + 26*x[13] + 47*x[14] + 26*x[15] + 47*x[16] + 47*x[17] + 47*x[21] + 47*x[22])) +
5*(abs(47*x[8] - 27*x[10] - 27*x[12] - 27*x[13] - 27*x[15] + 47*x[18] + 47*x[19] + 47*x[20]) / (47*x[8] + 27*x[10] + 27*x[12] + 27*x[13] + 27*x[15] + 47*x[18] + 47*x[19] + 47*x[20])) +
5*(abs(-90 + 1*x[2]) / (90 + 1*x[2])) +
5*(abs(-21542 + 1*x[4] + 1*x[5]) / (21542 + 1*x[4] + 1*x[5])) +
5*(abs(-1200000 + 1*x[1] + 1*x[6] + 1*x[7] + 1*x[9] + 1*x[11] + 1*x[13] + 1*x[14] + 1*x[16] + 1*x[17] + 1*x[21] + 1*x[22]) / (1200000 + 1*x[1] + 1*x[6] + 1*x[7] + 1*x[9] + 1*x[11] + 1*x[13] + 1*x[14] + 1*x[16] + 1*x[17] + 1*x[21] + 1*x[22])) +
5*(abs(-2*x[1] + 98*x[6] + 98*x[7] + 98*x[9] + 98*x[11] + 98*x[14] + 98*x[16] + 98*x[17] + 98*x[21] + 98*x[22]) / (2*x[1] + 98*x[6] + 98*x[7] + 98*x[9] + 98*x[11] + 98*x[14] + 98*x[16] + 98*x[17] + 98*x[21] + 98*x[22])) +
5*(abs(-42*x[3] + 58*x[8] + 58*x[18] + 58*x[19] + 58*x[20]) / (42*x[3] + 58*x[8] + 58*x[18] + 58*x[19] + 58*x[20]))
}
control <- list( temperature = 130, max.time=900, smooth=TRUE )
results = matrix(nrow=ntrials, ncol=nparams)
for (i in 1:ntrials) {
# compute random initial values
initial = vector("numeric", length(lower))
for (j in 1:length(lower)) { initial[j] <- runif(1, lower[j], upper[j]) }
# run one trial of generalized simulated annealing
print(paste("iteration ", i))
res <- unlist(GenSA(par=initial, lower=lower, upper=upper, fn=obj_func, control=control)["par"])
results[i,] <- as.integer(res)
}
write.csv(results, file='', row.names=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment