Error in travelling salesman problem cost function

1 回表示 (過去 30 日間)
ahmad karim
ahmad karim 2015 年 6 月 3 日
回答済み: Alan Weiss 2015 年 6 月 4 日
Hello, please any one can help me ? i have travelling salesman cost function but when i implement it its give me error.
% cost function for traveling salesperson problem
% Haupt & Haupt
% 2003
function dist=tspfun(pop)
global iga x y
[Npop,Ncity]=size(pop);
tour=[pop pop(:,1)];
%distance between cities
for ic=1:Ncity
for id=1:Ncity
dcity(ic,id)=sqrt((x(ic)-x(id))^2+(y(ic)-y(id))^2);
end % id
end %ic
% cost of each chromosome for ic=1:Npop
dist(ic,1)=0;
for id=1:Ncity
dist(ic,1)=dist(ic)+dcity(tour(ic,id),tour(ic,id+1));
end % id
end % ic

回答 (1 件)

Alan Weiss
Alan Weiss 2015 年 6 月 4 日
It would be easier to read your code if you marked it with the {} Code button. And it would be easier to diagnose your problem if you showed us the entire error message that your solver gave, and how you called your function.
If you want to see working code for a travelling salesman problem, look at this example.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation

カテゴリ

Help Center および File ExchangeSolver-Based Optimization Problem Setup についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by