Class: Legion::Extensions::Agentic::Inference::Abductive::Helpers::Hypothesis

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

Constant Summary collapse

SUPPORT_EVIDENCE_THRESHOLD =
3

Constants included from Constants

Constants::CONTRADICTION_PENALTY, Constants::DECAY_RATE, Constants::DEFAULT_PLAUSIBILITY, Constants::EVIDENCE_BOOST, Constants::EXPLANATORY_POWER_WEIGHT, Constants::HYPOTHESIS_STATES, Constants::MAX_EXPLANATIONS, Constants::MAX_HISTORY, Constants::MAX_HYPOTHESES, Constants::MAX_OBSERVATIONS, Constants::PLAUSIBILITY_CEILING, Constants::PLAUSIBILITY_FLOOR, Constants::PRIOR_WEIGHT, Constants::QUALITY_LABELS, Constants::SIMPLICITY_WEIGHT, Constants::STALE_THRESHOLD, Constants::SURPRISE_LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content:, observation_ids:, domain:, simplicity:, explanatory_power:, prior_probability: Constants::DEFAULT_PLAUSIBILITY) ⇒ Hypothesis

Returns a new instance of Hypothesis.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/legion/extensions/agentic/inference/abductive/helpers/hypothesis.rb', line 22

def initialize(content:, observation_ids:, domain:, simplicity:, explanatory_power:,
               prior_probability: Constants::DEFAULT_PLAUSIBILITY)
  @id                 = SecureRandom.uuid
  @content            = content
  @observation_ids    = Array(observation_ids)
  @domain             = domain
  @plausibility       = prior_probability
  @simplicity         = simplicity
  @explanatory_power  = explanatory_power
  @prior_probability  = prior_probability
  @evidence_for       = 0
  @evidence_against   = 0
  @state              = :candidate
  @created_at         = Time.now.utc
  @last_evaluated_at  = Time.now.utc
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



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

def content
  @content
end

#created_atObject (readonly)

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#domainObject (readonly)

Returns the value of attribute domain.



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

def domain
  @domain
end

#evidence_againstObject (readonly)

Returns the value of attribute evidence_against.



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

def evidence_against
  @evidence_against
end

#evidence_forObject (readonly)

Returns the value of attribute evidence_for.



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

def evidence_for
  @evidence_for
end

#explanatory_powerObject (readonly)

Returns the value of attribute explanatory_power.



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

def explanatory_power
  @explanatory_power
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#last_evaluated_atObject (readonly)

Returns the value of attribute last_evaluated_at.



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

def last_evaluated_at
  @last_evaluated_at
end

#observation_idsObject (readonly)

Returns the value of attribute observation_ids.



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

def observation_ids
  @observation_ids
end

#plausibilityObject

Returns the value of attribute plausibility.



18
19
20
# File 'lib/legion/extensions/agentic/inference/abductive/helpers/hypothesis.rb', line 18

def plausibility
  @plausibility
end

#prior_probabilityObject (readonly)

Returns the value of attribute prior_probability.



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

def prior_probability
  @prior_probability
end

#simplicityObject (readonly)

Returns the value of attribute simplicity.



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

def simplicity
  @simplicity
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

Instance Method Details

#add_evidence(supporting:) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/legion/extensions/agentic/inference/abductive/helpers/hypothesis.rb', line 45

def add_evidence(supporting:)
  @last_evaluated_at = Time.now.utc
  if supporting
    @evidence_for += 1
    @plausibility = (@plausibility + Constants::EVIDENCE_BOOST).clamp(
      Constants::PLAUSIBILITY_FLOOR,
      Constants::PLAUSIBILITY_CEILING
    )
  else
    @evidence_against += 1
    @plausibility = (@plausibility - Constants::CONTRADICTION_PENALTY).clamp(
      Constants::PLAUSIBILITY_FLOOR,
      Constants::PLAUSIBILITY_CEILING
    )
  end
  support! if @evidence_for >= SUPPORT_EVIDENCE_THRESHOLD && @state == :candidate
end

#overall_scoreObject



39
40
41
42
43
# File 'lib/legion/extensions/agentic/inference/abductive/helpers/hypothesis.rb', line 39

def overall_score
  (Constants::SIMPLICITY_WEIGHT * @simplicity) +
    (Constants::EXPLANATORY_POWER_WEIGHT * @explanatory_power) +
    (Constants::PRIOR_WEIGHT * @prior_probability)
end

#quality_labelObject



73
74
75
76
77
78
79
# File 'lib/legion/extensions/agentic/inference/abductive/helpers/hypothesis.rb', line 73

def quality_label
  score = overall_score
  Constants::QUALITY_LABELS.each do |range, label|
    return label if range.include?(score)
  end
  :implausible
end

#refute!Object



63
64
65
66
# File 'lib/legion/extensions/agentic/inference/abductive/helpers/hypothesis.rb', line 63

def refute!
  @state = :refuted
  @last_evaluated_at = Time.now.utc
end

#support!Object



68
69
70
71
# File 'lib/legion/extensions/agentic/inference/abductive/helpers/hypothesis.rb', line 68

def support!
  @state = :supported
  @last_evaluated_at = Time.now.utc
end

#to_hObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/legion/extensions/agentic/inference/abductive/helpers/hypothesis.rb', line 81

def to_h
  {
    id:                @id,
    content:           @content,
    observation_ids:   @observation_ids,
    domain:            @domain,
    plausibility:      @plausibility,
    simplicity:        @simplicity,
    explanatory_power: @explanatory_power,
    prior_probability: @prior_probability,
    evidence_for:      @evidence_for,
    evidence_against:  @evidence_against,
    state:             @state,
    overall_score:     overall_score,
    quality_label:     quality_label,
    created_at:        @created_at,
    last_evaluated_at: @last_evaluated_at
  }
end