Class: Legion::Extensions::Agentic::Inference::FreeEnergy::Helpers::Belief

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#contentObject (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_atObject (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

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

#idObject (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_observationObject (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

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

#predictionObject (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_errorObject (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_atObject (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_precisionObject



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_energyObject



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

Returns:

  • (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_labelObject



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

Returns:

  • (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_hObject



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