Class: FeldtRuby::Optimize::PopulationSampler

Inherits:
Object
  • Object
show all
Defined in:
lib/feldtruby/optimize/optimizer.rb

Overview

Sample the indices of a population. This default super class just randomly samples without replacement.

Direct Known Subclasses

RadiusLimitedPopulationSampler

Instance Method Summary collapse

Constructor Details

#initialize(optimizer, options = FeldtRuby::Optimize::DefaultOptimizationOptions) ⇒ PopulationSampler

Returns a new instance of PopulationSampler.

[View source]

101
102
103
104
105
# File 'lib/feldtruby/optimize/optimizer.rb', line 101

def initialize(optimizer, options = FeldtRuby::Optimize::DefaultOptimizationOptions)
  @optimizer = optimizer
  @population_size = @optimizer.population_size
  initialize_all_indices()
end

Instance Method Details

#initialize_all_indicesObject

[View source]

107
108
109
110
111
112
# File 'lib/feldtruby/optimize/optimizer.rb', line 107

def initialize_all_indices
  # We set up an array of the indices to all candidates of the population so we can later sample from it
  # This should always contain all indices even if they might be out of order. This is because we
  # only swap! elements in this array, never delete any.
  @all_indices = (0...@population_size).to_a
end

#sample_indices_without_replacement(numSamples, indices) ⇒ Object

[View source]

118
119
120
121
122
123
124
125
126
127
# File 'lib/feldtruby/optimize/optimizer.rb', line 118

def sample_indices_without_replacement(numSamples, indices)
  sampled_indices = []
  size = indices.length
  numSamples.times do |i|
    index = i + rand_int(size - i)
    sampled_index, skip = indices.swap!(i, index)
    sampled_indices << sampled_index
  end
  sampled_indices
end

#sample_population_indices_without_replacement(numSamples) ⇒ Object

[View source]

114
115
116
# File 'lib/feldtruby/optimize/optimizer.rb', line 114

def sample_population_indices_without_replacement(numSamples)
  sample_indices_without_replacement numSamples, @all_indices
end