Search code examples
algorithmrmsminimization

select a group of pairs in order to minimize rms of group


Simplified problem

I have ~40 resistors (all the same value +-5%) and I need to select 12 of them so that they are as similar as possible.

Solution: I list them in order and take the 12 consecutive with the smallest RMS.

The actual problem

I have ~40 resistors (all the same value +-5%) and I have to choose 12 pairs of them so that the resistance of the pairs is as similar as possible.

Notes

The resistance of the pair (R1,R2) is R1+R2. I do not really care about the programming language, but let's say that I'm looking for a solution in C++ or Python, the two languages I'm most familiar with.


Solution

  • This gives reasonably good results (in MATLAB)

    a = ones(40,1) + rand(40,1)*0.1-0.05; % The resistors
    vec = zeros(40,2);        % Initialize matrix
    indices = zeros(40,2);    % Initialize matrix
    a = sort(a);              % Sort vector of resistors
    for ii = 1:length(a)
      vec(ii,:) = [a(ii) a(ii)];    % Assign resistor values to row ii of vec
      indices(ii,:) = [ii,ii];      % Corresponding resistor number (index)
      for jj = 1:length(a)
        if sum(abs((a(ii)+a(jj))-2*mean(a))) < abs(sum(vec(ii,:))-2*mean(a))
          vec(ii,:) =  [a(ii) a(jj)];    % Check if the new set is better than the
          indices(ii,:) = [ii, jj];      % previous, and update vec and indices if true.
        end
      end
    end
    
    [x, idx] = sort(sum(vec')');   % Sort the sum of the pairs 
    final_list = indices(idx);     % The indices of the sorted pairs
    

    This is the result when I plot it:

    enter image description here