0

When you have something that you want to sell or something you want to buy there's basically two options: to use money or to trade it with another item. This happens in real life and also in games where you have a trading system in place (an open market).

My question is: is there an optimal algorithm capable of receiving all of the available deals on the market and returning the best, optimal sequence of trades to get to item x that minimizes the overall cost?

I'll give you an example: Lets say that on the market we have this two tables, one for the people that want to trade/sell something, and one for the people that want to buy something:

Sellers:

Item I Have Items I Want (or) Price I Want
Item A - 140
Item A - 160
Item B - 50
Item C Item A + Item B 220
Item D Item C 240
Item E Item A + Item D 360
Item X Item E 360

Buyers:

Item I Want Price I'll Pay
Item A 150
Item B 70

It has to take into consideration some of the following scenarios:

  • Get to Item x only using money;
  • Get to Item x using money and trades;
  • Get to Item x using only trades;
  • Consider several small trades before getting the final trade;
  • The market is dynamic and the prices are not really "fixed". Meaning that some sellers/buyers are definitely buying/selling at prices bellow or up the average. The algorithm should thrive on this "misplaced" deals.

I don't know if I was able to make myself clear. I have a background in engineering and programming, and tried to find something that would tackle this problem but couldn't find much. I imagine that there's something to do with Greedy Algorithms, but I honestly don't know it there's something already proven to be good for this problem or if neural networks could do something for me here.

I know that this problem isn't trivial at all. In fact, there's a lot of people that make a living buying/selling items for profit like that. But they rely mostly on their knowhow and mining of opportunities that appear on the market.


I initially thought that I could transform this problem into a "traveling salesman" type of problem, where the cities are the items, and the distance between the cities is the price, and I would use some of the already established algorithms for this problem on it. But I don't know if it will work (if someone else already tried it).

  • _"is there a optimization algorithm capable of receiving all of the available deals on the market and returning the best, optimal sequence of trades to get to item x that minimizes the overall cost?"_ Yes, brute force. Try all possible combinations and take the minimum. – Thomas Sablik Feb 02 '21 at 14:34
  • @ThomasSablik I don't really consider "brute force" as "optimal", and it is quite obvious that this is the default answer. But thank you for the reply anyways. – Thomas Marcelo Feb 02 '21 at 14:40
  • @ThomasMarcelo: You did not ask for an optimal algorithm, you asked for an algorithm that produced an optimal solution. A brute force algorithm that finds an optimal solution satisfies that. – Eric Postpischil Feb 02 '21 at 14:41
  • You will need to define cost or add a constraint such as starting with only money. As it is, if I start with $100 and Item A, and I can get the goal Item B for either $100 or Item A, there is no criterion for whether it is lower cost to pay $100 or to give up Item A. – Eric Postpischil Feb 02 '21 at 14:43
  • 1
    _"optimal algorithm"_ regarding what? Time complexity, memory complexity, simplicity, ... "Optimal" is pretty opinion based if you don't specify it. – Thomas Sablik Feb 02 '21 at 14:44
  • @EricPostpischil Sorry, english is not my first language, I thought that this was clear on the question. I want to know if there's a mathematical, optimal way of getting to a good result to this problem – Thomas Marcelo Feb 02 '21 at 14:44
  • @EricPostpischil there's no initial condition. This algorithm would suggest the minimal needed for getting to item X. Something like "you'll need $ 150, $ 120 to buy Item A and $ 30 later to buy Item D" – Thomas Marcelo Feb 02 '21 at 14:48
  • @ThomasSablik In this case, having "brute force" as a baseline, anything that doesn't have to look to every single possibility (which I imagine would take exponential-type of time depending on the number os possibilities) could be optimal. Time is a constraint, but it could take hours, no problem – Thomas Marcelo Feb 02 '21 at 14:50
  • So your actual question is if there exists an algorithm with polynomial time and memory complexity? – Thomas Sablik Feb 02 '21 at 14:52
  • You need to give us criteria to determine whether “Give $100 for Item B” or “Give Item A for Item B” is lower cost. If we do not have criteria for knowing which one is lower cost, we cannot design an algorithm to produce an answer, because we will not know what answer to produce. – Eric Postpischil Feb 02 '21 at 14:55
  • @EricPostpischil I think that we can assume that everything begins with money. So the first thing the algorithm does it to buy an Item. What he does next (if it trades with another or if it sells it for a slightly higher price) is up to the optmization – Thomas Marcelo Feb 02 '21 at 15:02
  • @EricPostpischil on the market there's people wanting Item A in exchange for Item B, but there's also people wanting to buy Item A for a price. At the time of the analysis the algorithm would determine that to get to Item X it should trade Item A for Item B or sell it so it could buy Item C with the profits, for example. Something like that – Thomas Marcelo Feb 02 '21 at 15:07
  • Isn't this a classical dynamic programming problem? – Thomas Sablik Feb 02 '21 at 15:08

1 Answers1

2

You can solve this with a single source shortest path algorithm for weighted directed graphs which allows negative-weight edges, such as the Bellman–Ford algorithm.

The nodes in the graph are sets of items, the source node is the set of items you start with (possibly the empty set), the edges are either buying an item (the edge weight is the purchase price), selling an item (the edge weight is the negative sale price), or trading a set of items (the edge weight is zero).

The Bellman–Ford algorithm will find paths minimising the cost (i.e. maximising the amount of money you have remaining) to each possible set of items you can end up with. Then you can simply take the minimum cost of a path to any set containing the item you wish to acquire.

It's possible that your economy allows arbitrage, i.e. some sequence of trades, purchases and sales by which you can end up with the same items you started with but with more money. (In your example, you can buy item A for 140 and sell it for 150.) In this case the graph contains a negative-weight cycle, so there is no "best" price for any item because you can exploit the arbitrage opportunity for unbounded profit. If there is a negative-weight cycle, the Bellman–Ford algorithm will detect and report it instead of finding shortest paths.

kaya3
  • 47,440
  • 4
  • 68
  • 97
  • Thank you for your answer, @kaya3. It sounds like framing the problem this way and using such algorithm would work. I'll start experimenting with an implementation soon. Your last paragraph is actually what was the confusing part of the problem to me initially, and it was the reason I opened this question. I just could not figure it out how to allow for the algorithm to make this "turns", where it ends up with the same item but a little profit. I imagine that for really expensive final items, several of this minor trades should take place – Thomas Marcelo Feb 02 '21 at 17:11