sann: Simulated Anneal Optimization

Description Usage Arguments Details Value Author(s) Examples

Description

sann performs an optimization using a simulated annealing algorithm.

Usage

1
sann(start.seq, fn, gr, maxit=10000, REPORT=10, tmax=10, temp=10)

Arguments

start.seq

is a vector of values representing the initial starting sequence

fn

is the function to be optimized

gr

is the function for selection of new points in the sequence

maxit

is the maximum number of iteration

REPORT

is how frequently (number of iterations) status information is reported to the screen

tmax

is the maximum number of trials per temperature

temp

is the starting temperature

Details

need to fill in!!!

Value

a list containing the optimized sequence ($sequence) of values and the solution value ($value).

Author(s)

Jeremy VanDerWal jjvanderwal@gmail.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
## Traveling salesman problem (modified from \code{optim})
library(stats) # normally loaded

eurodistmat <- as.matrix(eurodist)

distance <- function(sq) {  # Target function
	sq2 <- embed(sq, 2)
	return(as.numeric(sum(eurodistmat[cbind(sq2[,2],sq2[,1])])))
}

genseq <- function(sq) {  # Generate new candidate sequence
	idx <- seq(2, NROW(eurodistmat)-1, by=1)
	changepoints <- sample(idx, size=2, replace=FALSE)
	tmp <- sq[changepoints[1]]
	sq[changepoints[1]] <- sq[changepoints[2]]
	sq[changepoints[2]] <- tmp
	return(as.numeric(sq))
}

sq <- c(1,2:NROW(eurodistmat),1)  # Initial sequence
distance(sq)

set.seed(123) # chosen to get a good soln relatively quickly
res <- sann(sq, distance, genseq, maxit=30000, REPORT=500, temp=2000)
res  # Near optimum distance around 12842

loc <- cmdscale(eurodist)
rx <- range(x <- loc[,1])
ry <- range(y <- -loc[,2])
tspinit <- loc[sq,]
tspres <- loc[res$sequence,]
s <- seq(NROW(tspres)-1)

plot(x, y, type="n", asp=1, xlab="", ylab="", main="initial solution of traveling salesman problem")
arrows(tspinit[s,1], -tspinit[s,2], tspinit[s+1,1], -tspinit[s+1,2], angle=10, col="green")
text(x, y, labels(eurodist), cex=0.8)

plot(x, y, type="n", asp=1, xlab="", ylab="", main="optim() 'solving' traveling salesman problem")
arrows(tspres[s,1], -tspres[s,2], tspres[s+1,1], -tspres[s+1,2], angle=10, col="red")
text(x, y, labels(eurodist), cex=0.8)

jjvanderwal/ConsPlan documentation built on May 19, 2019, 11:40 a.m.