Class: FeldtRuby::Optimize::SearchSpace::LatinHypercubeSampler

Inherits:
SetSampler show all
Defined in:
lib/feldtruby/optimize/search_space.rb

Overview

LatinHypercube sampling is a stratified random sampling in bins over the search space. It is a simple way to ensure a more even spread over the search space when sampling. It has shown to be generally useful for seeding initial populations in population based search without requiring any fitness evaluations. For example used in Deb’s OmniOptimizer and in Tiwari’s AMGA2 etc.

Instance Attribute Summary

Attributes inherited from Sampler

#search_space

Instance Method Summary collapse

Methods inherited from SetSampler

#chunk_empty?, #initialize, #pop_candidate_from_chunk, #sample_candidate, #sample_new_chunk

Methods inherited from Sampler

#initialize, #sample_candidate, #sample_value_for_dimension

Constructor Details

This class inherits a constructor from FeldtRuby::Optimize::SearchSpace::SetSampler

Instance Method Details

#latin_samples_for_dimension(index, numSamples) ⇒ Object

Evenly spread numSamples random samples over the search space dimension with index.



161
162
163
164
165
166
167
168
169
# File 'lib/feldtruby/optimize/search_space.rb', line 161

def latin_samples_for_dimension(index, numSamples)
	low, delta = search_space.min_values[index], search_space.deltas[index]
	interval_size = delta / numSamples.to_f
	i = 0
	Array.new(numSamples).map do
		i += 1
		(low + (i-1) * interval_size) + (interval_size * rand())
	end
end

#sample_candidates(numCandidates) ⇒ Object

Sample the latin hypercube evenly for each dimension in the search space and then use Knuth unbiased shuffling to create individuals from the evenly spread out samples.



150
151
152
153
154
155
156
157
158
# File 'lib/feldtruby/optimize/search_space.rb', line 150

def sample_candidates(numCandidates)
	set = Array.new(numCandidates).map {Array.new}
	(0...(search_space.num_variables)).each do |dimension|
		samples = latin_samples_for_dimension(dimension, numCandidates)
		pi = (0...numCandidates).to_a.shuffle  # Ruby has Knuth shuffle built in so no need to implement
		(0...numCandidates).each {|i| set[i] << samples[pi[i]]}
	end
	set
end