FindMaximum under binary constrains

I'm trying to find a maximum for a function whose variables have binary values (either -1 or 1). The clumsy code for that constraint I use is shown below. There must be a more compact code, and I would be grateful for any suggestion.

That said, the sum works (1st expression given below), but the product doesn't (2nd expression). What am I doing wrong? Any clue how to fix all this?

I would like to obtain Max of f[x, y , z, ...] where the arguments take on only binary values.

FindMaximum[
{x + y,
x >= -1 && x <= 1 && (x ∈ NegativeIntegers ∨ x ∈ PositiveIntegers),
y >= -1 && y <= 1 && (y ∈ NegativeIntegers ∨ y ∈ PositiveIntegers)},
{x, y}]

{2., {x -> 1, y -> 1}}

FindMaximum[
{x * y,
x >= -1 && x <= 1 && ((x ∈ NegativeIntegers) ∨ (x ∈ PositiveIntegers)),
y >= -1 && y <= 1 && ((y ∈ NegativeIntegers) ∨ (y ∈ PositiveIntegers))},
{x, y}]


Error := Constraints in ({x ∈ Z, y ∈ Z, x > 0, y > 0, x >= -1, y >= -1, x <= 1, y <= 1}) are not all equality or inequality constraints.

Here is a compact code that works for your two examples.

FindMaximum[{x + y, x^2 == 1, y^2 == 1}, {x, y}]
FindMaximum[{x * y, x^2 == 1, y^2 == 1}, {x, y}]

(* {2., {x -> 1., y -> 1.}} *)
(* {1., {x -> 1., y -> 1.}} *)


We note that there are two solutions that maximize the product, but only one is found. Two other ways are

FindMaximum[{x + y, Abs[x] == 1, Abs[y] == 1}, {x, y}]
FindMaximum[{x*y, Abs[x] == 1, Abs[y] == 1}, {x, y}]


and

Maximize[{x + y, Abs[x] == 1, Abs[y] == 1}, {x, y}]
Maximize[{x*y, Abs[x] == 1, Abs[y] == 1}, {x, y}]


In the above FindMaximum and Maximize evaluate to the same expression. Now suppose we enumerate the legal values for $$x$$ and $$y$$ as a constraint to Maximize:

Maximize[{x + y, (x == 1) || (x == -1), (y == 1) || (y == -1)}, {x, y}]
Maximize[{x*y, (x == 1) || (x == -1), (y == 1) || (y == -1)}, {x, y}]

(* {2., {x -> 1., y -> 1.}} *)
(* {1., {x -> 1., y -> 1.}} *)


That works well enough and we might expect FindMaximum to produce the same result, which it does for the product $$x * y$$ but not for the sum $$x+y$$.

FindMaximum[{x + y,
(x == 1) || (x == -1),
(y == 1) || (y == -1)}, {x, y}]  (*  {-2., {x -> -1., y -> -1.}}  WRONG  *)
FindMaximum[{x*y,
(x == 1) || (x == -1),
(y == 1) || (y == -1)}, {x, y}]  (* {1., {x -> 1., y -> 1.}} *)

• "there are two solutions that maximize the product, but only one is found." Yes, that remains to be solved. The solution (for Max[x * y]) (* {1., {x -> -1., y -> -1.}} *) should also be generated.
– Fvwm
Sep 21, 2020 at 0:34
• Maximize [{x * y, Abs[x] == 1, Abs[y] == 1}, {x, y}] gives (* {1., {x -> -1., y -> -1.}} * ) but it does not give (* {1., {x -> 1., y -> 1.}} *)
– Fvwm
Sep 21, 2020 at 0:42

Maximize and related functions make no attempt to find all maxima if there is more than one. If you want all of them identified you will need to use a different method.

Clear["Global*"]

max[func_, vars_] :=
Module[{table, m},
table = Table[{vars, func @@ vars} // Flatten,
Evaluate[Sequence @@ ({#, {-1, 1}} & /@ vars)]] //
Flatten[#, Length[vars] - 1] &;
m = Max@table[[All, -1]];
{#[[-1]], Thread[vars -> Most[#]]} & /@
Select[table, #[[-1]] == m &]]

max[Plus, {x, y}]

(* {{2, {x -> 1, y -> 1}}} *)

max[Plus, {x, y, z}]

(* {{3, {x -> 1, y -> 1, z -> 1}}} *)

max[Times, {x, y}]

(* {{1, {x -> -1, y -> -1}}, {1, {x -> 1, y -> 1}}} *)

max[Times, {x, y, z}]

(* {{1, {x -> -1, y -> -1, z -> 1}}, {1, {x -> -1, y -> 1,
z -> -1}}, {1, {x -> 1, y -> -1, z -> -1}}, {1, {x -> 1, y -> 1, z -> 1}}} *)
`