GitLab maintenance scheduled form Friday, 2021-06-18 5:00pm to Satursday, 2021-06-19 10:00pm CT - Services will be unavailable during this time.

Commit 8fec6645 authored by Brice Videau's avatar Brice Videau

Finished first ruby bindings.

parent bb2d4d94
...@@ -11,3 +11,4 @@ require_relative 'cconfigspace/configuration_space' ...@@ -11,3 +11,4 @@ require_relative 'cconfigspace/configuration_space'
require_relative 'cconfigspace/configuration' require_relative 'cconfigspace/configuration'
require_relative 'cconfigspace/objective_space' require_relative 'cconfigspace/objective_space'
require_relative 'cconfigspace/evaluation' require_relative 'cconfigspace/evaluation'
require_relative 'cconfigspace/tuner'
...@@ -97,6 +97,7 @@ module CCS ...@@ -97,6 +97,7 @@ module CCS
:CCS_INVALID_CONFIGURATION, :CCS_INVALID_CONFIGURATION,
:CCS_INVALID_NAME, :CCS_INVALID_NAME,
:CCS_INVALID_CONDITION, :CCS_INVALID_CONDITION,
:CCS_INVALID_TUNER,
:CCS_INVALID_GRAPH, :CCS_INVALID_GRAPH,
:CCS_TYPE_NOT_COMPARABLE, :CCS_TYPE_NOT_COMPARABLE,
:CCS_INVALID_BOUNDS, :CCS_INVALID_BOUNDS,
...@@ -388,7 +389,11 @@ module CCS ...@@ -388,7 +389,11 @@ module CCS
add_property :object_type, :ccs_object_type_t, :ccs_object_get_type, memoize: true add_property :object_type, :ccs_object_type_t, :ccs_object_get_type, memoize: true
add_property :refcount, :uint32, :ccs_object_get_refcount add_property :refcount, :uint32, :ccs_object_get_refcount
attr_reader :handle attr_reader :handle
def initialize(handle, retain: false, auto_release: true) def initialize(handle, retain: false, auto_release: true)
if !handle
raise StandardError, :CCS_INVALID_OBJECT
end
@handle = handle @handle = handle
if retain if retain
res = CCS.ccs_retain_object(handle) res = CCS.ccs_retain_object(handle)
......
...@@ -37,13 +37,6 @@ module CCS ...@@ -37,13 +37,6 @@ module CCS
add_property :dimension, :size_t, :ccs_distribution_get_dimension, memoize: true add_property :dimension, :size_t, :ccs_distribution_get_dimension, memoize: true
add_property :scale, :ccs_scale_type_t, :ccs_distribution_get_scale_type, memoize: true add_property :scale, :ccs_scale_type_t, :ccs_distribution_get_scale_type, memoize: true
def initialize(handle, retain: false)
if !handle
raise StandardError, :CCS_INVALID_OBJECT
end
super
end
def self.from_handle(handle) def self.from_handle(handle)
ptr = MemoryPointer::new(:ccs_distribution_type_t) ptr = MemoryPointer::new(:ccs_distribution_type_t)
res = CCS.ccs_distribution_get_type(handle, ptr) res = CCS.ccs_distribution_get_type(handle, ptr)
......
...@@ -52,6 +52,10 @@ module CCS ...@@ -52,6 +52,10 @@ module CCS
end end
end end
def self.from_handle(handle)
self::new(handle, retain: true)
end
def error=(err) def error=(err)
res = CCS.ccs_evaluation_set_error(@handle, err) res = CCS.ccs_evaluation_set_error(@handle, err)
CCS.error_check(res) CCS.error_check(res)
......
...@@ -31,12 +31,6 @@ module CCS ...@@ -31,12 +31,6 @@ module CCS
class Hyperparameter < Object class Hyperparameter < Object
add_property :type, :ccs_hyperparameter_type_t, :ccs_hyperparameter_get_type, memoize:true add_property :type, :ccs_hyperparameter_type_t, :ccs_hyperparameter_get_type, memoize:true
add_property :user_data, :pointer, :ccs_hyperparameter_get_user_data, memoize: true add_property :user_data, :pointer, :ccs_hyperparameter_get_user_data, memoize: true
def initialize(handle, retain: false)
if !handle
raise StandardError, :CCS_INVALID_OBJECT
end
super
end
def self.default_name def self.default_name
"param#{"%03d"%CCS.get_id}" "param#{"%03d"%CCS.get_id}"
......
module CCS
TunerType = enum FFI::Type::INT32, :ccs_tuner_type_t, [
:CCS_RANDOM,
:CCS_GENERIC
]
class MemoryPointer
def read_ccs_tuner_type_t
TunerType.from_native(read_int32, nil)
end
end
attach_function :ccs_tuner_get_type, [:ccs_tuner_t, :pointer], :ccs_result_t
attach_function :ccs_tuner_get_name, [:ccs_tuner_t, :pointer], :ccs_result_t
attach_function :ccs_tuner_get_user_data, [:ccs_tuner_t, :pointer], :ccs_result_t
attach_function :ccs_tuner_get_configuration_space, [:ccs_tuner_t, :pointer], :ccs_result_t
attach_function :ccs_tuner_get_objective_space, [:ccs_tuner_t, :pointer], :ccs_result_t
attach_function :ccs_tuner_ask, [:ccs_tuner_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_tuner_tell, [:ccs_tuner_t, :size_t, :pointer], :ccs_result_t
attach_function :ccs_tuner_get_optimums, [:ccs_tuner_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_tuner_get_history, [:ccs_tuner_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_create_random_tuner, [:string, :ccs_configuration_space_t, :ccs_objective_space_t, :pointer, :pointer], :ccs_result_t
class Tuner < Object
add_property :type, :ccs_tuner_type_t, :ccs_tuner_get_type, memoize: true
add_property :user_data, :pointer, :ccs_tuner_get_user_data, memoize: true
add_handle_property :configuration_space, :ccs_configuration_space_t, :ccs_tuner_get_configuration_space, memoize: true
add_handle_property :objective_space, :ccs_objective_space_t, :ccs_tuner_get_objective_space, memoize: true
def from_handle(handle)
ptr = MemoryPointer::new(:ccs_tuner_type_t)
res = CCS.ccs_tuner_get_type(handle, ptr)
CCS.error_check(res)
case ptr.read_ccs_tuner_type_t
when :CCS_RANDOM
RandomTuner::new(handle, retain: true)
when :CCS_GENERIC
GenericTuner::new(handle, retain: true)
else
raise StandardError, :CCS_INVALID_TUNER
end
end
def name
@name ||= begin
ptr = MemoryPointer::new(:pointer)
res = CCS.ccs_tuner_get_name(@handle, ptr)
CCS.error_check(res)
ptr.read_pointer.read_string
end
end
def ask(count = 1)
p_confs = MemoryPointer::new(:ccs_configuration_t, count)
p_num = MemoryPointer::new(:size_t)
res = CCS.ccs_tuner_ask(@handle, count, p_confs, p_num)
CCS.error_check(res)
count = p_num.read_size_t
count.times.collect { |i| Configuration::new(p_confs[i].read_pointer, retain: false) }
end
def tell(evaluations)
count = evaluations.size
p_evals = MemoryPointer::new(:ccs_evaluation_t, count)
p_evals.write_array_of_pointer(evaluations.collect(&:handle))
res = CCS.ccs_tuner_tell(@handle, count, p_evals)
CCS.error_check(res)
self
end
def history_size
p_count = MemoryPointer::new(:size_t)
res = CCS.ccs_tuner_get_history(@handle, 0, nil, p_count)
CCS.error_check(res)
return p_count.read_size_t
end
def history
count = history_size
p_evals = MemoryPointer::new(:ccs_evaluation_t, count)
res = CCS.ccs_tuner_get_history(@handle, count, p_evals, nil)
CCS.error_check(res)
count.times.collect { |i| Evaluation::from_handle(p_evals[i].read_pointer) }
end
def num_optimums
p_count = MemoryPointer::new(:size_t)
res = CCS.ccs_tuner_get_optimums(@handle, 0, nil, p_count)
CCS.error_check(res)
return p_count.read_size_t
end
def optimums
count = num_optimums
p_evals = MemoryPointer::new(:ccs_evaluation_t, count)
res = CCS.ccs_tuner_get_optimums(@handle, count, p_evals, nil)
CCS.error_check(res)
count.times.collect { |i| Evaluation::from_handle(p_evals[i].read_pointer) }
end
end
class RandomTuner < Tuner
def initialize(handle = nil, retain: false, name: nil, configuration_space: nil, objective_space: nil, user_data: nil)
if handle
super(handle, retain: retain)
else
ptr = MemoryPointer::new(:ccs_tuner_t)
res = CCS.ccs_create_random_tuner(name, configuration_space, objective_space, user_data, ptr)
CCS.error_check(res)
super(ptr.read_ccs_tuner_t, retain: false)
end
end
end
end
[ '../lib', 'lib' ].each { |d| $:.unshift(d) if File::directory?(d) }
require 'minitest/autorun'
require 'cconfigspace'
class CConfigSpaceTestTuner < Minitest::Test
def setup
CCS.init
end
def test_create
cs = CCS::ConfigurationSpace::new(name: "cspace")
h1 = CCS::NumericalHyperparameter::new(lower: -5.0, upper: 5.0)
h2 = CCS::NumericalHyperparameter::new(lower: -5.0, upper: 5.0)
h3 = CCS::NumericalHyperparameter::new(lower: -5.0, upper: 5.0)
cs.add_hyperparameters [h1, h2, h3]
os = CCS::ObjectiveSpace::new(name: "ospace")
v1 = CCS::NumericalHyperparameter::new(lower: -Float::INFINITY, upper: Float::INFINITY)
v2 = CCS::NumericalHyperparameter::new(lower: -Float::INFINITY, upper: Float::INFINITY)
os.add_hyperparameters [v1, v2]
e1 = CCS::Variable::new(hyperparameter: v1)
e2 = CCS::Variable::new(hyperparameter: v2)
os.add_objectives( [e1, e2] )
t = CCS::RandomTuner::new(name: "tuner", configuration_space: cs, objective_space: os)
assert_equal( "tuner", t.name )
assert_equal( :CCS_RANDOM, t.type )
func = lambda { |(x, y, z)|
[(x-2)**2, Math.sin(z+y)]
}
evals = t.ask(100).collect { |c|
CCS::Evaluation::new(objective_space: os, configuration: c, values: func[c.values])
}
t.tell evals
hist = t.history
assert_equal(100, hist.size)
evals = t.ask(100).collect { |c|
CCS::Evaluation::new(objective_space: os, configuration: c, values: func[c.values])
}
t.tell evals
assert_equal(200, t.history_size)
objs = t.optimums.collect(&:objective_values).sort
objs.collect { |(_, v)| v }.each_cons(2) { |v1, v2| assert( (v1 <=> v2) > 0 ) }
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment