Class: Legion::Extensions::Agentic::Homeostasis::NeuralOscillation::Helpers::OscillationNetwork

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb

Constant Summary

Constants included from Constants

Constants::BANDS, Constants::BAND_INFO, Constants::COGNITIVE_STATES, Constants::COUPLING_BOOST, Constants::COUPLING_DECAY, Constants::COUPLING_FLOOR, Constants::DEFAULT_POWER, Constants::DOMINANT_THRESHOLD, Constants::MAX_COUPLINGS, Constants::MAX_HISTORY, Constants::MAX_OSCILLATORS, Constants::PHASE_INCREMENT, Constants::POWER_DECAY, Constants::POWER_FLOOR, Constants::POWER_LABELS, Constants::SYNC_THRESHOLD

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOscillationNetwork

Returns a new instance of OscillationNetwork.



14
15
16
17
18
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 14

def initialize
  @oscillators = {}
  @couplings   = []
  @history     = []
end

Instance Attribute Details

#couplingsObject (readonly)

Returns the value of attribute couplings.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 12

def couplings
  @couplings
end

#oscillatorsObject (readonly)

Returns the value of attribute oscillators.



12
13
14
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 12

def oscillators
  @oscillators
end

Instance Method Details

#activate_band(oscillator_id:, band:, amount: DEFAULT_POWER) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 27

def activate_band(oscillator_id:, band:, amount: DEFAULT_POWER)
  osc = @oscillators[oscillator_id]
  return nil unless osc

  osc.activate(band: band, amount: amount)
  propagate_activation(oscillator_id, band, amount)
  osc
end

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



20
21
22
23
24
25
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 20

def add_oscillator(id:, domain: :general)
  return @oscillators[id] if @oscillators.key?(id)
  return nil if @oscillators.size >= MAX_OSCILLATORS

  @oscillators[id] = Oscillator.new(id: id, domain: domain)
end

#cognitive_stateObject



90
91
92
93
94
95
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 90

def cognitive_state
  rhythm = global_rhythm
  return :idle unless rhythm

  COGNITIVE_STATES.fetch(rhythm, :unknown)
end

#couple(oscillator_a:, oscillator_b:, band:) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 44

def couple(oscillator_a:, oscillator_b:, band:)
  return nil unless @oscillators.key?(oscillator_a) && @oscillators.key?(oscillator_b)
  return nil if oscillator_a == oscillator_b

  existing = find_coupling(oscillator_a, oscillator_b, band)
  if existing
    existing.strengthen
    return existing
  end

  return nil if @couplings.size >= MAX_COUPLINGS

  c = Coupling.new(oscillator_a: oscillator_a, oscillator_b: oscillator_b, band: band)
  @couplings << c
  c
end

#couplings_for(oscillator_id:) ⇒ Object



118
119
120
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 118

def couplings_for(oscillator_id:)
  @couplings.select { |c| c.involves?(oscillator_id) }.map(&:to_h)
end

#decouple(oscillator_a:, oscillator_b:, band:) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 61

def decouple(oscillator_a:, oscillator_b:, band:)
  idx = @couplings.index { |c| c.key == [[oscillator_a, oscillator_b].sort, band].flatten }
  return false unless idx

  @couplings.delete_at(idx)
  true
end

#desynchronize(band:) ⇒ Object



97
98
99
100
101
102
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 97

def desynchronize(band:)
  @couplings.select { |c| c.band == band }.each do |c|
    c.strength = [c.strength * 0.5, 0.0].max
  end
  @oscillators.each_value { |osc| osc.suppress(band: band, amount: 0.1) }
end

#global_rhythmObject



69
70
71
72
73
74
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 69

def global_rhythm
  return nil if @oscillators.empty?

  best_band, best_power = aggregate_band_powers.max_by { |_, v| v }
  best_power&.positive? ? best_band : nil
end

#network_synchronyObject



83
84
85
86
87
88
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 83

def network_synchrony
  return 0.0 if @couplings.empty?

  synced = @couplings.count(&:synchronized?)
  synced.to_f / @couplings.size
end

#oscillators_in_band(band:) ⇒ Object



114
115
116
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 114

def oscillators_in_band(band:)
  @oscillators.values.select { |o| o.dominant_band == band }.map(&:to_h)
end

#suppress_band(oscillator_id:, band:, amount: DEFAULT_POWER) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 36

def suppress_band(oscillator_id:, band:, amount: DEFAULT_POWER)
  osc = @oscillators[oscillator_id]
  return nil unless osc

  osc.suppress(band: band, amount: amount)
  osc
end

#synchrony_for(band:) ⇒ Object



76
77
78
79
80
81
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 76

def synchrony_for(band:)
  relevant = @couplings.select { |c| c.band == band && c.synchronized? }
  return 0.0 if relevant.empty?

  relevant.sum(&:strength) / relevant.size
end

#tickObject



104
105
106
107
108
109
110
111
112
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 104

def tick
  @oscillators.each_value do |osc|
    osc.advance_phase
    osc.decay
  end
  @couplings.each(&:decay)
  @couplings.reject!(&:weak?)
  record_state
end

#to_hObject



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillation_network.rb', line 122

def to_h
  {
    oscillator_count:  @oscillators.size,
    coupling_count:    @couplings.size,
    global_rhythm:     global_rhythm,
    cognitive_state:   cognitive_state,
    network_synchrony: network_synchrony.round(4),
    band_powers:       aggregate_band_powers,
    history_size:      @history.size
  }
end