Class: KMeans::Agent

Inherits:
Object show all
Includes:
TeguGears
Defined in:
lib/kmeans/agent.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#centroidsObject (readonly)

The centroids used for the clustering



22
23
24
# File 'lib/kmeans/agent.rb', line 22

def centroids
  @centroids
end

#kObject (readonly)

The number of clusters we’re after



15
16
17
# File 'lib/kmeans/agent.rb', line 15

def k
  @k
end

#onlineObject (readonly)

Whether we’re interested in keeping the results after processing. To re-process: Agent.rebase(*new_node_list)



19
20
21
# File 'lib/kmeans/agent.rb', line 19

def online
  @online
end

Class Method Details

.rebase(*node_list) ⇒ Object

Only works if the agent was processed as an online algorithm



8
9
10
11
# File 'lib/kmeans/agent.rb', line 8

def rebase(*node_list)
  return false unless @@agent
  @@agent.rebase(*node_list)
end

Instance Method Details

#nodesObject

All the affectd nodes



25
26
27
# File 'lib/kmeans/agent.rb', line 25

def nodes
  Node.nodes
end

#process(opts = {}, *node_list) ⇒ Object

Example: KMeans::Agent.call(3, [1,2,1], [2,1,3], …) KMeans::Agent.call(:k => 3, :centroids => [[1,2,3],,[3,4,2]], [1,2,1], [2,1,3], …)



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kmeans/agent.rb', line 32

def process(opts={}, *node_list)
  
  unless self.online
    Node.clear_nodes! 
    @centroids = nil
  end
  
  
  @scaling = opts.fetch(:scaling, false) if opts.is_a?(Hash)
  Node.add_nodes(*node_list)
  
  if opts.is_a?(Hash)
    @k = opts[:k]
    @centroids = opts.fetch(:centroids, false)
    @online = opts.fetch(:online, false)
  else
    @k = opts
  end

  stabilize_centroids
  @@agent = self if self.online
  self

end

#rebase(*node_list) ⇒ Object



57
58
59
60
61
# File 'lib/kmeans/agent.rb', line 57

def rebase(*node_list)
  Node.add_nodes(*node_list)
  stabilize_centroids
  self
end