2
$\begingroup$

I have a set $S=\{s_1,\ldots,s_k\}$ and would like to maximize a fixed expression over permutations of $S$: $\max f(x_1,\ldots,x_k)$ with $x_i\in S$ and $x_i=x_j\Rightarrow i=j$.

Is there a convenient way to do this? As an example, with $S=\{1,2,3,4\}$ I can write

Maximize[
 {f[a,b,c,d],
  a != b && a != c && a != d && b != c && b != d && c != d &&
  0 < a < 5 && 0 < b < 5 && 0 < c < 5 && 0 < d < 5},
{a, b, c, d}, Integers]

but obvious this scales poorly.

You can assume $S\subset\mathbb{Z}$ if it helps. Most of the time I'm just using initial segments of the positive integers, as in my example.

$\endgroup$
4
  • 2
    $\begingroup$ Have you seen this? These problems tend to be computationally hard when stated in a general way, as you did. There's not fast method that work in all cases, but there may be one for a specific case. If k is small enough you can brute force it (Permutations). $\endgroup$
    – Szabolcs
    Sep 11, 2013 at 19:19
  • $\begingroup$ @Szabolcs: I have a reasonably fast brute force program in another language, but I was hoping that Mathematica would be able to get by without fully evaluating everything by symbolic simplification. I don't think it will be competitive at brute force. Thanks for the link, I'll check it out. $\endgroup$
    – Charles
    Sep 11, 2013 at 19:22
  • 1
    $\begingroup$ If f is linear then it can be done using integer linear programming, and Minimize will use this under the hood if it is properly set up. I'd go with an nxn matrix of variables, all in range [0,1], with row sums constrained to = 1, col sums <= 1, and a 1 in row i, col j meaning the ith variable is the jth set element. Now write f[vars] to reflect that. $\endgroup$ Sep 11, 2013 at 23:52
  • $\begingroup$ @DanielLichtblau: f is linear in any given variable, but of degree $n$ overall. $\endgroup$
    – Charles
    Sep 13, 2013 at 21:23

1 Answer 1

1
$\begingroup$

Well, I would say that brute force is definitely faster. You just have to input your conditions in the right way.

The following code shows your example with even using Reduce to get a better form of the conditions:

 f[x_] := Plus @@ (#^2 & /@ x );

 AbsoluteTiming[
 conditions = {Reduce[
     a != b && a != c && a != d && b != c && b != d && c != d && 
      0 < a < 5 && 0 < b < 5 && 0 < c < 5 && 0 < d < 5, {a, b, c, d}, 
     Integers] // ToRules};
 templist = f[{a, b, c, d}] /. conditions;
 max = Max[templist];
 pos = Position[templist, max, 1] // First;
 argmax = {a, b, c, d} /. conditions[[Sequence @@ pos]];
 ]

output:

{0.006307, Null}

Though this code is far from being optimized (you should for example replace the Reduce statement with some Permutations construct), it is way faster than your example code:

f[x_] := Plus @@ (#^2 & /@ x );

AbsoluteTiming[
 Maximize[{f[{a, b, c, d}], 
   a != b && a != c && a != d && b != c && b != d && c != d && 
    0 < a < 5 && 0 < b < 5 && 0 < c < 5 && 0 < d < 5}, {a, b, c, d}, 
  Integers]]

output:

{0.283801, Null}

$\endgroup$

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.