Вы находитесь на странице: 1из 6

Travelling sales man problem

-Raviteja Namburu
int main() {

int matrix[10][10] =
{ { 0, 94, 100, 135, 235, 280, 190, 120, 35, 100 },

{ 95, 0, 190, 45, 165, 120, 130, 115, 110, 60 },

{ 100, 190, 0, 230, 330, 380, 290, 117, 105, 175 },

{ 130, 42, 230, 0, 120, 170, 90, 160, 150, 100 },

{ 230, 160, 330, 120, 0, 110, 40, 290, 250, 225 },

{ 270, 125, 380, 170, 115, 0, 145, 280, 290, 220 },

{ 180, 130, 290, 95, 45, 145, 0, 250, 200, 190 },

{ 120, 115, 115, 160, 290, 270, 250, 0, 145, 60 },

{ 30, 115, 105, 150, 250, 290, 200, 145, 0, 115 },

{ 90, 60, 175, 100, 225, 220, 190, 60, 115, 0 }


};

int route[10][10] =
{ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },

{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },

{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },

{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },

{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },

{ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }
};
int hold = 38340;
int ind1 = 0;
int ind2 = 0;
int loop = 0;
int x = 0;

do {

int distance = 0;
int row = 0;
int column = 0;
int penalty = 0;
int penality1 = 0;
int penality2 = 0;

if (route[ind1][ind2] == 0)

{
route[ind1][ind2] = 1;
}
else
{
route[ind1][ind2] = 0;
}

for (int i = 0; i < 10; i++)


{
for (int j = 0; j < 10; j++)
{
row += route[i][j];
column += route[j][i];

if (route[i][j] == 1 && route[j][i] == 1)


{
penalty += 15000;
}
}

if (row != 1)
{
penality1 = 5000*(abs(1-row));
penalty += penality1;
}
if (column != 1)
{
penality2 = 5000*(abs(1 - column));
penalty += penality2;
}
penality1 = 0;
penality2 = 0;

row = 0;
column = 0;
}

if (route[ind1][ind1] == 1)
{
penalty += 50000;
}

for (int a = 0; a < 10; a++)


{
for (int b = 0; b < 10; b++)
{
distance += (route[a][b])*(matrix[a][b]);
}

distance += penalty;

if (distance < hold)


{
hold = distance;
x = penalty;

}
else
{
if (route[ind1][ind2] == 0)
{
route[ind1][ind2] = 1;
}
else

{
route[ind1][ind2] = 0;
}
}

ind1++;
if (ind1 > 9)
{
ind1 = 0;
ind2++;
}
if (ind2 > 9)
{

ind2 = 0;
loop++;
}

cout << distance << endl;

}while (loop < 200);

for (int i = 0; i < 10; i++)


{
for (int j = 0; j < 10; j++)
{
if (j < 9)
{
cout << route[i][j] << " ";
}

else
{
cout << route[i][j] << endl;
}
}
}
cout << x << endl;
cout << hold << endl;
return 0;
}

Results:
Total distance =1052
0 0 0
0 0 0
0 0 0
1 0 0
0 0 0
0 0 0
0 0 0
0 1 0
0 0 1
0 0 0
0
1052

0
0
0
0
0
0
1
0
0
0

0
0
0
0
0
1
0
0
0
0

0
1
0
0
0
0
0
0
0
0

0
0
0
0
1
0
0
0
0
0

0
0
1
0
0
0
0
0
0
0

0
0
0
0
0
0
0
0
0
1

1
0
0
0
0
0
0
0
0
0

Вам также может понравиться