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

#maxObject (readonly)

The maximum number of iterations allowed



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

def max
  @max
end

#num_iterationsObject (readonly)

The number of iterations used



28
29
30
# File 'lib/kmeans/agent.rb', line 28

def num_iterations
  @num_iterations
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



31
32
33
# File 'lib/kmeans/agent.rb', line 31

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], …)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kmeans/agent.rb', line 38

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)
    @max = opts.fetch(:max, 10_000)
  else
    @k = opts
  end

  stabilize_centroids
  @@agent = self if self.online
  self

end

#rebase(*node_list) ⇒ Object



64
65
66
67
68
# File 'lib/kmeans/agent.rb', line 64

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