# 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}]]]? – J. M.'s ennui Nov 3 '15 at 8:26
• I need something general, true for many arguments. – Al Guy Nov 3 '15 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. – J. M.'s ennui Nov 3 '15 at 8:30
• Great! I was just thinking there should be some special ways for categorial optimization. – Al Guy Nov 3 '15 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}] – kirma Nov 3 '15 at 15:39

## 1 Answer

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. – Daniel Lichtblau Dec 13 '17 at 23:31