Class: Legion::Extensions::Agentic::Attention::LatentInhibition::Helpers::InhibitionEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/attention/latent_inhibition/helpers/inhibition_engine.rb

Constant Summary

Constants included from Constants

Constants::DISINHIBITION_RATE, Constants::INHIBITION_LABELS, Constants::INHIBITION_RATE, Constants::MAX_STIMULI, Constants::NOVELTY_LABELS, Constants::NOVELTY_THRESHOLD

Instance Method Summary collapse

Constructor Details

#initializeInhibitionEngine

Returns a new instance of InhibitionEngine.



12
13
14
# File 'lib/legion/extensions/agentic/attention/latent_inhibition/helpers/inhibition_engine.rb', line 12

def initialize
  @stimuli = {}
end

Instance Method Details

#attempt_association(label:, outcome:) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/legion/extensions/agentic/attention/latent_inhibition/helpers/inhibition_engine.rb', line 23

def attempt_association(label:, outcome:)
  stimulus = find_or_create(label)
  effectiveness = stimulus.associate!(outcome: outcome)
  {
    label:            label,
    outcome:          outcome,
    effectiveness:    effectiveness,
    inhibition_score: stimulus.inhibition_score,
    inhibition_label: stimulus.inhibition_label,
    blocked:          effectiveness < 0.1
  }
end

#disinhibit(label:, intensity:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/legion/extensions/agentic/attention/latent_inhibition/helpers/inhibition_engine.rb', line 36

def disinhibit(label:, intensity:)
  return { label: label, status: :not_found } unless @stimuli.key?(label)

  stimulus = @stimuli[label]
  prior_score = stimulus.inhibition_score
  stimulus.disinhibit!(intensity: intensity)
  {
    label:       label,
    prior_score: prior_score,
    new_score:   stimulus.inhibition_score,
    reduction:   (prior_score - stimulus.inhibition_score).round(10)
  }
end

#expose_stimulus(label:) ⇒ Object



16
17
18
19
20
21
# File 'lib/legion/extensions/agentic/attention/latent_inhibition/helpers/inhibition_engine.rb', line 16

def expose_stimulus(label:)
  stimulus = find_or_create(label)
  stimulus.expose!
  prune_if_needed
  stimulus.to_h
end

#inhibition_reportObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/legion/extensions/agentic/attention/latent_inhibition/helpers/inhibition_engine.rb', line 61

def inhibition_report
  stimuli = @stimuli.values
  return empty_report if stimuli.empty?

  {
    total_stimuli:   stimuli.size,
    novel_count:     stimuli.count(&:novel?),
    inhibited_count: stimuli.count { |s| s.inhibition_score > 0.0 },
    saturated_count: stimuli.count { |s| s.inhibition_label == :saturated },
    mean_inhibition: (stimuli.sum(&:inhibition_score) / stimuli.size).round(10),
    max_inhibition:  stimuli.map(&:inhibition_score).max.round(10),
    label_breakdown: label_breakdown(stimuli)
  }
end

#most_inhibited(limit: 10) ⇒ Object



54
55
56
57
58
59
# File 'lib/legion/extensions/agentic/attention/latent_inhibition/helpers/inhibition_engine.rb', line 54

def most_inhibited(limit: 10)
  @stimuli.values
          .sort_by { |s| -s.inhibition_score }
          .first(limit)
          .map(&:to_h)
end

#novel_stimuliObject



50
51
52
# File 'lib/legion/extensions/agentic/attention/latent_inhibition/helpers/inhibition_engine.rb', line 50

def novel_stimuli
  @stimuli.values.select(&:novel?).map(&:to_h)
end

#prune_if_neededObject



76
77
78
79
80
81
82
# File 'lib/legion/extensions/agentic/attention/latent_inhibition/helpers/inhibition_engine.rb', line 76

def prune_if_needed
  return if @stimuli.size <= Constants::MAX_STIMULI

  overflow = @stimuli.size - Constants::MAX_STIMULI
  oldest = @stimuli.values.sort_by { |s| s.last_exposed_at || Time.at(0) }.first(overflow)
  oldest.each { |s| @stimuli.delete(s.label) }
end

#to_hObject



84
85
86
87
88
89
# File 'lib/legion/extensions/agentic/attention/latent_inhibition/helpers/inhibition_engine.rb', line 84

def to_h
  {
    stimuli: @stimuli.transform_values(&:to_h),
    report:  inhibition_report
  }
end