Class: DecisionAgent::Scoring::Consensus

Inherits:
Base
  • Object
show all
Defined in:
lib/decision_agent/scoring/consensus.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(minimum_agreement: 0.5) ⇒ Consensus



8
9
10
# File 'lib/decision_agent/scoring/consensus.rb', line 8

def initialize(minimum_agreement: 0.5)
  @minimum_agreement = minimum_agreement.to_f
end

Instance Attribute Details

#minimum_agreementObject (readonly)

Returns the value of attribute minimum_agreement.



6
7
8
# File 'lib/decision_agent/scoring/consensus.rb', line 6

def minimum_agreement
  @minimum_agreement
end

Instance Method Details

#score(evaluations) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/decision_agent/scoring/consensus.rb', line 12

def score(evaluations)
  return { decision: nil, confidence: 0.0 } if evaluations.empty?

  grouped = evaluations.group_by(&:decision)
  total_count = evaluations.size

  candidates = grouped.map do |decision, evals|
    agreement = evals.size.to_f / total_count
    avg_weight = evals.sum(&:weight) / evals.size

    [decision, agreement, avg_weight]
  end

  candidates.sort_by! { |_, agreement, weight| [-agreement, -weight] }

  winning_decision, agreement, avg_weight = candidates.first

  confidence = if agreement >= @minimum_agreement
                 agreement * avg_weight
               else
                 agreement * avg_weight * 0.5
               end

  {
    decision: winning_decision,
    confidence: round_confidence(normalize_confidence(confidence))
  }
end