Class: Evoc::RecommendationCache

Inherits:
Object
  • Object
show all
Extended by:
Logging
Defined in:
lib/evoc/recommendation_cache.rb

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Logging

configure_logger_for, logger, logger_for, set_level

Class Attribute Details

.base_recommendationObject

Returns the value of attribute base_recommendation.



12
13
14
# File 'lib/evoc/recommendation_cache.rb', line 12

def base_recommendation
  @base_recommendation
end

.evaluationObject

Returns the value of attribute evaluation.



12
13
14
# File 'lib/evoc/recommendation_cache.rb', line 12

def evaluation
  @evaluation
end

.filtered_model_sizeObject

Returns the value of attribute filtered_model_size.



12
13
14
# File 'lib/evoc/recommendation_cache.rb', line 12

def filtered_model_size
  @filtered_model_size
end

.last_recommendationObject

Returns the value of attribute last_recommendation.



12
13
14
# File 'lib/evoc/recommendation_cache.rb', line 12

def last_recommendation
  @last_recommendation
end

.tagObject

Returns the value of attribute tag.



12
13
14
# File 'lib/evoc/recommendation_cache.rb', line 12

def tag
  @tag
end

.time_aggregationObject

Returns the value of attribute time_aggregation.



12
13
14
# File 'lib/evoc/recommendation_cache.rb', line 12

def time_aggregation
  @time_aggregation
end

.time_measurecalculationObject

Returns the value of attribute time_measurecalculation.



12
13
14
# File 'lib/evoc/recommendation_cache.rb', line 12

def time_measurecalculation
  @time_measurecalculation
end

.time_rulegenerationObject

Returns the value of attribute time_rulegeneration.



12
13
14
# File 'lib/evoc/recommendation_cache.rb', line 12

def time_rulegeneration
  @time_rulegeneration
end

Class Method Details

.evaluate_last(evaluators:, expected_outcome:, measure_combination:, topk: nil, unique_consequents: nil) ⇒ Hash[aggregator][evaluator][result]

Evaluate the currently cached recommendation

Parameters:

  • evaluators (Array<String>)

    the evaluators to apply

  • expected_outcome (Array<String>)

    the expected outcome to use in evaluations

  • measure_combinations (Array<String>)

    the list of measures to use when sorting a recommendation before evaluating

Returns:

  • (Hash[aggregator][evaluator][result])

    the hash of results



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/evoc/recommendation_cache.rb', line 79

def self.evaluate_last(evaluators: ,expected_outcome:,measure_combination:,topk: nil, unique_consequents: nil)
  if !self.last_recommendation.nil?
      self.evaluation = self.last_recommendation.evaluate_with(evaluators: evaluators,
                                                    topk: topk,
                                                    unique_consequents: unique_consequents,
                                                    expected_outcome: expected_outcome,
                                                    measure_combination: measure_combination)
  else
    STDERR.puts "TAG = #{self.tag}No recommendation to evaluate"
  end
end

.get_recommendation(algorithm:, query:, model_start:, model_end:, max_size: nil, aggregator: nil, measures: []) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/evoc/recommendation_cache.rb', line 24

def self.get_recommendation(algorithm:,
                            query:,
                            model_start:,
                            model_end:,
                            max_size: nil,
                            aggregator: nil,
                            measures: [])
  # check if a new base recommendation needs to be generated
    tag = [algorithm,query,model_start,model_end,max_size].hash
    if self.tag != tag
        # clear out any evaluation done
        self.evaluation = Hash.new
        # new recommendation
        logger.debug "Caching new recommendation: algorithm: #{algorithm}, query: #{query}, model_start/end: #{model_start} - #{model_end}, maxsize: #{max_size}"
        self.tag = tag
        tx_store = Evoc::HistoryStore.get_history(model_start,
                                                     model_end,
                                                     max_size)
        self.filtered_model_size = tx_store.size

        t1 = Time.new
        self.base_recommendation = Evoc::Algorithm.execute(tx_store: tx_store,
                                                     query: query,
                                                     algorithm: algorithm)
        self.last_recommendation = self.base_recommendation
        t2 = Time.new
        self.time_rulegeneration = TimeDifference.between(t1,t2).in_seconds.round(8)
    end

    # calculate measures on rules
    t1 = Time.new
    self.base_recommendation.calculate_measures(measures)
    t2 = Time.new
    self.time_measurecalculation = TimeDifference.between(t1,t2).in_seconds.round(8)

    # perform aggregation
    if !aggregator.nil?
      t1 = Time.new
      self.last_recommendation = self.base_recommendation.aggregate_by(aggregator: aggregator.to_sym,measures: measures) {|r| r.rhs}
      t2 = Time.new
      self.time_aggregation = TimeDifference.between(t1,t2).in_seconds.round(8)
    else
      self.last_recommendation = self.base_recommendation
    end
    return self.last_recommendation
end

.recommendation_cached?(algorithm:, query:, model_start:, model_end:, max_size: nil) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'lib/evoc/recommendation_cache.rb', line 15

def self.recommendation_cached?(algorithm:,
                            query:,
                            model_start:,
                            model_end:,
                            max_size: nil)
    return self.tag == [algorithm,query,model_start,model_end,max_size].hash
end

.to_h(measures: Evoc::Rule.measures) ⇒ Object

format:

{
    time: 'execution time',
    filtered_model_size: 
    number_of_rules :
    average_precision: 
    rules: [
      {
        lhs: [lhs]
        rhs: [rhs],
        measures: {
          measure_1: value,
          measure_n: value
        }
      },
      ..next rule..
]
}

measures: the interestingness measures that you want to output in the hash



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/evoc/recommendation_cache.rb', line 112

def self.to_h(measures: Evoc::Rule.measures)
    recommendation_hash = Hash.new
    recommendation_hash[:recommendation_tag] = self.tag
    recommendation_hash[:time_rulegeneration] = self.time_rulegeneration
    recommendation_hash[:time_measurecalculation] = self.time_measurecalculation
    recommendation_hash[:time_aggregation] = self.time_aggregation
    recommendation_hash[:filtered_model_size] = self.filtered_model_size
    recommendation_hash[:number_of_baserules] = self.base_recommendation.size
    recommendation_hash[:number_of_rules] = self.last_recommendation.size
    recommendation_hash[:aggregator] = self.last_recommendation.aggregator
    recommendation_hash[:number_of_hyper_rules] = self.last_recommendation.number_of_hyper_rules
    recommendation_hash[:mean_hyper_coefficient] = self.last_recommendation.inject(0.0){ |sum, r| 
      sum + r.get_measure('m_hyper_coefficient').value } / self.last_recommendation.size
    recommendation_hash[:largest_antecedent] = self.last_recommendation.largest_antecedent
    if !self.evaluation.nil?
      self.evaluation.each do |evaluator,results|
        recommendation_hash[evaluator] = results['value']
        # time can also be added like this:
        # recommendation_hash[evaluator+'_time'] = results['time']
      end
    end
    recommendation_hash[:rules] = []
    self.last_recommendation.each do |rule|
        rule_hash = Hash.new
        rule_hash[:lhs] = rule.lhs.is_a?(String) ? rule.lhs : rule.lhs.join(',')
        rule_hash[:rhs] = rule.rhs.is_a?(String) ? rule.rhs : rule.rhs.join(',')
        rule_hash[:measures] = Hash.new
        measures.each do |m| 
            if rule.measure_instantiated?(m)
                rule_hash[:measures][m] = rule.get_measure(m).value
            end
        end
        recommendation_hash[:rules] << rule_hash    
    end
    return recommendation_hash
end