Hi mosahab,
I don't know much about TSP, but I was reading about it here :
Hopefully you have already read / studied this.
If you haven't, I noted these points :
wiki wrote: |
---|
Exact algorithms
The most direct solution would be to try all permutations (ordered combinations) and see which one is cheapest (using brute force search). The running time for this approach lies within a polynomial factor of O(n!), the factorial of the number of cities, so this solution becomes impractical even for only 20 cities. One of the earliest applications of dynamic programming is the Held–Karp algorithm that solves the problem in time O(n^2 2^n) |
wiki wrote: |
---|
Other approaches include:
Various branch-and-bound algorithms, which can be used to process TSPs containing 40–60 cities. |
So it seems that brute force algorithms that try for an exact solution would only work for a small number of cities. That's why it seems to be wise to get a solution working with a small system first.
More practical solutions seem to use heuristics that give a high probability of being close to an exact solution.
Just curious about what your assignment said about how many cities the solution needs to work for. 10,000 seems to be a very mean & difficult target to achieve. Or is it one of the On-line problems, like online judge for example or Project Euler? If that is the case then I hope you realise that these problems are definitely not as trivial as they first seem.
For your basic Data Structure you could have a class, like
mutexe said :
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class City {
private:
double X; // The X ordinate of the position
double Y; // The Y ordinate of the position
std::size_t Index;
std::size_t Demand;
public:
// interface functions here
};
|
With your methodology (your problem flowchart), was this given to you as a hint, or did you think of it your self? Some of the solutions on the wiki page might be better - like Branch & Bound for example.
I have some problems with it :
So for your methodology to work, you might have only 8 cities, because the list of all the permutations would have 40,320 items in it (8 factorial) or 5,040 (7 factorial).
Step 1. You seem to want to store the random combination of the Cities and their associated info into one data structure - which doesn't make sense to me. Better to separate them into a City Table and a Route Table.
1 2 3
|
const unsigned int NumCities = 8;
// use City class shown above
City CityTable[NumCities];
|
Step 2. This seems to be the Route Table, which could be a 2d array - 20 by number of cities. But choosing only 20 random permutations, seems to be a very small sample out of a total of 20,160 which is only for 8 cities (8 factorial / 2) . Even though in step 5 you do 3000 more combinations, there is absolutely no way of knowing whether these 20 combinations are anywhere near being close to a good solution. By choosing 5 of these, seems only to further reduce the probability of a good answer. A permutation of one of the other 15 might prove better than the smallest of the 5 chosen.
1 2 3 4 5 6 7 8 9
|
class Route {
std::size_t Entry[NumCities]; // contains City Identifiers
double DistanceCost;
};
const unsigned NumRoutes = 20;
Route RouteTable[NumRoutes]; // put the random permutations in here
|
So this is much better because [20][8] plus [20][4] = 240, is much better than [20][8][4] = 640, and represents a big saving in memory usage and time to process.
Step 4. If the City table & Route Table are separate, then there is no need to go back and search for a corresponding route - you already have this.
For which ever method you choose in the end, here are two things you can use to help you:
There is the STL
std::next_permutation
algorithm which you could use to create routes, but you need to get rid of half of these because 1,2,3,4 is the same as 4,3,2,1 for example.
You can use
std::push_back
to put various routes into a
std::vector
of all the routes. Then go through and calc the cost for each route, using the info in the City Array.
Well hopefully all this is of some help, I look forward to seeing how you get on :-)