Travelling salesman problem GA Matlab code debug

3 次查看(过去 30 天)
clc;
tic;
x=[ 1000 225 304 236 213 339 187 197 226
225 1000 140 153 15 175 84 160 110
304 140 1000 152 132 41 121 190 108
236 153 152 1000 143 188 70 73 63
213 15 132 143 1000 166 74 145 102
339 175 41 188 166 1000 157 226 144
187 84 121 70 74 157 1000 81 43
197 160 190 73 145 226 81 1000 90
226 110 108 63 102 144 43 90 1000]
xnv=x;
x1=x;
minr= nnminr(x);
[r c ] = size(x);
for i=1:r
for j=1:c
x(i,j)=x(i,j)/minr(i);
end
end
x;
minc=nnminr(x);
for i=1:r
for j=1:c
x(j,i)=x(j,i)/minc(i);
end
end
x;
count=0;
for l=1:r
for i=1:r
for j=1:c
if x(i,j)==1
count = count+ 1;
end
end
if count <= 1
for I=i
for j=1:c
if x(I,j)==1
x(I,j)= x(I,j)*10;
end
for i=1:r
[yc yr] = find (x == 10);
l = length(yc);
for m=1:l
x(yr(m),yc(m)) = 10;
for j=1:c
if x(yr(m),j) == 10
x(yr(m),j)=0;
end
if x(j,yc(m)) == 10
x(j,yc(m))=0;
end
end
continue
end
x;
end
x;
end
end
end
count=0;
end
end
x;
for i= 1:r
for j=1:c
if x(i,j) == 0
x(i,j)=1;
end
end
end
x;
count=0;
for i=1:r
for j=1:c
if x(i,j)==1
count = count+ 1;
end
end
if count > 1
for I=i
for j=1:c
x(I,j)= x(I,j)*10;
[yc yr] = find (x == 10,1);
l = length(yc);
for m=1:l
x(yr(m),yc(m)) = 10;
for j=1:c
if x(yr(m),j) == 10
x(yr(m),j)=0;
end
if x(j,yc(m)) == 10
x(j,yc(m))=0;
end
end
continue
end
x;
end
x;
end
end
count=0;
x;
for i= 1:r
for j=1:c
if x(i,j) == 0
x(i,j)=1;
end
end
end
x;
z=xnv.*x
Minvalue=(sum(sum(z)))
plot(z)
toc;
Please debug this code showing error as
Error: File: GA1.m Line: 77 Column: 1
At least one END is missing: the statement may begin here.
and how to conver function from nnmin to MIN()

采纳的回答

Joost
Joost 2020-5-5
It look like you would like to reproduce from this article:
The nnminr function computes the minimum for each row (is explained in the article).
It can be achieved in Matlab using:
min(A, [], 2)
where A is the matrix you want the minimum of each row for.
So this line:
minc=nnminr(x);
then would become
minc = min(x, [], 2);
Regarding the missing end statement, it should be added after line 106
count = 0;
end
  3 个评论
Joost
Joost 2020-5-7
Around line 22 you miss a '.
minc = min(x, [], 2);
should be:
minc = min(x', [], 2);
or, which is essentially the same as:
minc = min(x, [], 1);
and which is also the same as:
minc = min(x);
At two positions in the code you find things like:
[yc yr] = find(x == 10);
These should also get an extra ' :
[yc yr] = find(x' == 10);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Particle Swarm 的更多信息

产品


版本

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by