lex-homeostasis 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +13 -0
- data/LICENSE +21 -0
- data/README.md +90 -0
- data/lex-homeostasis.gemspec +29 -0
- data/lib/legion/extensions/homeostasis/client.rb +18 -0
- data/lib/legion/extensions/homeostasis/helpers/allostatic_load.rb +87 -0
- data/lib/legion/extensions/homeostasis/helpers/constants.rb +64 -0
- data/lib/legion/extensions/homeostasis/helpers/regulator.rb +115 -0
- data/lib/legion/extensions/homeostasis/helpers/setpoint.rb +92 -0
- data/lib/legion/extensions/homeostasis/runners/homeostasis.rb +161 -0
- data/lib/legion/extensions/homeostasis/version.rb +9 -0
- data/lib/legion/extensions/homeostasis.rb +17 -0
- data/spec/legion/extensions/homeostasis/client_spec.rb +23 -0
- data/spec/legion/extensions/homeostasis/helpers/allostatic_load_spec.rb +115 -0
- data/spec/legion/extensions/homeostasis/helpers/constants_spec.rb +51 -0
- data/spec/legion/extensions/homeostasis/helpers/regulator_spec.rb +107 -0
- data/spec/legion/extensions/homeostasis/helpers/setpoint_spec.rb +113 -0
- data/spec/legion/extensions/homeostasis/runners/homeostasis_spec.rb +118 -0
- data/spec/spec_helper.rb +36 -0
- metadata +80 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 24b989d7a6ff4c7dccbcf03f5d43f79c777e9bfac5d5549cb175fe9faa4787d0
|
|
4
|
+
data.tar.gz: d3b1994e7103256f6d6d4f4501304178b296f3c37ba01f66b1e066579dd43b7b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 9db26cd90fada5f87e7801377b2d576d3e98c4401b9debc3b50ca09e15ec661d030d72090eef3881851bec2256b15084093eb62f59e1d071e94828318b9f61b0
|
|
7
|
+
data.tar.gz: 03735a57a5408073d4d3816d28eedd7d83de90fdc7a9f6cd2e45c9d3736a369f74d464d5384fe4288e46f80f13a65df016df6d2ffe0ec886a7b7a3043519dd15
|
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matthew Iverson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# lex-homeostasis
|
|
2
|
+
|
|
3
|
+
Cognitive self-regulation for LegionIO agents. Part of the LegionIO cognitive architecture extension ecosystem (LEX).
|
|
4
|
+
|
|
5
|
+
## What It Does
|
|
6
|
+
|
|
7
|
+
`lex-homeostasis` is the agent's internal thermostat. It maintains seven setpoints covering emotional arousal, curiosity, cognitive load, memory health, prediction accuracy, trust stability, and attention breadth. Each tick, observed values are compared to targets; deviations produce regulation signals (dampen, amplify, or hold) that other subsystems can consume to correct their behavior. Sustained deviation accumulates allostatic load, modeling chronic cognitive strain.
|
|
8
|
+
|
|
9
|
+
Key capabilities:
|
|
10
|
+
|
|
11
|
+
- **Seven setpoints**: emotional_arousal, curiosity_intensity, cognitive_load, memory_health, prediction_accuracy, trust_stability, attention_breadth
|
|
12
|
+
- **Proportional negative feedback**: regulation magnitude scales with how far a value has drifted from its target
|
|
13
|
+
- **Allostatic load tracking**: cumulative stress from prolonged out-of-range states, with four classifications (healthy/elevated/high/critical)
|
|
14
|
+
- **Adaptive targets**: setpoints slowly drift toward the agent's natural operating range via EMA
|
|
15
|
+
- **Modulation signals**: per-subsystem dampen/amplify/hold signals consumable by other extensions
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
Add to your Gemfile:
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
gem 'lex-homeostasis'
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Or install directly:
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
gem install lex-homeostasis
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
require 'legion/extensions/homeostasis'
|
|
35
|
+
|
|
36
|
+
client = Legion::Extensions::Homeostasis::Client.new
|
|
37
|
+
|
|
38
|
+
# Run the regulation cycle with tick results from the current tick
|
|
39
|
+
result = client.regulate(tick_results: tick_phase_results)
|
|
40
|
+
# => { signals: { emotional_arousal: { type: :dampen, magnitude: 0.15 }, ... },
|
|
41
|
+
# regulation_health: 0.85, allostatic_load: 0.12, health_label: :excellent }
|
|
42
|
+
|
|
43
|
+
# Query the last regulation signal for a specific subsystem
|
|
44
|
+
signal = client.modulation_for(subsystem: :curiosity_intensity)
|
|
45
|
+
# => { type: :hold, magnitude: 0.0, subsystem: :curiosity_intensity }
|
|
46
|
+
|
|
47
|
+
# Check allostatic load
|
|
48
|
+
status = client.allostatic_status
|
|
49
|
+
# => { load: 0.12, classification: :healthy, recovering: true, trend: -0.01 }
|
|
50
|
+
|
|
51
|
+
# Overview of all setpoints
|
|
52
|
+
client.regulation_overview
|
|
53
|
+
|
|
54
|
+
# Summary stats
|
|
55
|
+
client.homeostasis_stats
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Runner Methods
|
|
59
|
+
|
|
60
|
+
| Method | Description |
|
|
61
|
+
|---|---|
|
|
62
|
+
| `regulate` | Full regulation cycle; returns signals + health summary |
|
|
63
|
+
| `modulation_for` | Last regulation signal for a specific subsystem |
|
|
64
|
+
| `allostatic_status` | Load value, classification, trend, recovering flag |
|
|
65
|
+
| `regulation_overview` | All setpoint current values, deviations, and regulation health |
|
|
66
|
+
| `homeostasis_stats` | Summary: health label, allostatic classification, worst deviation |
|
|
67
|
+
|
|
68
|
+
## Monitored Setpoints
|
|
69
|
+
|
|
70
|
+
| Subsystem | Target | Tolerance |
|
|
71
|
+
|---|---|---|
|
|
72
|
+
| emotional_arousal | 0.4 | 0.15 |
|
|
73
|
+
| curiosity_intensity | 0.5 | 0.2 |
|
|
74
|
+
| cognitive_load | 0.6 | 0.2 |
|
|
75
|
+
| memory_health | 0.7 | 0.15 |
|
|
76
|
+
| prediction_accuracy | 0.6 | 0.2 |
|
|
77
|
+
| trust_stability | 0.7 | 0.2 |
|
|
78
|
+
| attention_breadth | 0.5 | 0.2 |
|
|
79
|
+
|
|
80
|
+
## Development
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
bundle install
|
|
84
|
+
bundle exec rspec
|
|
85
|
+
bundle exec rubocop
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
MIT
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'lib/legion/extensions/homeostasis/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = 'lex-homeostasis'
|
|
7
|
+
spec.version = Legion::Extensions::Homeostasis::VERSION
|
|
8
|
+
spec.authors = ['Esity']
|
|
9
|
+
spec.email = ['matthewdiverson@gmail.com']
|
|
10
|
+
|
|
11
|
+
spec.summary = 'LEX Homeostasis'
|
|
12
|
+
spec.description = 'Cognitive self-regulation with setpoints, negative feedback loops, and allostatic load tracking'
|
|
13
|
+
spec.homepage = 'https://github.com/LegionIO/lex-homeostasis'
|
|
14
|
+
spec.license = 'MIT'
|
|
15
|
+
spec.required_ruby_version = '>= 3.4'
|
|
16
|
+
|
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
|
18
|
+
spec.metadata['source_code_uri'] = 'https://github.com/LegionIO/lex-homeostasis'
|
|
19
|
+
spec.metadata['documentation_uri'] = 'https://github.com/LegionIO/lex-homeostasis'
|
|
20
|
+
spec.metadata['changelog_uri'] = 'https://github.com/LegionIO/lex-homeostasis'
|
|
21
|
+
spec.metadata['bug_tracker_uri'] = 'https://github.com/LegionIO/lex-homeostasis/issues'
|
|
22
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
|
23
|
+
|
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
25
|
+
Dir.glob('{lib,spec}/**/*') + %w[lex-homeostasis.gemspec Gemfile LICENSE README.md]
|
|
26
|
+
end
|
|
27
|
+
spec.require_paths = ['lib']
|
|
28
|
+
spec.add_development_dependency 'legion-gaia'
|
|
29
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Homeostasis
|
|
6
|
+
class Client
|
|
7
|
+
include Runners::Homeostasis
|
|
8
|
+
|
|
9
|
+
attr_reader :regulator, :allostatic_load
|
|
10
|
+
|
|
11
|
+
def initialize(regulator: nil, allostatic_load: nil, **)
|
|
12
|
+
@regulator = regulator || Helpers::Regulator.new
|
|
13
|
+
@allostatic_load = allostatic_load || Helpers::AllostaticLoad.new
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Homeostasis
|
|
6
|
+
module Helpers
|
|
7
|
+
class AllostaticLoad
|
|
8
|
+
attr_reader :load, :history, :peak_load
|
|
9
|
+
|
|
10
|
+
def initialize
|
|
11
|
+
@load = 0.0
|
|
12
|
+
@peak_load = 0.0
|
|
13
|
+
@history = []
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def update(regulator)
|
|
17
|
+
in_tolerance, deviating = regulator.setpoints.values.partition(&:within_tolerance?)
|
|
18
|
+
|
|
19
|
+
accumulation = deviating.sum { |sp| sp.deviation_ratio * Constants::ALLOSTATIC_ACCUMULATION }
|
|
20
|
+
recovery = in_tolerance.size * Constants::ALLOSTATIC_DECAY_RATE
|
|
21
|
+
|
|
22
|
+
@load = (@load + accumulation - recovery).clamp(0.0, 1.0)
|
|
23
|
+
@peak_load = [@peak_load, @load].max
|
|
24
|
+
|
|
25
|
+
@history << { load: @load, deviating: deviating.size, at: Time.now.utc }
|
|
26
|
+
@history = @history.last(Constants::MAX_ALLOSTATIC_HISTORY)
|
|
27
|
+
|
|
28
|
+
@load
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def classification
|
|
32
|
+
if @load <= Constants::ALLOSTATIC_LOAD_HEALTHY
|
|
33
|
+
:healthy
|
|
34
|
+
elsif @load <= Constants::ALLOSTATIC_LOAD_ELEVATED
|
|
35
|
+
:elevated
|
|
36
|
+
elsif @load <= Constants::ALLOSTATIC_LOAD_CRITICAL
|
|
37
|
+
:high
|
|
38
|
+
else
|
|
39
|
+
:critical
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def recovering?
|
|
44
|
+
recent = @history.last(5)
|
|
45
|
+
return false if recent.size < 3
|
|
46
|
+
|
|
47
|
+
recent.last[:load] < recent.first[:load]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def trend(window: 20)
|
|
51
|
+
recent = @history.last(window)
|
|
52
|
+
return :insufficient_data if recent.size < 3
|
|
53
|
+
|
|
54
|
+
loads = recent.map { |h| h[:load] }
|
|
55
|
+
avg_first_half = loads[0...(loads.size / 2)].sum / (loads.size / 2).to_f
|
|
56
|
+
avg_second_half = loads[(loads.size / 2)..].sum / (loads.size - (loads.size / 2)).to_f
|
|
57
|
+
|
|
58
|
+
delta = avg_second_half - avg_first_half
|
|
59
|
+
if delta > 0.05
|
|
60
|
+
:accumulating
|
|
61
|
+
elsif delta < -0.05
|
|
62
|
+
:recovering
|
|
63
|
+
else
|
|
64
|
+
:stable
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def reset
|
|
69
|
+
@load = 0.0
|
|
70
|
+
@history.clear
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def to_h
|
|
74
|
+
{
|
|
75
|
+
load: @load,
|
|
76
|
+
peak_load: @peak_load,
|
|
77
|
+
classification: classification,
|
|
78
|
+
recovering: recovering?,
|
|
79
|
+
trend: trend,
|
|
80
|
+
history_size: @history.size
|
|
81
|
+
}
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Homeostasis
|
|
6
|
+
module Helpers
|
|
7
|
+
module Constants
|
|
8
|
+
# Regulated subsystems and their ideal setpoints (0.0-1.0 scale)
|
|
9
|
+
SETPOINTS = {
|
|
10
|
+
emotional_arousal: { target: 0.4, tolerance: 0.2 },
|
|
11
|
+
curiosity_intensity: { target: 0.5, tolerance: 0.25 },
|
|
12
|
+
cognitive_load: { target: 0.6, tolerance: 0.2 },
|
|
13
|
+
memory_health: { target: 0.7, tolerance: 0.15 },
|
|
14
|
+
prediction_accuracy: { target: 0.6, tolerance: 0.2 },
|
|
15
|
+
trust_stability: { target: 0.7, tolerance: 0.15 },
|
|
16
|
+
attention_breadth: { target: 0.5, tolerance: 0.2 }
|
|
17
|
+
}.freeze
|
|
18
|
+
|
|
19
|
+
# Regulation gains (how aggressively the system corrects)
|
|
20
|
+
# Higher gain = faster correction but more oscillation risk
|
|
21
|
+
REGULATION_GAIN = {
|
|
22
|
+
emotional_arousal: 0.3,
|
|
23
|
+
curiosity_intensity: 0.2,
|
|
24
|
+
cognitive_load: 0.4,
|
|
25
|
+
memory_health: 0.1,
|
|
26
|
+
prediction_accuracy: 0.15,
|
|
27
|
+
trust_stability: 0.1,
|
|
28
|
+
attention_breadth: 0.2
|
|
29
|
+
}.freeze
|
|
30
|
+
|
|
31
|
+
# Allostatic load thresholds
|
|
32
|
+
ALLOSTATIC_LOAD_HEALTHY = 0.3
|
|
33
|
+
ALLOSTATIC_LOAD_ELEVATED = 0.6
|
|
34
|
+
ALLOSTATIC_LOAD_CRITICAL = 0.85
|
|
35
|
+
ALLOSTATIC_DECAY_RATE = 0.02 # per tick when within tolerance
|
|
36
|
+
ALLOSTATIC_ACCUMULATION = 0.05 # per tick when outside tolerance
|
|
37
|
+
|
|
38
|
+
# Modulation signal bounds
|
|
39
|
+
MAX_DAMPEN = -0.5 # maximum dampening signal
|
|
40
|
+
MAX_AMPLIFY = 0.5 # maximum amplification signal
|
|
41
|
+
|
|
42
|
+
# EMA alpha for setpoint adaptation
|
|
43
|
+
SETPOINT_ADAPTATION_ALPHA = 0.05
|
|
44
|
+
|
|
45
|
+
# History limits
|
|
46
|
+
MAX_REGULATION_HISTORY = 100
|
|
47
|
+
MAX_ALLOSTATIC_HISTORY = 200
|
|
48
|
+
|
|
49
|
+
# Regulation signal types
|
|
50
|
+
SIGNAL_TYPES = %i[dampen amplify hold].freeze
|
|
51
|
+
|
|
52
|
+
# Health classification thresholds
|
|
53
|
+
REGULATION_HEALTH = {
|
|
54
|
+
(0.8..) => :stable,
|
|
55
|
+
(0.6...0.8) => :compensating,
|
|
56
|
+
(0.4...0.6) => :strained,
|
|
57
|
+
(0.2...0.4) => :dysregulated,
|
|
58
|
+
(..0.2) => :critical
|
|
59
|
+
}.freeze
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Homeostasis
|
|
6
|
+
module Helpers
|
|
7
|
+
class Regulator
|
|
8
|
+
attr_reader :setpoints, :signals, :regulation_count
|
|
9
|
+
|
|
10
|
+
def initialize
|
|
11
|
+
@setpoints = build_setpoints
|
|
12
|
+
@signals = {}
|
|
13
|
+
@regulation_count = 0
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def regulate(observations)
|
|
17
|
+
@regulation_count += 1
|
|
18
|
+
new_signals = {}
|
|
19
|
+
|
|
20
|
+
observations.each do |subsystem, value|
|
|
21
|
+
sp = @setpoints[subsystem]
|
|
22
|
+
next unless sp
|
|
23
|
+
|
|
24
|
+
error = sp.update(value)
|
|
25
|
+
gain = Constants::REGULATION_GAIN.fetch(subsystem, 0.2)
|
|
26
|
+
signal = compute_signal(error, sp.tolerance, gain)
|
|
27
|
+
|
|
28
|
+
new_signals[subsystem] = {
|
|
29
|
+
type: classify_signal(signal),
|
|
30
|
+
magnitude: signal.abs,
|
|
31
|
+
direction: signal,
|
|
32
|
+
error: error,
|
|
33
|
+
within_tolerance: sp.within_tolerance?
|
|
34
|
+
}
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
@signals = new_signals
|
|
38
|
+
new_signals
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def regulation_health
|
|
42
|
+
return 1.0 if @setpoints.empty?
|
|
43
|
+
|
|
44
|
+
in_tolerance = @setpoints.values.count(&:within_tolerance?)
|
|
45
|
+
in_tolerance.to_f / @setpoints.size
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def health_label
|
|
49
|
+
health = regulation_health
|
|
50
|
+
Constants::REGULATION_HEALTH.each do |range, label|
|
|
51
|
+
return label if range.cover?(health)
|
|
52
|
+
end
|
|
53
|
+
:unknown
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def worst_deviation
|
|
57
|
+
worst = @setpoints.values.max_by(&:deviation_ratio)
|
|
58
|
+
return nil unless worst
|
|
59
|
+
|
|
60
|
+
{ subsystem: worst.name, deviation_ratio: worst.deviation_ratio, error: worst.error }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def subsystem_status(subsystem)
|
|
64
|
+
sp = @setpoints[subsystem]
|
|
65
|
+
return nil unless sp
|
|
66
|
+
|
|
67
|
+
signal = @signals[subsystem]
|
|
68
|
+
sp.to_h.merge(signal: signal)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def adapt_setpoints
|
|
72
|
+
@setpoints.each_value do |sp|
|
|
73
|
+
sp.adapt_target if sp.within_tolerance?
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def to_h
|
|
78
|
+
{
|
|
79
|
+
setpoints: @setpoints.transform_values(&:to_h),
|
|
80
|
+
signals: @signals,
|
|
81
|
+
regulation_count: @regulation_count,
|
|
82
|
+
health: regulation_health,
|
|
83
|
+
health_label: health_label
|
|
84
|
+
}
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
private
|
|
88
|
+
|
|
89
|
+
def build_setpoints
|
|
90
|
+
Constants::SETPOINTS.to_h do |name, config|
|
|
91
|
+
[name, Setpoint.new(name: name, target: config[:target], tolerance: config[:tolerance])]
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def compute_signal(error, tolerance, gain)
|
|
96
|
+
return 0.0 if error.abs <= tolerance * 0.1
|
|
97
|
+
|
|
98
|
+
correction = -error * gain
|
|
99
|
+
correction.clamp(Constants::MAX_DAMPEN, Constants::MAX_AMPLIFY)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def classify_signal(signal)
|
|
103
|
+
if signal < -0.01
|
|
104
|
+
:dampen
|
|
105
|
+
elsif signal > 0.01
|
|
106
|
+
:amplify
|
|
107
|
+
else
|
|
108
|
+
:hold
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Homeostasis
|
|
6
|
+
module Helpers
|
|
7
|
+
class Setpoint
|
|
8
|
+
attr_reader :name, :target, :tolerance, :current_value, :error, :history
|
|
9
|
+
|
|
10
|
+
def initialize(name:, target:, tolerance:)
|
|
11
|
+
@name = name
|
|
12
|
+
@target = target.to_f
|
|
13
|
+
@tolerance = tolerance.to_f
|
|
14
|
+
@current_value = @target
|
|
15
|
+
@error = 0.0
|
|
16
|
+
@history = []
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def update(observed_value)
|
|
20
|
+
@current_value = observed_value.to_f
|
|
21
|
+
@error = @current_value - @target
|
|
22
|
+
@history << { value: @current_value, error: @error, at: Time.now.utc }
|
|
23
|
+
@history = @history.last(Constants::MAX_REGULATION_HISTORY)
|
|
24
|
+
@error
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def within_tolerance?
|
|
28
|
+
@error.abs <= @tolerance
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def deviation_ratio
|
|
32
|
+
return 0.0 if @tolerance.zero?
|
|
33
|
+
|
|
34
|
+
(@error.abs / @tolerance).clamp(0.0, 3.0)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def adapt_target(alpha: Constants::SETPOINT_ADAPTATION_ALPHA)
|
|
38
|
+
@target += (alpha * @error)
|
|
39
|
+
@target = @target.clamp(0.0, 1.0)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def trend(window: 10)
|
|
43
|
+
recent = @history.last(window)
|
|
44
|
+
return :insufficient_data if recent.size < 3
|
|
45
|
+
|
|
46
|
+
values = recent.map { |h| h[:value] }
|
|
47
|
+
slope = linear_slope(values)
|
|
48
|
+
|
|
49
|
+
if slope > 0.01
|
|
50
|
+
:rising
|
|
51
|
+
elsif slope < -0.01
|
|
52
|
+
:falling
|
|
53
|
+
else
|
|
54
|
+
:stable
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def to_h
|
|
59
|
+
{
|
|
60
|
+
name: @name,
|
|
61
|
+
target: @target,
|
|
62
|
+
tolerance: @tolerance,
|
|
63
|
+
current_value: @current_value,
|
|
64
|
+
error: @error,
|
|
65
|
+
within_tolerance: within_tolerance?,
|
|
66
|
+
deviation_ratio: deviation_ratio,
|
|
67
|
+
trend: trend,
|
|
68
|
+
history_size: @history.size
|
|
69
|
+
}
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
private
|
|
73
|
+
|
|
74
|
+
def linear_slope(values)
|
|
75
|
+
n = values.size
|
|
76
|
+
return 0.0 if n < 2
|
|
77
|
+
|
|
78
|
+
sum_x = n * (n - 1) / 2.0
|
|
79
|
+
sum_y = values.sum
|
|
80
|
+
sum_xy = values.each_with_index.sum { |y, x| x * y }
|
|
81
|
+
sum_x2 = (0...n).sum { |x| x * x }
|
|
82
|
+
|
|
83
|
+
denominator = (n * sum_x2) - (sum_x * sum_x)
|
|
84
|
+
return 0.0 if denominator.zero?
|
|
85
|
+
|
|
86
|
+
((n * sum_xy) - (sum_x * sum_y)) / denominator
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|