# Minimize function with logical arguments

How can I minimize or maximize a function (even numerically) with logical arguments, taking only 0s and 1s, also with constraints? E.g.

NMinimize[{x1 * 24 + x2*51 + x1*x2 *36, 12 x1+36 x2<44}, {x1,x2}]


The example of course is stupid one, but question as a matter of principle, when you have many arguments.

• Min[Pick[Outer[Function[{x1, x2}, x1*24 + x2*51 + x1*x2*36], {0, 1}, {0, 1}], Outer[Function[{x1, x2}, 12 x1 + 36 x2 < 44], {0, 1}, {0, 1}]]]? Commented Nov 3, 2015 at 8:26
• I need something general, true for many arguments. Commented Nov 3, 2015 at 8:27
• Min[Pick[Function[{x1, x2}, x1*24 + x2*51 + x1*x2*36] @@@ Tuples[{0, 1}, 2], Function[{x1, x2}, 12 x1 + 36 x2 < 44] @@@ Tuples[{0, 1}, 2]]]. Adjust the second argument of Tuples[] as seen fit. Commented Nov 3, 2015 at 8:30
• Great! I was just thinking there should be some special ways for categorial optimization. Commented Nov 3, 2015 at 8:40
• How about the following? NMinimize[{x1*24 + x2*51 + x1*x2*36, 12 x1 + 36 x2 < 44 && And @@ (0 <= # <= 1 && # \[Element] Integers & /@ {x1, x2})}, {x1, x2}] Commented Nov 3, 2015 at 15:39

You could use $v^2 = v$ as a constraint. For your example:

NMinimize[
{
x1*24 + x2*51 + x1*x2*36,
12 x1+36 x2<44 && x1^2==x1 && x2^2==x2},
{x1,x2},
Integers
]


{0., {x1 -> 0, x2 -> 0}}

• Alternatively, add constraint 0<=x1<=1 and similar for x2. And keep the Integers domain specification. Not sure which of these will perform better in general so might want to experiment. Commented Dec 13, 2017 at 23:31