What's wrong with my hill climbing? (salesman problem)

closed account (1Afj1hU5)
Hello, I have decided to try and solve one of the most common problems, that is the travelling salesman problem. For some reason my algorithm is not giving me the correct path from lowest distance to highest. I don't know where I'm going wrong but I'd gladly appreciate your input.

Thank you.

Here's my attempt:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Function Prototypes
int CalculateDistance(int distanceArray[], int);
void Swap(int[], int, int);

int main()
{
	srand(static_cast<unsigned int>(time(NULL)));

	int iRand = 0;
	int iOldCost = 0;
	int iNewCost = 0;
	int iSwaps = 0;

	const int NUM_CITIES = 6;
	int cities[NUM_CITIES];
	string citiesNames[NUM_CITIES];

	citiesNames[0] = "New York";
	citiesNames[1] = "Auckland";
	citiesNames[2] = "New Delhi";
	citiesNames[3] = "Rio de janeiro";
	citiesNames[4] = "New Orleans";
	citiesNames[5] = "Barcelona";

	for (int i = 0; i < NUM_CITIES; ++i)
	{
		iRand = rand() % 100 + 1;
		cities[i] = iRand;
	}

	cout << "List of cities and their distances: \n";
	for (int i = 0; i < NUM_CITIES; ++i)
	{
		cout << citiesNames[i] << "		: " << cities[i] << "km." << endl;
	}

	iOldCost = CalculateDistance(cities, NUM_CITIES);

	while (iOldCost > 0)
	{
		for (int i = 0; i < NUM_CITIES - 1; ++i)
		{
			Swap(cities, i, i++);
			iNewCost = CalculateDistance(cities, NUM_CITIES);

			if (iOldCost > iNewCost)
			{
				++iSwaps;

				printf("After swap %d: \n", iSwaps);
				for (int i = 0; i < NUM_CITIES; ++i)
				{
					cout << cities[i] << endl;
					iOldCost = iNewCost;
				}
			}
			else
			{
				Swap(cities, i, i++);
			}
		}
	}

	cout << "\nOur journey will look something like this: \n";
	for (int i = 0; i < NUM_CITIES; ++i)
	{
		cout << cities[i] << "km." << endl;
	}

	int iTemp = 0;
	cin >> iTemp;
	return 0;
}

int CalculateDistance(int distanceArray[], int num)
{
	int c = 0;

	for (int i = 0; i < num; ++i)
	{
		for (int j = i + 1; j < num; ++j)
		{
			if (distanceArray[j] < distanceArray[i])
			{
				c++;
			}
		}
	}
	return c;
}

void Swap(int myArray[], int x, int y)
{
	int iTemp = myArray[x];
	myArray[x] = myArray[y];
	myArray[y] = iTemp;
}
Last edited on
The problem is Swap(cities, i, i++);, for two reasons.

1. i is modified so if you go into the else and swap again you will not swap the same cities.

2. The evaluation of arguments are unsequenced. You want the third argument (i++) to be evaluated first so that the second argument (i) evaluates to the new value of i but that's not a safe assumption to make. The code has undefined behaviour so anything is allowed to happen.

I don't think you need to change i at all here. Instead of i++ you probably should use i+1.
Topic archived. No new replies allowed.