Class: Legion::Extensions::Agentic::Inference::PerceptualInference::Helpers::PerceptualHypothesis

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content:, modality:, domain: :general, prior: DEFAULT_PRIOR) ⇒ PerceptualHypothesis

Returns a new instance of PerceptualHypothesis.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb', line 15

def initialize(content:, modality:, domain: :general, prior: DEFAULT_PRIOR)
  @id         = SecureRandom.uuid
  @content    = content
  @modality   = modality
  @domain     = domain
  @prior      = clamp_prior(prior.to_f)
  @likelihood = 0.5
  @posterior  = @prior
  @state      = :active
  @created_at = Time.now.utc
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



12
13
14
# File 'lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.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/perceptual_inference/helpers/perceptual_hypothesis.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/perceptual_inference/helpers/perceptual_hypothesis.rb', line 12

def domain
  @domain
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#likelihoodObject

Returns the value of attribute likelihood.



13
14
15
# File 'lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb', line 13

def likelihood
  @likelihood
end

#modalityObject (readonly)

Returns the value of attribute modality.



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

def modality
  @modality
end

#posteriorObject

Returns the value of attribute posterior.



13
14
15
# File 'lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb', line 13

def posterior
  @posterior
end

#priorObject

Returns the value of attribute prior.



13
14
15
# File 'lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb', line 13

def prior
  @prior
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

Instance Method Details

#adapt_prior(outcome:) ⇒ Object



51
52
53
54
# File 'lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb', line 51

def adapt_prior(outcome:)
  delta    = outcome == :correct ? ADAPTATION_RATE : -ADAPTATION_RATE
  @prior   = clamp_prior(@prior + delta)
end

#compute_posterior(evidence_weight:) ⇒ Object



27
28
29
30
31
# File 'lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb', line 27

def compute_posterior(evidence_weight:)
  weight   = [evidence_weight.to_f, EVIDENCE_STRENGTH_FLOOR].max
  raw      = @prior * ((@likelihood * weight) + ((1.0 - weight) * @prior))
  @posterior = clamp_unit(raw)
end

#decayObject



56
57
58
59
60
61
62
63
# File 'lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb', line 56

def decay
  if @state == :suppressed
    @prior = clamp_prior(@prior - (DECAY_RATE * 5))
    @state = :decayed if @prior <= PRIOR_FLOOR + 0.001
  else
    @prior = clamp_prior(@prior + ((DEFAULT_PRIOR - @prior) * DECAY_RATE))
  end
end

#percept_labelObject



65
66
67
68
# File 'lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb', line 65

def percept_label
  PERCEPT_LABELS.each { |range, label| return label if range.cover?(@posterior) }
  :subliminal
end

#rival_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb', line 45

def rival_with?(other)
  return false unless other.is_a?(PerceptualHypothesis)

  (posterior - other.posterior).abs <= RIVALRY_MARGIN
end

#select!Object



33
34
35
# File 'lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb', line 33

def select!
  @state = :selected
end

#selected?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb', line 41

def selected?
  @state == :selected
end

#suppress!Object



37
38
39
# File 'lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb', line 37

def suppress!
  @state = :suppressed
end

#to_hObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_hypothesis.rb', line 70

def to_h
  {
    id:         @id,
    content:    @content,
    modality:   @modality,
    domain:     @domain,
    prior:      @prior.round(4),
    likelihood: @likelihood.round(4),
    posterior:  @posterior.round(4),
    state:      @state,
    label:      percept_label,
    created_at: @created_at
  }
end