Given the following problem (a slightly simplified description of trading in the computer game Escape Velocity: Nova (system map)):
- Given a set of (solar) systems.
- Each system is connected by hyperspace travel route to one or more other systems; each route connects two systems.
- Making a hyperspace jump has a cost in dollars (covering fuel costs). The cost is the same no matter which jump you're making.
- Each system has a trading center with various commodities (food, metal, equipment, etc.) available for trade at a given price. The price varies between systems.
- To keep it simple we'll say you can only hold a single unit of a single commodity at any given time.
- A trade route is a series of hyperspace jumps that start and end in the same system (a loop), buying and selling commodities along the way.
- The profit of a trade route is defined as profit made by buying and selling commodities along the way minus the total cost of the hyperspace jumps made.
I want to be able to answer questions like the following:
- What's the most profitable trade route?
- What's the most profitable trade route starting and ending in a specific system?
- What's the most profitable trade route involving fewer than X jumps?
- What's the most profitable trade route involving fewer than X jumps starting and ending in a specific system?
I plan to model this as a digraph:
- Each system is a vertex.
- For each hyperspace travel route add two edges (one in each direction) connecting the two vertexes with a weight equal to the cost of making a hyperspace jump.
- For each system
- for each commodity available in that system
- for each system where the price of the commodity is lower than in the current system (we'll never make a profit if the cost at the destination is higher so we omit those edges)
- add an edge from the current system to that system with weight of
(number of jumps to reach that system) * (cost per jump) - (difference in price of the commodity)
(i.e. profit to be made by buying in the current system and selling in the destination system)
- add an edge from the current system to that system with weight of
- for each system where the price of the commodity is lower than in the current system (we'll never make a profit if the cost at the destination is higher so we omit those edges)
- for each commodity available in that system
I believe the technical description of what I'm after is (for question 1): given the above digraph what's the negative cycle with the lowest average cost per edge.
I realize I can use a breadth-first search to answer questions 3 and 4 as long as the maximum number of jumps is small enough that I don't run out of memory, but I suspect it won't be practical to answer 1 or 2 this way.
I've read though the Solving Problems by Searching chapter in AI: A Modern Approach but as far as I can tell the algorithms there require positive edge weights.
A bit of Googling found me the Bellman–Ford algorithm and while that supports negative edge weights my understanding is that it doesn't give accurate costs when negative cycles are involved.
Are there any algorithms I can use to solve this problem more efficiently?