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

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/social/social_learning/helpers/model_agent.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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent_id:, domain:, prestige: Constants::DEFAULT_PRESTIGE) ⇒ ModelAgent

Returns a new instance of ModelAgent.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/model_agent.rb', line 19

def initialize(agent_id:, domain:, prestige: Constants::DEFAULT_PRESTIGE)
  @id                 = SecureRandom.uuid
  @agent_id           = agent_id
  @domain             = domain
  @prestige           = prestige.clamp(Constants::PRESTIGE_FLOOR, Constants::PRESTIGE_CEILING)
  @observed_behaviors = []
  @observation_count  = 0
  @success_count      = 0
  @created_at         = Time.now.utc
  @last_observed_at   = nil
end

Instance Attribute Details

#agent_idObject (readonly)

Returns the value of attribute agent_id.



14
15
16
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/model_agent.rb', line 14

def agent_id
  @agent_id
end

#created_atObject (readonly)

Returns the value of attribute created_at.



14
15
16
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/model_agent.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/social/social_learning/helpers/model_agent.rb', line 14

def domain
  @domain
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/model_agent.rb', line 14

def id
  @id
end

#last_observed_atObject (readonly)

Returns the value of attribute last_observed_at.



14
15
16
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/model_agent.rb', line 14

def last_observed_at
  @last_observed_at
end

#observation_countObject (readonly)

Returns the value of attribute observation_count.



14
15
16
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/model_agent.rb', line 14

def observation_count
  @observation_count
end

#observed_behaviorsObject (readonly)

Returns the value of attribute observed_behaviors.



14
15
16
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/model_agent.rb', line 14

def observed_behaviors
  @observed_behaviors
end

#prestigeObject

Returns the value of attribute prestige.



17
18
19
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/model_agent.rb', line 17

def prestige
  @prestige
end

#success_countObject (readonly)

Returns the value of attribute success_count.



14
15
16
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/model_agent.rb', line 14

def success_count
  @success_count
end

Instance Method Details

#observe!(behavior:, outcome:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/model_agent.rb', line 31

def observe!(behavior:, outcome:)
  @observation_count += 1
  @last_observed_at = Time.now.utc

  if outcome == :positive
    @success_count += 1
    @prestige = (@prestige + Constants::PRESTIGE_LEARNING_RATE).clamp(
      Constants::PRESTIGE_FLOOR,
      Constants::PRESTIGE_CEILING
    )
  elsif outcome == :negative
    @prestige = (@prestige - Constants::PRESTIGE_LEARNING_RATE).clamp(
      Constants::PRESTIGE_FLOOR,
      Constants::PRESTIGE_CEILING
    )
  end

  behavior
end

#prestige_labelObject



51
52
53
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/model_agent.rb', line 51

def prestige_label
  Constants::MODEL_LABELS.find { |range, _label| range.include?(@prestige) }&.last || :unknown
end

#success_rateObject



55
56
57
58
59
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/model_agent.rb', line 55

def success_rate
  return 0.0 if @observation_count.zero?

  (@success_count.to_f / @observation_count).round(4)
end

#to_hObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/legion/extensions/agentic/social/social_learning/helpers/model_agent.rb', line 61

def to_h
  {
    id:                @id,
    agent_id:          @agent_id,
    domain:            @domain,
    prestige:          @prestige.round(4),
    prestige_label:    prestige_label,
    observation_count: @observation_count,
    success_count:     @success_count,
    success_rate:      success_rate,
    behavior_count:    @observed_behaviors.size,
    created_at:        @created_at,
    last_observed_at:  @last_observed_at
  }
end