Discussion:
Travelling Salesman Problem
(too old to reply)
Robert Kozeluh
2004-10-28 17:08:02 UTC
Permalink
Hey guys, I could really use some help on a problem I have. I have
been stuggling with it in Matlab for many hours now to no avail. Here
it is:

Question 5 – Traveling salesman Location of 6 cities around UOIT is
given as follows. City X- coordinates Y-coordinates

Oshawa (UOIT) 0 0
Toronto -50 -10
Barrie -49 100
Ottawa 100 300
Brampton -100 5
Hamilton 130 -8

a. Locate these cities in a graph and use unique symbols to
distinguish them.
b. Using straight distance between cities, find out what is the best
sequence to visit all the cities with less fuel consumption and show
the best sequence on the previous graph.
c. Find what is the least amount of money in dollars the sales person
is going pay during his trip. (01 L of gas = 83 cents) and the time
he will be behind the wheel, assuming that sales person drives at a
constant speed of 110 Km/h.

If anyone can help me I would be very grateful! TIA!!!
Ken Davis
2004-10-28 18:28:28 UTC
Permalink
Post by Robert Kozeluh
Hey guys, I could really use some help on a problem I have. I have
been stuggling with it in Matlab for many hours now to no avail. Here
Question 5 - Traveling salesman Location of 6 cities around UOIT is
given as follows. City X- coordinates Y-coordinates
Oshawa (UOIT) 0 0
Toronto -50 -10
Barrie -49 100
Ottawa 100 300
Brampton -100 5
Hamilton 130 -8
a. Locate these cities in a graph and use unique symbols to
distinguish them.
b. Using straight distance between cities, find out what is the best
sequence to visit all the cities with less fuel consumption and show
the best sequence on the previous graph.
c. Find what is the least amount of money in dollars the sales person
is going pay during his trip. (01 L of gas = 83 cents) and the time
he will be behind the wheel, assuming that sales person drives at a
constant speed of 110 Km/h.
If anyone can help me I would be very grateful! TIA!!!
You have offered no evidence that you have been struggling. Please show us
some evidence--some code, some thoughts, etc.--and we'll be glad to help you
get unstuck.
Robert Kozeluh
2004-10-30 15:32:21 UTC
Permalink
Hi there Ken, I realize that it's difficult to feel sympathy for
someone that has not shown initiative, but I can assure you I have
already given myself high blood pressure over this Matlab assignment.
I didn't post any of my code because I am ony able to do the very
first part of this problem, which is plotting the points and
labelling them. I am terrible at Matlab and programming in general.
Anyways, here is what I have so far along with the question again:

Question 5 – Traveling salesman Location of 6 cities around UOIT is
given as follows. City X- coordinates Y-coordinates

Oshawa (UOIT) 0 0
Toronto -50 -10
Barrie -49 100
Ottawa 100 300
Brampton -100 5
Hamilton 130 -8

a. Locate these cities in a graph and use unique symbols to
distinguish them.
b. Using straight distance between cities, find out what is the best
sequence to visit all the cities with less fuel consumption and show
the best sequence on the previous graph.
c. Find what is the least amount of money in dollars the sales person

is going pay during his trip. (01 L of gas = 83 cents) and the time
he will be behind the wheel, assuming that sales person drives at a
constant speed of 110 Km/h.

% ENGR 2140 Assignment #4
% Question #5 - Travelling Salesman
clc
clear

% Coordinates:
Oshawa=[0,0];
Toronto=[-50,-10];
Barrie=[-49,100];
Ottawa=[100,300];
Brampton=[-100,5];
Hamilton=[-130,-8];
Data=[Oshawa; Toronto; Barrie; Ottawa; Brampton; Hamilton];

scatterplot(Data), axis([-220, 170, -50, 330]), title('Travel
Map'),...
xlabel('Distance (km)'), ylabel('Distance (km)'),...
text(5,15,'Oshawa')
text(-75,5,'Toronto')
text(-70,115,'Barrie')
text(75,315,'Ottawa')
text(-155,20,'Brampton')
text(-200,0,'Hamilton')

counter1=1;
counter2=1;

for one=1:6
for two=1:6
if two~=one

dist(counter1,counter2)=abs(sqrt((Data(one,1)-Data(two,1))^2+(Data(one
,2)-Data(two,2))^2))
end
counter1=counter1+1;
end
end

%counter=1;
%tripdist=0;

%for n=1:6
% if n<=5
%
dist(n)=abs(sqrt((Data(n,1)-Data(n+1,1))^2+(Data(n,2)-Data(n+1,2))^2))

% tripdist=tripdist+dist(n);
% end
%end
%disthome=abs(sqrt((Data(1,1)-Data(n,1))^2+(Data(1,2)-Data(n,2))^2));
%totaldist=tripdist+disthome

As you can see, it's really not much. I know that a vector must be
made for each trip combination, and that must vector must be stored,
converted to a scalar value (total trip distance) and compared to
the next combination route; if smaller it would be kept, if larger it
would be replaced by the shorter (better) route. However, I have NO
clue how to do this in Matlab. Any help you could offer me would be
great.....I am not one to give up, but I have probably spent more
than twelve hours with this question in Matlab, scouring the net for
similar problems and searching my textbook for help. I only need help
with part b. after that I can struggle my way further. TIA!
Post by Robert Kozeluh
Hey guys, I could really use some help on a problem I have. I have
been stuggling with it in Matlab for many hours now to no avail. Here
Question 5 – Traveling salesman Location of 6 cities around UOIT is
given as follows. City X- coordinates Y-coordinates
Oshawa (UOIT) 0 0
Toronto -50 -10
Barrie -49 100
Ottawa 100 300
Brampton -100 5
Hamilton 130 -8
a. Locate these cities in a graph and use unique symbols to
distinguish them.
b. Using straight distance between cities, find out what is the best
sequence to visit all the cities with less fuel consumption and show
the best sequence on the previous graph.
c. Find what is the least amount of money in dollars the sales
person
is going pay during his trip. (01 L of gas = 83 cents) and the time
he will be behind the wheel, assuming that sales person drives at a
constant speed of 110 Km/h.
If anyone can help me I would be very grateful! TIA!!!
Ken Davis
2004-10-30 18:40:54 UTC
Permalink
Post by Robert Kozeluh
Hi there Ken, I realize that it's difficult to feel sympathy for
someone that has not shown initiative, but I can assure you I have
already given myself high blood pressure over this Matlab assignment.
I didn't post any of my code because I am ony able to do the very
first part of this problem, which is plotting the points and
labelling them. I am terrible at Matlab and programming in general.
Question 5 - Traveling salesman Location of 6 cities around UOIT is
given as follows. City X- coordinates Y-coordinates
Oshawa (UOIT) 0 0
Toronto -50 -10
Barrie -49 100
Ottawa 100 300
Brampton -100 5
Hamilton 130 -8
a. Locate these cities in a graph and use unique symbols to
distinguish them.
b. Using straight distance between cities, find out what is the best
sequence to visit all the cities with less fuel consumption and show
the best sequence on the previous graph.
c. Find what is the least amount of money in dollars the sales person
is going pay during his trip. (01 L of gas = 83 cents) and the time
he will be behind the wheel, assuming that sales person drives at a
constant speed of 110 Km/h.
% ENGR 2140 Assignment #4
% Question #5 - Travelling Salesman
clc
clear
Oshawa=[0,0];
Toronto=[-50,-10];
Barrie=[-49,100];
Ottawa=[100,300];
Brampton=[-100,5];
Hamilton=[-130,-8];
Data=[Oshawa; Toronto; Barrie; Ottawa; Brampton; Hamilton];
scatterplot(Data), axis([-220, 170, -50, 330]), title('Travel
Map'),...
xlabel('Distance (km)'), ylabel('Distance (km)'),...
text(5,15,'Oshawa')
text(-75,5,'Toronto')
text(-70,115,'Barrie')
text(75,315,'Ottawa')
text(-155,20,'Brampton')
text(-200,0,'Hamilton')
counter1=1;
counter2=1;
for one=1:6
for two=1:6
if two~=one
dist(counter1,counter2)=abs(sqrt((Data(one,1)-Data(two,1))^2+(Data(one
,2)-Data(two,2))^2))
end
counter1=counter1+1;
end
end
%counter=1;
%tripdist=0;
%for n=1:6
% if n<=5
%
dist(n)=abs(sqrt((Data(n,1)-Data(n+1,1))^2+(Data(n,2)-Data(n+1,2))^2))
% tripdist=tripdist+dist(n);
% end
%end
%disthome=abs(sqrt((Data(1,1)-Data(n,1))^2+(Data(1,2)-Data(n,2))^2));
%totaldist=tripdist+disthome
As you can see, it's really not much. I know that a vector must be
made for each trip combination, and that must vector must be stored,
converted to a scalar value (total trip distance) and compared to
the next combination route; if smaller it would be kept, if larger it
would be replaced by the shorter (better) route. However, I have NO
clue how to do this in Matlab. Any help you could offer me would be
great.....I am not one to give up, but I have probably spent more
than twelve hours with this question in Matlab, scouring the net for
similar problems and searching my textbook for help. I only need help
with part b. after that I can struggle my way further. TIA!
Post by Robert Kozeluh
Hey guys, I could really use some help on a problem I have. I have
been stuggling with it in Matlab for many hours now to no avail. Here
Question 5 - Traveling salesman Location of 6 cities around UOIT is
given as follows. City X- coordinates Y-coordinates
Oshawa (UOIT) 0 0
Toronto -50 -10
Barrie -49 100
Ottawa 100 300
Brampton -100 5
Hamilton 130 -8
a. Locate these cities in a graph and use unique symbols to
distinguish them.
b. Using straight distance between cities, find out what is the best
sequence to visit all the cities with less fuel consumption and show
the best sequence on the previous graph.
c. Find what is the least amount of money in dollars the sales person
is going pay during his trip. (01 L of gas = 83 cents) and the time
he will be behind the wheel, assuming that sales person drives at a
constant speed of 110 Km/h.
If anyone can help me I would be very grateful! TIA!!!
First, just to clarify... My guess is that most people who respond to posts
here do so because it interests/pleases them to do so. Sympathy, or for that
matter, urgency, play little or no part. I think there is a feeling that we
don't want to just do other people's homework. For me, at least, that is
just contributing to deception and dishonesty... which I do not want to do.
That is the reason I like to see some evidence of effort.

Now... back to your problem...

There is probably a whole literature on this in the fields of algorithms or
graph theory, but for a simple (small) problem like this, I'd be inclined to
use brute force. This means enumerating all possible paths. I'm not certain,
but I suspect that it is only necessary to consider paths which visit each
city once so there are a total of n! (= 6! = 720) paths. They are indeed all
the permutations of the sequence, {1, 2, 3, 4, 5, 6}. Look at "help perms"
to generate that list.
Frequent Poster
2004-10-30 18:48:25 UTC
Permalink
Post by Ken Davis
First, just to clarify... My guess is that most people who respond to posts
here do so because it interests/pleases them to do so. Sympathy, or for that
matter, urgency, play little or no part. I think there is a feeling that we
don't want to just do other people's homework. For me, at least, that is
just contributing to deception and dishonesty... which I do not want to do.
That is the reason I like to see some evidence of effort.
Now... back to your problem...
There is probably a whole literature on this in the fields of
algorithms or
graph theory, but for a simple (small) problem like this, I'd be inclined to
use brute force. This means enumerating all possible paths. I'm not certain,
but I suspect that it is only necessary to consider paths which visit each
city once so there are a total of n! (= 6! = 720) paths. They are indeed all
the permutations of the sequence, {1, 2, 3, 4, 5, 6}. Look at "help perms"
to generate that list.
Ken (who's advice and experience is second to none, and always worth
reading) is about to blow! Stand back or be covered in blood and
guts. Joking aside, I'm always always impressed by the (trademark)
two-part answers:

1) Sod off!
2) Here's some help.

- fp
Marcus M. Edvall
2004-10-30 18:49:19 UTC
Permalink
Hi Robert,

Look in TOMLAB. There is a function in LDO (free) that will take care
of this.

See 'help salesman'

Best wishes, Marcus
Tomlab Optimization Inc.
<http://tomlab.biz>
frequent poster
2004-10-30 18:56:54 UTC
Permalink
Post by Marcus M. Edvall
Look in TOMLAB.
What is is with you tomlab/femlab/arselab people? CSSM is a sharing
place, not a market place. We (mainly sad people) are proud of our
(sometimes) clever answers to questions, not looking to profit from
them. Some of us waste hours of our own time trying to solve other
people's problems for them - for fun not money. Go advertise
somewhere else!

- Steve (yeah, me!)
Robert Kozeluh
2004-10-30 21:17:04 UTC
Permalink
Ken, thank you so much for your help. The perm() function was what I
was looking for. You must understand the frustration one goes through
when they are unaware of such functions.....basically what I was
trying to do is write this function myself....and from my very
limited skills, this was a futile effort!.....Thanks again!
Post by Robert Kozeluh
Post by Robert Kozeluh
Hi there Ken, I realize that it's difficult to feel sympathy
for
Post by Robert Kozeluh
Post by Robert Kozeluh
someone that has not shown initiative, but I can assure you I
have
Post by Robert Kozeluh
already given myself high blood pressure over this Matlab
assignment.
Post by Robert Kozeluh
I didn't post any of my code because I am ony able to do the
very
Post by Robert Kozeluh
Post by Robert Kozeluh
first part of this problem, which is plotting the points and
labelling them. I am terrible at Matlab and programming in
general.
Post by Robert Kozeluh
Anyways, here is what I have so far along with the question
Question 5 - Traveling salesman Location of 6 cities around
UOIT
Post by Robert Kozeluh
is
Post by Robert Kozeluh
given as follows. City X- coordinates Y-coordinates
Oshawa (UOIT) 0 0
Toronto -50 -10
Barrie -49 100
Ottawa 100 300
Brampton -100 5
Hamilton 130 -8
a. Locate these cities in a graph and use unique symbols to
distinguish them.
b. Using straight distance between cities, find out what is the
best
Post by Robert Kozeluh
sequence to visit all the cities with less fuel consumption and
show
Post by Robert Kozeluh
the best sequence on the previous graph.
c. Find what is the least amount of money in dollars the sales
person
Post by Robert Kozeluh
is going pay during his trip. (01 L of gas = 83 cents) and the
time
Post by Robert Kozeluh
he will be behind the wheel, assuming that sales person drives
at
Post by Robert Kozeluh
a
Post by Robert Kozeluh
constant speed of 110 Km/h.
% ENGR 2140 Assignment #4
% Question #5 - Travelling Salesman
clc
clear
Oshawa=[0,0];
Toronto=[-50,-10];
Barrie=[-49,100];
Ottawa=[100,300];
Brampton=[-100,5];
Hamilton=[-130,-8];
Data=[Oshawa; Toronto; Barrie; Ottawa; Brampton; Hamilton];
scatterplot(Data), axis([-220, 170, -50, 330]), title('Travel
Map'),...
xlabel('Distance (km)'), ylabel('Distance (km)'),...
text(5,15,'Oshawa')
text(-75,5,'Toronto')
text(-70,115,'Barrie')
text(75,315,'Ottawa')
text(-155,20,'Brampton')
text(-200,0,'Hamilton')
counter1=1;
counter2=1;
for one=1:6
for two=1:6
if two~=one
dist(counter1,counter2)=abs(sqrt((Data(one,1)-Data(two,1))^2+(Data(o
Post by Robert Kozeluh
ne
Post by Robert Kozeluh
,2)-Data(two,2))^2))
end
counter1=counter1+1;
end
end
%counter=1;
%tripdist=0;
%for n=1:6
% if n<=5
%
dist(n)=abs(sqrt((Data(n,1)-Data(n+1,1))^2+(Data(n,2)-Data(n+1,2))^2
Post by Robert Kozeluh
))
Post by Robert Kozeluh
% tripdist=tripdist+dist(n);
% end
%end
%disthome=abs(sqrt((Data(1,1)-Data(n,1))^2+(Data(1,2)-Data(n,2))^2))
Post by Robert Kozeluh
;
Post by Robert Kozeluh
%totaldist=tripdist+disthome
As you can see, it's really not much. I know that a vector must
be
Post by Robert Kozeluh
made for each trip combination, and that must vector must be
stored,
Post by Robert Kozeluh
converted to a scalar value (total trip distance) and compared to
the next combination route; if smaller it would be kept, if
larger it
Post by Robert Kozeluh
would be replaced by the shorter (better) route. However, I
have
Post by Robert Kozeluh
NO
Post by Robert Kozeluh
clue how to do this in Matlab. Any help you could offer me
would
Post by Robert Kozeluh
be
Post by Robert Kozeluh
great.....I am not one to give up, but I have probably spent
more
Post by Robert Kozeluh
Post by Robert Kozeluh
than twelve hours with this question in Matlab, scouring the
net
Post by Robert Kozeluh
for
Post by Robert Kozeluh
similar problems and searching my textbook for help. I only
need
Post by Robert Kozeluh
help
Post by Robert Kozeluh
with part b. after that I can struggle my way further. TIA!
Post by Robert Kozeluh
Hey guys, I could really use some help on a problem I
have. I
Post by Robert Kozeluh
have
Post by Robert Kozeluh
Post by Robert Kozeluh
been stuggling with it in Matlab for many hours now to no
avail.
Post by Robert Kozeluh
Post by Robert Kozeluh
Here
Question 5 - Traveling salesman Location of 6 cities
around
Post by Robert Kozeluh
UOIT is
Post by Robert Kozeluh
Post by Robert Kozeluh
given as follows. City X- coordinates Y-coordinates
Oshawa (UOIT) 0 0
Toronto -50 -10
Barrie -49 100
Ottawa 100 300
Brampton -100 5
Hamilton 130 -8
a. Locate these cities in a graph and use unique symbols
to
Post by Robert Kozeluh
Post by Robert Kozeluh
Post by Robert Kozeluh
distinguish them.
b. Using straight distance between cities, find out what
is the
Post by Robert Kozeluh
Post by Robert Kozeluh
Post by Robert Kozeluh
best
sequence to visit all the cities with less fuel
consumption and
Post by Robert Kozeluh
Post by Robert Kozeluh
Post by Robert Kozeluh
show
the best sequence on the previous graph.
c. Find what is the least amount of money in dollars the
sales
Post by Robert Kozeluh
Post by Robert Kozeluh
Post by Robert Kozeluh
person
is going pay during his trip. (01 L of gas = 83 cents) and
the
Post by Robert Kozeluh
time
Post by Robert Kozeluh
Post by Robert Kozeluh
he will be behind the wheel, assuming that sales person
drives
Post by Robert Kozeluh
at a
Post by Robert Kozeluh
Post by Robert Kozeluh
constant speed of 110 Km/h.
If anyone can help me I would be very grateful! TIA!!!
First, just to clarify... My guess is that most people who respond to posts
here do so because it interests/pleases them to do so. Sympathy, or for that
matter, urgency, play little or no part. I think there is a feeling that we
don't want to just do other people's homework. For me, at least, that is
just contributing to deception and dishonesty... which I do not want to do.
That is the reason I like to see some evidence of effort.
Now... back to your problem...
There is probably a whole literature on this in the fields of
algorithms or
graph theory, but for a simple (small) problem like this, I'd be inclined to
use brute force. This means enumerating all possible paths. I'm not certain,
but I suspect that it is only necessary to consider paths which visit each
city once so there are a total of n! (= 6! = 720) paths. They are indeed all
the permutations of the sequence, {1, 2, 3, 4, 5, 6}. Look at "help perms"
to generate that list.
Kyle
2004-10-30 23:55:13 UTC
Permalink
Robert,

As a self taught Basic programmer who is trying to break his bad
programming habits and improve his Matlab skills, I was intrigued by
your problem. But when I didn't see a response to Ken's message all
day yesterday, I refrained from posting my solution or hints. Now
that you have followed up and have demonstrated that you have indeed
put considerable effort into the problem, I'll share my solution. I
often have the same frustration as you - not being aware of Matlab
functions such as perms() that make things much easier.

The problem statement isn't clear in that it doesn't indicate if the
salesman has to start in Oshawa. If so, you can make a simple mod to
the program to only include permutations with Oshawa in the first
column. I also wasn't sure if the salesman had to return to the
starting point. Part c of the problem is trivial, so I didn't
include that. But you haven't said what mileage (km/l) the car gets,
so I don't know how you're going to determine how much the trip
costs.

Here's my solution -

x=[0 -50 -49 100 -100 130];
y=[0 -10 100 300 5 -8];
labels=cell(6,1);
labels{1}='Oshawa';
labels{2}='Toronto';
labels{3}='Barrie';
labels{4}='Ottawa';
labels{5}='Brampton';
labels{6}='Hamilton';

symbols(1)='x';
symbols(2)='o';
symbols(3)='s';
symbols(4)='d';
symbols(5)='.';
symbols(6)='+';

figure;
axes;
hold on

for i=1:6
plot(x(i),y(i),symbols(i))
text(x(i)+4,y(i), labels(i));
end

order = perms(1:6);
distance = zeros(size(order,1),1);

for i=1:length(distance)
for j=2:6
distance(i)=distance(i) +
(x(order(i,j))-x(order(i,j-1)))^2+(y(order(i,j))-y(order(i,j-1)))^2;
end
distance(i) = sqrt(distance(i));
end

[d, index] = min(distance)

labels{order(index,:)}

Kyle
Post by Robert Kozeluh
Ken, thank you so much for your help. The perm() function was what I
was looking for. You must understand the frustration one goes
through
when they are unaware of such functions.....basically what I was
trying to do is write this function myself....and from my very
limited skills, this was a futile effort!.....Thanks again!
Post by Robert Kozeluh
Post by Robert Kozeluh
Hi there Ken, I realize that it's difficult to feel
sympathy
Post by Robert Kozeluh
for
Post by Robert Kozeluh
Post by Robert Kozeluh
someone that has not shown initiative, but I can assure you
I
Post by Robert Kozeluh
Post by Robert Kozeluh
have
Post by Robert Kozeluh
already given myself high blood pressure over this Matlab
assignment.
Post by Robert Kozeluh
I didn't post any of my code because I am ony able to do
the
Post by Robert Kozeluh
very
Post by Robert Kozeluh
Post by Robert Kozeluh
first part of this problem, which is plotting the points
and
Post by Robert Kozeluh
Post by Robert Kozeluh
Post by Robert Kozeluh
labelling them. I am terrible at Matlab and programming in
general.
Post by Robert Kozeluh
Anyways, here is what I have so far along with the question
Question 5 - Traveling salesman Location of 6 cities around
UOIT
Post by Robert Kozeluh
is
Post by Robert Kozeluh
given as follows. City X- coordinates Y-coordinates
Oshawa (UOIT) 0 0
Toronto -50 -10
Barrie -49 100
Ottawa 100 300
Brampton -100 5
Hamilton 130 -8
a. Locate these cities in a graph and use unique symbols to
distinguish them.
b. Using straight distance between cities, find out what is
the
Post by Robert Kozeluh
best
Post by Robert Kozeluh
sequence to visit all the cities with less fuel consumption
and
Post by Robert Kozeluh
show
Post by Robert Kozeluh
the best sequence on the previous graph.
c. Find what is the least amount of money in dollars the
sales
Post by Robert Kozeluh
person
Post by Robert Kozeluh
is going pay during his trip. (01 L of gas = 83 cents) and
the
Post by Robert Kozeluh
time
Post by Robert Kozeluh
he will be behind the wheel, assuming that sales person
drives
at
Post by Robert Kozeluh
a
Post by Robert Kozeluh
constant speed of 110 Km/h.
% ENGR 2140 Assignment #4
% Question #5 - Travelling Salesman
clc
clear
Oshawa=[0,0];
Toronto=[-50,-10];
Barrie=[-49,100];
Ottawa=[100,300];
Brampton=[-100,5];
Hamilton=[-130,-8];
Data=[Oshawa; Toronto; Barrie; Ottawa; Brampton; Hamilton];
scatterplot(Data), axis([-220, 170, -50, 330]),
title('Travel
Post by Robert Kozeluh
Post by Robert Kozeluh
Post by Robert Kozeluh
Map'),...
xlabel('Distance (km)'), ylabel('Distance (km)'),...
text(5,15,'Oshawa')
text(-75,5,'Toronto')
text(-70,115,'Barrie')
text(75,315,'Ottawa')
text(-155,20,'Brampton')
text(-200,0,'Hamilton')
counter1=1;
counter2=1;
for one=1:6
for two=1:6
if two~=one
dist(counter1,counter2)=abs(sqrt((Data(one,1)-Data(two,1))^2+(Data(o
Post by Robert Kozeluh
Post by Robert Kozeluh
ne
Post by Robert Kozeluh
,2)-Data(two,2))^2))
end
counter1=counter1+1;
end
end
%counter=1;
%tripdist=0;
%for n=1:6
% if n<=5
%
dist(n)=abs(sqrt((Data(n,1)-Data(n+1,1))^2+(Data(n,2)-Data(n+1,2))^2
Post by Robert Kozeluh
Post by Robert Kozeluh
))
Post by Robert Kozeluh
% tripdist=tripdist+dist(n);
% end
%end
%disthome=abs(sqrt((Data(1,1)-Data(n,1))^2+(Data(1,2)-Data(n,2))^2))
Post by Robert Kozeluh
Post by Robert Kozeluh
;
Post by Robert Kozeluh
%totaldist=tripdist+disthome
As you can see, it's really not much. I know that a vector
must
Post by Robert Kozeluh
be
Post by Robert Kozeluh
made for each trip combination, and that must vector must
be
Post by Robert Kozeluh
Post by Robert Kozeluh
stored,
Post by Robert Kozeluh
converted to a scalar value (total trip distance) and
compared
to
Post by Robert Kozeluh
Post by Robert Kozeluh
the next combination route; if smaller it would be kept, if
larger it
Post by Robert Kozeluh
would be replaced by the shorter (better) route. However, I
have
Post by Robert Kozeluh
NO
Post by Robert Kozeluh
clue how to do this in Matlab. Any help you could offer me
would
Post by Robert Kozeluh
be
Post by Robert Kozeluh
great.....I am not one to give up, but I have probably
spent
Post by Robert Kozeluh
more
Post by Robert Kozeluh
Post by Robert Kozeluh
than twelve hours with this question in Matlab, scouring
the
Post by Robert Kozeluh
net
Post by Robert Kozeluh
for
Post by Robert Kozeluh
similar problems and searching my textbook for help. I only
need
Post by Robert Kozeluh
help
Post by Robert Kozeluh
with part b. after that I can struggle my way further. TIA!
Post by Robert Kozeluh
Hey guys, I could really use some help on a problem I
have. I
Post by Robert Kozeluh
have
Post by Robert Kozeluh
Post by Robert Kozeluh
been stuggling with it in Matlab for many hours now to
no
Post by Robert Kozeluh
avail.
Post by Robert Kozeluh
Post by Robert Kozeluh
Here
Question 5 - Traveling salesman Location of 6 cities
around
Post by Robert Kozeluh
UOIT is
Post by Robert Kozeluh
Post by Robert Kozeluh
given as follows. City X- coordinates Y-coordinates
Oshawa (UOIT) 0 0
Toronto -50 -10
Barrie -49 100
Ottawa 100 300
Brampton -100 5
Hamilton 130 -8
a. Locate these cities in a graph and use unique
symbols
Post by Robert Kozeluh
to
Post by Robert Kozeluh
Post by Robert Kozeluh
Post by Robert Kozeluh
distinguish them.
b. Using straight distance between cities, find out
what
Post by Robert Kozeluh
is the
Post by Robert Kozeluh
Post by Robert Kozeluh
Post by Robert Kozeluh
best
sequence to visit all the cities with less fuel
consumption and
Post by Robert Kozeluh
Post by Robert Kozeluh
Post by Robert Kozeluh
show
the best sequence on the previous graph.
c. Find what is the least amount of money in dollars
the
Post by Robert Kozeluh
sales
Post by Robert Kozeluh
Post by Robert Kozeluh
Post by Robert Kozeluh
person
is going pay during his trip. (01 L of gas = 83 cents)
and
the
Post by Robert Kozeluh
time
Post by Robert Kozeluh
Post by Robert Kozeluh
he will be behind the wheel, assuming that sales
person
Post by Robert Kozeluh
drives
Post by Robert Kozeluh
at a
Post by Robert Kozeluh
Post by Robert Kozeluh
constant speed of 110 Km/h.
If anyone can help me I would be very grateful! TIA!!!
First, just to clarify... My guess is that most people who
respond
Post by Robert Kozeluh
to posts
here do so because it interests/pleases them to do so.
Sympathy,
Post by Robert Kozeluh
or
Post by Robert Kozeluh
for that
matter, urgency, play little or no part. I think there is a
feeling
Post by Robert Kozeluh
that we
don't want to just do other people's homework. For me, at
least,
Post by Robert Kozeluh
Post by Robert Kozeluh
that is
just contributing to deception and dishonesty... which I do not want to do.
That is the reason I like to see some evidence of effort.
Now... back to your problem...
There is probably a whole literature on this in the fields of
algorithms or
graph theory, but for a simple (small) problem like this, I'd
be
Post by Robert Kozeluh
Post by Robert Kozeluh
inclined to
use brute force. This means enumerating all possible paths. I'm
not
Post by Robert Kozeluh
certain,
but I suspect that it is only necessary to consider paths which visit each
city once so there are a total of n! (= 6! = 720) paths. They
are
Post by Robert Kozeluh
Post by Robert Kozeluh
indeed all
the permutations of the sequence, {1, 2, 3, 4, 5, 6}. Look at
"help
Post by Robert Kozeluh
perms"
to generate that list.
Robert Kozeluh
2004-10-31 05:15:57 UTC
Permalink
Thank you Kyle, I appreciate your help as well, and I think I may
need to steal the symbol() part of your code because I did not
incorporate that in my solution. Anyways, here is what I came up with
after Ken pointed me in the right direction (it's probably a little
sloppy, but I think it gets the job done):

% ENGR 2140 Assignment #4
% Question #5 - Travelling Salesman
clc
clear

% Coordinates:
Oshawa=[0,0];
Toronto=[-50,-10];
Barrie=[-49,100];
Ottawa=[100,300];
Brampton=[-100,5];
Hamilton=[130,-8];
Data=[Oshawa; Toronto; Barrie; Ottawa; Brampton; Hamilton];
scatterplot(Data), axis([-220, 170, -50, 330]), title('Travel
Map'),...
xlabel('Distance (km)'), ylabel('Distance (km)'),...
text(-15,15,'Oshawa')
text(-75,10,'Toronto')
text(-90,110,'Barrie')
text(75,315,'Ottawa')
text(-170,15,'Brampton')
text(100,-20,'Hamilton')

%format bank
trip_dist=0;
accumulated_travel=0;
c1=1;
c2=1;

combinations = perms(1:6); %All combinations possible with numbers 1
to 6.
paths=combinations(601:720,:); %All combinations starting from point
1 (Oshawa)

for c1=1:120
for c2=1:5

dist=abs(sqrt((Data(paths(c1,c2),1)-Data(paths(c1,c2+1),1))^2+(Data(pa
ths(c1,c2),2)-Data(paths(c1,c2+1),2))^2));
accumulated_travel=accumulated_travel+dist; %Storage of
current loops total distance.
end

dist_home=abs(sqrt((Data(paths(c1,6),1)-Data(1,1))^2+(Data(paths(c1,6)
,2)-Data(1,2))^2)); %Distance from last visited city back to Oshawa
trip_dist(c1)=accumulated_travel +dist_home;
accumulated_travel=0;
end

hold on

shortest=min(trip_dist); %Find the shortest trip distance.
position=find(trip_dist==shortest) %Find the location of the shortest
distance in the vector [trip_dist].
fprintf('The shortest travel distance to visit all cities is is %4.2f
km',shortest)
sequence=paths(position(1),:) %Take only one of the shortest routes
if there happen to be more than one shortest.

xdata=Data(:,1); %Create vector containing only X coordinates of
cities.
ydata=Data(:,2); %Create vector containing only Y coordinates of
cities.

for c3=1:6
xpath(c3)=xdata(sequence(c3)); %Sort the X coordinates according
to the shortest path 'sequence'.
ypath(c3)=ydata(sequence(c3)); %Sort the Y coordinates according
to the shortest path 'sequence'.
end
plot(xpath,ypath)

!!!!Oh, and you are correct about part c, we were not provided with
sufficient info to complete it...the prof was asked in webct about
it, but has not yet replied....should be easy to calculate once he
gives us the fuel economy, etc.. Again, thanks for all the help!
Post by Marcus M. Edvall
Robert,
As a self taught Basic programmer who is trying to break his bad
programming habits and improve his Matlab skills, I was intrigued by
your problem. But when I didn't see a response to Ken's message all
day yesterday, I refrained from posting my solution or hints. Now
that you have followed up and have demonstrated that you have
indeed
put considerable effort into the problem, I'll share my solution. I
often have the same frustration as you - not being aware of Matlab
functions such as perms() that make things much easier.
The problem statement isn't clear in that it doesn't indicate if the
salesman has to start in Oshawa. If so, you can make a simple mod to
the program to only include permutations with Oshawa in the first
column. I also wasn't sure if the salesman had to return to the
starting point. Part c of the problem is trivial, so I didn't
include that. But you haven't said what mileage (km/l) the car
gets,
so I don't know how you're going to determine how much the trip
costs.
Kyle
2004-10-31 22:34:56 UTC
Permalink
Robert,

If you "steal" anything from me, make sure it isn't the HUGE mistake.
I summed (x(i)+x(i-1))^2+(y(i)+y(i-1))^2 over i, and then took the
square root. If it hadn't been a weekend, I imagine several people
would have chastised me. :)

Kyle
Ken Davis
2004-10-31 00:44:14 UTC
Permalink
Post by Robert Kozeluh
Ken, thank you so much for your help. The perm() function was what I
was looking for. You must understand the frustration one goes through
when they are unaware of such functions.....basically what I was
trying to do is write this function myself....and from my very
limited skills, this was a futile effort!.....Thanks again!
[snip original traveling salesman problem]

For your future reference...
Post by Robert Kozeluh
help lookfor
LOOKFOR Search all M-files for keyword.
LOOKFOR XYZ looks for the string XYZ in the first comment line
(the H1 line) of the HELP text in all M-files found on MATLABPATH.
For all files in which a match occurs, LOOKFOR displays the H1 line.

For example, "lookfor inverse" finds at least a dozen matches,
including the H1 lines containing "inverse hyperbolic cosine"
"two-dimensional inverse FFT", and "pseudoinverse".
Contrast this with "which inverse" or "what inverse", which run
more quickly, but which probably fail to find anything because
MATLAB does not ordinarily have a function "inverse".

LOOKFOR XYZ -all searches the entire first comment block of
each M-file.

In summary, WHAT lists the functions in a given directory,
WHICH finds the directory containing a given function or file, and
LOOKFOR finds all functions in all directories that might have
something to do with a given key word.

See also DIR, HELP, WHO, WHAT, WHICH.
Post by Robert Kozeluh
lookfor permutations
PERMS All possible permutations.
V õ l u r
2023-06-20 01:55:33 UTC
Permalink
Why does the salesman have to travel, when he can sell everything from home ?
Post by Robert Kozeluh
Hey guys, I could really use some help on a problem I have. I have
been stuggling with it in Matlab for many hours now to no avail. Here
Question 5 – Traveling salesman Location of 6 cities around UOIT is
given as follows. City X- coordinates Y-coordinates
Oshawa (UOIT) 0 0
Toronto -50 -10
Barrie -49 100
Ottawa 100 300
Brampton -100 5
Hamilton 130 -8
a. Locate these cities in a graph and use unique symbols to
distinguish them.
b. Using straight distance between cities, find out what is the best
sequence to visit all the cities with less fuel consumption and show
the best sequence on the previous graph.
c. Find what is the least amount of money in dollars the sales person
is going pay during his trip. (01 L of gas = 83 cents) and the time
he will be behind the wheel, assuming that sales person drives at a
constant speed of 110 Km/h.
If anyone can help me I would be very grateful! TIA!!!
Loading...