Class: Legion::Extensions::Agentic::Homeostasis::Core::Helpers::Setpoint
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Homeostasis::Core::Helpers::Setpoint
- Defined in:
- lib/legion/extensions/agentic/homeostasis/core/helpers/setpoint.rb
Instance Attribute Summary collapse
-
#current_value ⇒ Object
readonly
Returns the value of attribute current_value.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#tolerance ⇒ Object
readonly
Returns the value of attribute tolerance.
Instance Method Summary collapse
- #adapt_target(alpha: Constants::SETPOINT_ADAPTATION_ALPHA) ⇒ Object
- #deviation_ratio ⇒ Object
-
#initialize(name:, target:, tolerance:) ⇒ Setpoint
constructor
A new instance of Setpoint.
- #to_h ⇒ Object
- #trend(window: 10) ⇒ Object
- #update(observed_value) ⇒ Object
- #within_tolerance? ⇒ Boolean
Constructor Details
#initialize(name:, target:, tolerance:) ⇒ Setpoint
Returns a new instance of Setpoint.
12 13 14 15 16 17 18 19 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/setpoint.rb', line 12 def initialize(name:, target:, tolerance:) @name = name @target = target.to_f @tolerance = tolerance.to_f @current_value = @target @error = 0.0 @history = [] end |
Instance Attribute Details
#current_value ⇒ Object (readonly)
Returns the value of attribute current_value.
10 11 12 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/setpoint.rb', line 10 def current_value @current_value end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
10 11 12 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/setpoint.rb', line 10 def error @error end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
10 11 12 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/setpoint.rb', line 10 def history @history end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/setpoint.rb', line 10 def name @name end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
10 11 12 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/setpoint.rb', line 10 def target @target end |
#tolerance ⇒ Object (readonly)
Returns the value of attribute tolerance.
10 11 12 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/setpoint.rb', line 10 def tolerance @tolerance end |
Instance Method Details
#adapt_target(alpha: Constants::SETPOINT_ADAPTATION_ALPHA) ⇒ Object
39 40 41 42 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/setpoint.rb', line 39 def adapt_target(alpha: Constants::SETPOINT_ADAPTATION_ALPHA) @target += (alpha * @error) @target = @target.clamp(0.0, 1.0) end |
#deviation_ratio ⇒ Object
33 34 35 36 37 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/setpoint.rb', line 33 def deviation_ratio return 0.0 if @tolerance.zero? (@error.abs / @tolerance).clamp(0.0, 3.0) end |
#to_h ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/setpoint.rb', line 60 def to_h { name: @name, target: @target, tolerance: @tolerance, current_value: @current_value, error: @error, within_tolerance: within_tolerance?, deviation_ratio: deviation_ratio, trend: trend, history_size: @history.size } end |
#trend(window: 10) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/setpoint.rb', line 44 def trend(window: 10) recent = @history.last(window) return :insufficient_data if recent.size < 3 values = recent.map { |h| h[:value] } slope = linear_slope(values) if slope > 0.01 :rising elsif slope < -0.01 :falling else :stable end end |
#update(observed_value) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/setpoint.rb', line 21 def update(observed_value) @current_value = observed_value.to_f @error = @current_value - @target @history << { value: @current_value, error: @error, at: Time.now.utc } @history = @history.last(Constants::MAX_REGULATION_HISTORY) @error end |
#within_tolerance? ⇒ Boolean
29 30 31 |
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/setpoint.rb', line 29 def within_tolerance? @error.abs <= @tolerance end |