Class: Hiccup::Inferable::Scorer

Inherits:
Object
  • Object
show all
Defined in:
lib/hiccup/inferable/scorer.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Scorer

Returns a new instance of Scorer.



5
6
7
# File 'lib/hiccup/inferable/scorer.rb', line 5

def initialize(options={})
  @verbose = options.fetch(:verbose, false)
end

Instance Method Details

#complexity_of(schedule) ⇒ Object



59
60
61
62
63
# File 'lib/hiccup/inferable/scorer.rb', line 59

def complexity_of(schedule)
  return schedule.weekly_pattern.length if schedule.weekly?
  return schedule.monthly_pattern.length if schedule.monthly?
  1
end

#pick_best_guess(guesses, dates) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hiccup/inferable/scorer.rb', line 11

def pick_best_guess(guesses, dates)
  scored_guesses = guesses \
    .map { |guess| [guess, score_guess(guess, dates)] } \
    .sort_by { |(guess, score)| -score.to_f }

  if @verbose
    puts "\nGUESSES FOR #{dates}:"
    scored_guesses.each do |(guess, score)|
      puts "  (%.3f p/%.3f b/%.3f c/%.3f) #{guess.humanize}" % [
        score.to_f,
        score.prediction_rate,
        score.brick_penalty,
        score.complexity_penalty]
    end
    puts ""
  end

  scored_guesses.first
end

#score_guess(guess, input_dates) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hiccup/inferable/scorer.rb', line 33

def score_guess(guess, input_dates)
  predicted_dates = guess.occurrences_between(guess.start_date, guess.end_date)

  # prediction_rate is the percent of input dates predicted
  predictions = (predicted_dates & input_dates).length
  prediction_rate = Float(predictions) / Float(input_dates.length)

  # bricks are dates predicted by this guess but not in the input
  bricks = (predicted_dates - input_dates).length

  # brick_rate is the percent of bricks to predictions
  # A brick_rate >= 1 means that this guess bricks more than it predicts
  brick_rate = Float(bricks) / Float(input_dates.length)

  # complexity measures how many rules are necesary
  # to describe the pattern
  complexity = complexity_of(guess)

  # complexity_rate is the number of rules per inputs
  complexity_rate = Float(complexity) / Float(input_dates.length)

  Score.new(prediction_rate, brick_rate, complexity_rate)
end