I'm new to the whole traveling-salesman problem as well as stackoverflow so let me know if I say something that isn't quite right.
Intro:
I'm trying to code a profit/time-optimized multiple-trade algorithm for a game which involves multiple cities (nodes) within multiple countries (areas), where:
- The physical time it takes to travel between two connected cities is always the same ;
- Cities aren't linearly connected (you can teleport between some cities in the same time);
- Some countries (areas) have teleport routes which make the shortest path available through other countries.
- The traveler (or trader) has a limit on his coin-purse, the weight of his goods, and the quantity tradeable in a certain trade-route. The trade route can span multiple cities.
Question Parameters:
There already exists a database in memory (python:sqlite) which holds trades based on their source city and their destination city, the shortest-path cities inbetween as an array and amount, and the limiting factor with its % return on total capital (or in the case that none of the factors are limiting, then just the method that gives the highest return on total capital).
- I'm trying to find the optimal profit for a certain preset chunk of time (i.e. 30 minutes)
- The act of crossing into a new city is actually simultaneous
- It usually takes the same defined amount of time to travel across the city map (i.e. 2 minutes)
- The act of initiating the first or any new trade takes the same time as crossing one city map (i.e. 2 minutes)
- My starting point might not actually have a valid trade ( I would have to travel to the first/nearest/best one )
Pseudo-Solution So Far
Optimization
First, I realize that because I have a limit on the time it takes, and I know how long each hop takes (including -1 for intiating the trade), I can limit the graph to all trades whose hops are under or equal to max_hops=int(max_time/route_time) -1
. I cut elements of the trade database that don't fall within this time limit, pruning cities that have shortest-path lengths greater than max_hops
.
I make another entry into the trades database that includes the shortest-paths between my current city and the starting cities of all the existing trades that aren't my current city, and give them a return of 0%. I would limit these to where the number of city hops is less than max_hops
, and I would also calculate whether the current city to the starting city plus that trades shortest-path-hops would excede max_hops
and remove those that exceded this limit.
Then I take the remaining trades that aren't (current_city->starting_city)
and add trade routes with return of 0% between all destination and starting cities wheres the hops doesn't excede max_hops
Then I make one last prune for all cities that aren't in the trades database as either a starting city, destination city, or part of the shortest path city arrays.
Graph Search I am left with a (much) smaller graph of trades feasible within the time limit (i.e. 30 mins).
Because all the nodes that are connected are adjacent, the connections are by default all weighted 1. I divide the %return over the number of hops in the trade then take the inverse and add + 1 (this would mean a weight of 1.01 for a 100% return route). In the case where the return is 0%, I add ... 2?
It should then return the most profitable route...
The Question:
Mostly,
How do I add the ability to take multiple routes when I have left over money or space and keep route finding through path discrete to single trade routes? Due to the nature of the goods being sold at multiple prices and quantities within the city, there would be a lot of overlapping routes.
How do I penalize initiating a new trade route?
Is graph search even useful in this situation?
On A Side Note,
- What kinds of prunes/optimizations to the graph should I (or should I not) make?
- Is my weighting method correct? I have a feeling it will give me disproportional weights. Should I use the actual return instead of percentage return?
- If I am coding in python are libraries such as python-graph suitable for my needs? Or would it save me a lot of overhead (as I understand, graph search algorithms can be computationally intensive) to write a specialized function?
- Am I best off using A* search ?
- Should I be precalculating shortest-path points in the trade database and maxing optimizations or should I leave it all to the graph-search?
- Can you notice anything that I could improve?