Class: Stochastic::AdaptiveRandomSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/Algorithmically/Stochastic/adaptive_random_search.rb

Instance Method Summary collapse

Constructor Details

#initialize(max_iter, bounds, init_factor, s_factor, l_factor, iter_mult, max_no_impr) ⇒ AdaptiveRandomSearch

Returns a new instance of AdaptiveRandomSearch.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/Algorithmically/Stochastic/adaptive_random_search.rb', line 5

def initialize(max_iter, bounds, init_factor, s_factor, l_factor, iter_mult, max_no_impr)
  problem_size = bounds
  @bounds1 = Array.new(problem_size) {|i| [-5, +5]}
  @max_iter = max_iter
  @init_factor = init_factor
  @s_factor = s_factor
  @l_factor = l_factor
  @iter_mult = iter_mult
  @max_no_impr = max_no_impr
  @best = search(@max_iter, @bounds1, @init_factor, @s_factor, @l_factor, @iter_mult, @max_no_impr)
  puts "Done. Best Solution: c=#{@best[:cost]}, v=#{@best[:vector].inspect}"
end

Instance Method Details

#large_step_size(iter, step_size, s_factor, l_factor, iter_mult) ⇒ Object



42
43
44
45
# File 'lib/Algorithmically/Stochastic/adaptive_random_search.rb', line 42

def large_step_size(iter, step_size, s_factor, l_factor, iter_mult)
  step_size * l_factor if iter > 0 and iter.modulo(iter_mult) == 0
  step_size * s_factor
end

#objective_function(vector) ⇒ Object



18
19
20
# File 'lib/Algorithmically/Stochastic/adaptive_random_search.rb', line 18

def objective_function(vector)
  vector.inject(0) { |sum, x| sum + (x ** 2.0) }
end

#rand_in_bounds(min, max) ⇒ Object



22
23
24
# File 'lib/Algorithmically/Stochastic/adaptive_random_search.rb', line 22

def rand_in_bounds(min, max)
  min + ((max-min) * rand())
end

#random_vector(minmax) ⇒ Object



26
27
28
29
30
# File 'lib/Algorithmically/Stochastic/adaptive_random_search.rb', line 26

def random_vector(minmax)
  Array.new(minmax.size) do |i|
    rand_in_bounds(minmax[i][0], minmax[i][1])
  end
end

#search(max_iter, bounds, init_factor, s_factor, l_factor, iter_mult, max_no_impr) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/Algorithmically/Stochastic/adaptive_random_search.rb', line 56

def search(max_iter, bounds, init_factor, s_factor, l_factor, iter_mult, max_no_impr)
  step_size = (bounds[0][1]-bounds[0][0]) * init_factor
  current, count = {}, 0
  current[:vector] = random_vector(bounds)
  current[:cost] = objective_function(current[:vector])
  max_iter.times do |iter|
    big_stepsize = large_step_size(iter, step_size, s_factor, l_factor, iter_mult)
    step, big_step = take_steps(bounds, current, step_size, big_stepsize)
    if step[:cost] <= current[:cost] or big_step[:cost] <= current[:cost]
      if big_step[:cost] <= step[:cost]
        step_size, current = big_stepsize, big_step
      else
        current = step
      end
      count = 0
    else
      count += 1
      count, step_size = 0, (step_size/s_factor) if count >= max_no_impr
    end
    puts " > iteration #{(iter+1)}, best=#{current[:cost]}"
  end
  current
end

#take_step(minmax, current, step_size) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/Algorithmically/Stochastic/adaptive_random_search.rb', line 32

def take_step(minmax, current, step_size)
  position = Array.new(current.size)
  position.size.times do |i|
    min = [minmax[i][0], current[i]-step_size].max
    max = [minmax[i][1], current[i]+step_size].min
    position[i] = rand_in_bounds(min, max)
  end
  position
end

#take_steps(bounds, current, step_size, big_stepsize) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/Algorithmically/Stochastic/adaptive_random_search.rb', line 47

def take_steps(bounds, current, step_size, big_stepsize)
  step, big_step = {}, {}
  step[:vector] = take_step(bounds, current[:vector], step_size)
  step[:cost] = objective_function(step[:vector])
  big_step[:vector] = take_step(bounds, current[:vector], big_stepsize)
  big_step[:cost] = objective_function(big_step[:vector])
  return step, big_step
end