matlab运行程序时不报错,只显示出ans=程序名,这是为什么,以下是详细代码,用pso算法求解tsp问题clearclctic;global N D data1=load('vrpnc1.txt');n=size(data1,1)-1; %depot+customera=data1(2:(n+1),1);b=data1(2:(n+1),2);C=[a b];
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/05 18:33:10
![matlab运行程序时不报错,只显示出ans=程序名,这是为什么,以下是详细代码,用pso算法求解tsp问题clearclctic;global N D data1=load('vrpnc1.txt');n=size(data1,1)-1; %depot+customera=data1(2:(n+1),1);b=data1(2:(n+1),2);C=[a b];](/uploads/image/z/6108596-44-6.jpg?t=matlab%E8%BF%90%E8%A1%8C%E7%A8%8B%E5%BA%8F%E6%97%B6%E4%B8%8D%E6%8A%A5%E9%94%99%2C%E5%8F%AA%E6%98%BE%E7%A4%BA%E5%87%BAans%3D%E7%A8%8B%E5%BA%8F%E5%90%8D%2C%E8%BF%99%E6%98%AF%E4%B8%BA%E4%BB%80%E4%B9%88%2C%E4%BB%A5%E4%B8%8B%E6%98%AF%E8%AF%A6%E7%BB%86%E4%BB%A3%E7%A0%81%2C%E7%94%A8pso%E7%AE%97%E6%B3%95%E6%B1%82%E8%A7%A3tsp%E9%97%AE%E9%A2%98clearclctic%3Bglobal+N+D+data1%3Dload%28%27vrpnc1.txt%27%29%3Bn%3Dsize%28data1%2C1%29-1%3B+%25depot%2Bcustomera%3Ddata1%282%3A%28n%2B1%29%2C1%29%3Bb%3Ddata1%282%3A%28n%2B1%29%2C2%29%3BC%3D%5Ba+b%5D%3B)
matlab运行程序时不报错,只显示出ans=程序名,这是为什么,以下是详细代码,用pso算法求解tsp问题clearclctic;global N D data1=load('vrpnc1.txt');n=size(data1,1)-1; %depot+customera=data1(2:(n+1),1);b=data1(2:(n+1),2);C=[a b];
matlab运行程序时不报错,只显示出ans=程序名,这是为什么,以下是详细代码,用pso算法求解tsp问题
clear
clc
tic;
global N D
data1=load('vrpnc1.txt');
n=size(data1,1)-1; %depot+customer
a=data1(2:(n+1),1);
b=data1(2:(n+1),2);
C=[a b]; %城市坐标矩阵
D=zeros(n,n);
for i=1:n
for j=1:n
if =j
D(i,j)=((C(i,1)-C(j,1))^2+(C(i,2)-C(j,2))^2)^0.5;
end
D(j,i)=D(i,j);
end
end
s_size=1000; %种群数目
N=n-1;% 粒子的向量数,顾客数customer
c1=2;
c2=2;
Wmin=0.01;
Wmax=0.9;
w=0.4;
tmax=150;% 迭代次数
xr=zeros(s_size,N);
for i=1:s_size
xr(i,:)=randperm(N);
end
for i=1:s_size
for j=1:N-1
v_cost=v_cost+D(xr(i,j),xr(i,j+1));
j=j+1;
end
best_p(i)=v_cost(i);
best_pv=xr(i,:);
end
k=trace(max(v_cost));
best_g=v_cost(k);
best_gv=xr(k,:);
v_cost1=zeros(s_size,N);
xv=zeros(s_size,N);
for t=1:tmax
for i=1:s_size
w=Wmax-tt*(Wmax-Wmin)/tmax;
xv_temp=w*xv(i,:)+c1*rand()*(best_p(i,:)-xr(i,:))+c2*rand()*(best_g-xr(i,:))
xr(i,:)=xr(i,:)+xv_temp;
xv(i,:)=xv_temp;
for j=1:N-1
v_cost1(i)=v_cost1(i)+D(xr(i,j),xr(i,j+1));
j=j+1;
end
if v_cost1(i)
matlab运行程序时不报错,只显示出ans=程序名,这是为什么,以下是详细代码,用pso算法求解tsp问题clearclctic;global N D data1=load('vrpnc1.txt');n=size(data1,1)-1; %depot+customera=data1(2:(n+1),1);b=data1(2:(n+1),2);C=[a b];
1、v_cost按你写的程序只是一个数不是一个数组
2、最后一行多加了)和,
因为不能运行,所以只看了这么多