0

I went through the answers to this question: Modeling floor function exactly, but I still do not get how to model y = floor(x). Is that question answered and I just do not see it?

Nike Dattani
  • 1,278
  • 6
  • 32
Clement
  • 2,252
  • 7
  • 13

2 Answers2

1

in https://github.com/AlexFleischerParis/howtowithopl/blob/master/ceil.mod

    range r=1..4;
float x[r]=[1.5,4.0,2.0001,5.9999];

dvar int y[r];
dvar float f[r] in 0..0.9999999;

subject to
{
forall(i in r) y[i]==x[i]+f[i];


}

execute
{
writeln(x," ==> ",y);
}

assert forall(i in r) y[i]==ceil(x[i]);

//which gives

// [1.5 4 2.0001 5.9999] ==> [2 4 3 6]

I gave an OPL CPLEX example about how to model ceil. Floor is not very different.

range r=1..4;

float x[r]=[1.5,4.0,2.0001,5.9999];

dvar int y[r]; dvar float f[r] in 0..0.9999;

subject to { forall(i in r) y[i]==x[i]-f[i];

}

execute { writeln(x," ==> ",y); }

assert forall(i in r) y[i]==floor(x[i]);

Alex Fleischer
  • 4,000
  • 5
  • 11
1

I tried finding floor value together with ceil value. c = ceil(x) and f=floor(x)

$$x \leq c \leq x+1 $$ $$x \geq f \geq x-1 $$

$$ c-f \leq 1 $$ $$ x \leq f + M(c-x) $$ $$ x \geq c - M(x-f) $$ $$ x\in R^+, c,f \in Z^+ $$

Possible cases: Case 1: x=3.9 $$3.9 \leq c \leq 4.9 $$ $$3.9 \geq f \geq 2.9 $$

$$ c-f \leq 1 $$ $$ 3.9 \leq f + M(c-3.9) $$ $$ 3.9 \geq c - M(3.9-f) $$ Result c=4, f=3

Case 2: x=3.1 $$3.1 \leq c \leq 4.1 $$ $$3.1 \geq f \geq 2.1 $$

$$ c-f \leq 1 $$ $$ 3.1 \leq f + M(c-3.1) $$ $$ 3.1 \geq c - M(3.1-f) $$ Result c=4, f=3

Case 3: x=3 $$3 \leq c \leq 4 $$ $$3 \geq f \geq 2 $$

$$ c-f \leq 1 $$ $$ 3 \leq f + M(c-3) $$ $$ 3 \geq c - M(3-f) $$ 3.a. c=4, f=2 is possible from const 1 and 2 but const 3 inf.

3.b. c=4, f=3 is possible from const 1 and 2 but const 5 inf.

3.c. c=3, f=2 is possible from const 1 and 2 but const 4 inf. Therefore c=3, f=3.

M is big number and determines the sensivity of x. For example, x=3.9 , M must be equal or greater than 10. Likewise, if x=3.99, $M \geq 100$

kur ag
  • 815
  • 5
  • 14
  • What prevents $(x,y)=(3,2)$? – RobPratt Sep 22 '20 at 14:26
  • @RobPratt I gave a new answer, I do not know there is something missed. – kur ag Sep 22 '20 at 22:50
  • Your third constraint is implied by your first two constraints and can be omitted. Also, Case 3 should have 3 instead of 3.1. – RobPratt Sep 22 '20 at 23:48
  • I think you will find that $1/M$ plays the same role as $\epsilon$ in the linked question. – RobPratt Sep 23 '20 at 01:32
  • You still need to replace 3,1 with 3 in Case 3. And $(c,f)=(4,3)$ is feasible. – RobPratt Sep 23 '20 at 05:54
  • @RobPratt, the last const is written wrong, I fixed. – kur ag Sep 23 '20 at 06:30
  • You still have a magic constant M which will cause problems. You don't even have a feasible solution for $x = 0.5\cdot \frac{1}{M}$, i.e. it will work at the integer cases, and for significantly non-integer, but then for close to integer it will not only yield the wrong result as is common in most naive models but simply be infeasible. – Johan Löfberg Sep 23 '20 at 08:03