全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件
1341 1
2013-12-05
悬赏 500 个论坛币 未解决
求大神帮忙指导一下期末大作业的几个问题。叫做Implement Simulated Annealing for the Traveling salesman with Euclidean distance.  [backcolor=rgba(255, 255, 255, 0)]

[backcolor=rgba(255, 255, 255, 0)]教授要求,考虑坐标在10乘10位置上的100个城市,例如,城市的坐标为 (i,j), where i,j = 1,2......10. 代码已经在下面了,但是问题是100个城市没有准确的locate到10乘10的网格上。请大神帮忙把它们准确地放到10乘10的位置上。谢谢!

[backcolor=rgba(255, 255, 255, 0)]R Code:

n=100 # number of cities
M=10^4 # number of iterations
temp=50 # initial temperature
finaltemp=0.1
tempfactor= (temp/finaltemp)^(-1/M)
fixed.itr=5 # number of iteration for a fixed temperature
dist<-function(x1,y1,x2,y2) #computes the Euclidean distance between (x1,y1) and (x2,y2)
  {
    return(sqrt( ((x1-x2)^2) + ((y1-y2)^2) ))
    }
cost<-function(ord) #computes the total travelling distance of a order of points
    {
      p=length(ord)
      d=rep(0,p)
        for(i in 1:(p-1)){
            d=dist( xlist[ord],ylist[ord],xlist[ord[i+1]],ylist[ord[i+1]] )
                         }
    d[p]<- dist( xlist[ord[1]],ylist[ord[1]],xlist[ord[p]],ylist[ord[p]] ) # the distance between the first and last city
   
cost=sum(d)
return(cost)
    }
cost1=rep(0,M)
numaccept=0 # to keep track of accepting
templist=rep(0,M) # to keep track of temperature
xlist=runif(n,0,2) # generating random points in 2 dimension as to be cities
ylist=runif(n,0,2)
ord=seq(1:n) # initial order for the path


for(i in 1:M){
  cost1=cost(ord)
  for(t in 1:(fixed.itr)){
    # propose to move
      a=sample(1:n,2) # randomly choosing two cities
    result=ord
    result[a[2]]=ord[a[1]] # swapping
    result[a[1]]=ord[a[2]] # swapping
    U=runif(1)
    if( U < exp( (cost(ord)-cost(result))/temp )){
      ord<-result # accept/reject
      numaccept <- numaccept + 1 # to compute the acceptance rate
      }
    }
  templist=temp
  temp=temp*0.999 # update temperature
  # temp=tempfactor*temp
    }
library(calibrate)
ord


cat(" iterations = ",M,"\n")
iterations = 10000
cat("acceptance rate = ", numaccept/(M*fixed.itr),"\n")
acceptance rate = 0.4081
plot(xlist,ylist,xlab="X",ylab="Y",main="n=10 temp=50 M=10^4 fixed.itr=5")
textxy(xlist,ylist,(1:n),cx=1)
lines(xlist[ord],ylist[ord])
lines(c(xlist[ord[1]],xlist[ord[n]]),c(ylist[ord[1]],ylist[ord[n]]),cx="red")
plot(templist,type='l',main="temperature cooling down")
plot(cost1,type='l',main="Cost(travelling distance) for n=10")


library(calibrate)
ord
cat(" iterations = ",M,"\n")
cat("acceptance rate = ", numaccept/(M*fixed.itr),"\n")
plot(xlist,ylist,xlab="X",ylab="Y",main="n=100 temp=50 M=10^4 fixed.itr=5")
textxy(xlist,ylist,(1:n),col=1)


lines(xlist[ord],ylist[ord])


lines(c(xlist[ord[1]],xlist[ord[n]]),c(ylist[ord[1]],ylist[ord[n]]),col="red")


plot(templist,type='l',main="temperature cooling down")


plot(cost1,type='l',main="Cost(travelling distance) for n=10")


library(calibrate)
ord
cat(" iterations = ",M,"\n")
cat("acceptance rate = ", numaccept/(M*fixed.itr),"\n")


plot(xlist,ylist,xlab="X",ylab="Y",main="n=10 temp=50 M=10^4 fixed.itr=5")


textxy(xlist,ylist,(1:n),cx=1)
lines(xlist[ord],ylist[ord])
lines(c(xlist[ord[1]],xlist[ord[n]]),c(ylist[ord[1]],ylist[ord[n]]),col="red")
plot(templist,type='l',main="temperature cooling down")
plot(cost1,type='l',main="Cost(travelling distance) for n=10")


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2013-12-5 13:27:47
自己顶一下,希望能有大神相助!~
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

分享

扫码加好友,拉您进群