Class: Legion::Extensions::Agentic::Homeostasis::Core::Helpers::AllostaticLoad

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/homeostasis/core/helpers/allostatic_load.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAllostaticLoad

Returns a new instance of AllostaticLoad.



12
13
14
15
16
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/allostatic_load.rb', line 12

def initialize
  @load = 0.0
  @peak_load = 0.0
  @history = []
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/allostatic_load.rb', line 10

def history
  @history
end

#loadObject (readonly)

Returns the value of attribute load.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/allostatic_load.rb', line 10

def load
  @load
end

#peak_loadObject (readonly)

Returns the value of attribute peak_load.



10
11
12
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/allostatic_load.rb', line 10

def peak_load
  @peak_load
end

Instance Method Details

#classificationObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/allostatic_load.rb', line 33

def classification
  if @load <= Constants::ALLOSTATIC_LOAD_HEALTHY
    :healthy
  elsif @load <= Constants::ALLOSTATIC_LOAD_ELEVATED
    :elevated
  elsif @load <= Constants::ALLOSTATIC_LOAD_CRITICAL
    :high
  else
    :critical
  end
end

#recovering?Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/allostatic_load.rb', line 45

def recovering?
  recent = @history.last(5)
  return false if recent.size < 3

  recent.last[:load] < recent.first[:load]
end

#resetObject



70
71
72
73
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/allostatic_load.rb', line 70

def reset
  @load = 0.0
  @history.clear
end

#to_hObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/allostatic_load.rb', line 75

def to_h
  {
    load:           @load,
    peak_load:      @peak_load,
    classification: classification,
    recovering:     recovering?,
    trend:          trend,
    history_size:   @history.size
  }
end

#trend(window: 20) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/allostatic_load.rb', line 52

def trend(window: 20)
  recent = @history.last(window)
  return :insufficient_data if recent.size < 3

  loads = recent.map { |h| h[:load] }
  avg_first_half = loads[0...(loads.size / 2)].sum / (loads.size / 2).to_f
  avg_second_half = loads[(loads.size / 2)..].sum / (loads.size - (loads.size / 2)).to_f

  delta = avg_second_half - avg_first_half
  if delta > 0.05
    :accumulating
  elsif delta < -0.05
    :recovering
  else
    :stable
  end
end

#update(regulator) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/allostatic_load.rb', line 18

def update(regulator)
  in_tolerance, deviating = regulator.setpoints.values.partition(&:within_tolerance?)

  accumulation = deviating.sum { |sp| sp.deviation_ratio * Constants::ALLOSTATIC_ACCUMULATION }
  recovery = in_tolerance.size * Constants::ALLOSTATIC_DECAY_RATE

  @load = (@load + accumulation - recovery).clamp(0.0, 1.0)
  @peak_load = [@peak_load, @load].max

  @history << { load: @load, deviating: deviating.size, at: Time.now.utc }
  @history = @history.last(Constants::MAX_ALLOSTATIC_HISTORY)

  @load
end