Class: Bandit::EpsilonGreedyPlayer

Inherits:
BasePlayer show all
Includes:
Memoizable
Defined in:
lib/bandit/players/epsilon_greedy.rb

Instance Method Summary collapse

Methods included from Memoizable

included, #memoize

Methods inherited from BasePlayer

#get, get_player, #initialize, #name, #set

Constructor Details

This class inherits a constructor from Bandit::BasePlayer

Instance Method Details

#best_alternative(experiment) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bandit/players/epsilon_greedy.rb', line 16

def best_alternative(experiment)
  memoize(experiment.name) { 
    best = nil
    best_rate = nil
    experiment.alternatives.each { |alt|
      rate = experiment.conversion_rate(alt)
      if best_rate.nil? or rate > best_rate
        best = alt
        best_rate = rate
      end
    }
    best
  }
end

#choose_alternative(experiment) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/bandit/players/epsilon_greedy.rb', line 5

def choose_alternative(experiment)
  epsilon = @config['epsilon'].to_f || 0.1

  # choose best with probability of 1-epsilon
  if rand <= (1-epsilon)
    best_alternative(experiment)
  else
    experiment.alternatives.sample
  end
end