1 Introduction

In this paper, we consider the Online Unit Profit Knapsack Problem. The request sequence consists of n items with sizes in (0, 1]. An online algorithm receives them one at a time, with no knowledge of future items, and makes an irrevocable decision for each, either accepting or rejecting the item. It cannot accept an item if its size, plus the sum of the sizes of the already accepted items, is greater than 1. The goal is to accept as many items as possible. The obvious greedy algorithm solves the offline Unit Profit Knapsack Problem, since the set consisting of as many of the smallest items as fit in the knapsack is an optimal solution. Let \({\textsc {Opt}} _s\) denote this optimal solution. Even for this special case of the Knapsack Problem, no competitive online algorithm can exist.

We study the Online Unit Profit Knapsack Problem under the assumption that (an approximation of) the average item size, a, in \({\textsc {Opt}} _s\) is known to the algorithm. This particular information could likely be estimated from historical data. The measure we use for the prediction error is the ratio \(r=\frac{a}{\hat{a}}\), where a is the actual value for this average size in \({\textsc {Opt}} _s\) and \(\hat{a}\) is the prediction. We consider the case, where the exact value of \(\hat{a} =a\) is given to the algorithm by an oracle, calling this an accurate prediction, as well as the case where \(\hat{a} \) could possibly be incorrect. Predictions could be inaccurate, for example, because the characteristics of the input may be different depending on the time of day the input is produced, which source produced the input, etc.

For an algorithm using predictions, the competitive ratio obtained with accurate predictions is called the consistency of the algorithm [1, 2]. Note that in our case, the prediction is accurate when \(r=1\). The robustness of the algorithm is its competitive ratio in the case where there is no guarantee on the quality of the prediction. We obtain algorithms that are at least \(\frac{1}{2}\)-consistent and have a smooth transition between consistency and robustness that depends on the value of the error measure, r. Since no online algorithm can be competitive without predictions, no online algorithm with predictions can be better than 0-robust.

1.1 Preliminaries

For any input sequence, \(\sigma \), and algorithm, \({\textsc {Alg}}\), for the Online Unit Profit Knapsack Problem, \({\textsc {Alg}} (\sigma )\) denotes the number of items accepted by \({\textsc {Alg}}\) when processing \(\sigma \).

We use the asymptotic competitive ratio throughout this paper. Thus, an algorithm \({\textsc {Alg}}\), is c-competitive if there exists a constant b such that for all request sequences \(\sigma \), \({\textsc {Alg}} (\sigma ) \ge c{\textsc {Opt}} (\sigma )-b\). \({\textsc {Alg}}\) ’s competitive ratio is then \(\sup \{ c\mid {\textsc {Alg}} \mathrm {~is~}c\text{- }\textrm{competitive}\}\). Note that this is a maximization problem and all competitive ratios are in the interval [0, 1].

We use the notation \(\mathbb {N}=\left\{ 1, 2, 3, \ldots \right\} \). At any given time during the processing of the input sequence, the level of the knapsack denotes the sum of the sizes of the items already accepted.

1.2 Previous Work

The Knapsack Problem is well studied and comes in many variants; see Kellerer et al. [3]. Cygan et al. [4] refer to the online version we study, where all items give the same profit, as the unit case. They mention that it is well-known that no online algorithm for this version of the problem is competitive, i.e., has a finite competitive ratio, and they present a sequence showing this for the strict competitive ratio (where the additive constant b must be zero). The sequence starts with an item of size 1. The sequence either ends there or is followed by \(1/\varepsilon \) items of size \(\varepsilon \) – if and only if the algorithm accepts the item of size 1. Thus, the ratio is either \(0/1=0\) or \(1/(1/\varepsilon )=\varepsilon \). Since the adversary can choose \(\varepsilon \), the algorithm cannot have a strict competitive ratio better than zero.

In the General Knapsack Problem, each item comes not only with a size, but also with a profit, and the goal is to accept as much profit as possible given that the total size must be at most 1. The ratio of the profit to the size is the importance of an item. (This is sometimes called value, but we want to avoid confusion with other uses of that word.)

The Online General Knapsack Problem was first studied by Marchetti-Spaccamela and Vercellis [5]; they prove that the problem does not allow for competitive online algorithms, even for Relaxed Knapsack (fractions of items may be accepted), where all item sizes are 1. They concentrate on a stochastic version of the problem, where both the profit and size coefficients are random variables. They present both a fixed and an adaptive threshold algorithm.

The Online General Knapsack Problem with oracle-based advice was studied in [6]. In this model, the algorithm is given information, called advice, about the input, and the goal is to determine how many bits of advice are necessary and sufficient to achieve a given competitive ratio. The fundamental issues and many of the early results on oracle-based advice algorithms, primarily in the direction of advice complexity, can be found in [7, 8], though many newer results for specific problems have been published since. The advice used in [6] includes the maximum importance, v, for items to accept. Since there might be many items of that importance, but the optimal solution may contain very few of them, they also include further advice, the fraction of the knapsack filled by items of importance v. In addition, they limit the amount of advice by giving k-bit approximations to these two values, the maximum importance and the fraction of the knapsack needed for these items. They show that no online algorithm using fewer than \(\log n\) bits of advice is competitive, but for \(\varepsilon >0\), there exists a \((1+\varepsilon )\)-competitive algorithm using \(O(\log n)\) bits of advice.

The authors of [6] also study the Online Unweighted (or Simple) Knapsack Problem, where the profit of each item is equal to its size. This version of the Knapsack Problem is also called the proportional or uniform case. In [6], it is shown that 1 bit of advice is sufficient to be \(\frac{1}{2}\)-competitive, \(\Omega (\log n)\) bits are necessary to be better than \(\frac{1}{2}\)-competitive, and \(n-1\) advice bits are necessary and sufficient to be optimal.

In [9], the Online General Knapsack Problem is considered with the assumption that all items are much smaller than the unit capacity of the knapsack. The results depend on the ratio, \(\gamma \), between known upper and lower bounds on the importance of the items. For this setting, a threshold algorithm of optimal competitive ratio \(\frac{1}{\ln (\gamma )+1}\) is known [10]. The threshold depends on \(\gamma \) and the current level of the knapsack and it lower bounds the importance of accepted items. This is in contrast to our threshold function which depends on the number of relatively large items accepted so far. In [9], a parameter, \(\alpha \), is added to the threshold function, resulting in a family of algorithms with a competitive ratio which is proven to be at most a certain factor, depending on \(\alpha \), worse than the optimal competitive ratio. For \(\alpha =1\), this factor, called the degradation factor, is 1. Within this family, the subset of algorithms with a degradation factor no larger than a given factor, \(\phi \), is called the policy class of \(\phi \)-degraded algorithms. Within a given policy class, standard machine learning techniques are used on data from a real world application to choose an algorithm, balancing worst-case guarantees (competitive ratio) against the performance in typical cases.

The Online General Knapsack Problem is also considered in [11], again with upper and lower bounds on the possible importance of items. To obtain a finite number of importance values, each importance is rounded to the nearest power of \(1+\varepsilon \), for some \(\varepsilon >0\).

For each rounded importance, v, upper and lower bounds on the total size of items with importance v are predicted. The authors present an algorithm which has some similarities to ours. In particular their budget function has a similar function to our threshold function; both specify the maximum number of the low importance, large items that need to be accepted to obtain the proven competitive ratios. Their algorithm obtains what they prove to be the best possible competitive ratio (for the given predictions without rounding), up to an additive factor that goes to zero as the size of the largest item goes to zero. The paper also considers two generalizations of the Knapsack Problem, called the Generalized One-Way Trading Problem and the Two-Stage Online Knapsack Problem.

The Bin Packing Problem is closely related to the Knapsack Problem. This is especially true for the dual variant where the number of bins is fixed and the objective is to pack as many items as possible [12]; the Unit Price Knapsack Problem is Dual Bin Packing with one bin. The standard Bin Packing Problem was considered with predictions in [13]. They give an algorithm with a parameter that can be adjusted to obtain consistency r and robustness \(\max \{33-18r,7/4\}\), for any \(1.5<r\le 1.75\).

Bin Packing is also studied in [14] in the standard setting for online algorithms with predictions, giving a trade-off between consistency and robustness, with the performance degrading as a function of the prediction error. They assume discrete item sizes, and the prediction is the number of items of each possible size. They also have experimental results.

Much additional work has been done for other online problems, studying variants with predictions (machine-learned advice, for instance), inspired by the work of Lykouris and Vassilvitskii [1, 15] and Purohit et al. [2] in 2018. Some of this further work is in the directions of search-like problems [16,17,18,19,20], scheduling [21,22,23,24,25,26,27,28,29,30,31,32], rental problems [33,34,35], caching/paging [36,37,38,39,40], and other problems [14, 41,42,43,44,45], while some papers attack multiple problems [13, 46,47,48,49]. For a survey, see [50].

We note here that the error measure we use, \(r=\frac{a}{\hat{a}}\), has similarities to the distortion used for a non-clairvoyant flow problem in [24]. The distortion is based on the maximum ratios of the estimated-to-real and real-to-estimated processing times. In [24], the algorithm presented does not have or use knowledge of the distortion, and the competitive ratio found is a function of the distortion. They call this type of algorithm distortion-oblivious. Similarly, the algorithms we present do not have or use knowledge of the error ratio, and the competitive ratios we find are functions of the error ratio, so these algorithms could be called error ratio oblivious. Note that the non-clairvoyant flow time problem considered in [24] is one of the few problems considered with predictions, where there is no algorithm without predictions with a constant competitive ratio, and [24] also uses ratios for the error measure/distortion.

1.3 Our Results

We consider both the case where the prediction \(\hat{a} \) is known to be accurate, so \(r=1\), and the case where it might not be accurate. Different algorithms are presented, but they have a common form.

For our algorithm Consistent Adaptive Threshold (\({\textsc {CAT}}\)) where the prediction is accurate and, thus, \(\hat{a} =a\), the competitive ratio is \(\frac{e-1}{e}\), and we prove a matching upper bound that applies to any deterministic algorithm knowing a. This upper bound limits how well any algorithm using accurate predictions can do; the competitive ratio cannot be better than \(\frac{e-1}{e} \approx 0.6321\) for \(r=1\).

If \({\textsc {CAT}}\) is used for possibly inaccurate predictions, it obtains a competitive ratio of \(r\frac{e-1}{e}\) for \(r \le 1\), \(\frac{e-r}{e}\) for \(1 \le r \le e\), and 0 for \(r \ge e\). No deterministic algorithm can be better than this for both \(r<1\) and \(1< r < e\).

For another algorithm, Robust Adaptive Threshold (\({\textsc {RAT}}\)), we have results for two cases: for \(r\le 1\) the competitive ratio is \(\frac{r}{2}\), and for \(r\ge 1\) the competitive ratio is \(\frac{1}{2r}\). Thus, for accurate predictions, the competitive ratio of \({\textsc {RAT}}\) is \(\frac{1}{2}\), slightly worse than for the other algorithm. We show a negative result implying that a deterministic online algorithm cannot both be \(\frac{1}{2r}\)-competitive for a range of large r-values and better than \(\frac{1}{2}\)-competitive for \(r=1\). We summarize these results in Table 1.

Table 1 The competitive ratios obtained by the two algorithms are shown.

In Sect. 6, we consider two upper bounds on the advice complexity of the Online Unit Price Knapsack Problem. One is based on an algorithm in this paper, relating missing bits in advice to errors in predictions. The other is based on techniques from [6], with different predictions, the maximum size for items to accept, plus the fraction of the knapsack filled by items of that size. A lower bound for the amount of advice necessary for optimality is also presented.

The section on advice complexity is brief, since our main focus is on predictions of information that may be easily obtainable. It seems believable that the average size of items in \({\textsc {Opt}} _s\) of requests could be estimated: Since \({\textsc {Opt}} _s\) contains the smallest items in the request sequence, it is easy to calculate an average from previous request sequences. It is a single number to collect and store, as opposed to more detailed information about a distribution. Since so little storage is required, one could keep multiple copies if, for instance, the expected average changes during the day. Our results also hold if the prediction is \(\frac{1}{{\textsc {Opt}}}\). Thus, it is sufficient to store either the average size of the items in \({\textsc {Opt}} _s\) or the value \({\textsc {Opt}}\).

2 Online Unit Profit Knapsack Without Predictions

As mentioned earlier, it is well-known that no Online Unit Profit Knapsack algorithm can have a constant competitive ratio. For completeness, we give a proof for the asymptotic competitive ratio here, since we have not found it in the literature.

Theorem 1

No algorithm for the Online Unit Profit Knapsack Problem can have a constant competitive ratio.

Proof

Assume for the sake of contradiction that there exists an algorithm, \({\textsc {Alg}}\), and constants, b and c, such that for any input sequence, \(\sigma \), \({\textsc {Alg}} (\sigma ) \ge c {\textsc {Opt}} (\sigma ) - b\).

Consider a family of input sequences, \(\sigma _r\), \(r \in \mathbb {N}\), each consisting of r rounds, \(i=1,2,\ldots ,r\). In Round i, \(\left\lfloor \frac{i}{c}\right\rfloor \) items of size \(\frac{c}{i}\) arrive.

\({\textsc {Opt}}\) will accept all items of the last round, so after i rounds, \({\textsc {Alg}}\) must have accepted at least \(c\left\lfloor \frac{i}{c}\right\rfloor -b > i-1-b\) items. Since \({\textsc {Alg}}\) can only accept an integer number of items, this means that \({\textsc {Alg}}\) has accepted at least \(i-b\) items. Thus, the ith largest item accepted by \({\textsc {Alg}}\) must have size at least \(\frac{c}{b+i}\). Therefore, after r rounds, the total size of items accepted by \({\textsc {Alg}}\) must be at least

$$\begin{aligned} \sum _{k=b+1}^{r} \frac{c}{k}&= c \left( \sum _{k=1}^{r} \frac{1}{k} - \sum _{k=1}^{b} \frac{1}{k} \right) \\&> c (\ln (r)-\ln (b) -1), \text { since } \ln (n)< \sum _{k=1}^{n} \frac{1}{k} < \ln (n)+1, n \in \mathbb {N}\,. \end{aligned}$$

However, since \(\ln (b)\) is a constant and \(\lim _{r \rightarrow \infty } \ln (r) = \infty \), there must exist an r such that this total size is larger than 1. \(\square \)

3 Threshold Algorithms

The simplest way to use information about the average item size, a, in an optimal solution seems to be to choose some \(q>1\) and accept items of size at most qa, as long as they fit in the knapsack. The algorithm One-Threshold does exactly this, with \(q=2\).

Algorithm 1
figure a

Algorithm One-Threshold.

Theorem 2

The competitive ratio of One-Threshold, as defined in Algorithm 1, is \(\frac{1}{2}\).

Proof

For the lower bound, consider any input sequence, \(\sigma \). We first consider the case where One-Threshold rejects an item of size less than 2a. In this case, the knapsack is filled to more than \(1-2a\). Since all accepted items have size at most 2a, this means that the algorithm has accepted more than \(\frac{1}{2a}-1\) items. Hence, \(\textsc {One-Threshold} (\sigma ) > \frac{1}{2} {\textsc {Opt}} (\sigma ) -1\).

We now consider the case where One-Threshold rejects no items of size at most 2a. We let \(\ell \) and s denote the number of items in \({\textsc {Opt}}\) ’s knapsack that are larger than 2a and no larger than 2a, respectively. Note that \(\textsc {One-Threshold} (\sigma ) \ge s\), since the algorithm rejects no items of size at most 2a. Since the average size of the items in \({\textsc {Opt}}\) ’s knapsack is a, \(\ell < s\), so \({\textsc {Opt}} (\sigma )=\ell +s<2\,s\le 2 \cdot \textsc {One-Threshold} (\sigma )\).

For the upper bound, consider the sequence \(\sigma _1\) consisting of \(\left\lfloor \frac{1}{2a}\right\rfloor \) items of size 2a followed by \(\left\lfloor \frac{1}{a}\right\rfloor \) items of size a. We note that \(\textsc {One-Threshold} (\sigma _1) < \frac{1}{2} {\textsc {Opt}} (\sigma _1)+1\).

For an alternative proof of the upper bound, consider the sequence \(\sigma _2\) consisting of \(\ell \) items of size \(2a+\varepsilon _{\ell }\) and s items of size \(\varepsilon _s\), where \(\ell =s-1\), \(s = \left\lfloor \frac{1}{2a}-\frac{1}{2}\right\rfloor \), \(\varepsilon _{\ell } < a/\ell \), and \(\varepsilon _s = (a-\ell \varepsilon _{\ell })/s\). The number of items in \(\sigma _2\) is \(\ell +s = 2s-1\) and their total size is \(\ell (2a+\varepsilon _{\ell }) + s\varepsilon _s = \ell (2a + \varepsilon _{\ell }) + a - \ell \varepsilon _{\ell } = 2\ell a +a = (2\,s-1)a\), so the average size is a and the total size is \((2\left\lfloor \frac{1}{2a}-\frac{1}{2}\right\rfloor -1)a \le 1\). Thus, \({\textsc {Opt}} (\sigma _2) = \ell + s = 2s-1 = 2 \cdot \textsc {One-Threshold} (\sigma _2)-1\). \(\square \)

Considering \(\sigma _1\) in the proof of Theorem 2, it is clear that to improve the competitive ratio, one would have to lower the threshold to a value smaller than 2a. However, with a threshold smaller than 2a, the sequence \(\sigma _2\) of that same proof could be adapted to prove a competitive ratio lower than \(\frac{1}{2}\). Thus, working with just one threshold is not enough to obtain a competitive ratio better than \(\frac{1}{2}\). However, an algorithm starting with a threshold larger than 2a can obtain a competitive ratio better than \(\frac{1}{2}\) by sometimes lowering the threshold to a value below 2a at some point during execution. The algorithm Two-Thresholds outlined in Algorithm 2 is such an algorithm. It guards against both sequences \(\sigma _1\) and \(\sigma _2\) by allowing some items larger than 2a to be packed in the knapsack, but carefully limiting the number of such items.

Algorithm 2
figure b

Algorithm Two-Thresholds.

Theorem 3

The competitive ratio of Two-Thresholds, as defined in Algorithm 2, is \(\frac{5}{9}\).

Proof

For the lower bound, consider any input sequence, \(\sigma \). We first consider the case where Two-Thresholds rejects an item, x, even though its size is below the current threshold. Since the highest threshold is \(\frac{9a}{4}\), \({\text {size}}(x) \le \frac{9a}{4}\) and the knapsack is filled to more than \(1-\frac{9a}{4}\). Since Two-Thresholds accepts at most \(\big \lceil \frac{2}{9a}\big \rceil \) items larger than \(\frac{3a}{2}\),

We now consider the case, where Two-Thresholds never rejects an item of size no more than the current threshold. We let T be the final threshold of Two-Thresholds. Thus, \(T=\frac{9a}{4}\), if the algorithm never lowers the threshold, and otherwise, \(T = \frac{3a}{2}\). We let \(\ell \) and s denote the number of items in \({\textsc {Opt}}\) ’s knapsack of size more than T and at most T, respectively.

If \(T=\frac{9a}{4}\), then \(\ell < \frac{4s}{5}\), because the average size of the items in \({\textsc {Opt}}\) ’s knapsack is a. Thus, \({\textsc {Opt}} (\sigma ) = \ell + s < \frac{9s}{5}\). Since Two-Thresholds never rejects an item no larger than \(\frac{9a}{4}\), \(\textsc {Two-Thresholds} (\sigma ) \ge s \ge \frac{5}{9} {\textsc {Opt}} (\sigma )\). Otherwise, \(T=\frac{3a}{2}\), which means that \(\ell < 2s\), and \({\textsc {Opt}} (\sigma ) = \ell +s < 3s\). Thus, we arrive at \(\textsc {Two-Thresholds} (\sigma ) \ge \frac{2}{9a} + s > \frac{2}{9} {\textsc {Opt}} (\sigma ) + \frac{1}{3} {\textsc {Opt}} (\sigma ) = \frac{5}{9} {\textsc {Opt}} (\sigma )\).

For the matching upper bound, the adversary uses a sequence, \(\sigma \), consisting of \(\left\lfloor \frac{2}{9a}\right\rfloor \) items of size \(\frac{9a}{4}\) followed by \(\left\lfloor \frac{1}{3a}\right\rfloor \) items of size \(\frac{3a}{2}\), ending with \(\left\lfloor \frac{1}{a}\right\rfloor \) items of size a. The first \(\left\lfloor \frac{2}{9a}\right\rfloor +\left\lfloor \frac{1}{3a}\right\rfloor \) items have a total size of more than \(1-\frac{15a}{4} > 1-4a\). Hence, \(\textsc {Two-Thresholds} (\sigma ) \le \frac{5}{9a} + 3 = \frac{5}{9} {\textsc {Opt}} (\sigma )+3\). \(\square \)

As it turns out in Sect. 3.1, working with an unbounded number of different threshold values, an algorithm of optimal competitive ratio \(\frac{e-1}{e} \approx 0.63\) can be obtained.

3.1 The Adaptive Threshold Algorithm

In Algorithm 3, we introduce an algorithm template, which is used to establish algorithms with predictions in the two following sections. The template omits the definition of a threshold function, \(T(i)\), since it is different for the two algorithms. \(T(i)\) is a threshold function in the sense that all items larger than this threshold are rejected. It depends on the predictions given, though not in the same manner. The threshold decreases over time: In both algorithms, the threshold functions have the property that \(T(i) > T(i+1)\) for \(i\ge 1\). The role of the threshold function is to ensure that, for any \(i \ge 1\), items larger than T(i) are accepted, only if fewer than \(i-1\) items larger than T(i) have already been accepted. Note that the threshold could be lowered step by step from T(i) to \(T(i+1)\), \(i \ge 1\), by accepting an item of size between T(1) and T(2), then an item between T(2) and T(3), and so on. However, it could also, for instance, be lowered directly from T(1) to T(10). This could be because of having six items of sizes between T(8) and T(9) and three items between T(9) and T(10) in the knapsack. The intuition is that the more relatively large items we already have in the knapsack, the more selective we can be, since there is a limit to how many items above a certain size \({\textsc {Opt}}\) can accept while getting an average size of a. An example showing how the algorithm works is presented below as Example 1.

We use the notation \(n_{\!x} \) to denote the number of accepted items strictly larger than x.

Algorithm 3
figure c

Algorithm Adaptive Threshold, \({\textsc {AT}}\).

Note that using \(\max _{j\ge 0}\left\{ n_{\!T(j+1)} \ge j\right\} \) instead of \(\max _{j\ge 0}\left\{ n_{\!T(j+1)} =j\right\} \) in Line 4 would result in the same algorithm, since we stop accepting items above a given size when the quota set by \(T(i)\) has been reached. Thus, i is nondecreasing through the processing of the input sequence, and the value of the threshold function, \(T(i)\), is decreasing in i, so larger items cannot be accepted after i increases.

For the \(\frac{e-1}{e}\)-consistent algorithm, the threshold function is \(T(i)=\hat{a} e /(\hat{a} e(i-1)+1)\) (see Algorithm 4), and for the \(\frac{1}{2}\)-consistent algorithm, the threshold function is \(T(i)=\sqrt{\hat{a}/(2i)}\) (see Algorithm 7).

Example 1

Suppose a request sequence is

$$\begin{aligned} I = \left\langle \frac{3}{16},\frac{1}{24},\frac{3}{8},\frac{1}{3},\frac{1}{4},\frac{1}{3},\frac{1}{12},\frac{1}{6},\frac{1}{16},\frac{1}{9},\frac{2}{9} \right\rangle .\end{aligned}$$

Note that an optimal solution contains the seven smallest items, filling the knapsack to level \(\frac{3}{16} + \frac{1}{24} + \frac{1}{12} + \frac{1}{6} + \frac{1}{16} + \frac{1}{9} + \frac{2}{9} = \frac{7}{8}\). The average size in \({\textsc {Opt}} _s\) is \(\frac{1}{8}\). Suppose the prediction is correct, so \(\hat{a} =0.125\). Using the thresholds from Algorithm 4, we get that

$$\begin{aligned}&T(1)=\frac{e}{8}\approx 0.340,\\&T(2)=\frac{e}{e+8}\approx 0.254,\\&T(3)=\frac{e}{2e+8}\approx 0.202,\\&T(4)=\frac{e}{3e+8}\approx 0.168,\\&T(5)=\frac{e}{4e+8}\approx 0.144, \end{aligned}$$

and the smaller threshold values are not relevant. Figure 1 shows the sizes of the items in I compared to the threshold values.

The algorithm processes I as follows:

  • The first item is accepted, since \(\frac{3}{16} \le T(1)\). Since \(\frac{3}{16} \le T(2)\), the threshold remains T(1).

  • The second item is also accepted, since \(\frac{1}{24} \le T(1)\) and \(\frac{3}{16}+\frac{1}{24} \le 1\).

  • The third item is rejected, since \(\frac{3}{8} > T(1)\).

  • The fourth item is accepted, since \(\frac{1}{3} \le T(1)\) and \(\frac{3}{16}+\frac{1}{24}+\frac{1}{3} \le 1\). Since \(\frac{1}{3} > T(2)\), the threshold is lowered to T(2).

  • The fifth item is accepted, since \(\frac{1}{4} \le T(2)\) and \(\frac{3}{16}+\frac{1}{24}+\frac{1}{3}+\frac{1}{4} \le 1\). The threshold is lowered to T(4), since each of the three items of sizes \(\frac{3}{16}\), \(\frac{1}{3}\), and \(\frac{1}{4}\) is larger than T(4).

  • The sixth item is rejected, since \(\frac{1}{3} > T(4)\).

  • The seventh item is accepted, since \(\frac{1}{12} < T(4)\) and \(\frac{3}{16}+\frac{1}{24}+\frac{1}{3}+\frac{1}{4}+\frac{1}{12} = \frac{43}{48} \le 1\).

  • The eighth item is rejected, since the remaining empty space in the knapsack is only \(\frac{5}{48}<\frac{1}{6}\).

  • The ninth item is accepted, since \(\frac{1}{16}<T(4)\) and \(\frac{3}{16}+\frac{1}{24}+\frac{1}{3}+\frac{1}{4}+\frac{1}{12}+\frac{1}{16} = \frac{23}{24} \le 1\).

  • The two remaining items are rejected, since they do not fit in the knapsack.

Algorithm 4 accepts six items.

Fig. 1
figure 1

Illustrating Example 1. Items are shown in the order they appear in I. Black items are accepted by Algorithm 4 and shaded items are rejected

4 Accurate Predictions

In this section, we give an \(\frac{e-1}{e}\)-competitive algorithm which receives a, the exact average size of the items in \({\textsc {Opt}} _s\), as a prediction and prove that it is best possible among algorithms that gets the exact value of a and no other information.

4.1 Positive Result–Lower Bound

To define an algorithm for accurate predictions, we define a threshold function; see Algorithm 4. Throughout this section, we assume that \(\hat{a} =a\), but the algorithm is also used for possibly inaccurate predictions in Sect. 5.1.

Algorithm 4
figure d

Consistent Adaptive Threshold, \({\textsc {CAT}}\).

First, we prove that \({\textsc {CAT}}\) with \(\hat{a} = a\) has competitive ratio at least \(\frac{e-1}{e}\approx 0.6321\). For that, we need two simple lemmas, the first of which involves an obvious generalization of Harmonic numbers to non-integers:

Definition 1

For a function f and real-valued x and y such that \(y-x\in \mathbb {N}\cup \left\{ 0\right\} \), define \(\sum _{i=x}^{y}f(i) =f(x)+f(x+1)+\cdots + f(y)\).

For any real-valued \(k \ge 1\), define the generalized Harmonic number, \(H_k\), by \(H_k=\sum _{i=1+k-\left\lfloor k\right\rfloor }^{k}\frac{1}{i}\).

The proofs of the following two lemmas consist of simple calculations and can be found in Appendix  A.

Lemma 1

If \(k \ge p \ge 1\) and \(k-p\in \mathbb {N}\cup \left\{ 0\right\} \), then

$$\begin{aligned}\ln k - \ln (p+1) \le H_k - H_p \le \ln k - \ln p.\end{aligned}$$

Lemma 2

For any \(a > 0\), \(e^{1-ae} \ge e - e^2a\).

With those two lemmas, we can now prove the following theorem.

Theorem 4

For \(\hat{a} =a\), \({\textsc {CAT}}\), as defined in Algorithm 4, is \(\frac{e-1}{e}\)-competitive.

Proof

Consider a request sequence \(\sigma \). Considering the conditional statement in the algorithm, if \({\textsc {CAT}}\) rejects an item, x, then either \({\text {size}}(x) > T(i+1)\) or \({\text {level}} + {\text {size}}(x) > 1\).

Case 1: This is the case where, at some point, \({\textsc {CAT}}\) rejects an item, x, with a size no larger than the current threshold, because \({\text {level}} + {\text {size}}(x) > 1\). The value of \(T(k)\) from Algorithm 4 is an upper bound on the size of the kth largest item accepted by the algorithm. Thus, the kth largest accepted item has size at most

$$\begin{aligned} T(k) = \frac{ae}{ae(k-1)+1} = \frac{1}{k - 1 + \frac{1}{ae}}. \end{aligned}$$

Using the generalization of the Harmonic function over non-integer values given in Definition 1, we obtain an upper bound on the total size of items accepted by \({\textsc {CAT}}\) of

$$\begin{aligned} {\text {level}} \le \sum _{k=1}^{{\textsc {CAT}} (\sigma )}\frac{1}{k-1+\frac{1}{ae}} = \sum _{k=\frac{1}{ae}}^{{\textsc {CAT}} (\sigma )+\frac{1}{ae}-1}\frac{1}{k} = H_{{\textsc {CAT}} (\sigma )+\frac{1}{ae}-1} - H_{\frac{1}{ae} - 1}. \end{aligned}$$

By Lemma 1,

$$\begin{aligned} \begin{array}{rcl} H_{{\textsc {CAT}} (\sigma )+\frac{1}{ae}-1} - H_{\frac{1}{ae} - 1} &{} < &{} \ln \left( {\textsc {CAT}} (\sigma )+\frac{1}{ae}-1\right) - \ln \left( \frac{1}{ae} - 1\right) \\ &{} = &{} \ln \left( \frac{{\textsc {CAT}} (\sigma )+\frac{1}{ae}-1}{\frac{1}{ae}-1}\right) . \end{array} \end{aligned}$$

By assumption, \({\text {level}} + {\text {size}}(x) > 1\), and since \({\text {level}} \le \ln \left( \frac{{\textsc {CAT}} (\sigma )+\frac{1}{ae}-1}{\frac{1}{ae} - 1}\right) \), we have

$$\begin{aligned}&\ln \left( \frac{{\textsc {CAT}} (\sigma )+\frac{1}{ae}-1}{\frac{1}{ae} - 1}\right)> 1 - {\text {size}}(x) \\ \Leftrightarrow {}&\frac{{\textsc {CAT}} (\sigma )+\frac{1}{ae}-1}{\frac{1}{ae} - 1}> e^{1 - {\text {size}}(x)} \\ \Leftrightarrow {}&{\textsc {CAT}} (\sigma )> \left( \frac{1}{ae} - 1 \right) e^{1 - {\text {size}}(x)}-\frac{1}{ae}+1\\ \Leftrightarrow {}&{\textsc {CAT}} (\sigma ) > \frac{e^{1 - {\text {size}}(x)}-1}{ae} + 1 - e^{1 - {\text {size}}(x)}\,. \end{aligned}$$

In the algorithm, i is at least zero, so it does not consider accepting items larger than \(T(1)=ae\). Thus, \({\text {size}}(x) \le ae\).

$$\begin{aligned} \begin{aligned} {\textsc {CAT}} (\sigma )&> \frac{e^{1 - {\text{ size }}(x)}-1}{ae} + 1 - e,{}&{} \text{ since } - e^{1 - {\text{ size }}(x)}> -e \\ {}&> \frac{e^{1 - ae}-1}{ae} + 1 - e,{}&{} \text{ by } \text{ the } \text{ observation } \text{ above } \\ {}&\ge \frac{e-e^2a-1}{ae} + 1 - e,{}&{} \text{ by } \text{ Lemma } ~2 \\ {}&= \frac{e- 1}{ae} - 2e + 1 \\ {}&\ge \frac{e-1}{e}{\textsc {Opt}} (\sigma ) - 2e + 1,{}&{} \text{ since } {\textsc {Opt}} (\sigma ) \le \frac{1}{a}\,. \end{aligned} \end{aligned}$$

So, \(\lim _{{\textsc {Opt}} \rightarrow \infty }\frac{{\textsc {CAT}} (\sigma )}{{\textsc {Opt}} (\sigma )}\ge \frac{e-1}{e}\).

Case 2: This is the case where \({\textsc {CAT}}\) never rejects any item, x, when \({\text {size}}(x) \le T(i+1)\). Let \(i_t\) denote the final value of i as the algorithm terminates. Suppose \({\textsc {Opt}}\) accepts \(\ell \) items larger than \(T(i_t+1)\) and s items of size at most \(T(i_t+1)\). Since \({\textsc {Opt}}\) accepts \(\ell \) items larger than \(T(i_t+1)\) and \(\ell +s\) items in total, we have \(a > \ell \cdot T(i_t+1) / (\ell +s)\), which is equivalent to

$$\begin{aligned} \begin{aligned}{} & {} s&> \left( \frac{T(i_t+1)}{a}-1\right) \ell \,. \end{aligned} \end{aligned}$$
(1)

By the definition of \(T\), we have that \(T(i_t+1)=\frac{ae}{aei_t+1}\). Solving for the \(i_t\) on the right-hand side, we get

$$\begin{aligned} i_t=\frac{1}{T(i_t+1)}-\frac{1}{ae} . \end{aligned}$$
(2)

Thus, \({\textsc {CAT}}\) has accepted at least \(i_t=\frac{1}{T(i_t+1)}-\frac{1}{ae}\) items of size greater than \(T(i_t+1)\). Further, due to the assumption in this second case, \({\textsc {CAT}}\) has accepted all of the s items no larger than \(T(i_t+1)\). To see this, note that the values of the variable i of the algorithm can only increase, so at no point has there been a size demand more restrictive than \(T(i_t+1)\).

We split in two subcases, depending on how \(T(i_t+1)\) relates to the average size, a, in \({\textsc {Opt}} _s\).

Subcase 2a: \(T(i_t+1) > a\). In this subcase, the lower bound on s of Ineq. (1) is positive.

$$\begin{aligned} \frac{{\textsc {CAT}} (\sigma )}{{\textsc {Opt}} (\sigma )}&\ge \frac{\left( \frac{1}{T(i_t+1)}-\frac{1}{ae}\right) + s}{\ell +s}\,,{} & {} \text { by Eq.}~(2) \\&> \frac{\left( \frac{1}{T(i_t+1)}-\frac{1}{ae}\right) + \left( \frac{T(i_t+1)}{a}-1\right) \ell }{\ell +\left( \frac{T(i_t+1)}{a}-1\right) \ell }\,,{} & {} \text { by Ineq.}~(1)\\&= \frac{\left( \frac{1}{T(i_t+1)}-\frac{1}{ae}\right) + \left( \frac{T(i_t+1)}{a}-1\right) \ell }{\frac{T(i_t+1)}{a}\ell }\,. \end{aligned}$$

The second inequality follows since the ratio is smaller than one and s is replaced by a smaller, positive term in the numerator as well as the denominator.

We prove that this is bounded from below by \(\frac{e-1}{e}\):

$$\begin{aligned}&\frac{\left( \frac{1}{T(i_t+1)}-\frac{1}{ae}\right) + \left( \frac{T(i_t+1)}{a}-1\right) \ell }{\frac{T(i_t+1)}{a}\ell } \ge \frac{e-1}{e} \\ \Leftrightarrow {}&\frac{e}{T(i_t+1)} - \frac{1}{a} + e\left( \frac{T(i_t+1)}{a}-1\right) \ell \ge e\frac{T(i_t+1)}{a}\ell - \frac{T(i_t+1)}{a}\ell \\ \Leftrightarrow {}&\frac{e}{T(i_t+1)} - \frac{1}{a} \ge \left( e - \frac{T(i_t+1)}{a}\right) \ell \\ \Leftrightarrow {}&\frac{ea-T(i_t+1)}{aT(i_t+1)} \ge \frac{ea-T(i_t+1)}{a}\ell \\ \Leftrightarrow {}&\frac{1}{T(i_t+1)} \ge \ell \,. \end{aligned}$$

For the last biimplication, we must argue that \(ea-T(i_t+1) \ge 0\), but this holds since \(T(1)=ea\) and \(T\) is decreasing. Finally, the last statement, \(\frac{1}{T(i_t+1)} \ge \ell \) holds regardless of the relationship between \(T(i_t+1)\) and a, since the knapsack obviously cannot hold more than \(\frac{1}{T(i_t+1)}\) items of size greater than \(T(i_t+1)\).

Subcase 2b: \(T(i_t+1) \le a\).

$$\begin{aligned} \begin{aligned} \frac{{\textsc {CAT}} (\sigma )}{{\textsc {Opt}} (\sigma )}&\ge \frac{\left( \frac{1}{T(i_t+1)}-\frac{1}{ae}\right) + s}{\ell +s}\,,{} & {} \text { by Eq.}~(2) \\&\ge \frac{\left( \frac{1}{T(i_t+1)}-\frac{1}{ae}\right) }{\ell }\,,{} & {} \text { since } s \ge 0 \text { and } \frac{{\textsc {CAT}} (\sigma )}{{\textsc {Opt}} (\sigma )} \le 1 \\&> \frac{\left( \frac{1}{T(i_t+1)}-\frac{1}{ae}\right) }{\frac{1}{T(i_t+1)}}\,,{} & {} \text { since, as above} , \ell \le \frac{1}{T(i_t+1)}\\&= 1 - \frac{T(i_t+1)}{ae} \\&\ge 1 - \frac{a}{ae}\,,{} & {} \text { by the subcase we are in} \\&= \frac{e-1}{e}\,. \end{aligned} \end{aligned}$$

This concludes the second case, and, thus, the proof. \(\square \)

4.2 General Negative Result–Upper Bound

Now, we show that \({\textsc {CAT}}\) is best possible among online algorithms knowing a and nothing else, using Algorithm 5 for the adversary.

Algorithm 5
figure e

Adversarial sequence establishing the competitive ratio of a best possible online algorithm knowing a.

Before continuing with the analysis, consider the following example execution of Algorithm 5. The algorithm is used in the proof of Theorem 5.

Example 2

If \(a=\frac{1}{6}\), then \(\varepsilon =\frac{1}{12960}\) and \(k=2\) initially. Suppose that while \(k=2\), \({\textsc {Alg}}\) accepts only the second item. Then, the adversarial sequence starts with \(I_2 = \{ \frac{1}{2}-\varepsilon , \frac{1}{2}-\varepsilon \}\). We consider two of the possible cases after this:

  1. 1.

    If \({\textsc {Alg}}\) accepts none of the three items when \(k=3\), then the sequence ends with \(I_3=\{ \frac{1}{3}-\varepsilon , \frac{1}{3}-\varepsilon , \frac{1}{3}-\varepsilon ,\varepsilon ,\varepsilon ,\varepsilon \}\). \({\textsc {Opt}}\) accepts all six items in \(I_3\) and none in \(I_2\), while \({\textsc {Alg}}\) accepts one item from \(I_2\) and at most the last three from \(I_3\).

  2. 2.

    If \({\textsc {Alg}}\) accepts the first item when \(k=3\), then the sequence ends with \(I_3=\{ \frac{1}{3}-\varepsilon ,\frac{1}{6}, \frac{1}{6},\frac{1}{6},\frac{1}{6},\frac{1}{6},\frac{1}{6} \}\). \({\textsc {Opt}}\) accepts the last six items, while \({\textsc {Alg}}\) accepts one item from \(I_2\), plus the item of size \(\frac{1}{3}-\varepsilon \) and at most one item of size \(\frac{1}{6}\) from \(I_3\).

Theorem 5

Any deterministic algorithm getting only the correct value of a as a prediction has a competitive ratio of at most \(\frac{e-1}{e}\).

Proof

Let \({\textsc {Alg}}\) denote the online algorithm knowing a, and let \(\sigma \) be the adversarial sequence defined by Algorithm 5, which explains how the adversary defines its sequence based on \({\textsc {Alg}}\) ’s actions.

The adversary strategy is to make \({\textsc {Alg}}\) accept one item of each size \(\frac{1}{k(i)}\), \(i=0,1,\ldots \), where \(k(i)=\left\lfloor \frac{1}{ae}\right\rfloor +i\), until an item of the next size, size \(\frac{1}{k(i+1)}\), would not fit in \({\textsc {Alg}}\) ’s knapsack (Case 2), or k(i) items of size \(\frac{1}{k(i)}\) have been given without \({\textsc {Alg}}\) accepting any of them (Case 1). In Case 1, the sequence finishes with \(\frac{1}{a}-k(i)\) very small items, giving an average size of a when combined with the k(i) items of size \(\frac{1}{k(i)}\) rejected by \({\textsc {Alg}}\). These items are the items accepted by \({\textsc {Opt}}\). In Case 2, the sequence ends with \(\frac{1}{a}\) items of size a accepted by \({\textsc {Opt}}\). Since \({\textsc {Alg}}\) ’s knapsack is already almost full, \({\textsc {Alg}}\) can fit at most two of these items in its knapsack.

Let \(k_t \) be the value of k at the beginning of the last iteration of the while-loop. We perform a case analysis based on how the generation of the adversarial sequence terminates.

Case 1:

\({\textsc {Opt}}\) accepts the \(k_t \) items of size \(\frac{1}{k_t}-\varepsilon \) in the last iteration of the while-loop and the \(\frac{1}{a}-k_t \) items of size \(\frac{k_t a\varepsilon }{1-k_t a}\) for a total of \(\frac{1}{a}\) items of total size

$$\begin{aligned}k_t \left( \frac{1}{k_t}-\varepsilon \right) +\left( \frac{1}{a}-k_t \right) \frac{k_t a\varepsilon }{1-k_t a} = 1-k_t \varepsilon +(1-k_t a)\frac{k_t \varepsilon }{1-k_t a} = 1.\end{aligned}$$

Note that the average size of the items accepted by \({\textsc {Opt}}\) is a, consistent with the prediction.

\({\textsc {Alg}}\) accepts one item in each iteration of the while-loop, except the last iteration, and at most \(\frac{1}{a}-k_t \) items after that, so no more than

$$\begin{aligned}k_t-\left\lfloor \frac{1}{ae}\right\rfloor + \frac{1}{a}-k_t < \frac{1}{a}-\frac{1}{ae}+1 = \frac{e-1}{e}\cdot \frac{1}{a}+1.\end{aligned}$$

Thus,

$$\begin{aligned} {\textsc {Alg}} (\sigma ) \le \frac{e-1}{e}\cdot \frac{1}{a}+1 = \frac{e-1}{e} {\textsc {Opt}} (\sigma )+1.\end{aligned}$$

Case 2:

\({\textsc {Opt}}\) accepts the \(\frac{1}{a}\) items of size a. For the analysis of \({\textsc {Alg}}\), we start by establishing an upper bound on \(k_t \). The following inequality holds since \({\textsc {Alg}}\) accepts one item per round, and \({\textsc {Alg}}\) ’s level just before the last round is at most \(1-\frac{1}{k_t}-k_t \varepsilon \) before the last item of size \(\frac{1}{k_t}-\varepsilon \) is accepted.

$$\begin{aligned} \sum _{k=\left\lfloor \frac{1}{ae}\right\rfloor }^{k_t}\left( \frac{1}{k}-\varepsilon \right) \le 1-(k_t +1)\varepsilon \Rightarrow {}&H_{k_t} - H_{\left\lfloor \frac{1}{ae}\right\rfloor -1} - k_t \varepsilon< 1 - k_t \varepsilon \nonumber \\ \Rightarrow {}&\ln (k_t) - \ln \left( \left\lfloor \frac{1}{ae}\right\rfloor \right)< 1, \text{ by } \text{ Lemma }~ 1\nonumber \\ \Leftrightarrow {}&k_t < e\left\lfloor \frac{1}{ae}\right\rfloor \,. \end{aligned}$$
(3)

In the case we are treating, \({\textsc {Alg}}\) leaves the while-loop because its level is more than \(1-\frac{1}{k_t +1}-(k_t +1)\varepsilon \). Now, we give a bound on the amount of space available at that point. For the first inequality, note that by the initialization of k in the algorithm, \(k_t \ge \left\lfloor \frac{1}{ae}\right\rfloor \).

$$\begin{aligned} \frac{1}{k_t +1}+(k_t +1)\varepsilon&< \frac{1}{\left\lfloor \frac{1}{ae}\right\rfloor +1} + \left( e\left\lfloor \frac{1}{ae}\right\rfloor +1\right) \varepsilon< ae + \left( \frac{1}{a} + 1\right) \frac{a^2}{10} \\&< \left( e + \left( \frac{1+a}{10}\right) \right) a < 3a\,. \end{aligned}$$

Thus, after the while-loop, \({\textsc {Alg}}\) can accept at most two of the items of size a. Clearly, the number of rounds in the while-loop is \(k_t-\left\lfloor \frac{1}{ae}\right\rfloor +1\). Using \(k_t < e\left\lfloor \frac{1}{ae}\right\rfloor \) from Eq. (3), we can now bound \({\textsc {Alg}}\) ’s profit:

$$\begin{aligned}{\textsc {Alg}} (\sigma )\le & {} k_t-\left\lfloor \frac{1}{ae}\right\rfloor +1+2<(e-1)\left\lfloor \frac{1}{ae}\right\rfloor +3 \\ {}\le & {} \frac{e-1}{e}\frac{1}{a}+3 =\frac{e-1}{e}{\textsc {Opt}} (\sigma )+3.\end{aligned}$$

This establishes the bound on the competitive ratio of \(\frac{e-1}{e}\).

Finally, to ensure that our proof is valid, we must argue that the number of rounds we count in the algorithm and the sizes of items we give are non-negative. For the remainder of this proof, we go through the terms in the algorithm, thereby establishing this.

The largest value of k in the algorithm is \(k_t \), and by Eq. (3) \(k_t<e\left\lfloor \frac{1}{ae}\right\rfloor <\frac{1}{a}\). Additionally, from the start value of k, we know that \(\left\lfloor \frac{1}{ae}\right\rfloor \le k\). Using these facts, together with the assumption from the algorithm that \(a<\frac{1}{2e}\), we get the following bounds on the various terms.

$$\begin{aligned} 1-\frac{1}{k}-k\varepsilon> 1-\frac{1}{\left\lfloor \frac{1}{ae}\right\rfloor }-\frac{1}{a}\frac{a^2}{10}> 1-\frac{1}{\left\lfloor \frac{1}{1/2}\right\rfloor }-\frac{1}{20e} > 0. \end{aligned}$$

Further, \(\frac{1}{k}-\varepsilon \ge \frac{1}{k_t}-\varepsilon>\frac{1}{\frac{1}{a}}-\frac{a^2}{10}>0\) and \(\frac{1}{a}-k\ge \frac{1}{a}-k_t >\frac{1}{a}-\frac{1}{a}=0\).

For the last relevant value, \(1-ka\ge 1-k_t a>1-\frac{1}{a}a=0\) and from Case 1, we know that the \(\frac{1}{a}-k_t \) items given in Line 9 of the algorithm sum up to at most one. \(\square \)

5 Predictions

For the case where the predictions may be inaccurate, the algorithm \({\textsc {CAT}}\) can be used with \(\hat{a}\) possibly not being a, obtaining a positive competitive ratio as long as \(r < e\), see Subsect. 5.1. In Subsect. 5.2, we give an adaptive threshold algorithm, \({\textsc {RAT}}\), that has a positive competitive ratio for all positive r.

For \(r < \frac{1}{2}(e+\sqrt{e^2-2e}) \approx 2.06\), \({\textsc {CAT}}\) has a better competitive ratio than \({\textsc {RAT}}\). Thus, if an upper bound on r of approximately 2 (or lower) is known, \({\textsc {CAT}}\) may be preferred, and if a guarantee for any r is needed, \({\textsc {RAT}}\) should be used.

5.1 Predictions with \(r<e\)

In this section, we consider the algorithm \({\textsc {CAT}}\) with a prediction, \(\hat{a}\), guaranteed to be less than e times as large as the correct value, a.

5.1.1 Positive Result–Lower Bound

In this section, we consider the algorithm \({\textsc {CAT}}\) with \(\hat{a}\) instead of a. Note that the lower bound of the theorem below is positive only when \(r < e\). For \(r \ge e\), the algorithm may not accept any items, and, hence, its competitive ratio is 0.

Theorem 6

\({\textsc {CAT}}\) has a competitive ratio of at least

$$\begin{aligned} c_{{\textsc {CAT}}}(r) \ge {\left\{ \begin{array}{ll} \displaystyle \frac{e-1}{e} \cdot r, &{} \text {if }r \le 1\\ \displaystyle \frac{e-r}{e}, &{} \text {if } r \ge 1\,. \end{array}\right. } \end{aligned}$$

Proof

The proof is analogous to the proof of Theorem 4.

In Case 1, replacing a by \(\hat{a}\), since the algorithm bases its actions on \(\hat{a}\) instead of a, and setting \({\textsc {Opt}} (\sigma ) = \frac{1}{r\hat{a}}\), results in a ratio of

$$\begin{aligned} \frac{{\textsc {CAT}} (\sigma )}{{\textsc {Opt}} (\sigma )} \ge \frac{e-1}{e} \cdot r \end{aligned}$$

instead of \(\frac{e-1}{e}\).

In Case 2, the lower bound on s given in Ineq. (1) depends on the actual average size, a, whereas the value of \(i_t\) given in Eq. (2) depends on \(\hat{a}\), since the algorithm uses \(\hat{a}\). The subcase distinction is still based on a.

In Subcase 2a, we obtain

$$\begin{aligned} \frac{{\textsc {CAT}} (\sigma )}{{\textsc {Opt}} (\sigma )}&\ge \frac{\left( \frac{1}{T(i_t+1)}-\frac{1}{\hat{a} e}\right) + s}{\ell +s} \ge \frac{\left( \frac{1}{T(i_t+1)}-\frac{1}{\hat{a} e}\right) + \left( \frac{T(i_t+1)}{a}-1\right) \ell }{\frac{T(i_t+1)}{a}\ell }\,. \end{aligned}$$

Going through the same calculations as in the proof of Theorem 4, we get that

$$\begin{aligned}&\frac{\left( \frac{1}{T(i_t+1)}-\frac{1}{\hat{a} e}\right) + \left( \frac{T(i_t+1)}{a}-1\right) \ell }{\frac{T(i_t+1)}{a}\ell } \ge \frac{e-r}{e} \\ \Leftrightarrow {}&\frac{e}{T(i_t+1)} - \frac{1}{\hat{a}} \ge \left( e - r \cdot \frac{T(i_t+1)}{r \hat{a}}\right) \ell \\ \Leftrightarrow {}&\frac{1}{T(i_t+1)} \ge \ell \,. \end{aligned}$$

In subcase 2b, we obtain

$$\begin{aligned} \frac{{\textsc {CAT}} (\sigma )}{{\textsc {Opt}} (\sigma )}&\ge \frac{\left( \frac{1}{T(i_t+1)}-\frac{1}{\hat{a} e}\right) + s}{\ell +s} > 1 - \frac{T(i_t+1)}{\hat{a} e} \ge 1 - \frac{a}{\hat{a} e} = 1 - \frac{r \hat{a}}{\hat{a} e} = \frac{e-r}{e}\,. \end{aligned}$$

Thus, we obtain a lower bound of \(\frac{e-1}{e} \cdot r\) in Case 1 and a lower bound of \(\frac{e-r}{e}\) in Case 2. For \(r \le 1\), \(\frac{e-1}{e} \cdot r \le \frac{e-r}{e}\), and for \(r \ge 1\), \(\frac{e-1}{e} \cdot r \ge \frac{e-r}{e}\). \(\square \)

5.1.2 General Negative Result–Upper Bound

The following result shows that, for \(r<e\), no algorithm can be better than \({\textsc {CAT}}\) for both \(r < 1\) and \(r > 1\).

Theorem 7

If a deterministic algorithm is \(\frac{e-r}{e}\)-competitive for all \(1 \le r<e\), it cannot be better than \(r \cdot \frac{e-1}{e}\)-competitive for any \(r \le 1\).

Proof

Consider an algorithm, \({\textsc {Alg}}\), and assume that \({\textsc {Alg}}\) is \(\frac{e-r}{e}\)-competitive for all \(1 \le r<e\). Then there exists a constant, b, such that \({\textsc {Alg}} (\sigma ) \ge \frac{e-r}{e} {\textsc {Opt}} (\sigma )-b\), for any sequence \(\sigma \) and any \(1 \le r < e\). This constant b is given as a parameter to Algorithm 6, constructing an adversarial sequence, \(\sigma \).

Algorithm 6
figure f

Adversarial sequence for \(r<e\); the adversarial algorithm takes two parameters, \(r_2<1\) and \(b \ge 0\).

Let \(k_t\) be the value of k at the end of the last iteration of the while-loop. If the adversarial algorithm terminates in Line 9, then \({\textsc {Alg}}\) has accepted at most \(k_t-\left\lfloor \frac{1}{\hat{a} e}\right\rfloor -b-1\) items. For termination in Line 9, \({\textsc {Alg}}\) has not accepted any of the \(k_t \) items in the for-loop immediately preceding this, so \(k_t \) items of size \(\frac{1}{k_t}\) were given. In this case, \({\textsc {Opt}}\) accepts exactly these \(k_t\) items from the last iteration of the while-loop, and \(a=\frac{1}{k_t}\). Let \(r_1 = a/\hat{a} \). Since \(\frac{1}{k_t} \ge \hat{a} \), \(r_1 \ge 1\). Then,

$$\begin{aligned} {\textsc {Alg}} (\sigma )&\le k_t-\left\lfloor \frac{1}{\hat{a} e}\right\rfloor -b-1 < {\textsc {Opt}} (\sigma ) - \frac{1}{\hat{a} e} - b = {\textsc {Opt}} (\sigma ) - \frac{r_1}{ae} - b \\&= {\textsc {Opt}} (\sigma ) - \frac{r_1}{e}{\textsc {Opt}} (\sigma ) - b = \frac{e-r_1}{e} {\textsc {Opt}} (\sigma ) - b, \end{aligned}$$

contradicting that \({\textsc {Alg}} (\sigma ) \ge \frac{e-r}{e} {\textsc {Opt}} (\sigma )-b\). Thus, the adversarial algorithm cannot terminate in Line 9.

Since the adversarial algorithm does not terminate in Line 9, it must accept its first item no later than in the \((b+2)\)nd iteration of the while-loop, and the ith item accepted by \({\textsc {Alg}}\) has size at least \(\frac{1}{\left\lfloor \frac{1}{\hat{a} e}\right\rfloor +b+i} \ge \frac{1}{\frac{1}{\hat{a} e}+b+i}\). Thus, the total size, \(S_t\), of the items accepted by \({\textsc {Alg}}\) in the while-loop is

$$\begin{aligned} S_t&\ge \sum _{k=\frac{1}{\hat{a} e}+b+1}^{k_t} \frac{1}{k}\\&= \sum _{k=1}^{k_t} \frac{1}{k}- \sum _{k=1}^{\frac{1}{\hat{a} e}-1} \frac{1}{k}- \sum _{k=\frac{1}{\hat{a} e}}^{\frac{1}{\hat{a} e}+b} \frac{1}{k} \\&> H_{k_t} - H_{\frac{1}{\hat{a} e}-1} - (b+1)\hat{a} e\\&\ge \ln (k_t) - \ln \left( \frac{1}{\hat{a} e} \right) - (b+1)\hat{a} e,{} & {} \text { by Lemma} ~1. \end{aligned}$$

Thus, we have

$$\begin{aligned} S_t > \ln (k_t) - \ln \left( \frac{1}{\hat{a} e} \right) - (b+1)\hat{a} e\,. \end{aligned}$$
(4)

By the first condition of the while-loop, and since \({\textsc {Alg}}\) accepts at most one item per iteration, \(S_t \le 1-(b+1)\hat{a} e\). By Ineq. (4), this means that \(\ln (k_t) - \ln \left( \frac{1}{\hat{a} e} \right) - (b+1)\hat{a} e < 1-\frac{1}{k_t}-(b+1)\hat{a} e\), and we get

$$\begin{aligned}&\ln (k_t) -\ln \left( \frac{1}{\hat{a} e} \right) - (b+1)\hat{a} e< 1-(b+1)\hat{a} e\nonumber \\ \Leftrightarrow {}&\ln \left( \frac{k_t}{\frac{1}{\hat{a} e}} \right)< 1\nonumber \\ \Leftrightarrow {}&\frac{k_t}{\frac{1}{\hat{a} e}}< e\nonumber \\ \Leftrightarrow {}&k_t < \frac{1}{\hat{a}} \,. \end{aligned}$$
(5)

Furthermore, by the conditions of the while-loop, we have that \(S_t > 1-\frac{1}{k_t +1} - (b+1)\hat{a} e\) or \(\frac{1}{k_t +1} < \hat{a} \). If \(S_t > 1-\frac{1}{k_t +1} - (b+1)\hat{a} e\), then, using that \(k_t\ge \left\lfloor \frac{1}{\hat{a} e}\right\rfloor \), \(S_t> 1-\frac{1}{ \left\lfloor \frac{1}{\hat{a} e}\right\rfloor +1} - (b+1)\hat{a} e > 1-\frac{1}{\frac{1}{\hat{a} e}} - (b+1)\hat{a} e = 1-\hat{a} e - (b+1)\hat{a} e = 1 - (b+2)\hat{a} e.\) Otherwise, we get

$$\begin{aligned}&\frac{1}{k_t +1} < \hat{a} \nonumber \\ \Leftrightarrow {}&k_t > \frac{1}{\hat{a}}-1 \,. \end{aligned}$$
(6)

Plugging this into Ineq. (4), we get

$$\begin{aligned} S_t&> \ln \left( \frac{1}{\hat{a}}-1\right) - \ln \left( \frac{1}{\hat{a} e} \right) - (b+1)\hat{a} e\\&= \ln \left( \frac{\frac{1}{\hat{a}}-1}{\frac{1}{\hat{a} e}} \right) - (b+1)\hat{a} e\\&= \ln \left( e-\hat{a} e \right) - (b+1)\hat{a} e\\&= 1 + \ln (1-\hat{a}) -(b+1)\hat{a} e \\&> 1-(b+2)\hat{a} e, \text { since } \hat{a} < \frac{1}{2} \,. \end{aligned}$$

Thus, in either case, we get \(S_t > 1-(b+2)\hat{a} e\). Therefore, the algorithm can fit at most \(\frac{(b+2)\hat{a} e}{r_2 \hat{a}} = \frac{(b+2)e}{r_2}\) of the items of size \(r_2 \hat{a} \) into its knapsack, where \(r_2\) is one of the parameters to the adversarial algorithm as defined in Algorithm 6. Since \({\textsc {Alg}}\) packs at most one item per iteration of the while-loop, this means that

$$\begin{aligned} {\textsc {Alg}} (\sigma )&\le k_t- \left\lfloor \frac{1}{\hat{a} e}\right\rfloor + 1 + \frac{(b+2)e}{r_2} \\&< k_t- \frac{1}{\hat{a} e} + 2 + \frac{(b+2)e}{r_2} \\&< \frac{1}{\hat{a}} - \frac{1}{\hat{a} e} + 2 + \frac{(b+2)e}{r_2}, \text { by Ineq}.~(5) \\&= \frac{e-1}{\hat{a} e} + 2 + \frac{(b+2)e}{r_2}\\&= r_2 \frac{e-1}{e}{\textsc {Opt}} (\sigma ) + 2 + \frac{(b+2)e}{r_2} \,. \end{aligned}$$

For any \(r < 1\), this yields an upper bound on the competitive ratio of \(r \frac{e-1}{e}\), since for any given r, \( 2 + \frac{(b+2)e}{r}\) is a constant. \(\square \)

We also note the contrapositive version of this theorem, i.e., that if a deterministic algorithm is better than \(r \cdot \frac{e-1}{e}\)-competitive for some \(r \le 1\), it cannot be \(\frac{e-r}{e}\)-competitive for all \(1 \le r<e\).

5.1.3 The Competitive Ratio of \({\textsc {CAT}}\)

Combining the positive result from Theorem 6 with the negative result from Theorem 7, we obtain that, if r is guaranteed to be smaller than e, no deterministic algorithm can be better than \({\textsc {CAT}}\) for both \(r < 1\) and \(r > 1\). Moreover, we get the following tight result on the performance of \({\textsc {CAT}}\).

Theorem 8

\({\textsc {CAT}}\) has a competitive ratio of

$$\begin{aligned} c_{{\textsc {CAT}}}(r) = {\left\{ \begin{array}{ll} \displaystyle r \cdot \frac{e-1}{e}, &{} \text {if } r \le 1\\ \displaystyle \frac{e-r}{e}, &{} \text {if } 1 \le r \le e \\ \displaystyle 0, &{} \text {if } r \ge e. \end{array}\right. } \end{aligned}$$

Proof

For different cases depending on r, we want to determine the exact value of \(c_{{\textsc {CAT}}}(r)\). We do this by proving upper and lower bounds on those values, case by case. The lower bounds have already been established in Theorem 6, except for the case \(r\ge e\). However, for any case, the competitive ratio is non-negative.

Since \({\textsc {CAT}}\) is \(\frac{e-r}{e}\)-competitive for \(1 \le r < e\), the upper bound for \(r < 1\) follows from Theorem 7.

For \(1 \le r < e\), consider an input sequence, \(\sigma \), consisting of \(\ell \) items of size \(\hat{a} e + \varepsilon _{\ell }\) followed by s items of size \(\varepsilon _s\), where

$$\begin{aligned} \ell = \frac{1}{\hat{a} e}-1, \; s = \frac{1}{\hat{a} r} - \frac{1}{\hat{a} e} +1, \; \varepsilon _s = \frac{\hat{a} e - \ell \varepsilon _{\ell }}{s}, \text { and } \varepsilon _{\ell } < \frac{\hat{a} e}{\ell }. \end{aligned}$$

The number of items in \(\sigma \) is \(\ell +s = \frac{1}{r \hat{a}}\), and their total size is

$$\begin{aligned} \ell (\hat{a} e + \varepsilon _{\ell }) + s \varepsilon _s = \ell \hat{a} e + \ell \varepsilon _{\ell } + \hat{a} e - \ell \varepsilon _{\ell } = (\ell +1)\hat{a} e = 1 . \end{aligned}$$

Thus, the average size of the items in \(\sigma \) is \(r \hat{a} = a\) and \({\textsc {Opt}} (\sigma ) = \frac{1}{r \hat{a}}\). \({\textsc {CAT}}\) accepts only the small items, so

$$\begin{aligned} {\textsc {CAT}} (\sigma ) = \frac{1}{\hat{a} r} - \frac{1}{\hat{a} e} +1 = \frac{e-r}{e} \frac{1}{r \hat{a}} +1 = \frac{e-r}{e} {\textsc {Opt}} (\sigma ) + 1 . \end{aligned}$$

For \(r > e\), consider the input sequence consisting of \(\frac{1}{r\hat{a}}\) items of size \(r\hat{a} \). \({\textsc {Opt}}\) accepts all items and \({\textsc {CAT}}\) accepts none. \(\square \)

5.2 Predictions with No Guarantee

In this section, we again consider predictions but now with no restriction on how large r can be.

5.2.1 Positive Result–Lower Bound

When considering the case where the average size of items in \({\textsc {Opt}} _s\) is estimated to be \(\hat{a} \), and the accurate value is \(a=r\cdot \hat{a} \), we consider two cases, \(r>1\) and \(r<1\). In either case, we have the problem that we do not even know which case we are in, so, when large items arrive, we have to accept some to be competitive. The algorithm we consider when the value of r is not necessarily one achieves similar competitive ratios in both cases. Algorithm 7, \({\textsc {RAT}}\), is Adaptive Threshold with a different threshold function than was used for more accurate predictions in \({\textsc {CAT}}\).

Algorithm 7
figure g

Robust Adaptive Threshold, \({\textsc {RAT}}\).

Since we need to accept larger items than in the case of accurate predictions, we need a threshold function that decreases faster than the threshold function used in Sect. 4, in order not to risk filling up the knapsack before the small items arrive. Therefore, it may seem surprising that we are using a threshold function that decreases as \(\frac{1}{\sqrt{i}}\), when the threshold function of Sect. 4 decreases as \(\frac{1}{i}\). However, the \(\frac{1}{i}\)-function of the algorithm for accurate predictions is essentially offset by \(\frac{1}{ae}\).

We prove a number of more or less technical results before stating the positive results for \(r \le 1\) (Theorem 10) and \(r \ge 1\) (Theorem 9).

Lemma 3

For any \(k \ge 1\), the total size of the k largest items accepted by \({\textsc {RAT}}\) is at most \(\sqrt{2k\hat{a}}\).

Proof

\({\textsc {RAT}}\) calls Algorithm 3, and by the condition in its if-statement, as soon as i items of size greater than \(T(i+1)\) have been accepted, no more items larger than \(T(i+1)\) are accepted after that. Thus, for each \(i \ge 0\), at most i items of size greater than \(\sqrt{\frac{\hat{a}}{2(i+1)}}\) are accepted. This means that the ith largest item accepted by \({\textsc {RAT}}\) has size at most \(\sqrt{\frac{\hat{a}}{2i}}\). Thus, the total size of the k largest accepted items is bounded by

$$\sum _{i=1}^{k} \sqrt{\frac{\hat{a}}{2i}}\le \sqrt{\frac{\hat{a}}{2}}\int _0^{k} \frac{1}{\sqrt{i}}di = \sqrt{\frac{\hat{a}}{2}} \cdot 2\sqrt{k}=\sqrt{2k\hat{a}},$$

since \(f(i)=\frac{1}{\sqrt{i}}\) is a decreasing function. \(\square \)

Corollary 1

If \({\textsc {RAT}}\) rejects an item based on the level being too high, it has accepted at least \(\lfloor \frac{1}{2\hat{a}}\rfloor \) items.

Proof

If \({\textsc {RAT}}\) has accepted k items when it receives an item with a size no larger than the current bound, \(T(i+1)\), that does not fit in the knapsack, then by Lemma 3, \(\sqrt{2(k+1)\hat{a}} > 1\). Now,

$$\sqrt{2(k+1)\hat{a}}> 1 \Leftrightarrow k > \frac{1}{2\hat{a}}-1 \Rightarrow k \ge \left\lfloor \frac{1}{2\hat{a}} \right\rfloor \,.$$

\(\square \)

The following corollary implies that \({\textsc {RAT}}\) never rejects an item based on the level being too high if \(r>2\). This is because \(r>2\) means that the items in \({\textsc {Opt}}\) are relatively large compared to \(\hat{a} \). Since \({\textsc {Opt}}\) accepts the smallest items of the sequence, it means that the sequence contains relatively few small items. Thus, the algorithm reserves space for small items that never arrive.

Corollary 2

If \({\textsc {RAT}}\) rejects an item based on the level being too high, \({\textsc {RAT}} (\sigma ) > \frac{r}{2}{\textsc {Opt}} (\sigma )-1\).

Proof

By Corollary 1,

$${\textsc {RAT}} (\sigma ) > \frac{1}{2\hat{a}}-1 = \frac{r}{2}\cdot \frac{1}{r\hat{a}} -1 \ge \frac{r}{2}{\textsc {Opt}} (\sigma )-1\,.$$

\(\square \)

For proving Theorems 9 and 10 below, we need Lemmas 4 and 5. Lemma 4 is a technical lemma, the proof of which can be found in Appendix  A.

Lemma 4

Assume that \(r, \hat{a}, q > 0\), \(i \ge 0\), and \(\ell < \sqrt{\frac{2(i+1)}{\hat{a}}}\). If

$$ 2r\sqrt{2\hat{a} (i+1)}-2r\hat{a} (i+1)+q-2 \le 0,$$

then

$$ \frac{(i+1)+\left( \frac{1}{r\sqrt{2\hat{a} (i+1)}} -1\right) \ell }{\frac{\ell }{r\sqrt{2\hat{a} (i+1)}}} > \frac{q}{2}\,.$$

Lemma 5

Assume that \({\textsc {Opt}} \) accepts \(\ell \) items larger than \(\sqrt{\frac{\hat{a}}{2(i+1)}}\) and s items of size at most \(\sqrt{\frac{\hat{a}}{2(i+1)}}\), \(i \ge 0\). Then, the following inequalities hold:

1:

\(\displaystyle s > \ell \left( \frac{1}{r\sqrt{2\hat{a} (i+1)}}-1\right) \), and

2:

\(\displaystyle \ell < \sqrt{\frac{2(i+1)}{\hat{a}}}\) .

Proof

Since \({\textsc {Opt}}\) ’s accepted items have average size a, we have that

$$\begin{aligned}r\hat{a} =a > \frac{\ell \cdot \sqrt{\frac{\hat{a}}{2(i+1)}}+s\cdot 0}{\ell +s},\end{aligned}$$

and, equivalently,

$$\begin{aligned} s > \ell \left( \frac{1}{r\sqrt{2\hat{a} (i+1)}}-1\right) . \end{aligned}$$

In addition, since \({\textsc {Opt}}\) accepts \(\ell \) items larger than \(\sqrt{\frac{\hat{a}}{2(i+1)}}\), \(\ell \sqrt{\frac{\hat{a}}{2(i+1)}}<1\), so

$$\begin{aligned} \ell < \sqrt{\frac{2(i+1)}{\hat{a}}}. \end{aligned}$$

\(\square \)

For the case where the actual average size in \({\textsc {Opt}} _s\) is at least as large as the predicted average size, we get the following result:

Theorem 9

For all request sequences \(\sigma \), such that \(r\ge 1\),

$${\textsc {RAT}} (\sigma ) \ge \frac{1}{2r}{\textsc {Opt}} (\sigma )-1\,.$$

Proof

By Corollary 2, if \({\textsc {RAT}}\) rejects an item in \(\sigma \) due to the knapsack not having room for the item, \({\textsc {RAT}} (\sigma ) \ge \frac{r}{2}{\textsc {Opt}} (\sigma )-1\ge \frac{1}{2r}{\textsc {Opt}} (\sigma )-1\) for \(r\ge 1\).

Now, suppose that \({\textsc {RAT}}\) does not reject any item due to it not fitting in the knapsack. If \({\textsc {RAT}}\) is not optimal, it must reject due to the size of the item. Let \(i_t\) denote the final value of i when the algorithm is run. This means that \({\textsc {RAT}}\) has accepted \(i_t\) items of size greater than \(\sqrt{\frac{\hat{a}}{2(i_t+1)}}\). We perform a case analysis based on whether this value is smaller or larger than \(r\hat{a} \).

Case 1: \(r\ge \frac{1}{\sqrt{2\hat{a} (i_t+1)}}\).

In this case, \(i_t+1\ge \frac{1}{2r^2\hat{a}}\) and \({\textsc {Opt}} (\sigma ) \le \frac{1}{r\hat{a}} \le \sqrt{\frac{2(i_t+1)}{\hat{a}}}\). Thus,

$$\begin{aligned} \frac{{\textsc {RAT}} (\sigma )+1}{{\textsc {Opt}} (\sigma )} \ge \frac{i_t+1}{\sqrt{\frac{2(i_t+1)}{\hat{a}}}} = \sqrt{\frac{\hat{a} (i_t+1)}{2}} \ge \sqrt{\frac{\hat{a} \frac{1}{2r^2\hat{a}}}{2}}=\frac{1}{2r}. \end{aligned}$$

Therefore, \({\textsc {RAT}} (\sigma ) \ge \frac{1}{2r}{\textsc {Opt}} (\sigma )-1\).

Case 2: \(r<\frac{1}{\sqrt{2\hat{a} (i_t+1)}}\).

Suppose \({\textsc {Opt}}\) accepts \(\ell \) items larger than \(\sqrt{\frac{\hat{a}}{2(i_t+1)}}\) and s items of size at most \(\sqrt{\frac{\hat{a}}{2(i_t+1)}}\). Note that \({\textsc {RAT}}\) also accepts the s items of size at most \(\sqrt{\frac{\hat{a}}{2(i_t+1)}}\), since we are in the case where it does not reject items because of the knapsack being too full. Given the input sequence \(\sigma \), we consider the ratio

$$\begin{aligned} \frac{{\textsc {RAT}} (\sigma )+1}{{\textsc {Opt}} (\sigma )} \ge \frac{(i_t+1)+s}{\ell +s}\,. \end{aligned}$$
(7)

The result follows if this ratio is always at least \(\frac{1}{2r}\).

Subcase 2a: \(i_t+1 \ge \frac{1}{2\hat{a}}\).

In this case, \({\textsc {RAT}} (\sigma ) \ge i_t \ge \frac{1}{2\hat{a}}-1\), while \({\textsc {Opt}} (\sigma ) \le \frac{1}{r\hat{a}}\). Thus, \({\textsc {RAT}} (\sigma )\ge \frac{r}{2}{\textsc {Opt}} (\sigma )-1\).

Subcase 2b: \(i_t+1 < \frac{1}{2\hat{a}}\).

By Ineq. (7) and Item 1 of Lemma 5, and since \(\frac{{\textsc {RAT}} (\sigma )+1}{{\textsc {Opt}} (\sigma )}\le 1\),

$$\begin{aligned} \frac{{\textsc {RAT}} (\sigma )+1}{{\textsc {Opt}} (\sigma )} \ge \frac{(i_t+1)+s}{\ell +s} \ge \frac{(i_t+1)+\left( \frac{1}{r\sqrt{2\hat{a} (i_t+1)}} -1\right) \ell }{\frac{\ell }{r\sqrt{2\hat{a} (i_t+1)}}}. \end{aligned}$$

We now show that this is at least \(\frac{1}{2r}\). From our case conditions, \(i_t+1<\frac{1}{2\hat{a}}\) and \(1\le r<\frac{1}{\sqrt{2\hat{a} (i_t+1)}}\), we get that \(\frac{1}{r^2} >2\hat{a} (i_t+1)\) and \(0<2\hat{a} (i_t+1)<1\). Consider the function

$$f(r)=2r\sqrt{2\hat{a} (i_t+1)}-2r\hat{a} (i_t+1)+\frac{1}{r}-2.$$

Taking the derivative with respect to r gives

$$\begin{aligned} f'(r)&=2\sqrt{2\hat{a} (i_t+1)}-2\hat{a} (i_t+1)-\frac{1}{r^2}. \end{aligned}$$

Setting this equal to zero and solving for r, we find

$$\begin{aligned} r^{*} = \frac{1}{\sqrt{2\sqrt{2\hat{a} (i_t+1)}-2\hat{a} (i_t+1)}}. \end{aligned}$$

The possible maximum value for f(r) in the range for r is then at 1, \(r^{*}\), or \(\frac{1}{\sqrt{2\hat{a} (i_t+1)}}\). For all three values, \(f(r)\le 0\). The hardest (but still simple) case is for \(r=r^{*}\), where

$$\begin{aligned}f(r^{*}) = \frac{2\sqrt{v} - v}{\sqrt{2\sqrt{v}-v}} + \sqrt{2\sqrt{v}-v} - 2,\end{aligned}$$

where we let v denote \(2\hat{a} (i_t+1)\). Note that due to the subcase we are in, \(0<v<1\). Now,

$$\begin{aligned}&\frac{2\sqrt{v} - v}{\sqrt{2\sqrt{v}-v}} + \sqrt{2\sqrt{v}-v} - 2 \le 0 \; \Leftrightarrow {} \; 2\sqrt{v} \le v + 1 \; \Leftrightarrow {} \; 0 \le (v-1)^2\,, \end{aligned}$$

which clearly holds. By Item 2 of Lemma 5, the result now follows from Lemma 4 with \(q=\frac{1}{r}\). \(\square \)

For the case where the actual average size in \({\textsc {Opt}} _s\) is no larger than the predicted average size, we get the following result:

Theorem 10

For all request sequences \(\sigma \), such that \(r< 1\),

$${\textsc {RAT}} (\sigma ) \ge \frac{r}{2}{\textsc {Opt}} (\sigma )-1.$$

Proof

The proof follows that of the previous theorem.

Case 1. \(i_t+1 \ge \frac{1}{2\hat{a}}\).

Since \(\ell \ge i_t+1\) (otherwise \({\textsc {RAT}}\) is optimal), \({\textsc {RAT}}\) has accepted at least \(\frac{1}{2\hat{a}}-1\) items, while \({\textsc {Opt}}\) can accept at most \(\frac{1}{r\hat{a}}\). Thus, \({\textsc {RAT}} (\sigma )\ge \frac{r}{2}\cdot \frac{1}{r\hat{a}}-1\ge \frac{r}{2}{\textsc {Opt}} (\sigma )-1\).

Case 2. \(i_t+1<\frac{1}{2\hat{a}}\).

By Item 1 of Lemma 5 and since \(\frac{{\textsc {RAT}} (\sigma )+1}{{\textsc {Opt}} (\sigma )}\le 1\),

$$\begin{aligned} \frac{{\textsc {RAT}} (\sigma )+1}{{\textsc {Opt}} (\sigma )} \ge \frac{(i_t+1)+s}{\ell +s} \ge \frac{(i_t+1)+\left( \frac{1}{r\sqrt{2\hat{a} (i_t+1)}} -1\right) \ell }{\frac{\ell }{r\sqrt{2\hat{a} (i_t+1)}}}. \end{aligned}$$

We will show that this is at least \(\frac{r}{2}\). Consider the function

$$f(r)=2r\sqrt{2\hat{a} (i_t+1)}-2r\hat{a} (i_t+1)+r-2.$$

Taking the derivative with respect to r gives

$$f'(r)=2\sqrt{2\hat{a} (i_t+1)}-2\hat{a} (i_t+1)+1,$$

which is positive, since by the case condition, \(0<2\hat{a} (i_t+1)<1\). Thus, f(r) is an increasing function for the values of \(\hat{a} \), \(i_t+1\), and r considered in this case, so the maximum value is at the maximum value of r, \(r=1\), giving that

$$\begin{aligned} f(r) \le 2r\sqrt{2\hat{a} (i_t+1)}-2r\hat{a} (i_t+1)+r-2<0. \end{aligned}$$

By Item 2 of Lemma 5, the result now follows from Lemma 4 with \(q=r\). \(\square \)

5.2.2 General Negative Result–Upper Bound

In Sect. 4, we showed that, even with accurate predictions, no deterministic algorithm can be better than \(\frac{e-1}{e}\)-competitive. In this section, we give a trade-off in the competitive ratio attainable by any algorithm for different values of r. Theorem 11 below leads to Corollary 3, which shows that if an algorithm, \({\textsc {Alg}}\), performs as well as \({\textsc {RAT}}\) for all \(1\le r\le \frac{1}{2\hat{a}}\), then \({\textsc {Alg}}\) cannot perform better than \({\textsc {RAT}}\) for any \(r\le 1\). The theorem gives a more general result using a parameter z, which, if set equal to 2, gives the trade-off just described.

Theorem 11

Let \(0< z\le 2\) and consider a deterministic algorithm, \({\textsc {Alg}}\). If \({\textsc {Alg}}\) is \(\frac{1}{zr}\)-competitive for every r between \(\frac{2}{z}\) and \(\frac{1}{\sqrt{z\hat{a}}}\), its competitive ratio is at most \(\frac{zr}{4}\), for every \(r \le \frac{2}{z}\).

Proof

We consider the adversary that gives the input sequence \(\sigma _z\) defined by Algorithm 8.

Algorithm 8
figure h

Adversarial sequence establishing trade-off in performance with respect to r; the adversarial algorithm takes parameters, z, q, and b, such that \(0 < z \le 2\), \(0< q < \frac{1}{\sqrt{z\hat{a}}}\), and \(b\ge 0\).

Consider an online algorithm, \({\textsc {Alg}}\), and assume that there exists a constant, b, such that \({\textsc {Alg}} (\sigma ) \ge \frac{1}{zr} {\textsc {Opt}} (\sigma )-b\), for any sequence \(\sigma \) and any r such that \(\frac{2}{z} \le r \le \frac{1}{\sqrt{z\hat{a}}}\). Now, consider the adversary that gives the input sequence \(\sigma _z\) defined by Algorithm 8.

If the adversarial algorithm terminates in Line 6, then, \({\textsc {Alg}}\) has accepted at most \(k-b-1\) items. In this case, \(a=\sqrt{\frac{\hat{a}}{zk}}\), and \({\textsc {Opt}}\) accepts exactly the \(\left\lfloor \sqrt{\frac{zk}{\hat{a}}}\right\rfloor \) items from the last iteration of the while-loop. Since \(a=r\hat{a} \), \(r=\sqrt{\frac{1}{zk\hat{a}}}\), which lies between \(\sqrt{\frac{1}{zp\hat{a}}} \ge \sqrt{\frac{1}{z\hat{a}} \cdot \frac{4\hat{a}}{z}} = \frac{2}{z}\) and \(\frac{1}{\sqrt{z\hat{a}}}\). Thus,

$$\begin{aligned} {\textsc {Alg}} (\sigma _z)&\; \le \; k-b-1 \; \le \; \frac{k-1}{\left\lfloor \sqrt{\frac{zk}{\hat{a}}}\right\rfloor }{\textsc {Opt}} (\sigma _z)-b \;< \; \frac{k-1}{\sqrt{\frac{zk}{\hat{a}}}-1}{\textsc {Opt}} (\sigma _z)-b \\&\; < \; \frac{k}{\sqrt{\frac{zk}{\hat{a}}}}{\textsc {Opt}} (\sigma _z)-b \; = \; \sqrt{\frac{k\hat{a}}{z}}{\textsc {Opt}} (\sigma _z)-b \; = \; \frac{1}{zr}{\textsc {Opt}} (\sigma _z)-b \,, \end{aligned}$$

where the second strict inequality holds because 1 is added to the numerator and denominator of a positive fraction less than 1. This contradicts the assumption that for each r between \(\frac{2}{z}\) and \(\frac{1}{\sqrt{z\hat{a}}}\), \({\textsc {Alg}} (\sigma ) \ge \frac{1}{zr} {\textsc {Opt}} (\sigma )-b\), for any sequence \(\sigma \), when the adversarial algorithm terminates in Line 6. Thus, the adversarial algorithm does not terminate there.

If the adversarial algorithm does not terminate in Line 6, \(r=q\) and \({\textsc {Opt}} (\sigma _z) = \frac{1}{q\hat{a}} = \frac{1}{r\hat{a}}\). Moreover, for \({\textsc {Alg}}\), the ith accepted item must have size at least \(\sqrt{\frac{\hat{a}}{z(i+b)}}\), for \(1 \le i \le p-b\). Thus, these first \(p-b\) items fill the knapsack to at least

$$\sum _{i=b+1}^{p} \sqrt{\frac{\hat{a}}{zi}}\ge \sqrt{\frac{\hat{a}}{z}}\int _{b+1}^{p+1} \frac{1}{\sqrt{i}}di = \sqrt{\frac{\hat{a}}{z}} (2\sqrt{p+1}-2\sqrt{b+1}),$$

where we use that \(\frac{1}{\sqrt{i}}\) is a decreasing function.

Since the items of size \(r\hat{a} \) are the smallest items of the sequence, this means that

$$\begin{aligned} {\textsc {Alg}} (\sigma _z)&\le p+\frac{1-\sqrt{\frac{\hat{a}}{z}} (2\sqrt{p+1}-2\sqrt{b+1})}{r\hat{a}} \\&\le \frac{z}{4\hat{a}}+\frac{1-\sqrt{\frac{\hat{a}}{z}} \left( 2\sqrt{\frac{z}{4\hat{a}}}-2\sqrt{b+1}\right) }{r\hat{a}} \\&= \frac{z}{4\hat{a}}+\frac{1- 1 + 2\sqrt{\frac{\hat{a} (b+1)}{z}}}{r\hat{a}} \\&=\frac{1}{r\hat{a}}\left( \frac{zr}{4}+2\sqrt{\frac{\hat{a} (b+1)}{z}}\right) \\&= \left( \frac{zr}{4}+2\sqrt{\frac{\hat{a} (b+1)}{z}}\right) {\textsc {Opt}} (\sigma _z)\,. \end{aligned}$$

As a function of \(\hat{a} \), the upper bound is \(\frac{zr}{4}+2\sqrt{\frac{\hat{a} (b+1)}{z}}\), but the second term becomes insignificant as \(\hat{a} \) approaches zero. \(\square \)

We also note the contrapositive version of this theorem, i.e., if \({\textsc {Alg}}\) is better than \(\frac{zr}{4}\)-competitive for some \(r \le \frac{2}{z}\), it cannot be \(\frac{1}{\sqrt{z\hat{a}}}\)-competitive for all r between \(\frac{2}{z}\) and \(\frac{1}{\sqrt{z\hat{a}}}\).

Setting \(z=2\) in Theorem 11 demonstrates a Pareto-like optimality for \({\textsc {RAT}}\):

Corollary 3

Consider a deterministic algorithm, \({\textsc {Alg}}\).

If \({\textsc {Alg}}\) is \(\frac{1}{2r}\)-competitive for every r between 1 and \(\frac{1}{\sqrt{2\hat{a}}}\), it has a competitive ratio of at most \(\frac{r}{2}\), for every positive \(r \le 1\).

Moreover, if \({\textsc {Alg}}\) is better than \(\frac{r}{2}\)-competitive for some \(r \le 1\), it cannot be \(\frac{1}{2r}\)-competitive for all r between 1 and \(\frac{1}{\sqrt{2\hat{a}}}\).

6 Advice Complexity

In this section, we briefly consider the Online Unit Profit Knapsack Problem in terms of advice complexity, concentrating on upper bounds on the number of bits, following the techniques in [6], used for the General Knapsack Problem with advice, and many other articles on advice complexity including [51, 52]: When representing (an approximation of) a number, x, the k most significant bits of x are given along with a number, z, representing the number of bits between these k bits and the binary point in x. Two different possible algorithms are explained, the first demonstrating how algorithms with predictions may be useful in defining algorithms with good advice complexity, since approximating values with few bits is related to having some limited error in a prediction. In addition, a non-constant lower bound on the number of bits needed for optimality is presented.

An algorithm based on CAT

The prediction given in the algorithms \({\textsc {CAT}}\) and \({\textsc {RAT}}\) is the value a, representing the average size of an item in \({\textsc {Opt}} _s\), and it could have some error. One could use \({\textsc {CAT}}\) in the advice complexity setting, assuming that an oracle gives two values: z, the number of zeros between the binary point and the most significant bit in the binary representation of a, followed by s, the k most significant bits of a. In this case, the prediction \(\hat{a} \) given for a is \(\frac{s}{2^{z+k}}\). Since the high order bit of s is 1, this value is at least \(\frac{1}{2^{z+1}}\). The error in the prediction, \(\hat{a} \), is only due to the missing low order bits (assumed, possibly incorrectly, to be zero). The missing bits represent a number less than \(\frac{1}{2^{z+k}}\). Thus, the ratio, r, in \(a=r\hat{a} \) is in the range \(1\le r\le 1+\frac{1}{2^{k-1}}\).

We denote by \({\textsc {CATa}}\) the advice based version of \({\textsc {CAT}}\) described above. Since \(1\le r\le 1+\frac{1}{2^{k-1}} < e\), Theorem 6 gives the following guarantee.

$$\begin{aligned} {\textsc {CATa}} (\sigma ) \ge \frac{e-1-\frac{1}{2^{k-1}}}{e}{\textsc {Opt}} (\sigma ), \text{ for } \text{ all } \sigma . \end{aligned}$$

Note that the length of the advice is independent of the length of the request sequence, though dependent on the values in that sequence. The value, z, and the bitstring, s, must be specified using self-delimiting encoding, since we do not know how many bits are used for them. For example, \(\lceil \log (z+1)\rceil \) could be written in unary (\(\lceil \log (z+1)\rceil \) ones, followed by a zero) before writing z itself in binary. Treating s similarly, at most \(2(k+\lceil \log (z+1)\rceil +1)\) bits are used. This gives us:

Proposition 1

\({\textsc {CATa}}\) has a competitive ratio of at least \(\frac{e-1-\frac{1}{2^{k-1}}}{e}\), using at most \(2(k+\lceil \log (z+1)\rceil +1)\) bits of advice, when \(\frac{1}{2^{z+1}}\le a<\frac{1}{2^z}\).

An algorithm based on techniques in [6]

As mentioned at the end of Sect. 1.3, since \({\textsc {Opt}}\) can be viewed as accepting a prefix of the sequence of items sorted in non-decreasing order of size, there is another obvious type of advice to give. Let the advice be k-bit approximations to both the size, S, of the largest item that \({\textsc {Opt}} _s\) accepts and the fraction, t, of the knapsack not filled with items of size strictly smaller than S. The approximation to S can be given using the technique above, specifying the number of leading zeros first and then k significant bits. For t, we simply use the first k bits after the binary point, letting \(t'\) be the number represented by those k bits. The reason that it is necessary to give the fraction of the knapsack not filled with items of at most this size is the following. Even if the exact value of S was given, it is unknown if \({\textsc {Opt}} _s\) accepts one or many items of that size, and these “large” items could come before any smaller ones.

The algorithm, called \({\textsc {Alg}_{\textrm{S}}}\), will accept all items that are smaller than S, which is the optimal behavior on those items (so in the worst case for the performance ratio, no such items arrive). Using the notation s and z as above for approximating S, we are only interested in items of sizes between \(\frac{s}{2^{z+k}}\) and \(\frac{s+1}{2^{z+k}}\) and can calculate a bound on the competitive ratio just from the algorithm’s and \({\textsc {Opt}} _s\)’s performance on items in that range. Since the algorithm does not accept all items in the worst case, we may assume that there are enough items in this size range that it rejects some. Under this assumption, the algorithm accepts at least \(\left\lfloor \frac{t'/2^k}{(s+1)/2^{z+k}}\right\rfloor \) and \({\textsc {Opt}} _s\) accepts at most \(\left\lfloor \frac{(t'+1)/2^k}{s/2^{z+k}}\right\rfloor \). For an asymptotic result, ignoring the rounding down on the algorithm’s performance, this gives a performance ratio of at least

$$\frac{t'\cdot s}{(s+1)(t'+1)}\ge \frac{2^k\cdot 2^{z+k}}{(2^k+1)(2^{z+k}+1)}\ge \frac{2^{2k}}{(2^k +1)^2}.$$

Since we approximate both S (using \(2(k+\big \lceil \log (z+1)\big \rceil +1)\) bits) and t (using \(2k+1\) bits), we need a total of \(4k+2(\big \lceil \log (z+1)\big \rceil +1)\) advice bits. This gives us:

Proposition 2

\({\textsc {Alg}_{\textrm{S}}}\) has a competitive ratio of at least \(\frac{2^{2k}}{(2^k +1)^2}\), using at most \(4k+2(\lceil \log (z+1)\rceil +1)\) bits of advice, when \(\frac{1}{2^{z+1}}\le S<\frac{1}{2^z}\).

The competitive ratio with this approach is better than that of the first approach, but it also uses more advice.

Advice complexity for optimality

With respect to optimality, we note that the lower bound of \(\log n\) from [6] for the general Knapsack Problem cannot be used directly here, since the items used in their sequences all have size 1, so the weights are very important. In contrast to the upper bounds proven above, we prove that for optimality, the number of advice bits needed is a function of the length, n, of the input sequence:

Theorem 12

Any algorithm with advice, solving the Unit Profit Knapsack Problem to optimality, requires at least \(\log (n/3)\) bits of advice.

Proof

For any algorithm, \({\textsc {Alg}}\), consider the set of input sequences defined to have length n as follows. Let \(k \in \mathbb {N}\), \(n=3k\), and \(0\le \ell \le k\). Then \(I_\ell \) consists of (in the order listed)

  • k items of size \(\frac{1}{k}\),

  • \(2(k-\ell )\) items of size \(\frac{1}{2k}\),

  • \(2\ell \) items of size 1.

Suppose for the sake of contradiction that \({\textsc {Alg}}\) is optimal on all of these sequences and never reads \(\log (n/3)\) bits of advice. \({\textsc {Opt}}\) accepts \(\ell \) items of size \(\frac{1}{k}\) and then \(2(k-\ell )\) items of size \(\frac{1}{2k}\), completely filling up the knapsack with \(2k-\ell \) items. Intuitively, the advice needs to say how many of the first k items to accept. Since there are n/3 sequences in all and fewer than \(\log (n/3)\) bits of advice, there are at least two of the sequences, \(I_j\) and \(I_{j'}\), for which \({\textsc {Alg}}\) receives the same advice. Thus, \({\textsc {Alg}}\) accepts the same number, \(j^*\), of items of size \(\frac{1}{k}\) on both \(I_j\) and \(I_{j'}\). Without loss of generality, assume that \(j^*\not = j'\). If \(j^* > j'\), then \({\textsc {Alg}}\) can accept only \(2(k-j^*)\) items of size \(\frac{1}{2k}\). In all, \({\textsc {Alg}} (I_{j'}) \le j^*+2(k-j^*) <2k-j' = {\textsc {Opt}} (I_{j'})\). If \(j^* < j'\), then \({\textsc {Alg}} (I_{j'}) \le j^* +2(k-j') < 2k-j' = {\textsc {Opt}} (I_{j'})\). Thus, \({\textsc {Alg}}\) is not optimal on \(I_{j'}\), giving a contradiction.\(\square \)