Class: Biopsy::ParameterSweeper

Inherits:
Object
  • Object
show all
Defined in:
lib/biopsy/optimisers/parameter_sweeper.rb

Overview

options - is a hash of two hashes, :settings and :parameters

:ranges are arrays to be parameter sweeped
  ---(single values may be present, these are also remain unchanged but are accessible within the parameters hash to the constructor)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ranges, threads: 1, limit: 1000) ⇒ ParameterSweeper

Returns a new instance of ParameterSweeper.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/biopsy/optimisers/parameter_sweeper.rb', line 27

def initialize(ranges, threads:1, limit:1000)
  @ranges = ranges
  # parameter_counter: a count of input parameters to be used
  @parameter_counter = 1
  # input_combinations: an array of arrays of input parameters
  @combinations = []
  # if the number of threads is set, update the global variable, if not default to 1
  @threads = threads
  # set the limit to the number of parameters
  @limit = limit
  # convert all options to an array so it can be handled by the generate_combinations() method
  # ..this is for users entering single values e.g 4 as a parameter
  @ranges.each { |key, value| value = [value] unless value.kind_of? Array }
  self.generate_combinations(0, {})
  # restrict to a subsample?
  
  if @limit < @combinations.size
    @combinations = @combinations.sample @limit
  end
end

Instance Attribute Details

#combinationsObject (readonly)

Returns the value of attribute combinations.



25
26
27
# File 'lib/biopsy/optimisers/parameter_sweeper.rb', line 25

def combinations
  @combinations
end

Instance Method Details

#bestObject



84
85
86
# File 'lib/biopsy/optimisers/parameter_sweeper.rb', line 84

def best
  @best
end

#finished?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/biopsy/optimisers/parameter_sweeper.rb', line 100

def finished?
  @combinations.empty?
end

#generate_combinations(index, opts) ⇒ Object

generate all the parameter combinations to be applied



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/biopsy/optimisers/parameter_sweeper.rb', line 71

def generate_combinations(index, opts)
  if index == @ranges.length
    @combinations << opts.clone
    return
  end
  # recurse
  key = @ranges.keys[index]
  @ranges[key].each do |value|
    opts[key] = value
    generate_combinations(index + 1, opts)
  end
end

#knows_starting_point?Boolean

True if this algorithm chooses its own starting point

Returns:

  • (Boolean)


105
106
107
# File 'lib/biopsy/optimisers/parameter_sweeper.rb', line 105

def knows_starting_point?
  true
end

#random_start_pointObject



96
97
98
# File 'lib/biopsy/optimisers/parameter_sweeper.rb', line 96

def random_start_point
  @combinations.pop
end

#run_one_iteration(parameters, score) ⇒ Object

return the next parameter set to evaluate



56
57
58
59
60
61
62
# File 'lib/biopsy/optimisers/parameter_sweeper.rb', line 56

def run_one_iteration(parameters, score)
  @current = { :parameters => parameters, :score => score }
  self.update_best?
  @combinations.pop
rescue
  nil
end

#select_starting_pointObject



92
93
94
# File 'lib/biopsy/optimisers/parameter_sweeper.rb', line 92

def select_starting_point
  @combinations.pop
end

#setup(*_args) ⇒ Object



48
49
50
51
52
53
# File 'lib/biopsy/optimisers/parameter_sweeper.rb', line 48

def setup(*_args)
  @best = {
    :parameters => nil,
    :score => nil
  }
end

#update_best?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/biopsy/optimisers/parameter_sweeper.rb', line 64

def update_best?
  if @best[:score].nil? || @current[:score] > @best[:score]
    @best = @current.clone
  end
end