Class: Legion::Extensions::Agentic::Social::SocialLearning::Helpers::SocialLearningEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/social/social_learning/helpers/social_learning_engine.rb

Constant Summary

Constants included from Constants

Constants::ATTENTION_THRESHOLD, Constants::DEFAULT_PRESTIGE, Constants::LEARNING_STAGES, Constants::MAX_BEHAVIORS, Constants::MAX_HISTORY, Constants::MAX_MODELS, Constants::MAX_OBSERVATIONS, Constants::MODEL_LABELS, Constants::OUTCOME_TYPES, Constants::PRESTIGE_CEILING, Constants::PRESTIGE_FLOOR, Constants::PRESTIGE_LEARNING_RATE, Constants::PUNISHMENT_PENALTY, Constants::REINFORCEMENT_BOOST, Constants::REPRODUCTION_CONFIDENCE, Constants::RETENTION_DECAY, Constants::STALE_THRESHOLD

Instance Method Summary collapse

Constructor Details

#initializeSocialLearningEngine

Returns a new instance of SocialLearningEngine.



12
13
14
15
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/social_learning_engine.rb', line 12

def initialize
  @models     = {}  # id -> ModelAgent
  @behaviors  = {}  # id -> ObservedBehavior
end

Instance Method Details

#best_models(limit: 5) ⇒ Object



90
91
92
93
94
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/social_learning_engine.rb', line 90

def best_models(limit: 5)
  @models.values
         .sort_by { |mod| -mod.prestige }
         .first(limit)
end

#by_domain(domain:) ⇒ Object



96
97
98
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/social_learning_engine.rb', line 96

def by_domain(domain:)
  @models.values.select { |mod| mod.domain == domain }
end

#decay_allObject



100
101
102
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/social_learning_engine.rb', line 100

def decay_all
  @behaviors.each_value(&:decay_retention!)
end

#observe_behavior(model_id:, action:, domain:, outcome:, context: {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/social_learning_engine.rb', line 25

def observe_behavior(model_id:, action:, domain:, outcome:, context: {})
  model = @models.fetch(model_id, nil)
  return nil unless model
  return nil if model.prestige < Constants::ATTENTION_THRESHOLD

  prune_behaviors if @behaviors.size >= Constants::MAX_BEHAVIORS

  behavior = ObservedBehavior.new(
    model_agent_id: model_id,
    action:         action,
    domain:         domain,
    outcome:        outcome,
    context:        context
  )

  model.observe!(behavior: behavior, outcome: outcome)
  model.observed_behaviors << behavior
  @behaviors[behavior.id] = behavior
  behavior
end

#prune_forgottenObject



104
105
106
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/social_learning_engine.rb', line 104

def prune_forgotten
  @behaviors.delete_if { |_id, beh| beh.retention < 0.05 }
end

#register_model(agent_id:, domain:, prestige: Constants::DEFAULT_PRESTIGE) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/social_learning_engine.rb', line 17

def register_model(agent_id:, domain:, prestige: Constants::DEFAULT_PRESTIGE)
  prune_models if @models.size >= Constants::MAX_MODELS

  model = ModelAgent.new(agent_id: agent_id, domain: domain, prestige: prestige)
  @models[model.id] = model
  model
end

#reinforce_reproduction(behavior_id:, outcome:) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/social_learning_engine.rb', line 67

def reinforce_reproduction(behavior_id:, outcome:)
  behavior = @behaviors.fetch(behavior_id, nil)
  return nil unless behavior

  model = @models.fetch(behavior.model_agent_id, nil)
  return nil unless model

  case outcome
  when :positive
    model.prestige = (model.prestige + Constants::REINFORCEMENT_BOOST).clamp(
      Constants::PRESTIGE_FLOOR,
      Constants::PRESTIGE_CEILING
    )
  when :negative
    model.prestige = (model.prestige - Constants::PUNISHMENT_PENALTY).clamp(
      Constants::PRESTIGE_FLOOR,
      Constants::PRESTIGE_CEILING
    )
  end

  { behavior: behavior.to_h, model_prestige: model.prestige.round(4) }
end

#reproduce_behavior(behavior_id:) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/social_learning_engine.rb', line 58

def reproduce_behavior(behavior_id:)
  behavior = @behaviors.fetch(behavior_id, nil)
  return nil unless behavior
  return nil unless behavior.retained?

  behavior.reproduced = true
  behavior
end

#reproducible_behaviors(domain: nil) ⇒ Object



53
54
55
56
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/social_learning_engine.rb', line 53

def reproducible_behaviors(domain: nil)
  behaviors = retained_behaviors(domain: domain)
  behaviors.select { |beh| beh.retention >= Constants::REPRODUCTION_CONFIDENCE }
end

#retained_behaviors(domain: nil) ⇒ Object



46
47
48
49
50
51
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/social_learning_engine.rb', line 46

def retained_behaviors(domain: nil)
  behaviors = @behaviors.values.select(&:retained?)
  return behaviors unless domain

  behaviors.select { |beh| beh.domain == domain }
end

#to_hObject



108
109
110
111
112
113
114
115
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/social_learning_engine.rb', line 108

def to_h
  {
    model_count:        @models.size,
    behavior_count:     @behaviors.size,
    retained_count:     retained_behaviors.size,
    reproducible_count: reproducible_behaviors.size
  }
end