0
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ 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}]]]? $\endgroup$ Nov 3, 2015 at 8:26
  • $\begingroup$ I need something general, true for many arguments. $\endgroup$
    – Al Guy
    Nov 3, 2015 at 8:27
  • 1
    $\begingroup$ 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. $\endgroup$ Nov 3, 2015 at 8:30
  • $\begingroup$ Great! I was just thinking there should be some special ways for categorial optimization. $\endgroup$
    – Al Guy
    Nov 3, 2015 at 8:40
  • 1
    $\begingroup$ 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}] $\endgroup$
    – kirma
    Nov 3, 2015 at 15:39

1 Answer 1

1
$\begingroup$

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}}

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$ Dec 13, 2017 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.