Class: Legion::Extensions::Agentic::Inference::Coherence::Helpers::CoherenceEngine
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Inference::Coherence::Helpers::CoherenceEngine
show all
- Includes:
- Constants
- Defined in:
- lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb
Constant Summary
Constants included
from Constants
Legion::Extensions::Agentic::Inference::Coherence::Helpers::Constants::ACCEPTANCE_THRESHOLD, Legion::Extensions::Agentic::Inference::Coherence::Helpers::Constants::COHERENCE_LABELS, Legion::Extensions::Agentic::Inference::Coherence::Helpers::Constants::COHERENCE_WEIGHT, Legion::Extensions::Agentic::Inference::Coherence::Helpers::Constants::CONSTRAINT_TYPES, Legion::Extensions::Agentic::Inference::Coherence::Helpers::Constants::DECAY_RATE, Legion::Extensions::Agentic::Inference::Coherence::Helpers::Constants::DEFAULT_ACCEPTANCE, Legion::Extensions::Agentic::Inference::Coherence::Helpers::Constants::INCOHERENCE_PENALTY, Legion::Extensions::Agentic::Inference::Coherence::Helpers::Constants::MAX_CONSTRAINTS, Legion::Extensions::Agentic::Inference::Coherence::Helpers::Constants::MAX_HISTORY, Legion::Extensions::Agentic::Inference::Coherence::Helpers::Constants::MAX_PROPOSITIONS, Legion::Extensions::Agentic::Inference::Coherence::Helpers::Constants::PROPOSITION_STATES
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of CoherenceEngine.
14
15
16
17
|
# File 'lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb', line 14
def initialize
@propositions = {}
@history = []
end
|
Instance Attribute Details
#history ⇒ Object
Returns the value of attribute history.
12
13
14
|
# File 'lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb', line 12
def history
@history
end
|
#propositions ⇒ Object
Returns the value of attribute propositions.
12
13
14
|
# File 'lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb', line 12
def propositions
@propositions
end
|
Instance Method Details
#add_constraint(prop_a_id:, prop_b_id:, constraint_type:, positive: true) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb', line 28
def add_constraint(prop_a_id:, prop_b_id:, constraint_type:, positive: true)
prop_a = @propositions[prop_a_id]
prop_b = @propositions[prop_b_id]
return { success: false, reason: :proposition_not_found } unless prop_a && prop_b
unless CONSTRAINT_TYPES.include?(constraint_type)
return { success: false,
reason: :invalid_constraint_type }
end
if positive
prop_a.add_positive_constraint(proposition_id: prop_b_id)
prop_b.add_positive_constraint(proposition_id: prop_a_id)
else
prop_a.add_negative_constraint(proposition_id: prop_b_id)
prop_b.add_negative_constraint(proposition_id: prop_a_id)
end
record_history(:add_constraint,
{ prop_a: prop_a_id, prop_b: prop_b_id, type: constraint_type, positive: positive })
{ success: true, constraint_type: constraint_type, positive: positive }
end
|
#add_proposition(content:, domain: :general, acceptance: DEFAULT_ACCEPTANCE) ⇒ Object
19
20
21
22
23
24
25
26
|
# File 'lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb', line 19
def add_proposition(content:, domain: :general, acceptance: DEFAULT_ACCEPTANCE)
return nil if @propositions.size >= MAX_PROPOSITIONS
prop = Proposition.new(content: content, domain: domain, acceptance: acceptance)
@propositions[prop.id] = prop
record_history(:add_proposition, { id: prop.id, domain: domain })
prop.id
end
|
#by_domain(domain:) ⇒ Object
107
108
109
|
# File 'lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb', line 107
def by_domain(domain:)
@propositions.values.select { |prop| prop.domain == domain }.map(&:to_h)
end
|
#coherence_label ⇒ Object
89
90
91
92
|
# File 'lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb', line 89
def coherence_label
val = overall_coherence
COHERENCE_LABELS.find { |range, _| range.cover?(val) }&.last || :unknown
end
|
#compute_coherence(proposition_id:) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb', line 51
def compute_coherence(proposition_id:)
prop = @propositions[proposition_id]
return 0.0 unless prop
positive_sum = prop.positive_constraints.sum do |pid|
neighbor = @propositions[pid]
neighbor ? neighbor.acceptance * COHERENCE_WEIGHT : 0.0
end
negative_sum = prop.negative_constraints.sum do |pid|
neighbor = @propositions[pid]
neighbor ? neighbor.acceptance * INCOHERENCE_PENALTY : 0.0
end
(prop.acceptance + positive_sum - negative_sum).clamp(0.0, 1.0)
end
|
#decay_all ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb', line 111
def decay_all
count = 0
@propositions.each_value do |prop|
next if prop.acceptance == DEFAULT_ACCEPTANCE
delta = (DEFAULT_ACCEPTANCE - prop.acceptance) * DECAY_RATE
prop.adjust_acceptance(amount: delta) unless delta.abs < 0.0001
count += 1
end
{ success: true, decayed_count: count }
end
|
#find_contradictions ⇒ Object
94
95
96
97
98
99
|
# File 'lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb', line 94
def find_contradictions
accepted = @propositions.values.select(&:accepted?)
pairs = []
accepted.each { |prop| collect_contradiction_pairs(prop, pairs) }
pairs
end
|
#maximize_coherence ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb', line 68
def maximize_coherence
return { success: true, iterations: 0, proposition_count: 0 } if @propositions.empty?
@propositions.each_value { |prop| adjust_proposition(prop) }
record_history(:maximize_coherence, { overall: overall_coherence })
{
success: true,
iterations: 1,
proposition_count: @propositions.size,
overall_coherence: overall_coherence
}
end
|
#overall_coherence ⇒ Object
82
83
84
85
86
87
|
# File 'lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb', line 82
def overall_coherence
return 0.0 if @propositions.empty?
total = @propositions.values.sum { |prop| compute_coherence(proposition_id: prop.id) }
total / @propositions.size
end
|
#partition ⇒ Object
101
102
103
104
105
|
# File 'lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb', line 101
def partition
result = { accepted: [], rejected: [], undecided: [] }
@propositions.each_value { |prop| result[prop.state] << prop.to_h }
result
end
|
#to_h ⇒ Object
123
124
125
126
127
128
129
130
131
|
# File 'lib/legion/extensions/agentic/inference/coherence/helpers/coherence_engine.rb', line 123
def to_h
{
proposition_count: @propositions.size,
overall_coherence: overall_coherence,
coherence_label: coherence_label,
partition: partition,
history_size: @history.size
}
end
|