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

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/learning/hebbian/helpers/assembly_network.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

#initializeAssemblyNetwork

Returns a new instance of AssemblyNetwork.



14
15
16
17
18
19
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/assembly_network.rb', line 14

def initialize
  @units              = {}
  @assemblies         = {}
  @activation_history = []
  @assembly_counter   = 0
end

Instance Attribute Details

#activation_historyObject (readonly)

Returns the value of attribute activation_history.



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

def activation_history
  @activation_history
end

#assembliesObject (readonly)

Returns the value of attribute assemblies.



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

def assemblies
  @assemblies
end

#unitsObject (readonly)

Returns the value of attribute units.



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

def units
  @units
end

Instance Method Details

#activate_unit(id:, level: 1.0) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/assembly_network.rb', line 28

def activate_unit(id:, level: 1.0)
  ensure_unit(id)
  unit = @units[id]
  unit.activate(level: level)
  record_activation(id)
  hebbian_update(id)
  detect_assemblies
  unit
end

#add_unit(id:, domain: :general) ⇒ Object



21
22
23
24
25
26
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/assembly_network.rb', line 21

def add_unit(id:, domain: :general)
  return @units[id] if @units.key?(id)
  return nil if @units.size >= MAX_UNITS

  @units[id] = Unit.new(id: id, domain: domain)
end

#assemblies_containing(unit_id:) ⇒ Object



66
67
68
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/assembly_network.rb', line 66

def assemblies_containing(unit_id:)
  @assemblies.values.select { |a| a.includes?(unit_id) }
end

#assembly_countObject



97
98
99
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/assembly_network.rb', line 97

def assembly_count
  @assemblies.size
end

#co_activate(ids:, level: 1.0) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/assembly_network.rb', line 38

def co_activate(ids:, level: 1.0)
  ids = Array(ids)
  ids.each do |id|
    ensure_unit(id)
    @units[id].activate(level: level)
    record_activation(id)
  end

  ids.combination(2).each do |a, b|
    ensure_connection(a, b)
    @units[a].strengthen(b)
    @units[b].strengthen(a)
  end

  detect_assemblies
  ids.map { |id| @units[id].to_h }
end

#decay_allObject



87
88
89
90
91
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/assembly_network.rb', line 87

def decay_all
  @units.each_value(&:decay_weights)
  @assemblies.each_value(&:decay)
  @assemblies.reject! { |_, a| a.dissolving? }
end

#pattern_complete(partial_ids:) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/assembly_network.rb', line 70

def pattern_complete(partial_ids:)
  partial = Array(partial_ids)
  candidates = partial.flat_map { |uid| assemblies_containing(unit_id: uid) }.uniq(&:id)
  return nil if candidates.empty?

  best = candidates.max_by { |a| (partial & a.member_ids).size }
  missing = best.member_ids - partial
  { assembly_id: best.id, known: partial & best.member_ids, predicted: missing, coherence: best.coherence }
end

#query_assembly(id:) ⇒ Object



62
63
64
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/assembly_network.rb', line 62

def query_assembly(id:)
  @assemblies[id]
end

#query_weight(from:, to:) ⇒ Object



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

def query_weight(from:, to:)
  return 0.0 unless @units.key?(from)

  @units[from].weight_to(to)
end

#strongest_units(count = 10) ⇒ Object



80
81
82
83
84
85
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/assembly_network.rb', line 80

def strongest_units(count = 10)
  @units.values
        .sort_by { |u| -u.activation_count }
        .first(count)
        .map(&:to_h)
end

#to_hObject



101
102
103
104
105
106
107
108
109
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/assembly_network.rb', line 101

def to_h
  {
    units:             @units.size,
    assemblies:        @assemblies.size,
    total_connections: @units.values.sum(&:connection_count),
    history_size:      @activation_history.size,
    active_units:      @units.values.count(&:active?)
  }
end

#unit_countObject



93
94
95
# File 'lib/legion/extensions/agentic/learning/hebbian/helpers/assembly_network.rb', line 93

def unit_count
  @units.size
end