Class: Legion::Extensions::Agentic::Homeostasis::Core::Helpers::Regulator
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Homeostasis::Core::Helpers::Regulator
- Defined in:
- lib/legion/extensions/agentic/homeostasis/core/helpers/regulator.rb
Instance Attribute Summary collapse
-
#regulation_count ⇒ Object
readonly
Returns the value of attribute regulation_count.
-
#setpoints ⇒ Object
readonly
Returns the value of attribute setpoints.
-
#signals ⇒ Object
readonly
Returns the value of attribute signals.
Instance Method Summary collapse
- #adapt_setpoints ⇒ Object
- #health_label ⇒ Object
-
#initialize ⇒ Regulator
constructor
A new instance of Regulator.
- #regulate(observations) ⇒ Object
- #regulation_health ⇒ Object
- #subsystem_status(subsystem) ⇒ Object
- #to_h ⇒ Object
- #worst_deviation ⇒ Object
Constructor Details
#initialize ⇒ Regulator
Returns a new instance of Regulator.
12 13 14 15 16 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/regulator.rb', line 12 def initialize @setpoints = build_setpoints @signals = {} @regulation_count = 0 end |
Instance Attribute Details
#regulation_count ⇒ Object (readonly)
Returns the value of attribute regulation_count.
10 11 12 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/regulator.rb', line 10 def regulation_count @regulation_count end |
#setpoints ⇒ Object (readonly)
Returns the value of attribute setpoints.
10 11 12 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/regulator.rb', line 10 def setpoints @setpoints end |
#signals ⇒ Object (readonly)
Returns the value of attribute signals.
10 11 12 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/regulator.rb', line 10 def signals @signals end |
Instance Method Details
#adapt_setpoints ⇒ Object
73 74 75 76 77 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/regulator.rb', line 73 def adapt_setpoints @setpoints.each_value do |sp| sp.adapt_target if sp.within_tolerance? end end |
#health_label ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/regulator.rb', line 50 def health_label health = regulation_health Constants::REGULATION_HEALTH.each do |range, label| return label if range.cover?(health) end :unknown end |
#regulate(observations) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/regulator.rb', line 18 def regulate(observations) @regulation_count += 1 new_signals = {} observations.each do |subsystem, value| sp = @setpoints[subsystem] next unless sp error = sp.update(value) gain = Constants::REGULATION_GAIN.fetch(subsystem, 0.2) signal = compute_signal(error, sp.tolerance, gain) new_signals[subsystem] = { type: classify_signal(signal), magnitude: signal.abs, direction: signal, error: error, within_tolerance: sp.within_tolerance? } end @signals = new_signals new_signals end |
#regulation_health ⇒ Object
43 44 45 46 47 48 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/regulator.rb', line 43 def regulation_health return 1.0 if @setpoints.empty? in_tolerance = @setpoints.values.count(&:within_tolerance?) in_tolerance.to_f / @setpoints.size end |
#subsystem_status(subsystem) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/regulator.rb', line 65 def subsystem_status(subsystem) sp = @setpoints[subsystem] return nil unless sp signal = @signals[subsystem] sp.to_h.merge(signal: signal) end |
#to_h ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/regulator.rb', line 79 def to_h { setpoints: @setpoints.transform_values(&:to_h), signals: @signals, regulation_count: @regulation_count, health: regulation_health, health_label: health_label } end |
#worst_deviation ⇒ Object
58 59 60 61 62 63 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/regulator.rb', line 58 def worst_deviation worst = @setpoints.values.max_by(&:deviation_ratio) return nil unless worst { subsystem: worst.name, deviation_ratio: worst.deviation_ratio, error: worst.error } end |