#Simulate positions for n=15 cities
set.seed(2323)
p = 15
pos = data.frame(x=runif(p),y=runif(p))

par(mfrow=c(1,1))
plot(pos)
#dev.copy2pdf(file="../doc/example_trav_sale.pdf")

#par(mfrow=c(3,1))
plot(pos)

#Calculate pairwise distances between cities
d = as.matrix((dist(pos,diag=TRUE,upper=TRUE)))
#image(d)

#Convert to vector in order to access many components at a time
d = as.vector(d)

#Random order of visits
theta = sample(1:p,p)
#Convert sequential pairs into index in the d-vector
ind = (theta[-p]-1)*p+theta[-1]  # lookup in distance vector 


#Calculate total distance of order
V = sum(d[ind])
Vseq = V

###################################
P     =  50    # Population size
Numit = 100
mu    = 0.01 # Probability for mutation
##############################################

pop = matrix(NA,nrow=P,ncol=p)
V = rep(NA,P)
set.seed(232323)

for(k in 1:P)
{
  theta = sample(1:p,p)
  ind = (theta[-p]-1)*p+theta[-1]
  pop[k,] = theta
  V[k] = sum(d[ind])
}






