Sha256: 3987cdfeffeabad5f06354a922e2dabb20d26c2b62f6e728e4bdb8e4ac853b52

Contents?: true

Size: 1.9 KB

Versions: 1

Compression:

Stored size: 1.9 KB

Contents

module KMeans #:nodoc:
  class Agent
    
    include TeguGears

    class << self
      # Only works if the agent was processed as an online algorithm
      def rebase(*node_list)
        return false unless @@agent
        @@agent.rebase(*node_list)
      end
    end
    
    # The number of clusters we're after
    attr_reader :k

    # Whether we're interested in keeping the results after processing.  To re-process:
    # Agent.rebase(*new_node_list)
    attr_reader :online
    
    # The centroids used for the clustering
    attr_reader :centroids
    
    # All the affectd nodes
    def nodes
      Node.nodes
    end

    # Example:
    # KMeans::Agent.call(3, [1,2,1], [2,1,3], ...)
    # KMeans::Agent.call(:k => 3, :centroids => [[1,2,3],[2,3,4],[3,4,2]], [1,2,1], [2,1,3], ...)
    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
    
    def rebase(*node_list)
      Node.add_nodes(*node_list)
      stabilize_centroids
      self
    end

    protected
    
      # This is the core algorithm: assign nodes to centroids, rebalance
      # centroids, repeat until no new assignment is necessary. 
      def stabilize_centroids
        @centroids ||= infer_centroids(@k)
        n = Node.cluster_to(@centroids)
        while n > 0
          @centroids.each { |c| c.rebalance }
          n = Node.cluster_to(@centroids)
        end
      end
      
      def infer_centroids(k)
        (1..k).map do
          Node.random_centroid
        end
      end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
davidrichards-kmeans-0.0.3 lib/kmeans/agent.rb