# Modeling floor function exactly

Suppose we want to enforce a constraint $$y=\lfloor{x}\rfloor$$ where $$x$$ is some continuous variable. One option is to use $$x-1\leq{y}\leq{x},\quad y\in\mathbb{Z},$$ which fails on the edge case $$x\in\mathbb{Z}$$. One way around this which works approximately is to introduce some tolerance $$\varepsilon>0$$ and enforce $$x-1+\varepsilon\leq{y}\leq{x},\quad y\in\mathbb{Z},$$ which fails if $$x\in(\lceil{x}\rceil-\varepsilon,\lceil{x}\rceil)$$. Is there an exact way to enforce a floor function, perhaps by introducing additional integer variables?

• My answer is incorrect. I've been trying to salvage it but haven't figured out how yet. You should un-accept my answer, and I will delete it. I'll post a new one if I ever figure it out. – LarrySnyder610 Jun 14 '19 at 1:22
• @LarrySnyder610 Oh too bad! I’ll keep my eye out – David M. Jun 14 '19 at 1:26
• On the plus side, looks like I'll earn my Disciplined badge. – LarrySnyder610 Jun 14 '19 at 1:35
• As one answer shows, you can easily do this when you can enforce a strict inequality over continuous variables. Your $\varepsilon$ trick looks like a way to work around not being able to do that. It might be a good idea to indicate if strict inequalities don't work for you, if that is indeed the case. – Discrete lizard Jun 14 '19 at 7:20
• If you want to use this in a solver you'll have to work with the (carefully chosen) epsilon approach as it is not possible (due to machine precision) to have a real " strictly less than" constraint - only "less or equal" than constraints are meaningful. – JakobS Jun 14 '19 at 8:41

It is not possible to model the floor function as a constraint without modeling strict inequality. To prove this, I will show how the floor function can be used to model strict inequalities.

Thanks to @Rob Pratt for showing a cleaner formulation that does not need a possibly large M.

Suppose we can model the constraint $$y=\lfloor x\rfloor$$ for any real variable $$x$$ and integer variable $$y$$. First note that if we have the floor over the real numbers, we have the ceiling as well, since $$\lceil x\rceil = -\lfloor -x\rfloor$$. This means we can add constraints $$\lceil x\rceil -\lfloor x\rfloor =1$$, which is equivalent to $$x\notin \mathbb{Z}$$. The statement $$0 for a real variable $$x$$ is equivalent to $$0\leq x \wedge (1\leq x \vee x\notin \mathbb{Z})$$. So, we can encode the statement $$0 as follows:

\begin{align*}0&\leq x\\ 1-M\cdot (1-b_1)&\leq x\\ b_2 &= \lceil x\rceil -\lfloor x\rfloor\\ b_1 + b_2&\geq 1, \end{align*}

However, since $$x$$ is required to be positive in all cases, we can set $$M$$ to $$1$$ to obtain the following formulation:

\begin{align*}b_1&\le x \\ b_2 &= \lceil x \rceil - \lfloor x \rfloor \\ b_1 + b_2 &\ge 1\end{align*}

using only the additional binary variables $$b_1,b_2$$. As any strict inequality can be modeled via $$0, we are done.

In other words, if we assume we cannot model strict inequalities, then we cannot model a floor function.

Intuitively, what is going on here is that the floor function constraint is as 'powerful' as strict inequalities or integrality testing. Or, to speak in CS terms, all these constraints can be reduced to each-other and lie in the same equivalence class. (cf. complexity classes and poly-time reductions) I wonder if someone investigated these 'expressibility classes' in the context of linear programming before.

@Rob Pratt notes that we can even do without the additional binary variables and write the constraint $$0 as the single constraint $$1-\lceil x\rceil + \lfloor x \rfloor \leq x$$. However, note that even after removing the binary variables this reduction still only makes sense in the context of integer programming, as flooring an otherwise unconstrained real variable yields an unconstrained integer variable.

• Conjunctive normal form of your logical proposition leads to the following slightly simpler formulation: \begin{align*} 0 &\le x \\ 1 - M \cdot(1-b_1)&\le x \\ b_3 &= \lceil x \rceil - \lfloor x \rfloor \\ b_1 + b_3 &\ge 1\end{align*} – RobPratt Jun 16 '19 at 14:35
• And then taking $M=1$ simplifies further: \begin{align*} b_1&\le x \\ b_3 &= \lceil x \rceil - \lfloor x \rfloor \\ b_1 + b_3 &\ge 1\end{align*} – RobPratt Jun 16 '19 at 14:44
• @RobPratt Ah yes, you are right, we don't even need big-M when we already require x is positive. That dependency on big-M did seem weird. This makes it a lot cleaner, thanks! – Discrete lizard Jun 16 '19 at 14:51
• This is nice. I have to think about it a bit more but I think I am convinced. – LarrySnyder610 Jun 16 '19 at 20:10
• In fact, I guess we can eliminate the binary variables and just have $$1-\lceil x \rceil +\lfloor x \rfloor \le x.$$ – RobPratt Jun 17 '19 at 3:20

I think you can resort to bilevel optimization, in the spirit of slide 3 here

In bilevel optimization, some variables are the optimal solutions of another optimization problem called "the follower subproblem".

In our case, one can define the following follower subproblem:

$$y = \underset{y'}{\operatorname{argmax}} \left\{ y': y' \le x, \ y' \hbox{ integer} \right\}$$

This should work also if $$x=(x_1, \dots\,x_n)$$ is an $$n$$-dimensional vector, i.e., you can set-up a single follower problem to define all the rounded $$y_i$$'s:

$$y = \underset{y'}{\operatorname{argmax}}\left\{ \sum_{i=1}^n y'_i: y' \le x, \ y' \hbox{ integer}\right\}$$

Whether this is useful in practice I don’t know.

• How is this exact given non-zero achieved integrality (etc.) tolerances? – Mark L. Stone Jun 17 '19 at 11:27
• Well, the question asked for an exact formulation without tolerances, as the bilevel one. I agree that solving in practice that formulation brings tolerances back (see the last line in my answer) but I doubt you can get anything practical without tolerances and/or assumptions on x. – Matteo Fischetti Jun 17 '19 at 11:44
• I think this approach works. It is much harder to solve than introducing new binary variables, big-Ms, etc., as you pointed out, but I think this answer also makes sense in light of @Discrete lizard's answer, i.e., that floor is somehow fundamentally harder than, say, other logical constraints. – LarrySnyder610 Jun 17 '19 at 12:51
• @LarrySnyder610 Or in particular, that it is about as hard as modeling a strict inequality, which via my answer + this answer can also be done with bi-level optimisation. In that way, you can interpret this differently as follows: solving bi-level optimisation is at least as hard as solving ILP with strict constraints. In other words, this approach works for modeling the problem, but it probably isn't very useful for actually solving the problem. – Discrete lizard Jun 17 '19 at 13:07
• @Discrete lizard If all we want to do is "model" the problem, we can use the "floor" function, which exactly models the floor function. It is then up to the modeling language or solver how to (approximately) implement the floor function. For instance, YALMIP overloads the MATLAB floor function yalmip.github.io/command/floor , so that takes care of the (exact) "modeling". YALMIP uses mixed integer to convert this to the problem provided to the solver, which of course is not exact. – Mark L. Stone Jun 17 '19 at 14:26

As all have mentioned, the problem is intrinsically hard, as it effectively involves a strict inequality.

One way to represent it, using no strict inequalities or magic constants, is to use the simple trick that $$> 0$$ can be replace with $$\geq e^{-z}$$ for a new decision variable $$z$$. An exact solver (not allowed to work with infinities) could then never return a non-strict solution.

Here, that would translate to $$x-1 + e^{-z} \leq y \leq x$$. Nice convex exponential cone relaxation and all.

Of course, a practical exact solver would not be able to work exactly with expressions involving exponentials, so this is just silly acrobatics trying to avoid introducing some kind of tolerance in the end, i.e., a magic constant.

EDIT: A solution which (in theory...) can be implemented and wouldn't force you out in the transcendental world is to use $$x-1 + z^{-2} \leq y \leq x$$

• Fair point re: the practical solver, but I like this clever trick – David M. Jun 17 '19 at 17:10

You only specified $$x$$ is "continuous". I'll interpret this as $$x$$ is rational rather than real. This is not a terrible assumption, as floating point numbers in computers are rational anyways.

A rational $$x$$ can be replaced by a fraction of two integers $$x = \frac{p}{q}$$ with $$p$$ free and $$q$$ positive.

Now the constraint is $$y = \left\lfloor \frac{p}{q}\right\rfloor$$. This is equivalent to the two constraints

$$\begin{cases}y = \frac{p - m}{q}\\m = p \mod q\end{cases}$$ where $$m$$ is another integer variable.

Let's introduce yet another (non-negative) integer variable $$a$$ and additional constraints to get rid of modulo operator. $$\begin{cases}p = a \cdot q + m\\m \leq q - 1\end{cases}$$

So finally we can replace the constraint $$y = \lfloor x \rfloor$$ by

$$\begin{cases}x = \frac{p}{q}\\y = \frac{p-m}{q}\\p = a \cdot q + m\\m \leq q - 1\end{cases}$$ where $$p$$ is a free integer and $$a,m,q$$ are positive integers.

Of course, this is no longer linear and quite ugly to solve. In implementations it will bring tolerances back when approximating the nonlinear constraints.

• Floating point numbers are not rational. In particular, rationals allow for exact operations, which can be implemented as long as you can represent large enough integers, while floating points are not exact. It is correct that floating points have finite precision, but they are not rationals. – Discrete lizard Jun 18 '19 at 13:35
• Maybe I should have added that by "floating point numbers" I mean floats according to IEEE754. Any number re-presentable by a float or double in your favorite programming language is rational. – Michael Feldmeier Jun 18 '19 at 14:03
• Seems like another clever trick, although at the cost of new kinds of unpleasantness. – LarrySnyder610 Jun 18 '19 at 20:45
• @MichaelFeldmeier Hmm. While I would agree that floats (IEEE754 or not) themselves can be equally well considered to be an implementation for rationals as for reals, I personally would expect exact representation and arithmetic when people talk about 'rationals', though. Still, this is only a digression, it doesn't really matter for this answer whether floats are rational or not. – Discrete lizard Jun 19 '19 at 5:16

$$x-1 . We then can set $$y\in \mathbb {Z}$$.

• With the "strictly less than" constraint you'll run into problems when implementing this in a real model to be solved with a solver. You'll then have to do the epsilon trick that was already mentioned in the question. – JakobS Jun 14 '19 at 8:44
• $x-1 +\epsilon \leq y \leq x\implies y \leq x \leq y+1-\epsilon\implies0\leq x-y \leq1-\epsilon$. The same argument as the above answer, working for solvers, @Jakob. – Majid Jun 14 '19 at 13:38
• If $x=1-\epsilon$ then $y=0$ is not correct? – Majid Jun 14 '19 at 13:50
• I know that. But I'm still not convinced. Suppose $x=1-\frac{\varepsilon}{2}$ then $y$ has to be $1$ (as $1-\frac{\varepsilon}{2}>1-\varepsilon$), but it should be $0$. – JakobS Jun 14 '19 at 14:58
• – JakobS Jun 14 '19 at 15:00

I think the answer of Rob can be developed into an exact answer, if we can ascertain apriori that the fractional part $$s$$ can be no larger than a given bound.

Consider $$x$$ to be the floor of $$y$$. Further assume we can guarantee that $$x$$ does not lie in the following range (Integer + $$\delta$$, Integer+1] for any Integer. This assumption can be stated also as: $$0 \le \delta <1$$ is the largest possible value for the fractional part of $$x$$. Of course this is a hard condition to satisfy, but for many problems it can be easy. For example, consider this question: https://math.stackexchange.com/questions/3520916/proof-for-an-easy-empirical-result-on-fractions-and-ceilings

Then $$x-\delta \le y \le x$$ and $$y \in Z^+$$ model the floor exactly.

It is solved by introducing a slack variable, one simple explanation is here and a more detailed video tutorial is here (with online solver).

Alternative (homework preventing) example:

How to implement the Ceiling function in a Mixed Integer Program model.

The constraint in question is:

$$\left\lceil\frac{x - A}{B}\right\rceil \cdot C + D \cdot x \lt E$$

Introduce an integer variable $$y$$ and a continuous slack variable $$s$$ and write:

\begin{align}y \cdot C + D \cdot x &\leq E\\y&=\frac{x-A}{B}+s\\s &\in [0,0.999]\\y &\in \{\dots,−3,−2,−1,0,1,2,3,\dots\}\end{align}

Note that $$\lt$$ constraints are no good, so we make that $$\leq$$. We have a very small area for $$s$$ that we don’t allow: between $$0.999$$ and $$1$$. This is to prevent that the ceiling of an integer $$k$$ is the next integer $$k+1$$.

• The constraint you mention differs from the one in the question, which is $y=\lfloor x\rfloor$. In fact, the constraint $\lceil a\rceil< b$ in this answer is equivalent to $a\leq b-1$. (perhaps you meant to write $\leq$ there) The question also asked for exact methods to deal with this problem, not some finite tolerance (here $0.001$). – Discrete lizard Jun 16 '19 at 18:26
• No, follow the link; it is transcribed correctly. – Rob Jun 16 '19 at 18:37
• I see. Then your linked source is incorrect. I've noted it on their answer on math.se Also, first be sure to indicate verbatim copied text with a quote block, so that we can see what text is yours and what is written elsewhere. – Discrete lizard Jun 16 '19 at 19:10
• @Rob I agree with DL, in that the question (and my bounty) are looking for an exact method that doesn't require a finite tolerance. Otherwise, I think your approach is similar to the one in the OP and in the answer by Majid Salavati-Khoshghalb. (Somewhat different formulation, but same basic idea.) – LarrySnyder610 Jun 16 '19 at 20:04
• @Rob After discussion on that post, it turns out that my above claim that $a\leq b-1$ would work is incorrect. The point about the tolerance still remains, as LarrySnyder610 also notes. – Discrete lizard Jun 16 '19 at 21:15

An answer with no $$\epsilon$$:

We can set $$y+s=x$$, $$y \in \mathbb {Z}$$, $$s\geq 0$$.

Appropriate terms (appropriate in terms of sign for following terms, values for big-M's and minimization and maximization) have to be added in the objective function such as $$M_1x$$, $$M_2y$$ based on the sign of $$x$$ in the objective function and bounds on $$x$$.

In the general case one can add these two terms somehow $$x$$ and $$y$$ have the same magnitude of increase in OF in both terms value and sign, if minimization $$-M_1x$$, $$-M_2y$$ and maximization $$M_1x$$, $$M_2y$$.

• Unfortunately I don't think this works either. $M$ could easily be large enough to force $x$ itself to be an integer, when it's not optimal (in the original problem) to do so. Example: $\min -x\ \text{s.t.}\ \lfloor x\rfloor \le 1,\ x\le 1.8$. Optimal solution is $x^*=1.8$ with objective $-1.8$. Your reformulation is: $\min -x + Ms\ \text{s.t.}\ y \le 1,\ x\le 1.8,\ y+s=x$. For sufficiently large $M$, optimal solution is $x^*=y^*=1$, $s^*=0$, objective $-1$. – LarrySnyder610 Jun 19 '19 at 13:31
• And I don't think there's any way to set $M$ correctly a priori so we can be sure that the model will first optimize the "real" objective and then ensure that $s$ is as small as possible. – LarrySnyder610 Jun 19 '19 at 13:33
• Agree, what if I set:$\min -x -M_1x-M_2y+ Ms\ \text{s.t.}\ y \le 1,\ x\le 1.8,\ y+s=x$, what is the optimal solution considering $M_1,M_2 \ge \ge M$ ? – Majid Jun 19 '19 at 13:51
• @LarrySnyder610 Correction: $M_1,M_2>>M$ – Majid Jun 19 '19 at 13:59
• That might work in the specific case of my example (I haven't checked) but in general there is not (as far as I know) a way to set the coefficients in general that will ensure it works. Think about it this way: By adding a penalty $Ms$, you are essentially relaxing the constraint $s=0$ with a Lagrangian multiplier $M$. When you do so, you can't identify the optimal $M$ a priori; you'd need to use KKT conditions or an optimization over $M$, etc. – LarrySnyder610 Jun 19 '19 at 14:23