Class: Fabes::Experiment

Inherits:
Object show all
Defined in:
lib/fabes/experiment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *alternatives) ⇒ Experiment

Returns a new instance of Experiment.



5
6
7
8
9
10
11
# 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

Instance Attribute Details

#alternativesObject

Returns the value of attribute alternatives.



3
4
5
# File 'lib/fabes/experiment.rb', line 3

def alternatives
  @alternatives
end

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/fabes/experiment.rb', line 3

def description
  @description
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/fabes/experiment.rb', line 3

def name
  @name
end

Class Method Details

.allObject



23
24
25
# File 'lib/fabes/experiment.rb', line 23

def self.all
  Fabes.db.all_experiments
end

.find(name) ⇒ Object



19
20
21
# File 'lib/fabes/experiment.rb', line 19

def self.find(name)
  Fabes.db.find_experiment name
end

.find_or_create(name, *alternatives) ⇒ Object



13
14
15
16
17
# 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

Instance Method Details

#add_alternative(alternative) ⇒ Object



44
45
46
# File 'lib/fabes/experiment.rb', line 44

def add_alternative(alternative)
  @alternatives.push alternative
end

#controlObject



48
49
50
# File 'lib/fabes/experiment.rb', line 48

def control
  @alternatives.first
end

#find_alternative(id) ⇒ Object



36
37
38
# File 'lib/fabes/experiment.rb', line 36

def find_alternative(id)
  @alternatives.select {|alternative| alternative.id == id }.first or control
end

#saveObject



40
41
42
# File 'lib/fabes/experiment.rb', line 40

def save
  Fabes.db.save_experiment(self)
end

#select_alternative!Object

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



28
29
30
31
32
33
34
# File 'lib/fabes/experiment.rb', line 28

def select_alternative!
  if exploration?
    random_alternative
  else #exploitation
    heaviest_alternative
  end
end