% get_perm( L, zz, vp, cur, next, col, el) - find best permutation
%
function [best, i_best] = get_perm( L, zz, vp, cur, next, col, el)

global DEBUG;
global HUGE_VAL;
global combos;
global perms;
global MP;
global K;

if( DEBUG>2) disp('get_perm:'), cur, next, end;

s_cur = combos(:,cur);		% actual state indices
s_cur = s_cur( perms(:,vp));
s_next = combos(:,next);	%  for cur and next combinations

i_best = 1;
best = HUGE_VAL;
if( L > 1)
  best = HUGE_VAL * ones(MP,1);	% need to return all choices if L > 1
  i_best = [1:MP]';
end

if( DEBUG>2) s_cur, s_next, end;

for i = 1:MP

  s_tmp = s_next(perms(:,i));

  if( DEBUG>2) s_tmp, end;

  b = 0;
  for j = 1:K
    cost = get_cost( zz, s_cur(j), s_tmp(j), j, el);
    if( cost == HUGE_VAL)
	b = HUGE_VAL;
	break;
    end
    b = b + cost;
  end

  if( DEBUG>2) b, end;

  if( L > 1)
    best(i) = b;
  elseif( b < best)
    i_best = i;
    best = b;
  end

end
