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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_valueObject (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

#errorObject (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

#historyObject (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

#nameObject (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

#targetObject (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

#toleranceObject (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_ratioObject



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_hObject



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

Returns:

  • (Boolean)


29
30
31
# File 'lib/legion/extensions/agentic/homeostasis/core/helpers/setpoint.rb', line 29

def within_tolerance?
  @error.abs <= @tolerance
end