Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
2  
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). –  Szabolcs Sep 11 '13 at 19:19
    
@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. –  Charles Sep 11 '13 at 19:22
1  
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. –  Daniel Lichtblau Sep 11 '13 at 23:52
    
@DanielLichtblau: f is linear in any given variable, but of degree $n$ overall. –  Charles Sep 13 '13 at 21:23
add comment

1 Answer

up vote 1 down vote accepted

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}

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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