Class: Legion::Extensions::Agentic::Inference::BeliefRevision::Helpers::BeliefNetwork

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

Constant Summary collapse

PROPAGATION_SIGNS =
{
  %i[supports support]   => 1,
  %i[supports oppose]    => -1,
  %i[undermines support] => -1,
  %i[undermines oppose]  => 1
}.freeze

Constants included from Constants

Constants::BELIEF_STATES, Constants::CONTRADICTION_THRESHOLD, Constants::CREDENCE_CEILING, Constants::CREDENCE_FLOOR, Constants::CREDENCE_LABELS, Constants::DECAY_RATE, Constants::DEFAULT_CREDENCE, Constants::ENTRENCHMENT_ALPHA, Constants::EVIDENCE_TYPES, Constants::EVIDENCE_WEIGHT, Constants::LINK_TYPES, Constants::MAX_BELIEFS, Constants::MAX_EVIDENCE_PER_BELIEF, Constants::MAX_HISTORY, Constants::MAX_LINKS, Constants::STATE_THRESHOLDS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBeliefNetwork

Returns a new instance of BeliefNetwork.



14
15
16
17
18
19
20
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb', line 14

def initialize
  @beliefs  = {}
  @links    = []
  @counter  = 0
  @ev_count = 0
  @history  = []
end

Instance Attribute Details

#beliefsObject (readonly)

Returns the value of attribute beliefs.



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

def beliefs
  @beliefs
end

#historyObject (readonly)

Returns the value of attribute history.



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

def history
  @history
end

Returns the value of attribute links.



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

def links
  @links
end

Instance Method Details

#add_belief(proposition:, domain: :general, credence: DEFAULT_CREDENCE) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb', line 22

def add_belief(proposition:, domain: :general, credence: DEFAULT_CREDENCE)
  return nil if @beliefs.size >= MAX_BELIEFS

  @counter += 1
  belief_id = :"belief_#{@counter}"
  belief = Belief.new(id: belief_id, proposition: proposition, domain: domain, credence: credence)
  @beliefs[belief_id] = belief
  belief
end

#add_evidence(belief_id:, evidence_type:, content:, direction: :support, weight: EVIDENCE_WEIGHT, source: :unknown) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb', line 32

def add_evidence(belief_id:, evidence_type:, content:, direction: :support, weight: EVIDENCE_WEIGHT,
                 source: :unknown)
  belief = @beliefs[belief_id]
  return nil unless belief

  @ev_count += 1
  ev = Evidence.new(id: :"ev_#{@ev_count}", evidence_type: evidence_type,
                    content: content, weight: weight, source: source)
  result = direction == :support ? belief.add_supporting_evidence(ev) : belief.add_opposing_evidence(ev)
  propagate_evidence(belief_id, direction) if result
  record_event(:evidence, belief_id: belief_id, direction: direction)
  result
end

#beliefs_in(domain:) ⇒ Object



74
75
76
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb', line 74

def beliefs_in(domain:)
  @beliefs.values.select { |b| b.domain == domain }.map(&:to_h)
end

#believedObject



78
79
80
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb', line 78

def believed
  @beliefs.values.select(&:believed?).map(&:to_h)
end

#coherence_scoreObject



104
105
106
107
108
109
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb', line 104

def coherence_score
  return 1.0 if @links.empty?

  coherent = @links.count { |l| link_coherent?(l) }
  coherent.to_f / @links.size
end

#contradictionsObject



65
66
67
68
69
70
71
72
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb', line 65

def contradictions
  pairs = []
  ids = @beliefs.keys
  ids.combination(2) do |a, b|
    pairs << [a, b] if @beliefs[a].contradicts?(@beliefs[b]) && linked?(a, b)
  end
  pairs
end

#decay_allObject



100
101
102
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb', line 100

def decay_all
  @beliefs.each_value(&:decay)
end

#disbelievedObject



82
83
84
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb', line 82

def disbelieved
  @beliefs.values.select(&:disbelieved?).map(&:to_h)
end

#entrenchedObject



86
87
88
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb', line 86

def entrenched
  @beliefs.values.select { |b| b.state == :entrenched }.map(&:to_h)
end


46
47
48
49
50
51
52
53
54
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb', line 46

def link_beliefs(from_id:, to_id:, link_type:)
  return nil unless @beliefs.key?(from_id) && @beliefs.key?(to_id)
  return nil unless LINK_TYPES.include?(link_type)
  return nil if @links.size >= MAX_LINKS

  link = { from: from_id, to: to_id, type: link_type }
  @links << link
  link
end

#revise_belief(belief_id:, new_credence:) ⇒ Object



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

def revise_belief(belief_id:, new_credence:)
  belief = @beliefs[belief_id]
  return nil unless belief

  belief.revise(new_credence: new_credence)
  record_event(:revision, belief_id: belief_id, new_credence: new_credence)
  belief
end

#supported_beliefs(belief_id:) ⇒ Object



90
91
92
93
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb', line 90

def supported_beliefs(belief_id:)
  linked_ids = @links.select { |l| l[:from] == belief_id && l[:type] == :supports }.map { |l| l[:to] }
  linked_ids.filter_map { |id| @beliefs[id]&.to_h }
end

#to_hObject



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb', line 111

def to_h
  {
    belief_count:        @beliefs.size,
    link_count:          @links.size,
    believed_count:      @beliefs.values.count(&:believed?),
    disbelieved_count:   @beliefs.values.count(&:disbelieved?),
    entrenched_count:    @beliefs.values.count { |b| b.state == :entrenched },
    contradiction_count: contradictions.size,
    coherence:           coherence_score.round(4),
    history_size:        @history.size
  }
end

#undermining_beliefs(belief_id:) ⇒ Object



95
96
97
98
# File 'lib/legion/extensions/agentic/inference/belief_revision/helpers/belief_network.rb', line 95

def undermining_beliefs(belief_id:)
  linked_ids = @links.select { |l| l[:to] == belief_id && l[:type] == :undermines }.map { |l| l[:from] }
  linked_ids.filter_map { |id| @beliefs[id]&.to_h }
end