Class: Legion::Extensions::Agentic::Inference::FreeEnergy::Helpers::Belief
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Inference::FreeEnergy::Helpers::Belief
- Includes:
- Constants
- Defined in:
- lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb
Constant Summary
Constants included from Constants
Constants::DEFAULT_PRECISION, Constants::FREE_ENERGY_THRESHOLD, Constants::INFERENCE_MODES, Constants::LEARNING_RATE, Constants::MAX_ACTIONS, Constants::MAX_BELIEFS, Constants::MAX_HISTORY, Constants::PRECISION_CEILING, Constants::PRECISION_DECAY, Constants::PRECISION_FLOOR, Constants::PRECISION_UPDATE_RATE, Constants::STALE_THRESHOLD, Constants::SURPRISE_LABELS
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#last_observation ⇒ Object
readonly
Returns the value of attribute last_observation.
-
#precision ⇒ Object
readonly
Returns the value of attribute precision.
-
#prediction ⇒ Object
readonly
Returns the value of attribute prediction.
-
#prediction_error ⇒ Object
readonly
Returns the value of attribute prediction_error.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
Instance Method Summary collapse
- #decay_precision ⇒ Object
- #free_energy ⇒ Object
-
#initialize(id:, content:, domain: :general, prediction: {}, precision: DEFAULT_PRECISION) ⇒ Belief
constructor
A new instance of Belief.
- #observe(observation:) ⇒ Object
- #revise_prediction(observation:) ⇒ Object
- #stale?(now: Time.now.utc) ⇒ Boolean
- #surprise_label ⇒ Object
- #surprising? ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(id:, content:, domain: :general, prediction: {}, precision: DEFAULT_PRECISION) ⇒ Belief
Returns a new instance of Belief.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 15 def initialize(id:, content:, domain: :general, prediction: {}, precision: DEFAULT_PRECISION) @id = id @content = content @domain = domain @prediction = prediction @precision = precision.clamp(PRECISION_FLOOR, PRECISION_CEILING) @last_observation = nil @prediction_error = 0.0 @created_at = Time.now.utc @updated_at = @created_at end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
12 13 14 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 12 def content @content end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
12 13 14 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 12 def created_at @created_at end |
#domain ⇒ Object (readonly)
Returns the value of attribute domain.
12 13 14 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 12 def domain @domain end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
12 13 14 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 12 def id @id end |
#last_observation ⇒ Object (readonly)
Returns the value of attribute last_observation.
12 13 14 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 12 def last_observation @last_observation end |
#precision ⇒ Object (readonly)
Returns the value of attribute precision.
12 13 14 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 12 def precision @precision end |
#prediction ⇒ Object (readonly)
Returns the value of attribute prediction.
12 13 14 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 12 def prediction @prediction end |
#prediction_error ⇒ Object (readonly)
Returns the value of attribute prediction_error.
12 13 14 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 12 def prediction_error @prediction_error end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
12 13 14 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 12 def updated_at @updated_at end |
Instance Method Details
#decay_precision ⇒ Object
67 68 69 70 71 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 67 def decay_precision diff = @precision - DEFAULT_PRECISION @precision -= diff * PRECISION_DECAY @precision = @precision.clamp(PRECISION_FLOOR, PRECISION_CEILING) end |
#free_energy ⇒ Object
35 36 37 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 35 def free_energy @prediction_error * @precision end |
#observe(observation:) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 27 def observe(observation:) @last_observation = observation @prediction_error = compute_error(observation) update_precision @updated_at = Time.now.utc self end |
#revise_prediction(observation:) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 48 def revise_prediction(observation:) merged = @prediction.dup observation.each do |key, value| current = merged[key] merged[key] = if current.is_a?(Numeric) && value.is_a?(Numeric) current + (LEARNING_RATE * (value - current)) else value end end @prediction = merged @updated_at = Time.now.utc self end |
#stale?(now: Time.now.utc) ⇒ Boolean
63 64 65 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 63 def stale?(now: Time.now.utc) (now - @updated_at) > STALE_THRESHOLD end |
#surprise_label ⇒ Object
43 44 45 46 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 43 def surprise_label fe = free_energy SURPRISE_LABELS.find { |range, _| range.cover?(fe) }&.last || :trivial end |
#surprising? ⇒ Boolean
39 40 41 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 39 def surprising? free_energy > FREE_ENERGY_THRESHOLD end |
#to_h ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/legion/extensions/agentic/inference/free_energy/helpers/belief.rb', line 73 def to_h { id: @id, domain: @domain, content: @content, prediction: @prediction, last_observation: @last_observation, prediction_error: @prediction_error.round(4), precision: @precision.round(4), free_energy: free_energy.round(4), surprise_label: surprise_label, surprising: surprising?, created_at: @created_at, updated_at: @updated_at } end |