Class: OpenCLMinimization::BrentMinimizer
- Inherits:
-
Object
- Object
- OpenCLMinimization::BrentMinimizer
- Defined in:
- lib/opencl/opencl_minimization.rb
Overview
Direct port of Brent algorithm found on GSL. See Unidimensional for methods.
Usage
n = 3
start_point = [1, 3, 5]
expected_point = [1.5, 3.5, 5.5]
end_point = [3, 5, 7]
f = "pow((x-2)*(x-4)*(x-6), 2)+1"
min = OpenCLMinimization::BisectionMinimizer.new(n, start_point, expected_point, end_point, f)
min.minimize
min.x_minimum
min.f_minimum
Instance Attribute Summary collapse
-
#epsilon ⇒ Object
writeonly
Sets the attribute epsilon.
-
#f_minimum ⇒ Object
readonly
Returns the value of attribute f_minimum.
-
#golden ⇒ Object
writeonly
Sets the attribute golden.
-
#max_iterations ⇒ Object
writeonly
Sets the attribute max_iterations.
-
#sqrt_epsilon ⇒ Object
writeonly
Sets the attribute sqrt_epsilon.
-
#x_minimum ⇒ Object
readonly
Returns the value of attribute x_minimum.
Instance Method Summary collapse
-
#initialize(n, start_point, expected_point, end_point, f) ⇒ BrentMinimizer
constructor
Parameters: *
n
: Number of Jobs *start_point
: Lower possible value *expected_point
: Initial point *end_point
: Higher possible value *f
: Original function string. - #minimize ⇒ Object
Constructor Details
#initialize(n, start_point, expected_point, end_point, f) ⇒ BrentMinimizer
Parameters:
-
n
: Number of Jobs -
start_point
: Lower possible value -
expected_point
: Initial point -
end_point
: Higher possible value -
f
: Original function string
214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/opencl/opencl_minimization.rb', line 214 def initialize(n, start_point, expected_point, end_point, f) @n = n @start_point = start_point @expected_point = expected_point @end_point = end_point @f = f @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
203 204 205 |
# File 'lib/opencl/opencl_minimization.rb', line 203 def epsilon=(value) @epsilon = value end |
#f_minimum ⇒ Object (readonly)
Returns the value of attribute f_minimum.
200 201 202 |
# File 'lib/opencl/opencl_minimization.rb', line 200 def f_minimum @f_minimum end |
#golden=(value) ⇒ Object (writeonly)
Sets the attribute golden
204 205 206 |
# File 'lib/opencl/opencl_minimization.rb', line 204 def golden=(value) @golden = value end |
#max_iterations=(value) ⇒ Object (writeonly)
Sets the attribute max_iterations
202 203 204 |
# File 'lib/opencl/opencl_minimization.rb', line 202 def max_iterations=(value) @max_iterations = value end |
#sqrt_epsilon=(value) ⇒ Object (writeonly)
Sets the attribute sqrt_epsilon
205 206 207 |
# File 'lib/opencl/opencl_minimization.rb', line 205 def sqrt_epsilon=(value) @sqrt_epsilon = value end |
#x_minimum ⇒ Object (readonly)
Returns the value of attribute x_minimum.
199 200 201 |
# File 'lib/opencl/opencl_minimization.rb', line 199 def x_minimum @x_minimum end |
Instance Method Details
#minimize ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/opencl/opencl_minimization.rb', line 226 def minimize # create Buffers for inputs and outputs start_buffer = FFI::Buffer.alloc_inout(:pointer, @n) expected_buffer = FFI::Buffer.alloc_inout(:pointer, @n) end_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 start_buffer.write_array_of_float(@start_point) expected_buffer.write_array_of_float(@expected_point) end_buffer.write_array_of_float(@end_point) # call minimizer OpenCLMinimization::opencl_minimize(@n, start_buffer, expected_buffer, end_buffer, 3, @f, "", "", 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 |