MIP: Adding a Variable to Indicate Equality












0















I am trying to build a MIP model in the OR Tools Python API. I have two expressions x and y and want to make a variable b that is equal to 1 when x == y and 0 otherwise. What I've tried doing so far is adding the constraint that -M(1 - b) <= x - y <= M(1 - b) for some big value of M, which forces b to be 0 if x != y. Where I am stuck is adding a constraint that forces b to be 1 if x == y. I think I would want something such as x - y >= 1 - b or y - x >= 1 - b, but I don't know how to logically combine constraints like this. Any suggestions on how to do this? Or for some totally different approach?










share|improve this question























  • Important to know about the bools: False == 0 && True == 1

    – JacobIRR
    Jan 18 at 21:49













  • Do you mean that I should just say b = x == y? I don't think I am able to add a constraint in that form to the model.

    – jacob
    Jan 18 at 23:17











  • I mean that the boolean value True is literally equal to the number 1 and the boolean value False is literally equal to the number 0.

    – JacobIRR
    Jan 18 at 23:19
















0















I am trying to build a MIP model in the OR Tools Python API. I have two expressions x and y and want to make a variable b that is equal to 1 when x == y and 0 otherwise. What I've tried doing so far is adding the constraint that -M(1 - b) <= x - y <= M(1 - b) for some big value of M, which forces b to be 0 if x != y. Where I am stuck is adding a constraint that forces b to be 1 if x == y. I think I would want something such as x - y >= 1 - b or y - x >= 1 - b, but I don't know how to logically combine constraints like this. Any suggestions on how to do this? Or for some totally different approach?










share|improve this question























  • Important to know about the bools: False == 0 && True == 1

    – JacobIRR
    Jan 18 at 21:49













  • Do you mean that I should just say b = x == y? I don't think I am able to add a constraint in that form to the model.

    – jacob
    Jan 18 at 23:17











  • I mean that the boolean value True is literally equal to the number 1 and the boolean value False is literally equal to the number 0.

    – JacobIRR
    Jan 18 at 23:19














0












0








0








I am trying to build a MIP model in the OR Tools Python API. I have two expressions x and y and want to make a variable b that is equal to 1 when x == y and 0 otherwise. What I've tried doing so far is adding the constraint that -M(1 - b) <= x - y <= M(1 - b) for some big value of M, which forces b to be 0 if x != y. Where I am stuck is adding a constraint that forces b to be 1 if x == y. I think I would want something such as x - y >= 1 - b or y - x >= 1 - b, but I don't know how to logically combine constraints like this. Any suggestions on how to do this? Or for some totally different approach?










share|improve this question














I am trying to build a MIP model in the OR Tools Python API. I have two expressions x and y and want to make a variable b that is equal to 1 when x == y and 0 otherwise. What I've tried doing so far is adding the constraint that -M(1 - b) <= x - y <= M(1 - b) for some big value of M, which forces b to be 0 if x != y. Where I am stuck is adding a constraint that forces b to be 1 if x == y. I think I would want something such as x - y >= 1 - b or y - x >= 1 - b, but I don't know how to logically combine constraints like this. Any suggestions on how to do this? Or for some totally different approach?







python or-tools mixed-integer-programming






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 18 at 21:47









jacobjacob

362




362













  • Important to know about the bools: False == 0 && True == 1

    – JacobIRR
    Jan 18 at 21:49













  • Do you mean that I should just say b = x == y? I don't think I am able to add a constraint in that form to the model.

    – jacob
    Jan 18 at 23:17











  • I mean that the boolean value True is literally equal to the number 1 and the boolean value False is literally equal to the number 0.

    – JacobIRR
    Jan 18 at 23:19



















  • Important to know about the bools: False == 0 && True == 1

    – JacobIRR
    Jan 18 at 21:49













  • Do you mean that I should just say b = x == y? I don't think I am able to add a constraint in that form to the model.

    – jacob
    Jan 18 at 23:17











  • I mean that the boolean value True is literally equal to the number 1 and the boolean value False is literally equal to the number 0.

    – JacobIRR
    Jan 18 at 23:19

















Important to know about the bools: False == 0 && True == 1

– JacobIRR
Jan 18 at 21:49







Important to know about the bools: False == 0 && True == 1

– JacobIRR
Jan 18 at 21:49















Do you mean that I should just say b = x == y? I don't think I am able to add a constraint in that form to the model.

– jacob
Jan 18 at 23:17





Do you mean that I should just say b = x == y? I don't think I am able to add a constraint in that form to the model.

– jacob
Jan 18 at 23:17













I mean that the boolean value True is literally equal to the number 1 and the boolean value False is literally equal to the number 0.

– JacobIRR
Jan 18 at 23:19





I mean that the boolean value True is literally equal to the number 1 and the boolean value False is literally equal to the number 0.

– JacobIRR
Jan 18 at 23:19












2 Answers
2






active

oldest

votes


















0














I think the following expressions would work for you:



b <= x - y + 1
b <= y - x + 1
b >= 1-x + 1-y - 1
b >= y + x - 1





share|improve this answer
























  • Sorry, I should have mentioned that x and y are integer variables and not necessarily binary, and I think this only works when they are binary.

    – jacob
    Jan 19 at 18:17



















0














Please note that depending on the nature of the model, the CP-SAT solver could prove competitive. And it provides reification and half-reification natively.



Please have a look at




  • [Specific answer] https://github.com/google/or-tools/blob/master/ortools/sat/doc/channeling.md

  • [Introduction] https://developers.google.com/optimization/cp/cp_solver#cp-sat_example

  • [CP-SAT recipes] https://github.com/google/or-tools/blob/master/ortools/sat/doc/index.md






share|improve this answer


























  • The reason I am using OR-tools is to be able to plug my model into lots of different solvers in order to compare them. Does CP-SAT also support this?

    – jacob
    Jan 19 at 18:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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/3.0/"u003ecc by-sa 3.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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54261865%2fmip-adding-a-variable-to-indicate-equality%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I think the following expressions would work for you:



b <= x - y + 1
b <= y - x + 1
b >= 1-x + 1-y - 1
b >= y + x - 1





share|improve this answer
























  • Sorry, I should have mentioned that x and y are integer variables and not necessarily binary, and I think this only works when they are binary.

    – jacob
    Jan 19 at 18:17
















0














I think the following expressions would work for you:



b <= x - y + 1
b <= y - x + 1
b >= 1-x + 1-y - 1
b >= y + x - 1





share|improve this answer
























  • Sorry, I should have mentioned that x and y are integer variables and not necessarily binary, and I think this only works when they are binary.

    – jacob
    Jan 19 at 18:17














0












0








0







I think the following expressions would work for you:



b <= x - y + 1
b <= y - x + 1
b >= 1-x + 1-y - 1
b >= y + x - 1





share|improve this answer













I think the following expressions would work for you:



b <= x - y + 1
b <= y - x + 1
b >= 1-x + 1-y - 1
b >= y + x - 1






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 19 at 8:31









Magnus ÅhlanderMagnus Åhlander

4612




4612













  • Sorry, I should have mentioned that x and y are integer variables and not necessarily binary, and I think this only works when they are binary.

    – jacob
    Jan 19 at 18:17



















  • Sorry, I should have mentioned that x and y are integer variables and not necessarily binary, and I think this only works when they are binary.

    – jacob
    Jan 19 at 18:17

















Sorry, I should have mentioned that x and y are integer variables and not necessarily binary, and I think this only works when they are binary.

– jacob
Jan 19 at 18:17





Sorry, I should have mentioned that x and y are integer variables and not necessarily binary, and I think this only works when they are binary.

– jacob
Jan 19 at 18:17













0














Please note that depending on the nature of the model, the CP-SAT solver could prove competitive. And it provides reification and half-reification natively.



Please have a look at




  • [Specific answer] https://github.com/google/or-tools/blob/master/ortools/sat/doc/channeling.md

  • [Introduction] https://developers.google.com/optimization/cp/cp_solver#cp-sat_example

  • [CP-SAT recipes] https://github.com/google/or-tools/blob/master/ortools/sat/doc/index.md






share|improve this answer


























  • The reason I am using OR-tools is to be able to plug my model into lots of different solvers in order to compare them. Does CP-SAT also support this?

    – jacob
    Jan 19 at 18:19
















0














Please note that depending on the nature of the model, the CP-SAT solver could prove competitive. And it provides reification and half-reification natively.



Please have a look at




  • [Specific answer] https://github.com/google/or-tools/blob/master/ortools/sat/doc/channeling.md

  • [Introduction] https://developers.google.com/optimization/cp/cp_solver#cp-sat_example

  • [CP-SAT recipes] https://github.com/google/or-tools/blob/master/ortools/sat/doc/index.md






share|improve this answer


























  • The reason I am using OR-tools is to be able to plug my model into lots of different solvers in order to compare them. Does CP-SAT also support this?

    – jacob
    Jan 19 at 18:19














0












0








0







Please note that depending on the nature of the model, the CP-SAT solver could prove competitive. And it provides reification and half-reification natively.



Please have a look at




  • [Specific answer] https://github.com/google/or-tools/blob/master/ortools/sat/doc/channeling.md

  • [Introduction] https://developers.google.com/optimization/cp/cp_solver#cp-sat_example

  • [CP-SAT recipes] https://github.com/google/or-tools/blob/master/ortools/sat/doc/index.md






share|improve this answer















Please note that depending on the nature of the model, the CP-SAT solver could prove competitive. And it provides reification and half-reification natively.



Please have a look at




  • [Specific answer] https://github.com/google/or-tools/blob/master/ortools/sat/doc/channeling.md

  • [Introduction] https://developers.google.com/optimization/cp/cp_solver#cp-sat_example

  • [CP-SAT recipes] https://github.com/google/or-tools/blob/master/ortools/sat/doc/index.md







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 19 at 14:38

























answered Jan 19 at 14:19









Laurent PerronLaurent Perron

2266




2266













  • The reason I am using OR-tools is to be able to plug my model into lots of different solvers in order to compare them. Does CP-SAT also support this?

    – jacob
    Jan 19 at 18:19



















  • The reason I am using OR-tools is to be able to plug my model into lots of different solvers in order to compare them. Does CP-SAT also support this?

    – jacob
    Jan 19 at 18:19

















The reason I am using OR-tools is to be able to plug my model into lots of different solvers in order to compare them. Does CP-SAT also support this?

– jacob
Jan 19 at 18:19





The reason I am using OR-tools is to be able to plug my model into lots of different solvers in order to compare them. Does CP-SAT also support this?

– jacob
Jan 19 at 18:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54261865%2fmip-adding-a-variable-to-indicate-equality%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre