Class: Legion::Extensions::Agentic::Affect::Resonance::Helpers::Category

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/affect/resonance/helpers/category.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prototype:) ⇒ Category

Returns a new instance of Category.



14
15
16
17
18
19
# File 'lib/legion/extensions/agentic/affect/resonance/helpers/category.rb', line 14

def initialize(prototype:)
  @id              = SecureRandom.uuid
  @prototype       = prototype.map { |v| v.to_f.clamp(0.0, 1.0) }
  @match_count     = 0
  @last_matched_at = nil
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/legion/extensions/agentic/affect/resonance/helpers/category.rb', line 12

def id
  @id
end

#last_matched_atObject (readonly)

Returns the value of attribute last_matched_at.



12
13
14
# File 'lib/legion/extensions/agentic/affect/resonance/helpers/category.rb', line 12

def last_matched_at
  @last_matched_at
end

#match_countObject (readonly)

Returns the value of attribute match_count.



12
13
14
# File 'lib/legion/extensions/agentic/affect/resonance/helpers/category.rb', line 12

def match_count
  @match_count
end

#prototypeObject (readonly)

Returns the value of attribute prototype.



12
13
14
# File 'lib/legion/extensions/agentic/affect/resonance/helpers/category.rb', line 12

def prototype
  @prototype
end

Instance Method Details

#match_quality(input:) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/legion/extensions/agentic/affect/resonance/helpers/category.rb', line 21

def match_quality(input:)
  return 0.0 if @prototype.empty? || input.empty?

  len          = [@prototype.size, input.size].min
  proto_slice  = @prototype.first(len)
  input_slice  = input.first(len).map { |v| v.to_f.clamp(0.0, 1.0) }
  cosine_similarity(proto_slice, input_slice)
end

#to_hObject



60
61
62
63
64
65
66
67
68
# File 'lib/legion/extensions/agentic/affect/resonance/helpers/category.rb', line 60

def to_h
  {
    id:              @id,
    prototype:       @prototype,
    match_count:     @match_count,
    last_matched_at: @last_matched_at,
    dimensions:      @prototype.size
  }
end

#update_prototype!(input:, learning_rate: Constants::DEFAULT_LEARNING_RATE) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/legion/extensions/agentic/affect/resonance/helpers/category.rb', line 30

def update_prototype!(input:, learning_rate: Constants::DEFAULT_LEARNING_RATE)
  rate = learning_rate.clamp(0.0, 1.0)
  input_normalized = input.map { |v| v.to_f.clamp(0.0, 1.0) }

  len = [@prototype.size, input_normalized.size].max
  updated = Array.new(len) do |i|
    proto_val = @prototype[i] || 0.0
    input_val = input_normalized[i] || 0.0
    (proto_val + (rate * (input_val - proto_val))).round(10).clamp(0.0, 1.0)
  end

  @prototype = updated
  @match_count += 1
  @last_matched_at = Time.now.utc
  self
end