Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
CCS
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Brice Videau
CCS
Commits
8fec6645
Commit
8fec6645
authored
Jun 19, 2020
by
Brice Videau
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finished first ruby bindings.
parent
bb2d4d94
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
168 additions
and
13 deletions
+168
-13
bindings/ruby/lib/cconfigspace.rb
bindings/ruby/lib/cconfigspace.rb
+1
-0
bindings/ruby/lib/cconfigspace/base.rb
bindings/ruby/lib/cconfigspace/base.rb
+5
-0
bindings/ruby/lib/cconfigspace/distribution.rb
bindings/ruby/lib/cconfigspace/distribution.rb
+0
-7
bindings/ruby/lib/cconfigspace/evaluation.rb
bindings/ruby/lib/cconfigspace/evaluation.rb
+4
-0
bindings/ruby/lib/cconfigspace/hyperparameter.rb
bindings/ruby/lib/cconfigspace/hyperparameter.rb
+0
-6
bindings/ruby/lib/cconfigspace/tuner.rb
bindings/ruby/lib/cconfigspace/tuner.rb
+114
-0
bindings/ruby/test/test_tuner.rb
bindings/ruby/test/test_tuner.rb
+44
-0
No files found.
bindings/ruby/lib/cconfigspace.rb
View file @
8fec6645
...
...
@@ -11,3 +11,4 @@ require_relative 'cconfigspace/configuration_space'
require_relative
'cconfigspace/configuration'
require_relative
'cconfigspace/objective_space'
require_relative
'cconfigspace/evaluation'
require_relative
'cconfigspace/tuner'
bindings/ruby/lib/cconfigspace/base.rb
View file @
8fec6645
...
...
@@ -97,6 +97,7 @@ module CCS
:CCS_INVALID_CONFIGURATION
,
:CCS_INVALID_NAME
,
:CCS_INVALID_CONDITION
,
:CCS_INVALID_TUNER
,
:CCS_INVALID_GRAPH
,
:CCS_TYPE_NOT_COMPARABLE
,
:CCS_INVALID_BOUNDS
,
...
...
@@ -388,7 +389,11 @@ module CCS
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
def
initialize
(
handle
,
retain:
false
,
auto_release:
true
)
if
!
handle
raise
StandardError
,
:CCS_INVALID_OBJECT
end
@handle
=
handle
if
retain
res
=
CCS
.
ccs_retain_object
(
handle
)
...
...
bindings/ruby/lib/cconfigspace/distribution.rb
View file @
8fec6645
...
...
@@ -37,13 +37,6 @@ module CCS
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
def
initialize
(
handle
,
retain:
false
)
if
!
handle
raise
StandardError
,
:CCS_INVALID_OBJECT
end
super
end
def
self
.
from_handle
(
handle
)
ptr
=
MemoryPointer
::
new
(
:ccs_distribution_type_t
)
res
=
CCS
.
ccs_distribution_get_type
(
handle
,
ptr
)
...
...
bindings/ruby/lib/cconfigspace/evaluation.rb
View file @
8fec6645
...
...
@@ -52,6 +52,10 @@ module CCS
end
end
def
self
.
from_handle
(
handle
)
self
::
new
(
handle
,
retain:
true
)
end
def
error
=
(
err
)
res
=
CCS
.
ccs_evaluation_set_error
(
@handle
,
err
)
CCS
.
error_check
(
res
)
...
...
bindings/ruby/lib/cconfigspace/hyperparameter.rb
View file @
8fec6645
...
...
@@ -31,12 +31,6 @@ module CCS
class
Hyperparameter
<
Object
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
def
initialize
(
handle
,
retain:
false
)
if
!
handle
raise
StandardError
,
:CCS_INVALID_OBJECT
end
super
end
def
self
.
default_name
"param
#{
"%03d"
%
CCS
.
get_id
}
"
...
...
bindings/ruby/lib/cconfigspace/tuner.rb
0 → 100644
View file @
8fec6645
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
bindings/ruby/test/test_tuner.rb
0 → 100644
View file @
8fec6645
[
'../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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment