If you want the best solution, you need to evaluate all permutations.
Often, a good enough solution is sufficient.
If you can define a "good enough" criterion, you can define a break condition to stop searching.
E.g. (pseudo code):
iteration = MaxIterations;
best = none;
while (iteration-- > 0)
curr = GetNextPermutation();
best = Min(best, curr)
if (AcceptedLimit(curr)) break;
You have to design (not implement) first the
function, and the
Once you have an idea on these, you may talk about implementing these.