Class: FeldtRuby::Optimize::SearchSpace

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

Overview

A search space is a set of constraints that limits which values are searched for. The search space can generate valid candidate solutions that are inside the space. It can also check if a given candidate is in the space. The default search space has min and max values for each element of a continuous vector.

Defined Under Namespace

Classes: LatinHypercubeSampler, Sampler, SetSampler

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(minValues, maxValues, sampler = LatinHypercubeSampler.new) ⇒ SearchSpace

Returns a new instance of SearchSpace.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/feldtruby/optimize/search_space.rb', line 13

def initialize(minValues, maxValues, sampler = LatinHypercubeSampler.new)
	sampler.search_space = self
	@sampler = sampler
	# Check that we have valid min and max values
	raise "Not same num of min values (#{minValues.length}) as there are max values (#{maxValues.length})" if minValues.length != maxValues.length
	raise "A search space must have >= 1 variable to be searched, here you specified min values: #{minValues.inspect}" if minValues.length < 1
	minValues.zip(maxValues).each do |min,max|
		raise "The min value #{min} is larger than the max value #{max} in min values = #{minValues.inspect} and #{maxValues.inspect}" if min > max
	end
	@min_values, @max_values = minValues, maxValues
	@deltas = @min_values.zip(@max_values).map {|min,max| max-min}
end

Instance Attribute Details

#deltasObject (readonly)

Returns the value of attribute deltas.



11
12
13
# File 'lib/feldtruby/optimize/search_space.rb', line 11

def deltas
  @deltas
end

#max_valuesObject (readonly)

Returns the value of attribute max_values.



11
12
13
# File 'lib/feldtruby/optimize/search_space.rb', line 11

def max_values
  @max_values
end

#min_valuesObject (readonly)

Returns the value of attribute min_values.



11
12
13
# File 'lib/feldtruby/optimize/search_space.rb', line 11

def min_values
  @min_values
end

Class Method Details

.new_from_min_max(numVariables = 2, min = -1,, max = 1) ⇒ Object



62
63
64
65
66
# File 'lib/feldtruby/optimize/search_space.rb', line 62

def self.new_from_min_max(numVariables = 2, min = -1, max = 1)
	min_values = Array.new(numVariables).map {min}
	max_values = Array.new(numVariables).map {max}
	self.new(min_values, max_values)
end

.new_from_min_max_per_variable(minMaxPairs) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/feldtruby/optimize/search_space.rb', line 68

def self.new_from_min_max_per_variable(minMaxPairs)
	min_values = []
	max_values = []
	minMaxPairs.each do |min, max|
		min_values << min
		max_values << max
	end
	self.new(min_values, max_values)
end

.new_symmetric(numVariables = 2, distanceFromZero = 1) ⇒ Object



56
57
58
59
60
# File 'lib/feldtruby/optimize/search_space.rb', line 56

def self.new_symmetric(numVariables = 2, distanceFromZero = 1)
	min_values = Array.new(numVariables).map {-distanceFromZero}
	max_values = Array.new(numVariables).map {distanceFromZero}
	self.new_from_min_max(numVariables, -distanceFromZero, distanceFromZero)
end

Instance Method Details

#bound(candidate) ⇒ Object

Bound candidate using the min and max values. We randomly generate a new value inside the space for each element that is outside.



37
38
39
40
41
42
# File 'lib/feldtruby/optimize/search_space.rb', line 37

def bound(candidate)
	a = candidate.each_with_index.map do |v, i|
		in_range_for_position?(v, i) ? v : gen_value_for_position(i)
	end
	candidate.class.send(:[], *a)
end

#bound_at_index(index, candidate) ⇒ Object

Bound variable at index in candidate.



45
46
47
48
49
50
# File 'lib/feldtruby/optimize/search_space.rb', line 45

def bound_at_index(index, candidate)
	unless in_range_for_position?(candidate[index], index)
		candidate[index] = gen_value_for_position(index)
	end
	candidate
end

#gen_candidateObject

Generate a new candidate.



27
28
29
# File 'lib/feldtruby/optimize/search_space.rb', line 27

def gen_candidate
	@sampler.sample_candidate
end

#gen_value_for_position(index) ⇒ Object



31
32
33
# File 'lib/feldtruby/optimize/search_space.rb', line 31

def gen_value_for_position(index)
	@sampler.sample_value_for_dimension(index)
end

#in_range_for_position?(value, index) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/feldtruby/optimize/search_space.rb', line 52

def in_range_for_position?(value, index)
	(value >= @min_values[index]) && (value <= @max_values[index])
end

#is_candidate?(c) ⇒ Boolean

Returns:

  • (Boolean)


172
173
174
175
176
177
178
# File 'lib/feldtruby/optimize/search_space.rb', line 172

def is_candidate?(c)
	return false unless c.length == num_variables
	c.length.times do |i|
		return false unless c[i] >= min_values[i] && c[i] <= max_values[i]
	end
	return true
end

#num_variablesObject



78
79
80
# File 'lib/feldtruby/optimize/search_space.rb', line 78

def num_variables
	@min_values.length
end