I have to minimize a bunch of functions of n variables that can take values from an integer range.
The functions have the general form:
f[{s1_,... sn_}]:= Kxy KroneckerDelta[sx,sy] + Kwz KroneckerDelta[sw,sz] +/- ..
Where the Kmn are also integers.
As an example,
f[{s1_, s2_, s3_, s4_, s5_}:= KroneckerDelta[s1, s2] - KroneckerDelta[s1, s4] +
KroneckerDelta[s1, s5] + KroneckerDelta[s3, s4] +
KroneckerDelta[s3, s5] + KroneckerDelta[s4, s5];
Where the si_ must be in Range[3].
I can bruteforce easily, for example:
rulez = Table[s[i] -> #[[i]], {i, 5}] & /@ Tuples[Range[3], 5];
k1 = f[Table[s[i], {i, 5}]] /. rulez;
{Min[k1], Tuples[Range[3], 5][[#]] & /@ Position[k1, Min[k1]]}
(*
->
{-1,{{{1, 2, 2, 1, 3}}, {{1, 2, 3, 1, 2}}, {{1, 3, 2, 1, 3}}, {{1, 3, 3, 1, 2}},
{{2, 1, 1, 2, 3}}, {{2, 1, 3, 2, 1}}, {{2, 3, 1, 2, 3}}, {{2, 3, 3, 2, 1}},
{{3, 1, 1, 3, 2}}, {{3, 1, 2, 3, 1}}, {{3, 2, 1, 3, 2}}, {{3, 2, 2, 3, 1}}}}
*)
Obviously, that seems to take forever for large sets of variables and larger value ranges.
I tried Minimize[ ]
, but get results that don't satisfy the conditions (!):
Minimize[{f[Table[s[i], {i, 5}]], And @@ Table[1 <= s[i] <= 3, {i, 5}]},
Table[s[i], {i, 5}], Integers]
(*
-> {2, {s[1] -> 0, s[2] -> 0, s[3] -> 0, s[4] -> 0, s[5] -> 0}}
*)
Or in other cases, it just fails:
g[{s1_, s2_, s3_, s4_, s5_}]:= KroneckerDelta[s1, s3] - KroneckerDelta[s1, s4] +
KroneckerDelta[s1, s5] + KroneckerDelta[s3, s4] +
KroneckerDelta[s3, s5] + KroneckerDelta[s4, s5];
Minimize[{g[Table[s[i], {i, 5}]], And @@ Table[1 <= s[i] <= 3, {i, 5}]},
Table[s[i], {i, 5}], Integers]
(*
->
During evaluation of In[168]:= Minimize::infeas: There are no values of
{s[1],s[2],s[3],s[4],s[5]} for which the constraints 1<=s[1]<=3&&1<=s[2]<=3&&
1<=s[3]<=3&&1<=s[4]<=3&&1<=s[5]<=3 are satisfied and the objective function
KroneckerDelta[s[1],s[3]]-KroneckerDelta[s[1],s[4]]+KroneckerDelta[s[1],s[5]]+
KroneckerDelta[s[3],s[4]]+KroneckerDelta[s[3],s[5]]+KroneckerDelta[s[4],s[5]]
is real valued. >>
Out[169]= {\[Infinity], s[1]->Indeterminate, s[2]->Indeterminate,
s[3]->Indeterminate, s[4]->Indeterminate,
s[5]->Indeterminate}}
*)
So the question is twofold:
Why does Minimize[ ]
fail?, and What is the better way to tackle this kind of problems with mathematica?
Edit
Just to emphasize, the first question is:
Why does Minimize[ ] fail?
Not that the other part is less important, but I am trying to learn when to invest my time in lurking with Minimize[ ]
, and when I shouldn't.