Class: SameSame::RockAlgorithm

Inherits:
Object
  • Object
show all
Defined in:
lib/same_same/rock_algorithm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ RockAlgorithm

Returns a new instance of RockAlgorithm.



11
12
13
14
15
16
17
18
19
# File 'lib/same_same/rock_algorithm.rb', line 11

def initialize( attrs = {} )
  self.datapoints         = attrs.fetch( :datapoints )
  self.similarity_measure = attrs[ :similarity_measure ] || JaquardCoefficient.new
  self.k                  = attrs.fetch(:k)
  self.th                 = attrs.fetch(:th)

  self.similarity_matrix  = SimilarityMatrix.new( similarity_measure, datapoints.map {|d| d.data} )
  self.link_matrix        = LinkMatrix.new( datapoints: datapoints, similarity_matrix: similarity_matrix, th: th)
end

Instance Attribute Details

#datapointsObject

Returns the value of attribute datapoints.



9
10
11
# File 'lib/same_same/rock_algorithm.rb', line 9

def datapoints
  @datapoints
end

#kObject

Returns the value of attribute k.



9
10
11
# File 'lib/same_same/rock_algorithm.rb', line 9

def k
  @k
end

Returns the value of attribute link_matrix.



9
10
11
# File 'lib/same_same/rock_algorithm.rb', line 9

def link_matrix
  @link_matrix
end

#similarity_matrixObject

Returns the value of attribute similarity_matrix.



9
10
11
# File 'lib/same_same/rock_algorithm.rb', line 9

def similarity_matrix
  @similarity_matrix
end

#similarity_measureObject

Returns the value of attribute similarity_measure.



9
10
11
# File 'lib/same_same/rock_algorithm.rb', line 9

def similarity_measure
  @similarity_measure
end

#thObject

Returns the value of attribute th.



9
10
11
# File 'lib/same_same/rock_algorithm.rb', line 9

def th
  @th
end

Instance Method Details

#clusterObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/same_same/rock_algorithm.rb', line 21

def cluster
  Dendrogram.new( "Goodness" ).tap do |dnd|
    initial_clusters = one_point_per_cluster
    g = Float::INFINITY
    dnd.add_level(g.to_s, initial_clusters)
    goodness = MergeGoodnessMeasure.new( th )

    rock_clusters = RockClusters.new(
      link_matrix:      link_matrix,
      clusters:         initial_clusters,
      goodness_measure: goodness)

    number_of_clusters = rock_clusters.size
    while number_of_clusters > k do
      number_of_clusters_before_merge = number_of_clusters
      g = rock_clusters.merge_best_candidates
      number_of_clusters = rock_clusters.size
      
      # finish if there are no linked clusters to merge
      break if number_of_clusters == number_of_clusters_before_merge

      dnd.add_level(g.to_s, rock_clusters.clusters)
    end
  end
end

#one_point_per_clusterObject



47
48
49
# File 'lib/same_same/rock_algorithm.rb', line 47

def one_point_per_cluster
  datapoints.map {|point| Cluster.new([point])}
end