Commit 44a874d0 authored by Brice Videau's avatar Brice Videau

More bindings.

parent 5d5e097b
......@@ -10,3 +10,4 @@ require_relative 'cconfigspace/context'
require_relative 'cconfigspace/configuration_space'
require_relative 'cconfigspace/configuration'
require_relative 'cconfigspace/objective_space'
require_relative 'cconfigspace/evaluation'
......@@ -358,6 +358,7 @@ module CCS
CCS.ccs_release_object(@handle)
end
end
def self.add_property(name, type, accessor, memoize: false)
src = ""
src << "def #{name}\n"
......@@ -371,6 +372,19 @@ module CCS
class_eval src
end
def self.add_handle_property(name, type, accessor, memoize: false)
src = ""
src << "def #{name}\n"
src << " @#{name} ||= begin\n" if memoize
src << " ptr = MemoryPointer::new(:#{type})\n"
src << " res = CCS.#{accessor}(@handle, ptr)\n"
src << " CCS.error_check(res)\n"
src << " Object::from_handle(ptr.read_#{type})\n"
src << " end\n" if memoize
src << "end\n"
class_eval src
end
add_property :object_type, :ccs_object_type_t, :ccs_object_get_type, memoize: true
add_property :refcount, :uint32, :ccs_object_get_refcount
attr_reader :handle
......
......@@ -15,6 +15,7 @@ module CCS
include Comparable
add_property :user_data, :pointer, :ccs_configuration_get_user_data, memoize: true
add_property :hash, :ccs_hash_t, :ccs_configuration_hash, memoize: false
add_handle_property :configuration_space, :ccs_configuration_space_t, :ccs_configuration_get_configuration_space, memoize: true
def initialize(handle = nil, retain: false, configuration_space: nil, values: nil, user_data: nil)
if (handle)
......@@ -26,6 +27,8 @@ module CCS
p_values = MemoryPointer::new(:ccs_datum_t, count)
values.each_with_index { |v, i| Datum::new(p_values[i]).value = v }
values = p_values
else
count = 0
end
ptr = MemoryPointer::new(:ccs_configuration_t)
res = CCS.ccs_create_configuration(configuration_space, count, values, user_data, ptr)
......@@ -38,13 +41,17 @@ module CCS
self::new(handle, retain: true)
end
def configuration_space
@configuration_space ||= begin
ptr = MemoryPointer::new(:ccs_configuration_space_t)
res = CCS.ccs_configuration_get_configuration_space(@handle, ptr)
CCS.error_check(res)
ConfigurationSpace.from_handle(ptr.read_ccs_configuration_space_t)
def set_value(hyperparameter, value)
d = Datum.from_value(value)
case hyperparameter
when String
hyperparameter = configuration_space.hyperparameter_index_by_name(hyperparameter)
when Hyperparameter
hyperparameter = configuration_space.hyperparameter_index(hyperparameter)
end
res = CCS.ccs_configuration_set_value(@handle, hyperparameter, d)
CCS.error_check(res)
self
end
def value(hyperparameter)
......
......@@ -110,6 +110,13 @@ module CCS
ptr.read_size_t
end
def hyperparameter_index_by_name(name)
ptr = MemoryPointer::new(:size_t)
res = CCS.ccs_configuration_space_get_hyperparameter_index_by_name(@handle, name, ptr)
CCS.error_check(res)
ptr.read_size_t
end
def hyperparameters
count = num_hyperparameters
return [] if count == 0
......
module CCS
Comparison = enum FFI::Type::INT32, :ccs_comparison_t, [
:CCS_BETTER, -1,
:CCS_EQUIVALENT, 0,
:CCS_WORSE, 1,
:CCS_NOT_COMPARABLE, 2
]
class MemoryPointer
def read_ccs_comparison_t
Comparison.from_native(read_int32, nil)
end
end
attach_function :ccs_create_evaluation, [:ccs_objective_space_t, :ccs_configuration_t, :ccs_result_t, :size_t, :pointer, :pointer, :pointer], :ccs_result_t
attach_function :ccs_evaluation_get_objective_space, [:ccs_evaluation_t, :pointer], :ccs_result_t
attach_function :ccs_evaluation_get_configuration, [:ccs_evaluation_t, :pointer], :ccs_result_t
attach_function :ccs_evaluation_get_user_data, [:ccs_evaluation_t, :pointer], :ccs_result_t
attach_function :ccs_evaluation_get_error, [:ccs_evaluation_t, :pointer], :ccs_result_t
attach_function :ccs_evaluation_set_error, [:ccs_evaluation_t, :ccs_result_t], :ccs_result_t
attach_function :ccs_evaluation_get_value, [:ccs_evaluation_t, :size_t, :pointer], :ccs_result_t
attach_function :ccs_evaluation_set_value, [:ccs_evaluation_t, :size_t, :ccs_datum_t], :ccs_result_t
attach_function :ccs_evaluation_get_values, [:ccs_evaluation_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_evaluation_get_value_by_name, [:ccs_evaluation_t, :string, :ccs_datum_t], :ccs_result_t
attach_function :ccs_evaluation_get_objective_value, [:ccs_evaluation_t, :size_t, :pointer], :ccs_result_t
attach_function :ccs_evaluation_get_objective_values, [:ccs_evaluation_t, :size_t, :pointer, :pointer], :ccs_result_t
attach_function :ccs_evaluation_cmp, [:ccs_evaluation_t, :ccs_evaluation_t, :pointer], :ccs_result_t
class Evaluation < Object
add_handle_property :objective_space, :ccs_objective_space_t, :ccs_evaluation_get_objective_space, memoize: true
add_handle_property :configuration, :ccs_configuration_t, :ccs_evaluation_get_configuration, memoize: true
add_property :user_data, :pointer, :ccs_evaluation_get_user_data, memoize: true
add_property :error, :ccs_result_t, :ccs_evaluation_get_error, memoize: false
def initialize(handle = nil, retain: false, objective_space: nil, configuration: nil, error: :CCS_SUCCESS, values: nil, user_data: nil)
if handle
super(handle, retain: retain)
else
if values
count = values.size
raise StandardError, :CCS_INVALID_VALUE if count == 0
p_values = MemoryPointer::new(:ccs_datum_t, count)
values.each_with_index { |v, i| Datum::new(p_values[i]).value = v }
values = p_values
else
count = 0
end
ptr = MemoryPointer::new(:ccs_evaluation_t)
res = CCS.ccs_create_evaluation(objective_space, configuration, error, count, values, user_data, ptr)
CCS.error_check(res)
super(ptr.read_ccs_evaluation_t, retain: false)
@objective_space = objective_space
@configuration = configuration
end
end
def error=(err)
res = CCS.ccs_evaluation_set_error(@handle, err)
CCS.error_check(res)
err
end
def set_value(hyperparameter, value)
d = Datum.from_value(value)
case hyperparameter
when String
hyperparameter = objective_space.hyperparameter_index_by_name(hyperparameter)
when Hyperparameter
hyperparameter = objective_space.hyperparameter_index(hyperparameter)
end
res = CCS.ccs_evaluation_set_value(@handle, hyperparameter, d)
CCS.error_check(res)
self
end
def value(hyperparameter)
ptr = MemoryPointer::new(:ccs_datum_t)
case hyperparameter
when String
res = CCS.ccs_evaluation_get_value_by_name(@handle, hyperparameter, ptr)
when Hyperparameter
res = CCS.ccs_evaluation_get_value(@handle, objective_space.hyperparameter_index(hyperparameter), ptr)
when Integer
res = CCS.ccs_evaluation_get_value(@handle, hyperparameter, ptr)
else
raise StandardError, :CCS_INVALID_VALUE
end
CCS.error_check(res)
Datum::new(ptr).value
end
def num_values
@num_values ||= begin
ptr = MemoryPointer::new(:size_t)
res = CCS.ccs_evaluation_get_values(@handle, 0, nil, ptr)
CCS.error_check(res)
ptr.read_size_t
end
end
def values
count = num_values
return [] if count == 0
values = MemoryPointer::new(:ccs_datum_t, count)
res = CCS.ccs_evaluation_get_values(@handle, count, values, nil)
CCS.error_check(res)
count.times.collect { |i| Datum::new(values[i]).value }
end
def num_objective_values
@num_values ||= begin
ptr = MemoryPointer::new(:size_t)
res = CCS.ccs_evaluation_get_objective_values(@handle, 0, nil, ptr)
CCS.error_check(res)
ptr.read_size_t
end
end
def objective_values
count = num_values
return [] if count == 0
values = MemoryPointer::new(:ccs_datum_t, count)
res = CCS.ccs_evaluation_get_objective_values(@handle, count, values, nil)
CCS.error_check(res)
count.times.collect { |i| Datum::new(values[i]).value }
end
def cmp(other)
ptr = MemoryPointer::new(:ccs_objective_type_t)
res = CCS.ccs_evaluation_cmp(@handle, other, ptr)
CCS.error_check(res)
ptr.read_ccs_comparison_t
end
def <=>(other)
ptr = MemoryPointer::new(:ccs_objective_type_t)
res = CCS.ccs_evaluation_cmp(@handle, other, ptr)
CCS.error_check(res)
r = ptr.read_int32
r == 2 ? nil : r
end
end
end
......@@ -99,6 +99,13 @@ module CCS
ptr.read_size_t
end
def hyperparameter_index_by_name(name)
ptr = MemoryPointer::new(:size_t)
res = CCS.ccs_objective_space_get_hyperparameter_index_by_name(@handle, name, ptr)
CCS.error_check(res)
ptr.read_size_t
end
def hyperparameters
count = num_hyperparameters
return [] if count == 0
......@@ -115,6 +122,10 @@ module CCS
end
def add_objectives(expressions, types: nil)
if expressions.kind_of? Hash
types = expressions.values
expressions = expressions.keys
end
count = expressions.length
return self if count == 0
if types
......
......@@ -27,10 +27,12 @@ class CConfigSpaceTestConfigurationSpace < Minitest::Test
assert_equal( [h1, h2, h3], cs.hyperparameters )
assert_equal( h2, cs.hyperparameter_by_name(h2.name) )
cs.check(cs.default_configuration)
cs.check(cs.sample)
c = cs.sample
cs.check(c)
assert_equal( cs.handle, c.configuration_space.handle )
cs.check_values(cs.sample.values)
cs.samples(100).each { |c|
cs.check(cs.sample)
cs.check(c)
}
end
......
[ '../lib', 'lib' ].each { |d| $:.unshift(d) if File::directory?(d) }
require 'minitest/autorun'
require 'cconfigspace'
class CConfigSpaceTestEvaluation < Minitest::Test
def setup
CCS.init
end
def test_create
cs = CCS::ConfigurationSpace::new(name: "cspace")
h1 = CCS::NumericalHyperparameter::new
h2 = CCS::NumericalHyperparameter::new
h3 = CCS::NumericalHyperparameter::new
cs.add_hyperparameters [h1, h2, h3]
os = CCS::ObjectiveSpace::new(name: "ospace")
v1 = CCS::NumericalHyperparameter::new
v2 = CCS::NumericalHyperparameter::new
os.add_hyperparameters [v1, v2]
e1 = CCS::Variable::new(hyperparameter: v1)
e2 = CCS::Variable::new(hyperparameter: v2)
os.add_objectives( { e1 => :CCS_MAXIMIZE, e2 => :CCS_MINIMIZE } )
ev1 = CCS::Evaluation::new(objective_space: os, configuration: cs.sample)
ev1.set_value(0, 0.5)
ev1.set_value(v2, 0.6)
assert_equal( [0.5, 0.6], ev1.values )
assert_equal( [0.5, 0.6], ev1.objective_values )
ev2 = CCS::Evaluation::new(objective_space: os, configuration: cs.sample, values: [0.5, 0.6])
assert_equal( [0.5, 0.6], ev2.values )
assert_equal( [0.5, 0.6], ev2.objective_values )
assert_equal( 0, ev1 <=> ev2 )
assert_equal( :CCS_EQUIVALENT, ev1.cmp(ev2) )
ev3 = CCS::Evaluation::new(objective_space: os, configuration: cs.sample, values: [0.6, 0.5])
assert_equal( [0.6, 0.5], ev3.objective_values )
assert_equal( 1, ev1 <=> ev3 )
assert_equal( :CCS_WORSE, ev1.cmp(ev3) )
assert_equal( -1, ev3 <=> ev1 )
assert_equal( :CCS_BETTER, ev3.cmp(ev1) )
ev4 = CCS::Evaluation::new(objective_space: os, configuration: cs.sample, values: [0.6, 0.7])
assert_equal( [0.6, 0.7], ev4.objective_values )
assert_nil( ev1 <=> ev4 )
assert_equal( :CCS_NOT_COMPARABLE, ev1.cmp(ev4) )
assert_nil( ev4 <=> ev1 )
assert_equal( :CCS_NOT_COMPARABLE, ev4.cmp(ev1) )
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