Class: CrossEntropy::ContinuousProblem

Inherits:
AbstractProblem show all
Defined in:
lib/cross_entropy/continuous_problem.rb

Overview

Solve a continuous optimisation problem. The sampling distribution of each parameter is assumed to be a 1D Gaussian with given mean and variance.

Instance Attribute Summary

Attributes inherited from AbstractProblem

#elite_score, #max_iters, #min_score, #num_elite, #num_iters, #num_samples, #overall_min_score, #overall_min_score_sample, #params, #track_overall_min

Instance Method Summary collapse

Methods inherited from AbstractProblem

#for_stop_decision, #solve, #to_estimate, #to_generate_samples, #to_score_sample, #to_update

Constructor Details

#initialize(mean, stddev) {|_self| ... } ⇒ ContinuousProblem

Returns a new instance of ContinuousProblem.

Yields:

  • (_self)

Yield Parameters:



9
10
11
12
13
14
15
16
# File 'lib/cross_entropy/continuous_problem.rb', line 9

def initialize(mean, stddev)
  super [mean, stddev]

  to_generate_samples { generate_gaussian_samples }
  to_estimate { |elite| estimate_ml(elite) }

  yield(self) if block_given?
end

Instance Method Details

#estimate_ml(elite) ⇒ Array

Maximum likelihood estimate using only the given ‘elite’ solutions.

Parameters:

  • elite (NArray)

    elite samples; dimension 0 is the sample index; the remaining dimensions contain the samples

Returns:

  • (Array)

    the estimated parameter arrays



48
49
50
# File 'lib/cross_entropy/continuous_problem.rb', line 48

def estimate_ml(elite)
  [elite.mean(0), elite.stddev(0)]
end

#generate_gaussian_samplesObject

Generate samples.



33
34
35
36
37
38
# File 'lib/cross_entropy/continuous_problem.rb', line 33

def generate_gaussian_samples
  r = NArray.float(num_samples, *sample_shape).randomn
  mean = param_mean.reshape(1, *sample_shape)
  stddev = param_stddev.reshape(1, *sample_shape)
  mean + stddev * r
end

#param_meanObject



18
19
20
# File 'lib/cross_entropy/continuous_problem.rb', line 18

def param_mean
  params[0]
end

#param_stddevObject



22
23
24
# File 'lib/cross_entropy/continuous_problem.rb', line 22

def param_stddev
  params[1]
end

#sample_shapeObject



26
27
28
# File 'lib/cross_entropy/continuous_problem.rb', line 26

def sample_shape
  param_mean.shape
end