A question on Mathematics Stack Exchange asks how to solve an optimization problem related to a game apparently called "speedrunner". The problem boils down to selecting integer values for variables na, nc and nb,i (i=1,…,nc) so as to minimize elapsed time ttotal=tana+nc∑i=1tbnb,i+tcnc,
where ta, tb and tc are parameters with specified values. The sole constraint specified is that winnings, defined by
xtotal=x0nanc∏i=1nb,i(where x0 is another parameter), must be at least $1 billion. We can infer from the requirement of positive earnings that na≥1, nc≥1, and nb,i≥1 for i=1,…,nc. For modeling purposes, we will allow the index i to exceed nc and require that nb,i=0 for i>nc. An integer linear programming model would be a no-brainer were it not for two things: the product form of the expression for winnings is nonlinear; and it involves the product of a variable number of variables.
The approach I chose was to express the original variables in terms of binary variables. To do that, we first need upper bounds on the original variables. We get those by guessing an upper bound T on ttotal. (If we guess too low, we will either get a solution that exceeds that upper bound or the model will become infeasible, either of which will tip us off to try a larger guess.) Once we have that upper bound, we get an upper bound for each variable by setting the other two variables as small as possible. That leads to the following upper bounds:Na=⌊T−tb−tcta⌋
Nb=⌊T−ta−tctb⌋
and
Nc=⌊T−tatc+tb⌋.
The denominator in the third equation arises from the fact that adding 1 to nc not only directly costs tc units of time but also adds another nb,i variable with minimum value 1, costing tb units of time.
Armed with those upper bounds, we can introduce binary variables yj (j=1,…,Na), wj (j=1,…,Nc) and zi,j (i=1,…,Nc and j=0,…,Nb) along with constraints making them type 1 special ordered sets (i.e., each set contains exactly one variable with value 1) and expanding the original variables in terms of them. Specifically:
- na=∑Naj=1j⋅yj with constraint ∑Naj=1yj=1;
nc=∑Ncj=1j⋅wj with constraint ∑Ncj=1wj=1; and
- nb,i=∑Nbj=0j⋅zi,j for i=1,…,Nc
with the following constraints for all i∈{1,…,Nc}:
- ∑Nbj=0zi,j=1 (the value of nb,i is uniquely defined); and
- ∑Nbj=0zi,j=1 (the value of nb,i is uniquely defined); and
- zi,0+∑Nck=iwk=1 (nb,i=0 if and only if nc<i).
Armed with all that, the original objective function can be expressed as minimizingtana+tbNc∑i=1nb,i+tcnc,where the only tweak is that we now sum over a constant number Nc of terms rather than a variable number nc, relying on the fact that nb,i=0 for i>nc.
That brings us to the nonlinear formula for winnings. The original constraint is xtotal≥109, which we can rewrite as log(xtotal)≥log(109) using whatever flavor logarithm you like. Expanding the logs of na and nb,i in terms of the binary variables, we getlog(xtotal)=log(x0)+Na∑j=1log(j)⋅yj+Nc∑i=1Nb∑j=1log(j)⋅zi,j.So the constraint that log winnings equal or exceed the log of 109 is linear in the binary variables.
The solution in the Math Stack Exchange post (na=7, nc=3 and nb,1=nb,2=nb,3=18) turns out to be optimal given the parameter values in the question, and the IP model here will correctly achieve the minimum time (approximately 52.3 minutes) ... but it will not necessarily reproduce the best winnings within that time (approximately $1.02 billion). That is because there are multiple feasible solutions that hit the correct time value, with na=7 and with nc=3 but with the 54 cumulative nb units allocated differently (for instance, {19,19,16} rather than {18,18,18}. To get the best possible solution, we can use a bicriterion model with lexicographically ordered objectives, first minimizing time and then maximizing winnings (by minimizing the negative of the log of winnings).
I tested that model using the Java API to CPLEX 22.1. My code is available from my university repository.