Class: Legion::Extensions::Agentic::Learning::MetaLearning::Helpers::LearningDomain

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

Constant Summary

Constants included from Constants

Constants::DEFAULT_LEARNING_RATE, Constants::EFFICIENCY_LABELS, Constants::MAX_DOMAINS, Constants::MAX_EPISODES, Constants::MAX_STRATEGIES, Constants::PROFICIENCY_LABELS, Constants::RATE_BOOST, Constants::RATE_DECAY, Constants::STRATEGY_TYPES, Constants::TRANSFER_BONUS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, learning_rate: DEFAULT_LEARNING_RATE, related_domains: []) ⇒ LearningDomain

Returns a new instance of LearningDomain.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 17

def initialize(name:, learning_rate: DEFAULT_LEARNING_RATE, related_domains: [])
  @id               = SecureRandom.uuid
  @name             = name
  @proficiency      = 0.0
  @learning_rate    = learning_rate.clamp(0.001, 1.0)
  @episodes_count   = 0
  @successes        = 0
  @failures         = 0
  @preferred_strategy = nil
  @related_domains  = Array(related_domains).dup
  @created_at       = Time.now.utc
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



15
16
17
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 15

def created_at
  @created_at
end

#episodes_countObject (readonly)

Returns the value of attribute episodes_count.



15
16
17
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 15

def episodes_count
  @episodes_count
end

#failuresObject (readonly)

Returns the value of attribute failures.



15
16
17
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 15

def failures
  @failures
end

#idObject (readonly)

Returns the value of attribute id.



15
16
17
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 15

def id
  @id
end

#learning_rateObject (readonly)

Returns the value of attribute learning_rate.



15
16
17
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 15

def learning_rate
  @learning_rate
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 15

def name
  @name
end

#preferred_strategyObject

Returns the value of attribute preferred_strategy.



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

def preferred_strategy
  @preferred_strategy
end

#proficiencyObject (readonly)

Returns the value of attribute proficiency.



15
16
17
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 15

def proficiency
  @proficiency
end

Returns the value of attribute related_domains.



15
16
17
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 15

def related_domains
  @related_domains
end

#successesObject (readonly)

Returns the value of attribute successes.



15
16
17
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 15

def successes
  @successes
end

Instance Method Details

#adapt_rate!(delta:) ⇒ Object



58
59
60
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 58

def adapt_rate!(delta:)
  @learning_rate = (@learning_rate + delta).clamp(0.001, 1.0).round(10)
end

#efficiencyObject



43
44
45
46
47
48
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 43

def efficiency
  total = @successes + @failures
  return 0.0 if total.zero?

  (@successes.to_f / total).round(10)
end

#efficiency_labelObject



50
51
52
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 50

def efficiency_label
  EFFICIENCY_LABELS.find { |range, _| range.cover?(efficiency) }&.last || :struggling
end

#proficiency_labelObject



54
55
56
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 54

def proficiency_label
  PROFICIENCY_LABELS.find { |range, _| range.cover?(@proficiency) }&.last || :beginner
end

#record_failure!Object



36
37
38
39
40
41
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 36

def record_failure!
  @failures       += 1
  @episodes_count += 1
  penalty          = (@learning_rate * 0.5).round(10)
  @proficiency     = (@proficiency - penalty).clamp(0.0, 1.0).round(10)
end

#record_success!Object



30
31
32
33
34
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 30

def record_success!
  @successes      += 1
  @episodes_count += 1
  @proficiency     = (@proficiency + @learning_rate).clamp(0.0, 1.0).round(10)
end

#to_hObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/legion/extensions/agentic/learning/meta_learning/helpers/learning_domain.rb', line 62

def to_h
  {
    id:                 @id,
    name:               @name,
    proficiency:        @proficiency,
    proficiency_label:  proficiency_label,
    learning_rate:      @learning_rate,
    episodes_count:     @episodes_count,
    successes:          @successes,
    failures:           @failures,
    efficiency:         efficiency,
    efficiency_label:   efficiency_label,
    preferred_strategy: @preferred_strategy,
    related_domains:    @related_domains,
    created_at:         @created_at
  }
end