Modeling the Round (Nearest Integer) functionWhen to use indicator constraints versus big-M approaches in...
What is the white square near the viewfinder of the Fujica GW690?
Who inspired the character Geordi La Forge?
I can use x = y = z. Why not x < y < z?
Why can a T* be passed in register, but a unique_ptr<T> cannot?
How can I seal 8 inch round holes in my siding?
Why didn't Aboriginal Australians discover agriculture?
If you pass through the order of colors in Prismatic Wall one way, do you reverse the order of colors passing through the other way?
How to get to Antarctica without using a travel company
Are there any Baryons that have quark-antiquark combinations?
What is an informed evaluation of resource availability?
What exactly is meant by "partial function" in functional programming?
What is the name of this Korean mobile sports game?
Is using a photo reference for pose fair use?
What are these objects near the Cosmonaut's faces?
How much does freezing grapes longer sweeten them more?
How can a stock trade for a fraction of a cent?
How low is the lowest tone that a human can sing?
Why it is a big deal whether or not Adam Schiff talked to the whistleblower?
When applying for a visa has there ever been a case of embassy asking for proof of right to be in the present country?
In this day and age should the definition / categorisation of erotica be revised?
Why is 10.1.255.255 an invalid broadcast address?
Do the KKT conditions hold for mixed integer nonlinear problems?
In the example of guess a specified number between 1 and 20 (both inclusive), what is the sample space?
What are the advantages to banks being located in the City of London (the Square Mile)?
Modeling the Round (Nearest Integer) function
When to use indicator constraints versus big-M approaches in solving (mixed-)integer programsModeling floor function exactlyTightness of an LP relaxation without using objective functionIs deciding the presence of mixed-integer points in the relative interior of a polyhedron in NP?How to linearize min function as a constraint?Expressing a chain of boolean ORs using ILPRepresenting an indicator function: binary variables and “indicator constraints”Decoding a Deep Neural Network as an Analytical Expression for Optimization PurposeValid Inequalities and Strong InequalitiesIn the context of LASSO regression, how to introduce a constraint for max number of selected betas?
$begingroup$
Modeling various non-differentiable functions is quite common knowledge including $abs$, $min$ and $max$ functions. How would one go about modeling the nearest integer function , say in an inequality constraint:
$lfloor{x}rceil leq C$
mixed-integer-programming modeling
New contributor
$endgroup$
add a comment
|
$begingroup$
Modeling various non-differentiable functions is quite common knowledge including $abs$, $min$ and $max$ functions. How would one go about modeling the nearest integer function , say in an inequality constraint:
$lfloor{x}rceil leq C$
mixed-integer-programming modeling
New contributor
$endgroup$
1
$begingroup$
Do you have a specific rounding rule in mind for .5 cases? The Wikipedia page on 'Nearest integer function' (en.wikipedia.org/wiki/Nearest_integer_function) says "On most computer implementations, the selected rule is to round half-integers to the nearest even integer"
$endgroup$
– Dipayan Banerjee
8 hours ago
$begingroup$
@DipayanBanerjee Yes, let's assume the nearest even integer rule for this example.
$endgroup$
– Josh Allen
8 hours ago
add a comment
|
$begingroup$
Modeling various non-differentiable functions is quite common knowledge including $abs$, $min$ and $max$ functions. How would one go about modeling the nearest integer function , say in an inequality constraint:
$lfloor{x}rceil leq C$
mixed-integer-programming modeling
New contributor
$endgroup$
Modeling various non-differentiable functions is quite common knowledge including $abs$, $min$ and $max$ functions. How would one go about modeling the nearest integer function , say in an inequality constraint:
$lfloor{x}rceil leq C$
mixed-integer-programming modeling
mixed-integer-programming modeling
New contributor
New contributor
New contributor
asked 8 hours ago
Josh AllenJosh Allen
613 bronze badges
613 bronze badges
New contributor
New contributor
1
$begingroup$
Do you have a specific rounding rule in mind for .5 cases? The Wikipedia page on 'Nearest integer function' (en.wikipedia.org/wiki/Nearest_integer_function) says "On most computer implementations, the selected rule is to round half-integers to the nearest even integer"
$endgroup$
– Dipayan Banerjee
8 hours ago
$begingroup$
@DipayanBanerjee Yes, let's assume the nearest even integer rule for this example.
$endgroup$
– Josh Allen
8 hours ago
add a comment
|
1
$begingroup$
Do you have a specific rounding rule in mind for .5 cases? The Wikipedia page on 'Nearest integer function' (en.wikipedia.org/wiki/Nearest_integer_function) says "On most computer implementations, the selected rule is to round half-integers to the nearest even integer"
$endgroup$
– Dipayan Banerjee
8 hours ago
$begingroup$
@DipayanBanerjee Yes, let's assume the nearest even integer rule for this example.
$endgroup$
– Josh Allen
8 hours ago
1
1
$begingroup$
Do you have a specific rounding rule in mind for .5 cases? The Wikipedia page on 'Nearest integer function' (en.wikipedia.org/wiki/Nearest_integer_function) says "On most computer implementations, the selected rule is to round half-integers to the nearest even integer"
$endgroup$
– Dipayan Banerjee
8 hours ago
$begingroup$
Do you have a specific rounding rule in mind for .5 cases? The Wikipedia page on 'Nearest integer function' (en.wikipedia.org/wiki/Nearest_integer_function) says "On most computer implementations, the selected rule is to round half-integers to the nearest even integer"
$endgroup$
– Dipayan Banerjee
8 hours ago
$begingroup$
@DipayanBanerjee Yes, let's assume the nearest even integer rule for this example.
$endgroup$
– Josh Allen
8 hours ago
$begingroup$
@DipayanBanerjee Yes, let's assume the nearest even integer rule for this example.
$endgroup$
– Josh Allen
8 hours ago
add a comment
|
4 Answers
4
active
oldest
votes
$begingroup$
This is a hack of Robert Schwarz's answer, to accommodate the "round .5 to even" rule. We introduce integer variable $y$ and binary variable $z$, along with the constraints $$2y+z le x le 2y + z + 1$$ and $$x + z - 0.5 le C.$$ If $x$ is noninteger, the first constraint finds the nearest integers on either side. If the floor of $x$ is even (meaning a fraction of one half would round down), $z=0$ and we require that $x - 0.5 le C$. If the floor of $x$ is odd (meaning a fraction of one half would round up), $z=1$ and we require that $x + 0.5 le C$.
There is an ambiguity when $x$ is integer. For instance, if $x = 3$, both $(y,z)=(1,1)$ and $(y,z)=(1,0)$ satisfy the constraint. Fortunately, the solver will bail us out: if it "wants" to have $x=3$ be feasible (and if $C=3$), it will choose $(y,z)=(1,0)$, so that $x=3=C$ satisfies the second constraint.
$endgroup$
add a comment
|
$begingroup$
Assuming finite bounds on $x$, this could be modeled with disjunctions on the many cases to which $x$ can be rounded.
For example, if $x in [ 0, 2 ]$, we would have:
$$
begin{cases}
x le 0.5, & 0 le C \
0.5 le x le 1.5, & 1 le C \
1.5 le x , & 2 le C
end{cases}
$$
The disjunction itself could be modeled with auxiliary binary variables and big-$M$ constraints, for example.
Note that rounding in this context has some ambiguity, as a value of $0.5$ may be rounded either to $0$ or $1$, because strict inequalities can usually not be enforced by MIP solvers.
$endgroup$
add a comment
|
$begingroup$
As an alternative solution, I propose to add an auxiliary integral variable $y in mathbb{Z}$ that should play the role of the rounded $x$.
For your example of the inequality, I would add:
$$
begin{array}{rll}
y &le& C \
x - 0.5&le& y
end{array}
$$
$endgroup$
2
$begingroup$
This is close, but trips over the "round-to-even" rule. We can assume that $C$ is integer. Suppose that $C=3$. $x=3.5$, $y=3$ satisfies both inequalities, but $leftlceil xrightrfloor =4$.
$endgroup$
– prubin
6 hours ago
add a comment
|
$begingroup$
Assuming that the value $C + 0.5$ is valid, you could model this by simply adding the constraint
$$x leq C + 0.5$$
Otherwise, you define a constant say $epsilon = 10^{-7}$ and do
$$x leq C + 0.5 - epsilon$$
$endgroup$
add a comment
|
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "700"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Josh Allen is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2for.stackexchange.com%2fquestions%2f2790%2fmodeling-the-round-nearest-integer-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
This is a hack of Robert Schwarz's answer, to accommodate the "round .5 to even" rule. We introduce integer variable $y$ and binary variable $z$, along with the constraints $$2y+z le x le 2y + z + 1$$ and $$x + z - 0.5 le C.$$ If $x$ is noninteger, the first constraint finds the nearest integers on either side. If the floor of $x$ is even (meaning a fraction of one half would round down), $z=0$ and we require that $x - 0.5 le C$. If the floor of $x$ is odd (meaning a fraction of one half would round up), $z=1$ and we require that $x + 0.5 le C$.
There is an ambiguity when $x$ is integer. For instance, if $x = 3$, both $(y,z)=(1,1)$ and $(y,z)=(1,0)$ satisfy the constraint. Fortunately, the solver will bail us out: if it "wants" to have $x=3$ be feasible (and if $C=3$), it will choose $(y,z)=(1,0)$, so that $x=3=C$ satisfies the second constraint.
$endgroup$
add a comment
|
$begingroup$
This is a hack of Robert Schwarz's answer, to accommodate the "round .5 to even" rule. We introduce integer variable $y$ and binary variable $z$, along with the constraints $$2y+z le x le 2y + z + 1$$ and $$x + z - 0.5 le C.$$ If $x$ is noninteger, the first constraint finds the nearest integers on either side. If the floor of $x$ is even (meaning a fraction of one half would round down), $z=0$ and we require that $x - 0.5 le C$. If the floor of $x$ is odd (meaning a fraction of one half would round up), $z=1$ and we require that $x + 0.5 le C$.
There is an ambiguity when $x$ is integer. For instance, if $x = 3$, both $(y,z)=(1,1)$ and $(y,z)=(1,0)$ satisfy the constraint. Fortunately, the solver will bail us out: if it "wants" to have $x=3$ be feasible (and if $C=3$), it will choose $(y,z)=(1,0)$, so that $x=3=C$ satisfies the second constraint.
$endgroup$
add a comment
|
$begingroup$
This is a hack of Robert Schwarz's answer, to accommodate the "round .5 to even" rule. We introduce integer variable $y$ and binary variable $z$, along with the constraints $$2y+z le x le 2y + z + 1$$ and $$x + z - 0.5 le C.$$ If $x$ is noninteger, the first constraint finds the nearest integers on either side. If the floor of $x$ is even (meaning a fraction of one half would round down), $z=0$ and we require that $x - 0.5 le C$. If the floor of $x$ is odd (meaning a fraction of one half would round up), $z=1$ and we require that $x + 0.5 le C$.
There is an ambiguity when $x$ is integer. For instance, if $x = 3$, both $(y,z)=(1,1)$ and $(y,z)=(1,0)$ satisfy the constraint. Fortunately, the solver will bail us out: if it "wants" to have $x=3$ be feasible (and if $C=3$), it will choose $(y,z)=(1,0)$, so that $x=3=C$ satisfies the second constraint.
$endgroup$
This is a hack of Robert Schwarz's answer, to accommodate the "round .5 to even" rule. We introduce integer variable $y$ and binary variable $z$, along with the constraints $$2y+z le x le 2y + z + 1$$ and $$x + z - 0.5 le C.$$ If $x$ is noninteger, the first constraint finds the nearest integers on either side. If the floor of $x$ is even (meaning a fraction of one half would round down), $z=0$ and we require that $x - 0.5 le C$. If the floor of $x$ is odd (meaning a fraction of one half would round up), $z=1$ and we require that $x + 0.5 le C$.
There is an ambiguity when $x$ is integer. For instance, if $x = 3$, both $(y,z)=(1,1)$ and $(y,z)=(1,0)$ satisfy the constraint. Fortunately, the solver will bail us out: if it "wants" to have $x=3$ be feasible (and if $C=3$), it will choose $(y,z)=(1,0)$, so that $x=3=C$ satisfies the second constraint.
answered 5 hours ago
prubinprubin
5,2219 silver badges34 bronze badges
5,2219 silver badges34 bronze badges
add a comment
|
add a comment
|
$begingroup$
Assuming finite bounds on $x$, this could be modeled with disjunctions on the many cases to which $x$ can be rounded.
For example, if $x in [ 0, 2 ]$, we would have:
$$
begin{cases}
x le 0.5, & 0 le C \
0.5 le x le 1.5, & 1 le C \
1.5 le x , & 2 le C
end{cases}
$$
The disjunction itself could be modeled with auxiliary binary variables and big-$M$ constraints, for example.
Note that rounding in this context has some ambiguity, as a value of $0.5$ may be rounded either to $0$ or $1$, because strict inequalities can usually not be enforced by MIP solvers.
$endgroup$
add a comment
|
$begingroup$
Assuming finite bounds on $x$, this could be modeled with disjunctions on the many cases to which $x$ can be rounded.
For example, if $x in [ 0, 2 ]$, we would have:
$$
begin{cases}
x le 0.5, & 0 le C \
0.5 le x le 1.5, & 1 le C \
1.5 le x , & 2 le C
end{cases}
$$
The disjunction itself could be modeled with auxiliary binary variables and big-$M$ constraints, for example.
Note that rounding in this context has some ambiguity, as a value of $0.5$ may be rounded either to $0$ or $1$, because strict inequalities can usually not be enforced by MIP solvers.
$endgroup$
add a comment
|
$begingroup$
Assuming finite bounds on $x$, this could be modeled with disjunctions on the many cases to which $x$ can be rounded.
For example, if $x in [ 0, 2 ]$, we would have:
$$
begin{cases}
x le 0.5, & 0 le C \
0.5 le x le 1.5, & 1 le C \
1.5 le x , & 2 le C
end{cases}
$$
The disjunction itself could be modeled with auxiliary binary variables and big-$M$ constraints, for example.
Note that rounding in this context has some ambiguity, as a value of $0.5$ may be rounded either to $0$ or $1$, because strict inequalities can usually not be enforced by MIP solvers.
$endgroup$
Assuming finite bounds on $x$, this could be modeled with disjunctions on the many cases to which $x$ can be rounded.
For example, if $x in [ 0, 2 ]$, we would have:
$$
begin{cases}
x le 0.5, & 0 le C \
0.5 le x le 1.5, & 1 le C \
1.5 le x , & 2 le C
end{cases}
$$
The disjunction itself could be modeled with auxiliary binary variables and big-$M$ constraints, for example.
Note that rounding in this context has some ambiguity, as a value of $0.5$ may be rounded either to $0$ or $1$, because strict inequalities can usually not be enforced by MIP solvers.
answered 8 hours ago
Robert SchwarzRobert Schwarz
1,0753 silver badges14 bronze badges
1,0753 silver badges14 bronze badges
add a comment
|
add a comment
|
$begingroup$
As an alternative solution, I propose to add an auxiliary integral variable $y in mathbb{Z}$ that should play the role of the rounded $x$.
For your example of the inequality, I would add:
$$
begin{array}{rll}
y &le& C \
x - 0.5&le& y
end{array}
$$
$endgroup$
2
$begingroup$
This is close, but trips over the "round-to-even" rule. We can assume that $C$ is integer. Suppose that $C=3$. $x=3.5$, $y=3$ satisfies both inequalities, but $leftlceil xrightrfloor =4$.
$endgroup$
– prubin
6 hours ago
add a comment
|
$begingroup$
As an alternative solution, I propose to add an auxiliary integral variable $y in mathbb{Z}$ that should play the role of the rounded $x$.
For your example of the inequality, I would add:
$$
begin{array}{rll}
y &le& C \
x - 0.5&le& y
end{array}
$$
$endgroup$
2
$begingroup$
This is close, but trips over the "round-to-even" rule. We can assume that $C$ is integer. Suppose that $C=3$. $x=3.5$, $y=3$ satisfies both inequalities, but $leftlceil xrightrfloor =4$.
$endgroup$
– prubin
6 hours ago
add a comment
|
$begingroup$
As an alternative solution, I propose to add an auxiliary integral variable $y in mathbb{Z}$ that should play the role of the rounded $x$.
For your example of the inequality, I would add:
$$
begin{array}{rll}
y &le& C \
x - 0.5&le& y
end{array}
$$
$endgroup$
As an alternative solution, I propose to add an auxiliary integral variable $y in mathbb{Z}$ that should play the role of the rounded $x$.
For your example of the inequality, I would add:
$$
begin{array}{rll}
y &le& C \
x - 0.5&le& y
end{array}
$$
answered 8 hours ago
Robert SchwarzRobert Schwarz
1,0753 silver badges14 bronze badges
1,0753 silver badges14 bronze badges
2
$begingroup$
This is close, but trips over the "round-to-even" rule. We can assume that $C$ is integer. Suppose that $C=3$. $x=3.5$, $y=3$ satisfies both inequalities, but $leftlceil xrightrfloor =4$.
$endgroup$
– prubin
6 hours ago
add a comment
|
2
$begingroup$
This is close, but trips over the "round-to-even" rule. We can assume that $C$ is integer. Suppose that $C=3$. $x=3.5$, $y=3$ satisfies both inequalities, but $leftlceil xrightrfloor =4$.
$endgroup$
– prubin
6 hours ago
2
2
$begingroup$
This is close, but trips over the "round-to-even" rule. We can assume that $C$ is integer. Suppose that $C=3$. $x=3.5$, $y=3$ satisfies both inequalities, but $leftlceil xrightrfloor =4$.
$endgroup$
– prubin
6 hours ago
$begingroup$
This is close, but trips over the "round-to-even" rule. We can assume that $C$ is integer. Suppose that $C=3$. $x=3.5$, $y=3$ satisfies both inequalities, but $leftlceil xrightrfloor =4$.
$endgroup$
– prubin
6 hours ago
add a comment
|
$begingroup$
Assuming that the value $C + 0.5$ is valid, you could model this by simply adding the constraint
$$x leq C + 0.5$$
Otherwise, you define a constant say $epsilon = 10^{-7}$ and do
$$x leq C + 0.5 - epsilon$$
$endgroup$
add a comment
|
$begingroup$
Assuming that the value $C + 0.5$ is valid, you could model this by simply adding the constraint
$$x leq C + 0.5$$
Otherwise, you define a constant say $epsilon = 10^{-7}$ and do
$$x leq C + 0.5 - epsilon$$
$endgroup$
add a comment
|
$begingroup$
Assuming that the value $C + 0.5$ is valid, you could model this by simply adding the constraint
$$x leq C + 0.5$$
Otherwise, you define a constant say $epsilon = 10^{-7}$ and do
$$x leq C + 0.5 - epsilon$$
$endgroup$
Assuming that the value $C + 0.5$ is valid, you could model this by simply adding the constraint
$$x leq C + 0.5$$
Otherwise, you define a constant say $epsilon = 10^{-7}$ and do
$$x leq C + 0.5 - epsilon$$
edited 6 hours ago
answered 6 hours ago
Claudio ContardoClaudio Contardo
8262 silver badges9 bronze badges
8262 silver badges9 bronze badges
add a comment
|
add a comment
|
Josh Allen is a new contributor. Be nice, and check out our Code of Conduct.
Josh Allen is a new contributor. Be nice, and check out our Code of Conduct.
Josh Allen is a new contributor. Be nice, and check out our Code of Conduct.
Josh Allen is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Operations Research Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2for.stackexchange.com%2fquestions%2f2790%2fmodeling-the-round-nearest-integer-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
Do you have a specific rounding rule in mind for .5 cases? The Wikipedia page on 'Nearest integer function' (en.wikipedia.org/wiki/Nearest_integer_function) says "On most computer implementations, the selected rule is to round half-integers to the nearest even integer"
$endgroup$
– Dipayan Banerjee
8 hours ago
$begingroup$
@DipayanBanerjee Yes, let's assume the nearest even integer rule for this example.
$endgroup$
– Josh Allen
8 hours ago