class Fabes::Experiment

Attributes

alternatives[RW]
description[RW]
name[RW]

Public Class Methods

all() click to toggle source
# File lib/fabes/experiment.rb, line 23
def self.all
  Fabes.db.all_experiments
end
find(name) click to toggle source
# File lib/fabes/experiment.rb, line 19
def self.find(name)
  Fabes.db.find_experiment name
end
find_or_create(name, *alternatives) click to toggle source
# File lib/fabes/experiment.rb, line 13
def self.find_or_create(name, *alternatives)
  #TODO: What happens if same experiment but different alternatives?
  #TODO: check the validation of alternatives
  experiment = find(name) or new(name, *alternatives)
end
new(name, *alternatives) click to toggle source
# File lib/fabes/experiment.rb, line 5
def initialize(name, *alternatives)
  @name = name
  @alternatives = alternatives.map do |alternative|
    Fabes::Alternative.new alternative
  end
  save
end

Public Instance Methods

add_alternative(alternative) click to toggle source
# File lib/fabes/experiment.rb, line 44
def add_alternative(alternative)
  @alternatives.push alternative
end
control() click to toggle source
# File lib/fabes/experiment.rb, line 48
def control
  @alternatives.first
end
find_alternative(id) click to toggle source
# File lib/fabes/experiment.rb, line 36
def find_alternative(id)
  @alternatives.select {|alternative| alternative.id == id }.first or control
end
save() click to toggle source
# File lib/fabes/experiment.rb, line 40
def save
  Fabes.db.save_experiment(self)
end
select_alternative!() click to toggle source

TODO: make this an option and add different alternative selection algorithms

# File lib/fabes/experiment.rb, line 28
def select_alternative!
  if exploration?
    random_alternative
  else #exploitation
    heaviest_alternative
  end
end

Private Instance Methods

exploration?() click to toggle source
# File lib/fabes/experiment.rb, line 54
def exploration?
  rand() < Fabes.configuration.factor.to_f
end
heaviest_alternative() click to toggle source
# File lib/fabes/experiment.rb, line 62
def heaviest_alternative
  @alternatives.max {|alternative| alternative.weight}
end
random_alternative() click to toggle source
# File lib/fabes/experiment.rb, line 58
def random_alternative
  @alternatives.shuffle.first
end