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.



119
120
121
122
123
# File 'lib/feldtruby/optimize/optimizer.rb', line 119

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

Instance Method Details

#initialize_all_indicesObject



125
126
127
128
129
130
# File 'lib/feldtruby/optimize/optimizer.rb', line 125

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



136
137
138
139
140
141
142
143
144
145
# File 'lib/feldtruby/optimize/optimizer.rb', line 136

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



132
133
134
# File 'lib/feldtruby/optimize/optimizer.rb', line 132

def sample_population_indices_without_replacement(numSamples)
	sample_indices_without_replacement numSamples, @all_indices
end