我正在做我的课程:使用PSO算法解决带有时间窗的车辆路径问题。问题是如何在算法中添加车号和时间窗口约束? 在此问题中,车辆号为2,并且时间窗口为特定时间,例如2019年9月8日下午4点至2019年8月19日下午5点。
距离矩阵样本为 列表(V1 = c(0,417785.9991,236802.8021,268790.0919,184532.7186,282211.119) V2 = c(417785.9991,0,303563.5361,409239.0415,298731.5266,630949.8697) V3 = c(236802.8021,303563.5361,0,108580.2545,57116.6423,342264.9074), V4 = c(268790.0919,409239.0415,108580.2545,0,142942.1314,268532.373), V5 = c(184532.7186,298731.5266,57116.6423,142942.1314,0,332657.9301), V6 = c(282211.119,630949.8697,342264.9074,268532.373,332657.9301,0)),row.names = c(NA,6L),class =“ data.frame”)
#total distance fucntion
disM<-read.csv("disM.csv", header = FALSE)
disM
calcdist<-function(tour){
tour<-c(1, tour+1,1)
route<-embed(c(tour,1),2)[,2:1]
dist<-matrix(disM)
totaldist<-sum(dist(route))
return(totaldist)
}
#GA algorithm
install.packages("GA")
library(GA)
result<-ga(type="permutation", fitness = calcdist, lower = 1, upper = 49)
summary(result)
route<-c(1, result@solution+1,1)
route
#pso algorithm
install.packages("pso")
library(pso)
PSO1<-psoptim(fn= calcdist, par=c(NA,NA))
PSO1
PSO2<-psoptim(fn=calcdist, par = c(NA,NA), control = list(maxit=10000, s= 100, v.max=10))
PSO2
谢谢,我想获得一些有关如何编码这些约束的建议。