Class: DecisionAgent::Scoring::WeightedAverage

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

Instance Method Summary collapse

Instance Method Details

#score(evaluations) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/decision_agent/scoring/weighted_average.rb', line 6

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

  grouped = evaluations.group_by(&:decision)

  weighted_scores = grouped.map do |decision, evals|
    total_weight = evals.sum(&:weight)
    [decision, total_weight]
  end

  winning_decision, winning_weight = weighted_scores.max_by { |_, weight| weight }

  total_weight = evaluations.sum(&:weight)
  confidence = total_weight.positive? ? winning_weight / total_weight : 0.0

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