Class: Carmenere::Algorithm
- Inherits:
-
Object
- Object
- Carmenere::Algorithm
- Defined in:
- lib/carmenere/algorithm.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#distance_matrix ⇒ Object
readonly
Returns the value of attribute distance_matrix.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Instance Method Summary collapse
-
#initialize(k, nodes) ⇒ Algorithm
constructor
A new instance of Algorithm.
-
#run ⇒ Object
If a block is provided, it should yield each iteration of the clusters, otherwise, just returns the final state of the space.
Constructor Details
#initialize(k, nodes) ⇒ Algorithm
Returns a new instance of Algorithm.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/carmenere/algorithm.rb', line 9 def initialize k, nodes @k = k @nodes = Set.new(nodes).freeze @clusters = Set.new(@nodes.map{ |n| Cluster.new([n]) }) @distance_matrix = @nodes.each.with_object({}) do |i, row| row[i] = @nodes.each.with_object({}) do |j, col| col[j] = i.distance(j) if i != j end end end |
Instance Attribute Details
#distance_matrix ⇒ Object (readonly)
Returns the value of attribute distance_matrix.
7 8 9 |
# File 'lib/carmenere/algorithm.rb', line 7 def distance_matrix @distance_matrix end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
5 6 7 |
# File 'lib/carmenere/algorithm.rb', line 5 def nodes @nodes end |
Instance Method Details
#run ⇒ Object
If a block is provided, it should yield each iteration of the clusters, otherwise, just returns the final state of the space.
22 23 24 |
# File 'lib/carmenere/algorithm.rb', line 22 def run raise NotImplementedError.new end |