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

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

Constant Summary collapse

BAND_SPEEDS =
{ delta: 1.0, theta: 2.0, alpha: 3.0, beta: 4.0, gamma: 6.0 }.freeze

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

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

Returns a new instance of Oscillator.



16
17
18
19
20
21
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillator.rb', line 16

def initialize(id:, domain: :general)
  @id     = id
  @domain = domain
  @powers = BANDS.to_h { |b| [b, 0.0] }
  @phases = BANDS.to_h { |b| [b, rand * 2 * Math::PI] }
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



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

def domain
  @domain
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

Instance Method Details

#activate(band:, amount: DEFAULT_POWER) ⇒ Object



31
32
33
34
35
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillator.rb', line 31

def activate(band:, amount: DEFAULT_POWER)
  return unless BANDS.include?(band)

  @powers[band] = [@powers[band] + amount, 1.0].min
end

#advance_phaseObject



55
56
57
58
59
60
61
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillator.rb', line 55

def advance_phase
  BANDS.each do |band|
    next if @powers[band] <= POWER_FLOOR

    @phases[band] = (@phases[band] + (PHASE_INCREMENT * band_speed(band))) % (2 * Math::PI)
  end
end

#decayObject



73
74
75
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillator.rb', line 73

def decay
  BANDS.each { |b| @powers[b] = [@powers[b] - POWER_DECAY, 0.0].max }
end

#dominant?Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillator.rb', line 50

def dominant?
  band = dominant_band
  band && @powers[band] >= DOMINANT_THRESHOLD
end

#dominant_bandObject



43
44
45
46
47
48
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillator.rb', line 43

def dominant_band
  best = @powers.max_by { |_, v| v }
  return nil unless best && best[1] > POWER_FLOOR

  best[0]
end

#phase(band) ⇒ Object



27
28
29
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillator.rb', line 27

def phase(band)
  @phases.fetch(band, 0.0)
end

#phase_difference(other, band:) ⇒ Object



63
64
65
66
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillator.rb', line 63

def phase_difference(other, band:)
  diff = (@phases[band] - other.phase(band)).abs % (2 * Math::PI)
  diff > Math::PI ? (2 * Math::PI) - diff : diff
end

#power(band) ⇒ Object



23
24
25
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillator.rb', line 23

def power(band)
  @powers.fetch(band, 0.0)
end

#power_label(band) ⇒ Object



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

def power_label(band)
  val = @powers.fetch(band, 0.0)
  POWER_LABELS.each { |range, lbl| return lbl if range.cover?(val) }
  :silent
end

#suppress(band:, amount: DEFAULT_POWER) ⇒ Object



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

def suppress(band:, amount: DEFAULT_POWER)
  return unless BANDS.include?(band)

  @powers[band] = [@powers[band] - amount, 0.0].max
end

#synchrony_with(other, band:) ⇒ Object



68
69
70
71
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillator.rb', line 68

def synchrony_with(other, band:)
  diff = phase_difference(other, band: band)
  1.0 - (diff / Math::PI)
end

#to_hObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/legion/extensions/agentic/homeostasis/neural_oscillation/helpers/oscillator.rb', line 83

def to_h
  {
    id:            @id,
    domain:        @domain,
    powers:        @powers.transform_values { |v| v.round(4) },
    dominant_band: dominant_band,
    dominant:      dominant?,
    phases:        @phases.transform_values { |v| v.round(4) }
  }
end