Skip to content

Instantly share code, notes, and snippets.

@smackesey
Last active August 29, 2015 14:10
Show Gist options
  • Save smackesey/24f9ebca025ca17999bb to your computer and use it in GitHub Desktop.
Save smackesey/24f9ebca025ca17999bb to your computer and use it in GitHub Desktop.
#!/usr/bin/env Rscript
library("GenSA")
ntrials = 60
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) {
abs( 2*x[8]+2*x[18]-x[19]-x[20] ) +
abs( 11*x[19]-6*x[20]) +
abs( -6*x[4]+35*x[5] ) +
abs( 3*x[4]-9*x[5] ) +
abs( -17*x[19]+14*x[20] ) +
abs( 6*x[6]-x[7]-x[8] ) +
abs( -5*x[6]+x[10]+x[11]+x[12]+x[13]+x[14]+x[15]+x[16]+x[17]+x[21]+x[22] ) +
abs( -7*x[7]-7*x[8]+6*x[10] ) +
abs( 4*x[10]-7*x[13] ) +
abs( -8*x[7]-8*x[8]+4*x[11]+4*x[12]+4*x[13]+4*x[16] ) +
abs( -6*x[11]-6*x[12]-6*x[13]+8*x[14]+8*x[15]-6*x[16]+8*x[21] ) +
abs( -11*x[14]-11*x[15]-11*x[21]+6*x[22] ) +
abs( 11*x[17]-11*x[22] ) +
abs( 169*x[3]-64*x[8]-64*x[18] ) +
5*abs( x[10]+x[12]+x[13]+x[15]-16801 ) +
5*abs( x[2]+x[4]+x[5]+x[10]+x[12]+x[13]+x[15]-46734 ) +
5*abs( x[6]+x[7]+x[8]+x[9]+x[10]+x[11]+x[12]+x[13]+x[14]+x[15]+x[16]+x[17]+x[18]+x[19]+x[20]+x[21]+x[22]-35900 ) +
5*abs( -9.4*x[6]-9.4*x[7]-9.4*x[9]+26*x[10]-9.4*x[11]+26*x[12]+26*x[13]-9.4*x[14]+26*x[15]-9.4*x[16]-9.4*x[17]-9.4*x[21]-9.4*x[22] ) +
5*abs( 47*x[8]-5.4*x[10]-5.4*x[12]-5.4*x[13]-5.4*x[15]+47*x[18]+47*x[19]+47*x[20] ) +
5*abs( x[2]-90 ) +
5*abs( x[4]+x[5]-21542 ) +
5*abs( x[1]+x[6]+x[7]+x[8]+x[11]+x[13]+x[14]+x[16]+x[17]+x[21]+x[22]-1200000 ) +
5*abs( -2*x[1]+98*x[6]+98*x[7]+98*x[8]+98*x[11]+98*x[13]+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] )
}
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='/Users/smackesey/stm/tmp/rcounts.csv', row.names=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment