Module: Music::Performance::Optimization

Defined in:
lib/music-performance/util/optimization.rb

Class Method Summary collapse

Class Method Details

.linking(unlinked, untargeted) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/music-performance/util/optimization.rb', line 5

def self.linking unlinked, untargeted
  n = [unlinked.size, untargeted.size].min
  
  bestsol = nil
  bestscore = Float::INFINITY
  unlinked.combination(n).each do |comb|
    untargeted.permutation(n).each do |perm|
      score = 0
      n.times do |i|
        score += perm[i].diff(comb[i]).abs
      end
      
      if score < bestscore
        bestsol = [ comb, perm ]
        bestscore = score
      end
    end
  end
  
  solution = {}
  n.times do |i|
    solution[ bestsol[0][i] ] = bestsol[1][i]
  end
  return solution
end