Class: Legion::Extensions::Agentic::Learning::Anchoring::Helpers::Anchor

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/learning/anchoring/helpers/anchor.rb

Constant Summary

Constants included from Constants

Constants::ADJUSTMENT_RATE, Constants::ANCHOR_DECAY, Constants::ANCHOR_FLOOR, Constants::ANCHOR_LABELS, Constants::DEFAULT_ANCHOR_WEIGHT, Constants::LOSS_AVERSION_FACTOR, Constants::MAX_ANCHORS_PER_DOMAIN, Constants::MAX_DOMAINS, Constants::REFERENCE_SHIFT_THRESHOLD

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value:, domain: :general) ⇒ Anchor

Returns a new instance of Anchor.



16
17
18
19
20
21
22
23
# File 'lib/legion/extensions/agentic/learning/anchoring/helpers/anchor.rb', line 16

def initialize(value:, domain: :general)
  @id            = SecureRandom.uuid
  @value         = value.to_f
  @domain        = domain.to_sym
  @strength      = 1.0
  @created_at    = Time.now.utc
  @last_accessed = Time.now.utc
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



14
15
16
# File 'lib/legion/extensions/agentic/learning/anchoring/helpers/anchor.rb', line 14

def created_at
  @created_at
end

#domainObject (readonly)

Returns the value of attribute domain.



14
15
16
# File 'lib/legion/extensions/agentic/learning/anchoring/helpers/anchor.rb', line 14

def domain
  @domain
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/legion/extensions/agentic/learning/anchoring/helpers/anchor.rb', line 14

def id
  @id
end

#last_accessedObject (readonly)

Returns the value of attribute last_accessed.



14
15
16
# File 'lib/legion/extensions/agentic/learning/anchoring/helpers/anchor.rb', line 14

def last_accessed
  @last_accessed
end

#strengthObject (readonly)

Returns the value of attribute strength.



14
15
16
# File 'lib/legion/extensions/agentic/learning/anchoring/helpers/anchor.rb', line 14

def strength
  @strength
end

#valueObject (readonly)

Returns the value of attribute value.



14
15
16
# File 'lib/legion/extensions/agentic/learning/anchoring/helpers/anchor.rb', line 14

def value
  @value
end

Instance Method Details

#decayObject



25
26
27
# File 'lib/legion/extensions/agentic/learning/anchoring/helpers/anchor.rb', line 25

def decay
  @strength = [(@strength - Constants::ANCHOR_DECAY), 0.0].max
end

#labelObject



41
42
43
44
45
46
# File 'lib/legion/extensions/agentic/learning/anchoring/helpers/anchor.rb', line 41

def label
  Constants::ANCHOR_LABELS.each do |range, lbl|
    return lbl if range.cover?(@strength)
  end
  :fading
end

#pull(estimate:) ⇒ Object



36
37
38
39
# File 'lib/legion/extensions/agentic/learning/anchoring/helpers/anchor.rb', line 36

def pull(estimate:)
  weight = @strength * Constants::DEFAULT_ANCHOR_WEIGHT
  (weight * @value) + ((1.0 - weight) * estimate.to_f)
end

#reinforce(observed_value:) ⇒ Object



29
30
31
32
33
34
# File 'lib/legion/extensions/agentic/learning/anchoring/helpers/anchor.rb', line 29

def reinforce(observed_value:)
  @last_accessed = Time.now.utc
  alpha          = Constants::ADJUSTMENT_RATE
  @value         = ((1.0 - alpha) * @value) + (alpha * observed_value.to_f)
  @strength      = [@strength + 0.1, 1.0].min
end

#to_hObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/legion/extensions/agentic/learning/anchoring/helpers/anchor.rb', line 48

def to_h
  {
    id:            @id,
    value:         @value,
    domain:        @domain,
    strength:      @strength,
    label:         label,
    created_at:    @created_at,
    last_accessed: @last_accessed
  }
end