-
Notifications
You must be signed in to change notification settings - Fork 22
/
distribution.go
359 lines (316 loc) · 11.1 KB
/
distribution.go
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package goptuna
import (
"encoding/json"
"errors"
"math"
)
var (
// ErrUnknownDistribution returns the distribution is unknown.
ErrUnknownDistribution = errors.New("unknown distribution")
)
// Distribution represents a parameter that can be optimized.
type Distribution interface {
// ToExternalRepr to convert internal representation of a parameter value into external representation.
ToExternalRepr(float64) interface{}
// Single to test whether the range of this distribution contains just a single value.
Single() bool
// Contains to check a parameter value is contained in the range of this distribution.
Contains(float64) bool
}
var _ Distribution = &UniformDistribution{}
// UniformDistribution is a uniform distribution in the linear domain.
type UniformDistribution struct {
// High is higher endpoint of the range of the distribution (included in the range).
High float64 `json:"high"`
// Low is lower endpoint of the range of the distribution (included in the range).
Low float64 `json:"low"`
}
// UniformDistributionName is the identifier name of UniformDistribution
const UniformDistributionName = "UniformDistribution"
// ToExternalRepr to convert internal representation of a parameter value into external representation.
func (d *UniformDistribution) ToExternalRepr(ir float64) interface{} {
return ir
}
// Single to test whether the range of this distribution contains just a single value.
func (d *UniformDistribution) Single() bool {
return d.High == d.Low
}
// Contains to check a parameter value is contained in the range of this distribution.
func (d *UniformDistribution) Contains(ir float64) bool {
if d.Single() {
return ir == d.Low
}
return d.Low <= ir && ir <= d.High
}
var _ Distribution = &LogUniformDistribution{}
// LogUniformDistribution is a uniform distribution in the log domain.
type LogUniformDistribution struct {
// High is higher endpoint of the range of the distribution (included in the range).
High float64 `json:"high"`
// Low is lower endpoint of the range of the distribution (included in the range).
Low float64 `json:"low"`
}
// LogUniformDistributionName is the identifier name of LogUniformDistribution
const LogUniformDistributionName = "LogUniformDistribution"
// ToExternalRepr to convert internal representation of a parameter value into external representation.
func (d *LogUniformDistribution) ToExternalRepr(ir float64) interface{} {
return ir
}
// Single to test whether the range of this distribution contains just a single value.
func (d *LogUniformDistribution) Single() bool {
return d.High == d.Low
}
// Contains to check a parameter value is contained in the range of this distribution.
func (d *LogUniformDistribution) Contains(ir float64) bool {
if d.Single() {
return ir == d.Low
}
return d.Low <= ir && ir <= d.High
}
var _ Distribution = &IntUniformDistribution{}
// IntUniformDistribution is a uniform distribution on integers.
type IntUniformDistribution struct {
// High is higher endpoint of the range of the distribution (included in the range).
High int `json:"high"`
// Low is lower endpoint of the range of the distribution (included in the range).
Low int `json:"low"`
}
// IntUniformDistributionName is the identifier name of IntUniformDistribution
const IntUniformDistributionName = "IntUniformDistribution"
// ToExternalRepr to convert internal representation of a parameter value into external representation.
func (d *IntUniformDistribution) ToExternalRepr(ir float64) interface{} {
return int(math.Round(ir))
}
// Single to test whether the range of this distribution contains just a single value.
func (d *IntUniformDistribution) Single() bool {
return d.High == d.Low
}
// Contains to check a parameter value is contained in the range of this distribution.
func (d *IntUniformDistribution) Contains(ir float64) bool {
value := d.ToExternalRepr(ir).(int)
if d.Single() {
return value == d.Low
}
return d.Low <= value && value <= d.High
}
// StepIntUniformDistributionName is the identifier name of IntUniformDistribution
const StepIntUniformDistributionName = "StepIntUniformDistribution"
// StepIntUniformDistribution is a uniform distribution on integers.
type StepIntUniformDistribution struct {
// High is higher endpoint of the range of the distribution (included in the range).
High int `json:"high"`
// Low is lower endpoint of the range of the distribution (included in the range).
Low int `json:"low"`
// Step is a spacing between values.
Step int `json:"step"`
}
// ToExternalRepr to convert internal representation of a parameter value into external representation.
func (d *StepIntUniformDistribution) ToExternalRepr(ir float64) interface{} {
r := (ir - float64(d.Low)) / float64(d.Step) // shift to [0, (high-low)/step]
v := int(math.Round(r))*d.Step + d.Low
return v
}
// Single to test whether the range of this distribution contains just a single value.
func (d *StepIntUniformDistribution) Single() bool {
if d.High == d.Low {
return true
}
return (d.High - d.Low) < d.Step
}
// Contains to check a parameter value is contained in the range of this distribution.
func (d *StepIntUniformDistribution) Contains(ir float64) bool {
value := int(ir)
if d.Single() {
return value == d.Low
}
return d.Low <= value && value < d.High
}
var _ Distribution = &DiscreteUniformDistribution{}
// DiscreteUniformDistribution is a discretized uniform distribution in the linear domain.
type DiscreteUniformDistribution struct {
// High is higher endpoint of the range of the distribution (included in the range).
High float64 `json:"high"`
// Low is lower endpoint of the range of the distribution (included in the range).
Low float64 `json:"low"`
// Q is a discretization step.
Q float64 `json:"q"`
}
// DiscreteUniformDistributionName is the identifier name of DiscreteUniformDistribution
const DiscreteUniformDistributionName = "DiscreteUniformDistribution"
// ToExternalRepr to convert internal representation of a parameter value into external representation.
func (d *DiscreteUniformDistribution) ToExternalRepr(ir float64) interface{} {
return math.Floor((ir-d.Low)/d.Q+0.5)*d.Q + d.Low
}
// Single to test whether the range of this distribution contains just a single value.
func (d *DiscreteUniformDistribution) Single() bool {
return d.High == d.Low
}
// Contains to check a parameter value is contained in the range of this distribution.
func (d *DiscreteUniformDistribution) Contains(ir float64) bool {
if d.Single() {
return ir == d.Low
}
if d.Low > ir || ir > d.High {
return false
}
eps := 1e-6
if math.Mod(ir-d.Low+eps, d.Q) <= 2*eps {
return true
}
if math.Mod(d.High-ir+eps, d.Q) <= 2*eps {
return true
}
return false
}
var _ Distribution = &CategoricalDistribution{}
// CategoricalDistribution is a distribution for categorical parameters
type CategoricalDistribution struct {
// Choices is a candidates of parameter values
Choices []string `json:"choices"`
}
// CategoricalDistributionName is the identifier name of CategoricalDistribution
const CategoricalDistributionName = "CategoricalDistribution"
// ToExternalRepr to convert internal representation of a parameter value into external representation.
func (d *CategoricalDistribution) ToExternalRepr(ir float64) interface{} {
return d.Choices[int(ir)]
}
// Single to test whether the range of this distribution contains just a single value.
func (d *CategoricalDistribution) Single() bool {
return len(d.Choices) == 1
}
// Contains to check a parameter value is contained in the range of this distribution.
func (d *CategoricalDistribution) Contains(ir float64) bool {
index := int(ir)
return 0 <= index && index < len(d.Choices)
}
// ToExternalRepresentation converts to external representation
func ToExternalRepresentation(distribution interface{}, ir float64) (interface{}, error) {
switch d := distribution.(type) {
case UniformDistribution:
return d.ToExternalRepr(ir), nil
case LogUniformDistribution:
return d.ToExternalRepr(ir), nil
case IntUniformDistribution:
return d.ToExternalRepr(ir), nil
case StepIntUniformDistribution:
return d.ToExternalRepr(ir), nil
case DiscreteUniformDistribution:
return d.ToExternalRepr(ir), nil
case CategoricalDistribution:
return d.ToExternalRepr(ir), nil
default:
return nil, ErrUnknownDistribution
}
}
// DistributionIsSingle whether the distribution contains just a single value.
func DistributionIsSingle(distribution interface{}) (bool, error) {
switch d := distribution.(type) {
case UniformDistribution:
return d.Single(), nil
case LogUniformDistribution:
return d.Single(), nil
case IntUniformDistribution:
return d.Single(), nil
case StepIntUniformDistribution:
return d.Single(), nil
case DiscreteUniformDistribution:
return d.Single(), nil
case CategoricalDistribution:
return d.Single(), nil
default:
return false, ErrUnknownDistribution
}
}
// DistributionToJSON serialize a distribution to JSON format.
func DistributionToJSON(distribution interface{}) ([]byte, error) {
var ir struct {
Name string `json:"name"`
Attrs interface{} `json:"attributes"`
}
switch distribution.(type) {
case UniformDistribution:
ir.Name = UniformDistributionName
case LogUniformDistribution:
ir.Name = LogUniformDistributionName
case IntUniformDistribution:
ir.Name = IntUniformDistributionName
case DiscreteUniformDistribution:
ir.Name = DiscreteUniformDistributionName
case StepIntUniformDistribution:
ir.Name = StepIntUniformDistributionName
case CategoricalDistribution:
ir.Name = CategoricalDistributionName
default:
return nil, ErrUnknownDistribution
}
ir.Attrs = distribution
return json.Marshal(&ir)
}
// JSONToDistribution deserialize a distribution in JSON format.
func JSONToDistribution(jsonBytes []byte) (interface{}, error) {
var x struct {
Name string `json:"name"`
Attrs interface{} `json:"attributes"`
}
err := json.Unmarshal(jsonBytes, &x)
if err != nil {
return nil, err
}
switch x.Name {
case UniformDistributionName:
var y UniformDistribution
var dbytes []byte
dbytes, err = json.Marshal(x.Attrs)
if err != nil {
return nil, err
}
err = json.Unmarshal(dbytes, &y)
return y, err
case LogUniformDistributionName:
var y LogUniformDistribution
var dbytes []byte
dbytes, err = json.Marshal(x.Attrs)
if err != nil {
return nil, err
}
err = json.Unmarshal(dbytes, &y)
return y, err
case IntUniformDistributionName:
var y IntUniformDistribution
var dbytes []byte
dbytes, err = json.Marshal(x.Attrs)
if err != nil {
return nil, err
}
err = json.Unmarshal(dbytes, &y)
return y, err
case DiscreteUniformDistributionName:
var y DiscreteUniformDistribution
var dbytes []byte
dbytes, err = json.Marshal(x.Attrs)
if err != nil {
return nil, err
}
err = json.Unmarshal(dbytes, &y)
return y, err
case StepIntUniformDistributionName:
var y StepIntUniformDistribution
var dbytes []byte
dbytes, err = json.Marshal(x.Attrs)
if err != nil {
return nil, err
}
err = json.Unmarshal(dbytes, &y)
return y, err
case CategoricalDistributionName:
var y CategoricalDistribution
var dbytes []byte
dbytes, err = json.Marshal(x.Attrs)
if err != nil {
return nil, err
}
err = json.Unmarshal(dbytes, &y)
return y, err
}
return nil, ErrUnknownDistribution
}