Sha256: 22acf5fad486bd26eeaa9bf4bce369c43919a828b6874bfb8717834a10bd2668

Contents?: true

Size: 1.39 KB

Versions: 6

Compression:

Stored size: 1.39 KB

Contents

module Query

  # Calculates weights for certain combinations.
  #
  class Weights # :nodoc:all
    
    #
    #
    def initialize weights = {}
      @weights = weights
    end
    
    # Get the weight of an allocation.
    #
    def weight_for clustered
      @weights[clustered] || 0
    end
    
    # Returns an energy term E for allocation. this turns into a probability
    # by P(allocation) = 1/Z * exp (-1/T * E(allocation)),
    # where Z is the normalizing partition function
    # sum_allocations exp(-1/T *E(allocation)), and T is a temperature constant.
    # If T is high the distribution will be close to equally distributed.
    # If T is low, the distribution will be the indicator function
    # for min (E(allocation))…
    #
    # ...
    #
    # Just kidding. It's far more complicated than that. Ha ha ha ha ;)
    #
    # Note: Cache this if more complicated weighings become necessary.
    #
    def score combinations
      # TODO Or hide: combinations#to_weights_key
      #
      # TODO combinations could cluster uniq as combinations are added (since combinations don't change).
      #
      weight_for combinations.map(&:category_name).clustered_uniq_fast
    end
    
    # Are there any weights defined?
    #
    def empty?
      @weights.empty?
    end
    
    # Prints out a nice representation of the configured weights.
    #
    def to_s
      @weights.to_s
    end
    
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
picky-2.0.0 lib/picky/internals/query/weights.rb
picky-2.0.0.pre3 lib/picky/internals/query/weights.rb
picky-2.0.0.pre2 lib/picky/internals/query/weights.rb
picky-2.0.0.pre1 lib/picky/internals/query/weights.rb
picky-1.5.4 lib/picky/internals/query/weights.rb
picky-1.5.3 lib/picky/internals/query/weights.rb