Class: Legion::Extensions::Agentic::Inference::PredictiveProcessing::Helpers::PredictiveProcessor

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb

Constant Summary

Constants included from Constants

Constants::ACTIVE_INFERENCE_THRESHOLD, Constants::DEFAULT_PRECISION, Constants::FREE_ENERGY_THRESHOLD, Constants::INFERENCE_MODES, Constants::LEARNING_RATE, Constants::MAX_HISTORY, Constants::MAX_MODELS, Constants::MAX_PREDICTIONS_PER_MODEL, Constants::MODEL_CONFIDENCE_FLOOR, Constants::MODEL_STATES, Constants::PRECISION_DECAY, Constants::PRECISION_FLOOR, Constants::PRECISION_LABELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePredictiveProcessor

Returns a new instance of PredictiveProcessor.



14
15
16
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb', line 14

def initialize
  @models = {}
end

Instance Attribute Details

#modelsObject (readonly)

Returns the value of attribute models.



12
13
14
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb', line 12

def models
  @models
end

Instance Method Details

#active_inference_candidatesObject



84
85
86
87
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb', line 84

def active_inference_candidates
  @models.select { |_d, m| m.free_energy > ACTIVE_INFERENCE_THRESHOLD }
         .keys
end

#add_model(domain:) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb', line 18

def add_model(domain:)
  return { added: false, reason: :limit_reached } if @models.size >= MAX_MODELS
  return { added: false, reason: :already_exists } if @models.key?(domain)

  model = GenerativeModel.new(domain: domain)
  @models[domain] = model
  { added: true, domain: domain, model_id: model.id }
end

#free_energy_for(domain) ⇒ Object



59
60
61
62
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb', line 59

def free_energy_for(domain)
  model = @models[domain]
  model&.free_energy
end

#global_free_energyObject



64
65
66
67
68
69
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb', line 64

def global_free_energy
  return 0.0 if @models.empty?

  total = @models.values.sum(&:free_energy)
  total / @models.size
end

#inference_mode(domain) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb', line 51

def inference_mode(domain)
  model = @models[domain]
  return :perceptual unless model

  fe = model.free_energy
  determine_mode(fe, model.precision)
end

#models_needing_updateObject



76
77
78
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb', line 76

def models_needing_update
  @models.select { |_d, m| m.surprised? }.transform_values(&:to_h)
end

#observe(domain:, actual:, predicted:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb', line 32

def observe(domain:, actual:, predicted:)
  model = @models[domain]
  return { observed: false, reason: :domain_not_found } unless model

  error = model.observe(actual: actual, predicted: predicted)
  mode  = inference_mode(domain)

  model.update_model(error: error) if %i[perceptual hybrid].include?(mode)

  {
    observed:         true,
    domain:           domain,
    prediction_error: error,
    inference_mode:   mode,
    free_energy:      model.free_energy,
    state:            model.state
  }
end

#precision_weight(domain) ⇒ Object



71
72
73
74
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb', line 71

def precision_weight(domain)
  model = @models[domain]
  model ? model.precision : DEFAULT_PRECISION
end

#predict(domain:, context: {}) ⇒ Object



27
28
29
30
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb', line 27

def predict(domain:, context: {})
  model = find_or_create(domain)
  model.predict(context: context)
end

#stable_modelsObject



80
81
82
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb', line 80

def stable_models
  @models.select { |_d, m| m.stable? }.transform_values(&:to_h)
end

#tickObject



89
90
91
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb', line 89

def tick
  @models.each_value(&:decay)
end

#to_hObject



93
94
95
96
97
98
99
100
101
102
# File 'lib/legion/extensions/agentic/inference/predictive_processing/helpers/predictive_processor.rb', line 93

def to_h
  {
    model_count:              @models.size,
    global_free_energy:       global_free_energy,
    models_needing_update:    models_needing_update.size,
    stable_model_count:       stable_models.size,
    active_inference_domains: active_inference_candidates.size,
    models:                   @models.transform_values(&:to_h)
  }
end