4
$\begingroup$

The problem I am trying to solve is to find the fastest way to produce an total amount of something. There are several different producers that each have a different production and cost (based on the number of the item that is already owned) which can be purchased during production.

For example, assume that the goal is to produce 1000 units total (including the amount spent on buying new producers), and the three producers that can be bought $A$, $B$, and $C$, have productions and base costs of $1$ per second and $20$, $7$ per second and $50$, and $14$ per second and $100$, respectively. Buying a producer multiples the price of the next producer of the same type by 1.1 (so, for example, buying an $A$ for the base price of 20 means the next $A$ will cost $1.1 \cdot 20 = 22$, and the next $1.1 \cdot 22 = 24.2$, et cetera). When a producer is bought, the price of the produced is subtracted from the current units produced, but not the total production (so if you have 30 units, and you buy an A, you now have 10 units left). Selling a producer returns half of the units used to purchase it, and reduces the cost by 1.1 times (so if the cost was at 24.2, it would go down to 22). By starting with 1 $A$, what is shortest amount of time to produce 1000 units and in what order should you purchase producers?

The shortest time is 68 seconds, buying producers in the following order: A,B,B,B,C,C,C,C.

My first solution was simply to try all the different sequences, then determine the one that will be the fastest after $n$ purchases, but, naturally, this was very slow. My second solution was to try a greedy search where I just look for the best solution in terms of time at every step. My third solution was to try a search where I used the increased in production as the score for each sequence, and backtrack to earlier solutions if any of them had better scores.

Are there any other problems or generalizations I can make to find an optimal (or near optimal solution faster)?

$\endgroup$
  • $\begingroup$ Thanks for all the edits! 1. If I have 30 units and I buy a producer that costs 20, do I now have 10 units, or do I still have 30 units? 2. Have you tried dynamic programming? $\endgroup$ – D.W. Mar 14 '16 at 22:58
  • $\begingroup$ @D.W. 1. Yes, you have 10 units. 2. I'm not entirely sure what that is, but I'll read about it, try it, and get back to you. $\endgroup$ – Reed Oei Mar 15 '16 at 0:26
2
$\begingroup$

Graph search

I recommend you formulate your problem as a graph search problem. Each vertex represents a state of the system. The state of the system lists the number of units you currently have and the number of each producer you currently have (e.g., "20 units, 1 A, 3 B's, and 0 C's"). You add an edge $s \to s'$ if you can get from state $s$ to state $s'$ in a single step. The start state is "0 units, 0 A's, 0 B's, 0 C's". The goal states are all states with 1000 units.

You can now use breadth-first search to find the shortest path from the start state to a goal state.

For better performance, use A*. It's not too hard to construct a consistent and admissible heuristic that always underestimates the distance to the goal state.

As long as the number of producers is not too large, I expect this to be pretty efficient.

Greedy algorithm

It might be possible to build a greedy algorithm for your problem.

Consider a simplified variant where you are not allowed to sell producers you own, and where the number of units you have is allowed to go negative (you can still buy a producer even if it would put you in debt). This has an efficient greedy solution. In particular, consider the decision problem variant: given $t$, determine whether there exists a sequence that gets you to the goal within at most $t$ seconds. This can be answered greedily.

Consider our first action. We have $k$ options for actions: "buy $P_1$", "buy $P_2$", ..., or "buy $P_k$". Note that it's always advantageous to make all your producer-buys as early as possible; there's no point in waiting. Assume that for producer $P_i$ we have base cost $b_i$ and production $p_i$. If we buy $P_i$, the net profit from that purchase (over the entire $t$-second time window) is $(t-1) p_i - b_i$. So, for your first action, choose the action with largest net profit. Now, for your second action, we have the same problem, except that $t$ is one smaller, so we can continue iteratively. After $t$ seconds, we check whether the resulting sequence has reached a goal state.

[You can also consider a kind of exchange operation, where you compare "buy $P_i$ then $P_j$" to "buy $P_j$ then $P_i$". You'll find that the decision rule for making that choice is as follows. Associate to the action "buy $P_i$" a 'profit foregone if we defer buying $P_i$' value of $0.1 b_i + p_i$. Determine which of $P_i$ or $P_j$ has lower 'profit foregone', and then choose to do that one first.]

You could check whether you can generalize this analysis to the case where you're allowed to sell producers and where you're not allowed to let the number of units owned go negative. It looks a little tedious, so I'll leave the case analysis to you.

$\endgroup$
  • $\begingroup$ I tried the A* algorithm for the graph searching, as going into debt is not allowed (as the goal is total production, not the stockpiling of units, the best strategy would simple be to buy as much of the most best producer as possible). The cost to get to a state is obviously the time, but I am unsure of how to construct an admissible heuristic. My first thought was to simply calculate how long it would take at current production, until I realized that was definitely not admissible. Of course I could just use 0, but I suspect that leads to poor performance. $\endgroup$ – Reed Oei Mar 16 '16 at 13:34
  • $\begingroup$ @Kytuzian, one idea for getting a heuristic: calculate how long it would take to reach your goal (starting from the current state) if you had a way to buy producers without paying their base cost (but selling those 'free' producers didn't return you anything). The optimal strategy should be efficiently computable: it'll probably be to buy as many of the best producers as quickly as possible, and then at the very end sell off some the pre-existing producers. The optimal solution under those relaxed constraints will never overestimate the trust cost. $\endgroup$ – D.W. Mar 16 '16 at 17:09
  • $\begingroup$ I'm not sure I understand this suggestion. Won't this estimate the time for every scenario to be 1 second (buying as many producers as it takes to produce the desired amount, as you don't have to pay for them)? $\endgroup$ – Reed Oei Mar 18 '16 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.