Class: Legion::Extensions::Agentic::Learning::Hebbian::Helpers::Unit

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

Constant Summary

Constants included from Constants

Constants::ACTIVATION_THRESHOLD, Constants::ASSEMBLY_MIN_WEIGHT, Constants::ASSEMBLY_STATE_LABELS, Constants::ASSEMBLY_THRESHOLD, Constants::COMPETITION_FACTOR, Constants::CONSOLIDATION_BOOST, Constants::CO_ACTIVATION_WINDOW, Constants::DEFAULT_WEIGHT, Constants::LEARNING_RATE, Constants::MAX_ACTIVATION_HISTORY, Constants::MAX_ASSEMBLIES, Constants::MAX_CONNECTIONS_PER_UNIT, Constants::MAX_UNITS, Constants::MAX_WEIGHT, Constants::WEIGHT_DECAY, Constants::WEIGHT_FLOOR, Constants::WEIGHT_LABELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, domain: :general) ⇒ Unit

Returns a new instance of Unit.



15
16
17
18
19
20
21
22
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 15

def initialize(id:, domain: :general)
  @id               = id
  @domain           = domain
  @activation_level = 0.0
  @connections      = {}
  @activation_count = 0
  @last_activated   = nil
end

Instance Attribute Details

#activation_countObject (readonly)

Returns the value of attribute activation_count.



12
13
14
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 12

def activation_count
  @activation_count
end

#activation_levelObject

Returns the value of attribute activation_level.



13
14
15
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 13

def activation_level
  @activation_level
end

#connectionsObject (readonly)

Returns the value of attribute connections.



12
13
14
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 12

def connections
  @connections
end

#domainObject (readonly)

Returns the value of attribute domain.



12
13
14
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 12

def domain
  @domain
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 12

def id
  @id
end

#last_activatedObject (readonly)

Returns the value of attribute last_activated.



12
13
14
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 12

def last_activated
  @last_activated
end

Instance Method Details

#activate(level: 1.0) ⇒ Object



24
25
26
27
28
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 24

def activate(level: 1.0)
  @activation_level = level.to_f.clamp(0.0, 1.0)
  @activation_count += 1
  @last_activated = Time.now.utc
end

#active?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 30

def active?
  @activation_level >= ACTIVATION_THRESHOLD
end

#connect(other_id, weight: DEFAULT_WEIGHT) ⇒ Object



34
35
36
37
38
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 34

def connect(other_id, weight: DEFAULT_WEIGHT)
  return if @connections.size >= MAX_CONNECTIONS_PER_UNIT && !@connections.key?(other_id)

  @connections[other_id] = weight.to_f.clamp(WEIGHT_FLOOR, MAX_WEIGHT)
end

#connection_countObject



69
70
71
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 69

def connection_count
  @connections.size
end

#decay_weightsObject



62
63
64
65
66
67
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 62

def decay_weights
  @connections.each do |k, w|
    @connections[k] = [w - WEIGHT_DECAY, WEIGHT_FLOOR].max
  end
  @connections.reject! { |_, w| w <= WEIGHT_FLOOR }
end

#strengthen(other_id, amount: LEARNING_RATE) ⇒ Object



40
41
42
43
44
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 40

def strengthen(other_id, amount: LEARNING_RATE)
  return unless @connections.key?(other_id)

  @connections[other_id] = [@connections[other_id] + amount, MAX_WEIGHT].min
end

#strongest_connections(count = 5) ⇒ Object



73
74
75
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 73

def strongest_connections(count = 5)
  @connections.sort_by { |_, w| -w }.first(count).to_h
end

#to_hObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 77

def to_h
  {
    id:               @id,
    domain:           @domain,
    activation_level: @activation_level.round(4),
    active:           active?,
    connections:      @connections.size,
    activation_count: @activation_count,
    last_activated:   @last_activated
  }
end

#weaken(other_id, amount: COMPETITION_FACTOR) ⇒ Object



46
47
48
49
50
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 46

def weaken(other_id, amount: COMPETITION_FACTOR)
  return unless @connections.key?(other_id)

  @connections[other_id] = [@connections[other_id] - amount, WEIGHT_FLOOR].max
end

#weight_label(other_id) ⇒ Object



56
57
58
59
60
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 56

def weight_label(other_id)
  w = weight_to(other_id)
  WEIGHT_LABELS.each { |range, lbl| return lbl if range.cover?(w) }
  :nascent
end

#weight_to(other_id) ⇒ Object



52
53
54
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/unit.rb', line 52

def weight_to(other_id)
  @connections[other_id] || 0.0
end