Class: OpenCLMinimization::NewtonRampsonMinimizer

Inherits:
Object
  • Object
show all
Defined in:
lib/opencl/opencl_minimization.rb

Overview

Classic Newton-Raphson minimization method.
Requires first and second derivative == Usage n = 3 expected_point = [1, 100, 1000] f = "(x-3)(x-3)+5" fd = "2(x-3)" fdd = "2" min = OpenCLMinimization::NewtonRampsonMinimizer.new(n, expected_point, f, fd, fdd) min.minimize min.x_minimum min.f_minimum

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n, expected_point, f, fd, fdd) ⇒ NewtonRampsonMinimizer

== Parameters:

  • n: Number of Jobs
  • expected_point: Initial point
  • f: Original function
  • fd: First derivative function string
  • fdd: Second derivative function string


111
112
113
114
115
116
117
118
119
120
121
# File 'lib/opencl/opencl_minimization.rb', line 111

def initialize(n, expected_point, f, fd, fdd)
  @n              = n
  @expected_point = expected_point
  @f              = f
  @fd             = fd
  @fdd            = fdd
  @max_iterations = MAX_ITERATIONS_DEFAULT
  @epsilon        = EPSILON_DEFAULT
  @golden         = GOLDEN_DEFAULT
  @sqrt_epsilon   = SQRT_EPSILON_DEFAULT
end

Instance Attribute Details

#epsilon=(value) ⇒ Object (writeonly)

Sets the attribute epsilon

Parameters:

  • value

    the value to set the attribute epsilon to.



101
102
103
# File 'lib/opencl/opencl_minimization.rb', line 101

def epsilon=(value)
  @epsilon = value
end

#f_minimumObject (readonly)

Returns the value of attribute f_minimum.



98
99
100
# File 'lib/opencl/opencl_minimization.rb', line 98

def f_minimum
  @f_minimum
end

#golden=(value) ⇒ Object (writeonly)

Sets the attribute golden

Parameters:

  • value

    the value to set the attribute golden to.



102
103
104
# File 'lib/opencl/opencl_minimization.rb', line 102

def golden=(value)
  @golden = value
end

#max_iterations=(value) ⇒ Object (writeonly)

Sets the attribute max_iterations

Parameters:

  • value

    the value to set the attribute max_iterations to.



100
101
102
# File 'lib/opencl/opencl_minimization.rb', line 100

def max_iterations=(value)
  @max_iterations = value
end

#x_minimumObject (readonly)

Returns the value of attribute x_minimum.



97
98
99
# File 'lib/opencl/opencl_minimization.rb', line 97

def x_minimum
  @x_minimum
end

Instance Method Details

#minimizeObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/opencl/opencl_minimization.rb', line 123

def minimize
  # create Buffers for inputs and outputs
  expected_buffer = FFI::Buffer.alloc_inout(:pointer, @n)
  x_buffer        = FFI::Buffer.alloc_inout(:pointer, @n)
  f_buffer        = FFI::Buffer.alloc_inout(:pointer, @n)

  # set inputs
  expected_buffer.write_array_of_float(@expected_point)

  # call minimizer
  OpenCLMinimization::opencl_minimize(@n, nil, expected_buffer, nil, 1, @f, @fd, @fdd, x_buffer, f_buffer, 0,
                                      @max_iterations, @epsilon, @golden, @sqrt_epsilon, PATH_TO_KERNEL)

  @x_minimum = Array.new(@n)
  @f_minimum = Array.new(@n)
  # read results
  @x_minimum = x_buffer.read_array_of_float(@n)
  @f_minimum = f_buffer.read_array_of_float(@n)
end