0

I am working on my Masters project and was hoping you can give me some ideas on how to approach programming the following problem in java:

A trader wants to buy a list of items. There are multiple sellers/markets where he can buy the items from. The markets have different distances to the buyer. The buyer has to figure out a way of buying the cheapest items at the shortest distance possible.

Essentially the buyer wants to minimise his travel cost at the same time as trying to find the cheapest items.

I hope the description makes sense and if I am not clear please let me know and I will try to explain it differently.

So far I have a Buyer class, Seller class, Item class and Main class. I plan to put the buyer's location and the seller's locations using Java Point type.

I was thinking of using something like Dijkstra's algorithm for shortest path but the issue is that the buyer might get an item for cheaper if he travels slightly further.

Thanks in advance for your help and time.

Karussell
  • 17,085
  • 16
  • 97
  • 197
Jetnor
  • 521
  • 2
  • 11
  • 26
  • Does the buyer only get an item once? E.g. the buyer has a list of possible items and merchants A and B both carry item x for some price? If so, you might look at the http://en.wikipedia.org/wiki/Knapsack_problem too. – Nate Aug 09 '12 at 22:11
  • Hi Nate, The buyer has a list of items (say sugar, salt, milk). The Sellers have different items at different prices. The buyer might not necessarily find all the items he wants. The merchants don't necessarily have to have all or any of the items. As an overall objective my problem does match Kanapsack's problem description as I too want to minimise weight/distance and at same time increase value) – Jetnor Aug 09 '12 at 22:14
  • 1
    Djikstra's is for finding the shortest path between a start and finish, not finding the shortest path that visits all nodes - that would be TSP. Is this really for a master's project? o_O – BlueRaja - Danny Pflughoeft Aug 09 '12 at 22:29
  • 1
    You might be able to copy/paste/adjust [this TSP example](https://github.com/droolsjbpm/drools-planner/blob/master/drools-planner-examples/src/main/java/org/drools/planner/examples/tsp/app/TspApp.java) (Java, open source). – Geoffrey De Smet Aug 10 '12 at 10:50

4 Answers4

3

First, off this is an algorithm question, not a Java question. Once you figure out the algorithm to use, you should be able to easily implement it in your language of choice, (though Python would be a lot easier than Java).

As for algorithms, this is a NP-Hard problem, so there are no known polynomial time algorithms. (And if you find one you get a million dollar prize). What kind of inputs are you expected to handle? There might be something that works well in practice even if it has bad worst case complexity.

Antimony
  • 37,781
  • 10
  • 100
  • 107
  • Thanks for your answer. I understand what you mean that it is an algorithm question. I'm not sure if this is what you mean but as input I expect to have items names and prices, merchants and their location, a buyer and his initial location (not too sure if this is what you mean by input). so there will be a List of sellers, with each seller having a list of items, a buyer with a list of items that he wants to buy. – Jetnor Aug 09 '12 at 22:31
2

Assuming that travel costs factor into total cost, and you want to minimize the total travel+purchase cost, this sounds exactly like the Traveling Purchaser Problem, which the Traveling Salesman Problem is a special case of. The references in the wiki article have a few different solutions to the problem. Note that this problem is NP-Hard, so there is not a known solution that is guaranteed to both be fast and give the optimal solution.

Trying
  • 249
  • 2
  • 9
  • Yes you are right that is the problem. This problem was initially solved using Planners and I am now trying to find a better way of doing it. – Jetnor Aug 09 '12 at 22:43
  • Are you trying to find a novel approach to solving the problem, or is the goal of the project to implement a solution other than the one that uses Planners (I'm not sure what you mean when you say "Planners")? – Trying Aug 09 '12 at 22:59
  • by planners I mean the problem has been solved using a planning language http://en.wikipedia.org/wiki/Planning_Domain_Definition_Language this is a limiting way of solving the problem because the user decides on a route to be taken from market to market, predefines all the constraints and then runs the planner – Jetnor Aug 09 '12 at 23:11
1

I think Dijkstra's is the right direction. We had a similar issue where we needed to take customer priority into account with travel distance, and we simply decided on a way of relating priority to distance. In your case, it would be even easier, since travel costs and item costs can be easily compared. Just have the sum of both as your edge weight.

Egor
  • 1,622
  • 12
  • 26
  • I'm not sure this is extensible. What if we're buying a list of items in a non-specific order? – FrankieTheKneeMan Aug 09 '12 at 22:04
  • @FrankieTheKneeMan Then you get travelling salesman, I suppose, and have no easy solution. "the buyer might get an item for cheaper if he travels slightly further" does sound like a simplified problem, however – Egor Aug 09 '12 at 22:09
  • I mean, I agree. I just think you need to use a modified version of Dijkstra's, where link-length is dependent of the item you're trying to buy. – FrankieTheKneeMan Aug 09 '12 at 22:13
  • Actually, because the graph is complete, Dijkstra's isn't even necessary. – FrankieTheKneeMan Aug 09 '12 at 22:14
  • 1
    -1 this is not dijkstra if one already has the distances! this is Traveling salesman problem (or a variation) – Karussell Aug 10 '12 at 10:25
1

"The buyer has to figure out a way of buying the cheapest items at the shortest distance possible."

That's just find the locations of the cheapest items, then find the shortest route between them. If the same items are available at the same prices in diffferent locations, then you do shortest path for each route.

Or find the shortest route that covers all the items, and sum the price.

Only if you add a cost factor for distance travelled ( e.g petrol to drive between locations) would how far you travelled relate to how much your shopping trip costs.

You might want to google "genetic algorithm travelling salesman problem"

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • I think this will work if I do it but I am not very good in programming and not sure how to start solving the problem. Let's say I can find the cheapest items from the sellers by going through each item and comparing their prices. Once I have done that How would I go about programming wise remembering where the cheapest items were? Thanks again. – Jetnor Aug 09 '12 at 22:40
  • Oh dear. Travelling Salesman problem is the focus of a competition, for who can arrive at the best solution the fastest, for very talented and very mathematical programmers. You might have bit off a bit too much here, or may be you should starta lot simpler and iterate towards more comprehensive solutions. To answer your question, one way would be to create a struct / class of Seller, Location and Product. Then you shortestpath code would access a list of these, and use the location property for it's calculations. – Tony Hopkinson Aug 10 '12 at 11:46