snippetMinor
How can I do an optimization problem that involves finding a sequence?
Viewed 0 times
problemcanfindingsequenceoptimizationthathowinvolves
Problem
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)?
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)?
Solution
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.
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.
Context
StackExchange Computer Science Q#54467, answer score: 2
Revisions (0)
No revisions yet.