test_hyperparameter.rb 4.78 KB
Newer Older
Brice Videau's avatar
Brice Videau committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
[ '../lib', 'lib' ].each { |d| $:.unshift(d) if File::directory?(d) }
require 'minitest/autorun'
require 'cconfigspace'

class CConfigSpaceTestHyperparameter < Minitest::Test
  def setup
    CCS.init
  end

  def test_ordinal_compare
    values = ["foo", 2, 3.0]
    h = CCS::OrdinalHyperparameter::new(values: values)
    assert_equal( 0, h.compare("foo", "foo") )
    assert_equal( -1, h.compare("foo", 2) )
    assert_equal( -1, h.compare("foo", 3.0) )
    assert_equal( 1, h.compare(2, "foo") )
    assert_equal( 0, h.compare(2, 2) )
    assert_equal( -1, h.compare(2, 3.0) )
    assert_equal( 1, h.compare(3.0, "foo") )
    assert_equal( 1, h.compare(3.0, 2) )
    assert_equal( 0, h.compare(3.0, 3.0) )
    assert_raises(StandardError, :CCS_INVALID_VALUE) { h.compare(4.0, "foo") }
  end

  def test_from_handle_ordinal
    values = ["foo", 2, 3.0]
    h = CCS::OrdinalHyperparameter::new(values: values)
    h2 = CCS::Object::from_handle(h)
    assert_equal( h.class, h2.class )
  end

  def test_ordinal
    values = ["foo", 2, 3.0]
    h = CCS::OrdinalHyperparameter::new(values: values)
    assert_equal( :CCS_HYPERPARAMETER, h.object_type )
    assert_equal( :CCS_ORDINAL, h.type )
    assert_match( /param/, h.name )
    assert( h.user_data.null? )
    assert_equal( "foo", h.default_value )
    assert_equal( :CCS_UNIFORM, h.default_distribution.type )
    assert_equal( values, h.values )
    assert( h.check_value("foo") )
    assert( h.check_value(2) )
    assert( h.check_value(3.0) )
    refute( h.check_value(1.5) )
    v = h.sample
    assert( values.include? v )
    vals = h.samples(100)
    vals.each { |v|
      assert( values.include? v )
    }
  end

  def test_from_handle_categorical
    values = ["foo", 2, 3.0]
    h = CCS::CategoricalHyperparameter::new(values: values)
    h2 = CCS::Object::from_handle(h)
    assert_equal( h.class, h2.class )
  end

  def test_categorical
    values = ["foo", 2, 3.0]
    h = CCS::CategoricalHyperparameter::new(values: values)
    assert_equal( :CCS_HYPERPARAMETER, h.object_type )
    assert_equal( :CCS_CATEGORICAL, h.type )
    assert_match( /param/, h.name )
    assert( h.user_data.null? )
    assert_equal( "foo", h.default_value )
    assert_equal( :CCS_UNIFORM, h.default_distribution.type )
    assert_equal( values, h.values )
    assert( h.check_value("foo") )
    assert( h.check_value(2) )
    assert( h.check_value(3.0) )
    refute( h.check_value(1.5) )
    v = h.sample
    assert( values.include? v )
    vals = h.samples(100)
    vals.each { |v|
      assert( values.include? v )
    }
  end

  def test_from_handle_numerical
    h = CCS::NumericalHyperparameter::new
    h2 = CCS::Object::from_handle(h)
    assert_equal( h.class, h2.class )
  end

  def test_create_numerical
    h = CCS::NumericalHyperparameter::new
    assert_equal( :CCS_HYPERPARAMETER, h.object_type )
    assert_equal( :CCS_NUMERICAL, h.type )
    assert_match( /param/, h.name )
    assert( h.user_data.null? )
    assert_equal( 0.0, h.default_value )
    assert_equal( :CCS_UNIFORM, h.default_distribution.type )
    assert( h.check_value(0.5) )
    refute( h.check_value(1.5) )
    v = h.sample
    assert( v.kind_of?(Float) )
    assert( v >= 0.0 && v < 1.0 )
    vals = h.samples(100)
    vals.each { |v|
      assert( v.kind_of?(Float) )
      assert( v >= 0.0 && v < 1.0 )
    }
  end

  def test_create_numerical_float
    h = CCS::NumericalHyperparameter::float(lower: 0.0, upper: 1.0)
    assert_equal( :CCS_HYPERPARAMETER, h.object_type )
    assert_equal( :CCS_NUMERICAL, h.type )
    assert_match( /param/, h.name )
    assert( h.user_data.null? )
    assert_equal( 0.0, h.default_value )
    assert_equal( :CCS_UNIFORM, h.default_distribution.type )
    assert_equal( :CCS_NUM_FLOAT, h.data_type )
    assert_equal( 0.0, h.lower )
    assert_equal( 1.0, h.upper )
    assert_equal( 0.0, h.quantization )
    assert( h.check_value(0.5) )
    refute( h.check_value(1.5) )
    v = h.sample
    assert( v.kind_of?(Float) )
    assert( v >= 0.0 && v < 1.0 )
    vals = h.samples(100)
    vals.each { |v|
      assert( v.kind_of?(Float) )
      assert( v >= 0.0 && v < 1.0 )
    }
  end

  def test_create_numerical_int
    h = CCS::NumericalHyperparameter::int(lower: 0, upper: 100)
    assert_equal( :CCS_NUMERICAL, h.type )
    assert_match( /param/, h.name )
    assert( h.user_data.null? )
    assert_equal( 0, h.default_value )
    assert_equal( :CCS_UNIFORM, h.default_distribution.type )
    assert_equal( :CCS_NUM_INTEGER, h.data_type )
    assert_equal( 0, h.lower )
    assert_equal( 100, h.upper )
    assert_equal( 0, h.quantization )
    assert( h.check_value(50) )
    refute( h.check_value(150) )
    v = h.sample
    assert( v.kind_of?(Integer) )
    assert( v >= 0 && v < 100 )
    vals = h.samples(100)
    vals.each { |v|
      assert( v.kind_of?(Integer) )
      assert( v >= 0 && v < 100 )
    }
  end

end