## Can anyone suggest me to write a correct and working code for traveling salesman problem using simulated annealing? I got this but there are so many errors:

on 25 Feb 2013

### Walter Roberson (view profile)

```% Travellingsalesman problem
```
```% Create random city distribution
```
```n=20;
```
```x=random('unif',-1,1,n,1);
```
```y=random('unif',-1,1,n,1);
```
```gam=1; mu=sign(x);
```
```% End up where you start. Add starting point to end
```
```x=[x' x(1)]';
```
```y=[y' y(1)]';
```
```mu=[mu' mu(1)]'; figure(1); hold off; g=plot(x,y,'.r');
```
```set(g,'MarkerSize',20);
```
```c0=cost(x,y,mu,gam); k=1; % Boltzmanconstant
```
```nt=50; nr=200; % nt: temp steps. nr: city switches each T cp=zeros(nr,nt);
```
```iran=inline('round(random(d,1.5001,n+0.4999))','d','n');
```
```for i=1:nt
```
```T=1.0 -(i-1)/nt
```
```for j=1:nr
```
```% switch two random cities
```
```ic1=iran('unif',n); ic2=iran('unif',n);
```
```xs=x(ic1); ys=y(ic1); ms=mu(ic1);
```
```x(ic1)=x(ic2); y(ic1)=y(ic2); mu(ic1)=mu(ic2);
```
```x(ic2)=xs; y(ic2)=ys; mu(ic2)=ms;
```
```p=random('unif',0,1); c=cost(x,y,mu,gam);
```
```if (c < c0 | p < exp(-(c-c0)/(k*T))) % accept
```
```c0=c;
```
```else % reject and switch back
```
```xs=x(ic1); ys=y(ic1); ms=mu(ic1);
```
```x(ic1)=x(ic2); y(ic1)=y(ic2); mu(ic1)=mu(ic2);
```
```x(ic2)=xs; y(ic2)=ys; mu(ic2)=ms;
```
```end
```
```cp(j,i)=c0;
```
```end
```
```figure(2); plot(reshape(cp,nt*nr,1)); drawnow;
```
```figure(1); hold off; g=plot(x,y,'.r'); set(g,'MarkerSize',20); hold on;
```
```plot(x,y,'b'); g=plot(x(1),y(1),'.g'); set(g,'MarkerSize',30);
```
```p=plot([0 0],[-1 1],'r--'); set(g,'LineWidth',2); drawnow;
```
```end
```

Walter Roberson

### Walter Roberson (view profile)

on 25 Feb 2013

If you describe some of the errors, people could help you debug it.

Yogesh Shukla

### Yogesh Shukla (view profile)

on 25 Feb 2013

??? Undefined function or method 'random'

for input arguments of type 'char'.

Error in ==> salesman at 3

n=20; x=random('unif',-1,1,n,1);

y=random('unif',-1,1,n,1);

## Products

### Walter Roberson (view profile)

on 25 Feb 2013

That version of a random number routine is part of the Statistics Toolbox; see http://www.mathworks.com/help/stats/random.html

The Statistics Toolbox is optional extra cost for Academic and Professional licenses. It is, though, included in Student Version licenses, but the toolbox might not be installed by default.

Walter Roberson

### Walter Roberson (view profile)

on 27 Feb 2013

What is the purpose of your line

```p=rand(0,1);
```

rand(0,1) creates a 0 x 1 matrix of random numbers -- an empty matrix.

Yogesh Shukla

### Yogesh Shukla (view profile)

on 27 Feb 2013

I want to generate a random number between 0 and 1. Which function i can use?

Walter Roberson

on 27 Feb 2013

rand()

### Yogesh Shukla (view profile)

on 26 Feb 2013
Edited by Yogesh Shukla

### Yogesh Shukla (view profile)

on 26 Feb 2013

Instead of using this routine (ie not using uniform distribution or random function) what else can I change in my code to eliminate errors and make code running. However if I use other function instead of 'random' then I get 'error using ==> ctranspose.. Transpose on ND array is not defined' in following lines:

x=[x' x(1)]';

y=[y' y(1)]';

mu=[mu' mu(1)]';

#### Join the 15-year community celebration.

Play games and win prizes!

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi