Minimizing the sum of remainders with a bound dividend

97 Views Asked by At

I'm trying to figure out if there is an optimal algorithm for the following:

Let's say we have $n$ strips of length $l$ and a set of $n$ cutters. Each cutter is given a strip and it makes a cut every $c_i$ units. The remainder, $l \bmod c_i$, is thrown out. Moreover, $l$ has an upper and a lower bound: $0<a<l<b$. $b$ can be less than the least common multiple of $c_1, c_2, ..., c_n$.

Is there an efficient algorithm for finding such $l$, that the sum of remainders is minimized?

$\min_{a<l<b}\sum_{i=1}^n l \bmod c_i$

Or better, the sum of squared remainders:

$\min_{a<l<b}\sum_{i=1}^n (l \bmod c_i)^2$

Or better yet, the sum of (squared) ratios of remainders to frequencies of cuts per unit of length:

$\min_{a<l<b}\sum_{i=1}^n ({l \bmod c_i\over {1\over c_i}})^2 = min_{a<l<b}\sum_{i=1}^n ((l \bmod c_i)*c_i)^2$

I'm writing a program with an equivalent problem and so far I've been limiting $l$ to integers and using brute force to find the optimal $l$. While it gets the job done, it made me wondering if there's a better way of doing this.

1

There are 1 best solutions below

3
On BEST ANSWER

Since you've been "limiting $l$ to integer", I'll give you another more or less brute force method that works with real numbers. In some case, this other brute force may have better performance with integer $l$ values too. In other cases, it can also have much, much worse, performance.


Principle

First, let $q_i\in\mathbb Z$ and $0\le r_i<c_i$ the unique pair such that $l=q_ic_i+r_i$. Notice that $\frac l{c_i}=q_i+\frac{r_i}{c_i}$, thus $\left\lfloor\frac l{c_i}\right\rfloor=q_i$. This yields $r_i=l-\left\lfloor\frac l{c_i}\right\rfloor c_i=l\bmod c_i$.

As you may know, the floor function is is only derivable on an open interval of the form $(k,k+1)$, where $k\in\mathbb Z$. So in order to study your objective functions, you can brute force the issue by figuring out the collection of values $x$ where $r_i$ has a discontinuity for some $i$, say $$S=\left\{x : a<x<b,\ \exists i, \frac x{c_i}\in\mathbb Z\right\}$$ $S$ can be computed fairly easily, and this collection splits the interval $(a,b)$ into finitely many connected open intervals. For each of these open intervals, you have the nice property that $q_i$ is constant with respect to $l$, thus $r_i$ can be interpreted as linear with respect to $l$. You can thus compute the minimum value on those intervals, and also evaluate your function on every point of $S\cup\{a,b\}$, and the overall minimum is the solution you seek.


Examples

For instance, let $f(l)=\sum_{i=1}^n l\bmod c_i=\sum_{i=1}^n(l-q_ic_i)$. Then $\frac{\partial f}{\partial l}(l)=\sum_{i=1}^n1=n>0$. Thus the small open intervals do not contain any minimum in their interior, and it suffices to find the minimum for $x\in S\cup\{a,b\}$ to obtain the minimum on the whole interval $(a,b)$.


Likewise let $g(l)=\sum_{i=1}^n(l\bmod c_i)^2=\sum_{i=1}^n(l-q_ic_i)^2$, we have $\frac{\partial g}{\partial l}(l)=2\sum_{i=1}^nl-q_ic_i$, thus $g$ may reach an extremum at $\hat l=\frac 1n\sum_{i=1}^nq_ic_i$. In order for $g$ to actually reach that extremum, $\hat l$ must belong to the small interval you are currently processing. If $\hat l$ does not lie in that interval, then $\frac{\partial g}{\partial l}$ has constant sign over that interval, and the infemum value is reached at one of the endpoints. If $\hat l$ actually belongs to the interval, note that this extremum is a minimum, since $\frac{\partial^2g}{\partial l^2}=2n>0$. Once again you obtain the global minimum by processing every small intervals, and checking $S\cup\{a,b\}$.


Finally let $h(l)=\sum_{i=1}^n((l\bmod c_i)\times c_i)^2=\sum_{i=1}^n((l-q_ic_i)c_i)^2$. Then $\frac{\partial h}{\partial l}(l)=2\sum_{i=1}^n(l-q_ic_i)c_i^2$. Thus $h$ may reach an extremum at $\hat l=\frac{\sum_{i=1}^nq_ic_i^3}{\sum_{i=1}^nc_i^2}$, and that extremum would again be a minimum for similar reasons.


Comparison with your current approach

If you use the method above, you need constant time to process each intervals, and the number of intervals is linear with the size of $S$. So the time complexity is $O(\lvert S\rvert)$. The size of $S$ is highly dependant on your bounds $a,b$, and the values of the $c_i$'s. You can bound it with $$\sum_{i=1}^n \left\lfloor\frac{b-a}{c_i}\right\rfloor \le \lvert S\rvert \le \sum_{i=1}^n \left\lceil\frac{b-a}{c_i}\right\rceil$$ I think it is possible to come up with examples that reach the lower bound, and other that reach the upper bound. Also the value of these bounds can be anything in $\mathbb N$, so it is difficult to compare it directly with a brute force on every integer $l$ values in $(a,b)$ without additional information.

If the length of your interval $(a,b)$ is comparable to the magnitude of your $c_i$'s, your brute force method is probably faster. In that case, the only real advantage of my proposition, is that it can find the minimum over real numbers, and not only integer values. On the other hand, if your interval length is small compared to the magnitude of the $c_i$'s, you have good odds to have a small $S$, in which case this method is faster.