Class: Legion::Extensions::Agentic::Self::Agency::Helpers::EfficacyModel

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/self/agency/helpers/efficacy_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEfficacyModel

Returns a new instance of EfficacyModel.



12
13
14
15
# File 'lib/legion/extensions/agentic/self/agency/helpers/efficacy_model.rb', line 12

def initialize
  @domains = {}
  @history = []
end

Instance Attribute Details

#domainsObject (readonly)

Returns the value of attribute domains.



10
11
12
# File 'lib/legion/extensions/agentic/self/agency/helpers/efficacy_model.rb', line 10

def domains
  @domains
end

#historyObject (readonly)

Returns the value of attribute history.



10
11
12
# File 'lib/legion/extensions/agentic/self/agency/helpers/efficacy_model.rb', line 10

def history
  @history
end

Instance Method Details

#decay_allObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/legion/extensions/agentic/self/agency/helpers/efficacy_model.rb', line 37

def decay_all
  @domains.each_key do |domain|
    current = @domains[domain]
    diff = Constants::DEFAULT_EFFICACY - current
    @domains[domain] = (current + (diff * Constants::DECAY_RATE)).clamp(
      Constants::EFFICACY_FLOOR, Constants::EFFICACY_CEILING
    )
  end
  trim_domains
end

#domain_countObject



74
75
76
# File 'lib/legion/extensions/agentic/self/agency/helpers/efficacy_model.rb', line 74

def domain_count
  @domains.size
end

#domain_history(domain) ⇒ Object



48
49
50
# File 'lib/legion/extensions/agentic/self/agency/helpers/efficacy_model.rb', line 48

def domain_history(domain)
  @history.select { |e| e.domain == domain }
end

#efficacy_for(domain) ⇒ Object



17
18
19
20
# File 'lib/legion/extensions/agentic/self/agency/helpers/efficacy_model.rb', line 17

def efficacy_for(domain)
  @domains[domain] ||= Constants::DEFAULT_EFFICACY
  @domains[domain]
end

#efficacy_label(domain) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/legion/extensions/agentic/self/agency/helpers/efficacy_model.rb', line 22

def efficacy_label(domain)
  value = efficacy_for(domain)
  Constants::EFFICACY_LABELS.each do |range, label|
    return label if range.cover?(value)
  end
  :uncertain
end

#overall_efficacyObject



68
69
70
71
72
# File 'lib/legion/extensions/agentic/self/agency/helpers/efficacy_model.rb', line 68

def overall_efficacy
  return Constants::DEFAULT_EFFICACY if @domains.empty?

  @domains.values.sum / @domains.size
end

#record_outcome(event) ⇒ Object



30
31
32
33
34
35
# File 'lib/legion/extensions/agentic/self/agency/helpers/efficacy_model.rb', line 30

def record_outcome(event)
  @history << event
  update_efficacy(event)
  trim_history
  event
end

#strongest_domains(count = 5) ⇒ Object



60
61
62
# File 'lib/legion/extensions/agentic/self/agency/helpers/efficacy_model.rb', line 60

def strongest_domains(count = 5)
  @domains.sort_by { |_, v| -v }.first(count).to_h
end

#success_rate(domain) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/legion/extensions/agentic/self/agency/helpers/efficacy_model.rb', line 52

def success_rate(domain)
  events = domain_history(domain)
  return 0.0 if events.empty?

  successes = events.count(&:success?)
  successes.to_f / events.size
end

#to_hObject



78
79
80
81
82
83
84
85
# File 'lib/legion/extensions/agentic/self/agency/helpers/efficacy_model.rb', line 78

def to_h
  {
    domain_count:     @domains.size,
    overall_efficacy: overall_efficacy.round(4),
    history_size:     @history.size,
    domains:          @domains.transform_values { |v| v.round(4) }
  }
end

#weakest_domains(count = 5) ⇒ Object



64
65
66
# File 'lib/legion/extensions/agentic/self/agency/helpers/efficacy_model.rb', line 64

def weakest_domains(count = 5)
  @domains.sort_by { |_, v| v }.first(count).to_h
end