Method: MachineLearningWorkbench::Optimizer::NaturalEvolutionStrategies::Base#initialize

Defined in:
lib/machine_learning_workbench/optimizer/natural_evolution_strategies/base.rb

#initialize(ndims, obj_fn, opt_type, rseed: nil, mu_init: 0, sigma_init: 1, parallel_fit: false, rescale_popsize: 1, rescale_lrate: 1) ⇒ Base

NES object initialization

Parameters:

  • ndims (Integer)

    number of parameters to optimize

  • obj_fn (#call)

    any object defining a #call method (Proc, lambda, custom class)

  • opt_type (:min, :max)

    select minimization / maximization of obj_fn

  • rseed (Integer) (defaults to: nil)

    allow for deterministic execution on rseed provided

  • mu_init (Numeric) (defaults to: 0)

    values to initalize the distribution’s mean

  • sigma_init (Numeric) (defaults to: 1)

    values to initialize the distribution’s covariance

  • parallel_fit (boolean) (defaults to: false)

    whether the obj_fn should be passed all the individuals together. In the canonical case the fitness function always scores a single individual; in practical cases though it is easier to delegate the scoring parallelization to the external fitness function. Turning this to true will make the algorithm pass _an Array_ of individuals to the fitness function, rather than a single instance.

  • rescale_popsize (Float) (defaults to: 1)

    scaling for the default population size

  • rescale_lrate (Float) (defaults to: 1)

    scaling for the default learning rate

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/machine_learning_workbench/optimizer/natural_evolution_strategies/base.rb', line 22

def initialize ndims, obj_fn, opt_type, rseed: nil, mu_init: 0, sigma_init: 1, parallel_fit: false, rescale_popsize: 1, rescale_lrate: 1
  raise ArgumentError unless [:min, :max].include? opt_type
  raise ArgumentError unless obj_fn.respond_to? :call
  @ndims, @opt_type, @obj_fn, @parallel_fit = ndims, opt_type, obj_fn, parallel_fit
  @rescale_popsize, @rescale_lrate = rescale_popsize, rescale_lrate
  @eye = NArray.eye(ndims)
  rseed ||= Random.new_seed
  # puts "NES rseed: #{s}"  # currently disabled
  @rng = Random.new rseed
  @best = [(opt_type==:max ? -1 : 1) * Float::INFINITY, nil]
  @last_fits = []
  initialize_distribution mu_init: mu_init, sigma_init: sigma_init
end