-
Notifications
You must be signed in to change notification settings - Fork 22
/
storage_blackhole.go
571 lines (492 loc) · 14.4 KB
/
storage_blackhole.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package goptuna
import (
"errors"
"sync"
"time"
)
var (
// ErrTrialAlreadyDeleted means that trial is already deleted.
ErrTrialAlreadyDeleted = errors.New("trial is already deleted")
// ErrTrialsPartiallyDeleted means that trials are partially deleted.
ErrTrialsPartiallyDeleted = errors.New("some trials are already deleted")
// ErrDeleteNonFinishedTrial means that non finished trial is deleted.
ErrDeleteNonFinishedTrial = errors.New("non finished trial is deleted")
)
var _ Storage = &BlackHoleStorage{}
// NewBlackHoleStorage returns BlackHoleStorage.
func NewBlackHoleStorage(n int) *BlackHoleStorage {
return &BlackHoleStorage{
direction: StudyDirectionMinimize,
counter: 0,
nTrials: n,
trials: make([]FrozenTrial, n),
bestTrial: FrozenTrial{},
userAttrs: make(map[string]string, 8),
systemAttrs: make(map[string]string, 8),
studyName: DefaultStudyNamePrefix + InMemoryStorageStudyUUID,
}
}
// BlackHoleStorage is an in-memory storage, but designed for over 100k trials.
// Please note that this storage just holds the 'nTrials' trials.
//
// Methods to create a trial might return ErrDeleteNonFinishedTrial.
// GetAllTrials method might return ErrTrialsPartiallyDeleted.
// Methods to get or update a trial might return ErrTrialAlreadyDeleted.
//
// Currently, RandomSampler and CMA-ES sampler supports this storage.
type BlackHoleStorage struct {
direction StudyDirection
counter int
nTrials int
trials []FrozenTrial
bestTrial FrozenTrial
userAttrs map[string]string
systemAttrs map[string]string
studyName string
mu sync.RWMutex
}
// CreateNewStudy creates study and returns studyID.
func (s *BlackHoleStorage) CreateNewStudy(name string) (int, error) {
if name != "" {
s.studyName = name
}
return InMemoryStorageStudyID, nil
}
// DeleteStudy deletes a study.
func (s *BlackHoleStorage) DeleteStudy(studyID int) error {
if !s.checkStudyID(studyID) {
return ErrInvalidStudyID
}
s.mu.Lock()
defer s.mu.Unlock()
s.direction = StudyDirectionMinimize
s.trials = make([]FrozenTrial, 0, 128)
s.userAttrs = make(map[string]string, 8)
s.systemAttrs = make(map[string]string, 8)
s.studyName = DefaultStudyNamePrefix + InMemoryStorageStudyUUID
return nil
}
// SetStudyDirection sets study direction of the objective.
func (s *BlackHoleStorage) SetStudyDirection(studyID int, direction StudyDirection) error {
if !s.checkStudyID(studyID) {
return ErrInvalidStudyID
}
s.mu.Lock()
defer s.mu.Unlock()
s.direction = direction
return nil
}
// SetStudyUserAttr to store the value for the user.
func (s *BlackHoleStorage) SetStudyUserAttr(studyID int, key string, value string) error {
s.mu.Lock()
defer s.mu.Unlock()
s.userAttrs[key] = value
return nil
}
// SetStudySystemAttr to store the value for the system.
func (s *BlackHoleStorage) SetStudySystemAttr(studyID int, key string, value string) error {
s.mu.Lock()
defer s.mu.Unlock()
s.systemAttrs[key] = value
return nil
}
// GetStudyIDFromName return the study id from study name.
func (s *BlackHoleStorage) GetStudyIDFromName(name string) (int, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if name != s.studyName {
return -1, errors.New("study not found")
}
return InMemoryStorageStudyID, nil
}
// GetStudyIDFromTrialID return the study id from trial id.
func (s *BlackHoleStorage) GetStudyIDFromTrialID(trialID int) (int, error) {
s.mu.RLock()
defer s.mu.RUnlock()
// counter represents an id for next trial.
if s.counter > trialID {
return InMemoryStorageStudyID, nil
}
return -1, errors.New("study not found")
}
// GetStudyNameFromID return the study name from study id.
func (s *BlackHoleStorage) GetStudyNameFromID(studyID int) (string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if !s.checkStudyID(studyID) {
return "", errors.New("study not found")
}
return s.studyName, nil
}
// GetStudyDirection returns study direction of the objective.
func (s *BlackHoleStorage) GetStudyDirection(studyID int) (StudyDirection, error) {
if !s.checkStudyID(studyID) {
return StudyDirectionMinimize, ErrInvalidStudyID
}
s.mu.RLock()
defer s.mu.RUnlock()
return s.direction, nil
}
// GetStudyUserAttrs to restore the attributes for the user.
func (s *BlackHoleStorage) GetStudyUserAttrs(studyID int) (map[string]string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
n := make(map[string]string, len(s.userAttrs))
for k := range s.userAttrs {
n[k] = s.userAttrs[k]
}
return n, nil
}
// GetStudySystemAttrs to restore the attributes for the system.
func (s *BlackHoleStorage) GetStudySystemAttrs(studyID int) (map[string]string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
n := make(map[string]string, len(s.systemAttrs))
for k := range s.systemAttrs {
n[k] = s.systemAttrs[k]
}
return n, nil
}
// GetAllStudySummaries returns all study summaries.
func (s *BlackHoleStorage) GetAllStudySummaries() ([]StudySummary, error) {
s.mu.RLock()
defer s.mu.RUnlock()
var datetimeStart time.Time
sa := make(map[string]string, len(s.systemAttrs))
for k := range s.systemAttrs {
sa[k] = s.systemAttrs[k]
}
ua := make(map[string]string, len(s.userAttrs))
for k := range s.userAttrs {
ua[k] = s.userAttrs[k]
}
return []StudySummary{
{
ID: InMemoryStorageStudyID,
Name: s.studyName,
Direction: s.direction,
BestTrial: s.bestTrial,
UserAttrs: ua,
SystemAttrs: sa,
DatetimeStart: datetimeStart,
},
}, nil
}
// CreateNewTrial creates trial and returns trialID.
func (s *BlackHoleStorage) CreateNewTrial(studyID int) (int, error) {
s.mu.Lock()
defer s.mu.Unlock()
if !s.checkStudyID(studyID) {
return -1, ErrInvalidStudyID
}
number := s.counter
trialID := number
s.counter++
var err error
idx := s.getTrialIndex(trialID)
if s.isPartiallyDeleted() && !s.trials[idx].State.IsFinished() {
err = ErrDeleteNonFinishedTrial
}
s.trials[idx] = FrozenTrial{
ID: trialID,
Number: number,
State: TrialStateRunning,
Value: 0,
IntermediateValues: make(map[int]float64, 8),
DatetimeStart: time.Now(),
DatetimeComplete: time.Time{},
InternalParams: make(map[string]float64, 8),
Params: make(map[string]interface{}, 8),
Distributions: make(map[string]interface{}, 8),
UserAttrs: make(map[string]string, 8),
SystemAttrs: make(map[string]string, 8),
}
return trialID, err
}
// CloneTrial creates new Trial from the given base Trial.
func (s *BlackHoleStorage) CloneTrial(studyID int, baseTrial FrozenTrial) (int, error) {
s.mu.Lock()
defer s.mu.Unlock()
if !s.checkStudyID(studyID) {
return -1, ErrInvalidStudyID
}
number := s.counter
trialID := number
s.counter++
var err error
idx := s.getTrialIndex(trialID)
if !s.trials[idx].State.IsFinished() {
err = ErrDeleteNonFinishedTrial
}
s.trials[idx] = FrozenTrial{
ID: trialID,
StudyID: studyID,
Number: number,
State: baseTrial.State,
Value: baseTrial.Value,
IntermediateValues: baseTrial.IntermediateValues,
DatetimeStart: baseTrial.DatetimeStart,
DatetimeComplete: baseTrial.DatetimeComplete,
InternalParams: baseTrial.InternalParams,
Params: baseTrial.Params,
Distributions: baseTrial.Distributions,
UserAttrs: baseTrial.UserAttrs,
SystemAttrs: baseTrial.SystemAttrs,
}
return trialID, err
}
// SetTrialValue sets the value of trial.
func (s *BlackHoleStorage) SetTrialValue(trialID int, value float64) error {
s.mu.Lock()
defer s.mu.Unlock()
if err := s.checkTrialID(trialID); err != nil {
return err
}
idx := s.getTrialIndex(trialID)
trial := s.trials[idx]
if trial.State.IsFinished() {
return ErrTrialCannotBeUpdated
}
trial.Value = value
s.trials[idx] = trial
return nil
}
// SetTrialIntermediateValue sets the intermediate value of trial.
func (s *BlackHoleStorage) SetTrialIntermediateValue(trialID int, step int, value float64) error {
s.mu.Lock()
defer s.mu.Unlock()
if err := s.checkTrialID(trialID); err != nil {
return err
}
idx := s.getTrialIndex(trialID)
trial := s.trials[idx]
if trial.State.IsFinished() {
return ErrTrialCannotBeUpdated
}
for key := range trial.IntermediateValues {
if key == step {
return errors.New("step value is already exist")
}
}
trial.IntermediateValues[step] = value
s.trials[idx] = trial
return nil
}
// SetTrialParam sets the sampled parameters of trial.
func (s *BlackHoleStorage) SetTrialParam(
trialID int,
paramName string,
paramValueInternal float64,
distribution interface{},
) error {
s.mu.Lock()
defer s.mu.Unlock()
if err := s.checkTrialID(trialID); err != nil {
return err
}
idx := s.getTrialIndex(trialID)
trial := s.trials[idx]
// Check trial is able to update
if trial.State.IsFinished() {
return ErrTrialCannotBeUpdated
}
paramValueExternal, err := ToExternalRepresentation(distribution, paramValueInternal)
if err != nil {
return err
}
trial.Distributions[paramName] = distribution
trial.InternalParams[paramName] = paramValueInternal
trial.Params[paramName] = paramValueExternal
s.trials[idx] = trial
return nil
}
// SetTrialState sets the state of trial.
func (s *BlackHoleStorage) SetTrialState(trialID int, state TrialState) error {
s.mu.Lock()
defer s.mu.Unlock()
if err := s.checkTrialID(trialID); err != nil {
return err
}
idx := s.getTrialIndex(trialID)
trial := s.trials[idx]
if trial.State.IsFinished() {
return ErrTrialCannotBeUpdated
}
trial.State = state
if trial.State.IsFinished() {
trial.DatetimeComplete = time.Now()
s.updateBestTrial(trial)
}
s.trials[idx] = trial
return nil
}
// SetTrialUserAttr to store the value for the user.
func (s *BlackHoleStorage) SetTrialUserAttr(trialID int, key string, value string) error {
s.mu.Lock()
defer s.mu.Unlock()
if err := s.checkTrialID(trialID); err != nil {
return err
}
idx := s.getTrialIndex(trialID)
if s.trials[idx].State.IsFinished() {
return ErrTrialCannotBeUpdated
}
s.trials[idx].UserAttrs[key] = value
return nil
}
// SetTrialSystemAttr to store the value for the system.
func (s *BlackHoleStorage) SetTrialSystemAttr(trialID int, key string, value string) error {
s.mu.Lock()
defer s.mu.Unlock()
if err := s.checkTrialID(trialID); err != nil {
return err
}
idx := s.getTrialIndex(trialID)
if s.trials[idx].State == TrialStateComplete {
return ErrTrialCannotBeUpdated
}
s.trials[idx].SystemAttrs[key] = value
return nil
}
// GetTrialNumberFromID returns the trial's number.
func (s *BlackHoleStorage) GetTrialNumberFromID(trialID int) (int, error) {
s.mu.RLock()
defer s.mu.RUnlock()
err := s.checkTrialID(trialID)
if err == ErrTrialAlreadyDeleted {
return trialID, err
}
return -1, err
}
// GetTrialParam returns the internal parameter of the trial
func (s *BlackHoleStorage) GetTrialParam(trialID int, paramName string) (float64, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if err := s.checkTrialID(trialID); err != nil {
return -1, err
}
idx := s.getTrialIndex(trialID)
ir, ok := s.trials[idx].InternalParams[paramName]
if !ok {
return -1.0, errors.New("param doesn't exist")
}
return ir, nil
}
// GetTrial returns Trial.
func (s *BlackHoleStorage) GetTrial(trialID int) (FrozenTrial, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if err := s.checkTrialID(trialID); err != nil {
return FrozenTrial{}, err
}
idx := s.getTrialIndex(trialID)
return s.trials[idx], nil
}
// GetAllTrials returns the all trials.
func (s *BlackHoleStorage) GetAllTrials(studyID int) ([]FrozenTrial, error) {
s.mu.RLock()
defer s.mu.RUnlock()
var err error
n := s.nTrials
if s.isPartiallyDeleted() {
err = ErrTrialsPartiallyDeleted
} else {
n = s.counter
}
trials := make([]FrozenTrial, 0, n)
for i := 0; i < len(s.trials); i++ {
idx := s.getTrialIndex(s.counter + i)
trials = append(trials, s.trials[idx])
}
return trials, err
}
// GetBestTrial returns the best trial.
func (s *BlackHoleStorage) GetBestTrial(studyID int) (FrozenTrial, error) {
var err error
if s.bestTrial.State != TrialStateComplete {
err = ErrNoCompletedTrials
}
return s.bestTrial, err
}
// GetTrialParams returns the external parameters in the trial
func (s *BlackHoleStorage) GetTrialParams(trialID int) (map[string]interface{}, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if err := s.checkTrialID(trialID); err != nil {
return nil, err
}
idx := s.getTrialIndex(trialID)
return s.trials[idx].Params, nil
}
// GetTrialUserAttrs to restore the attributes for the user.
func (s *BlackHoleStorage) GetTrialUserAttrs(trialID int) (map[string]string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if err := s.checkTrialID(trialID); err != nil {
return nil, err
}
idx := s.getTrialIndex(trialID)
n := make(map[string]string, len(s.trials[idx].UserAttrs))
for k := range s.trials[idx].UserAttrs {
n[k] = s.trials[idx].UserAttrs[k]
}
return n, nil
}
// GetTrialSystemAttrs to restore the attributes for the system.
func (s *BlackHoleStorage) GetTrialSystemAttrs(trialID int) (map[string]string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if err := s.checkTrialID(trialID); err != nil {
return nil, err
}
idx := s.getTrialIndex(trialID)
n := make(map[string]string, len(s.trials[idx].SystemAttrs))
for k := range s.trials[idx].SystemAttrs {
n[k] = s.trials[idx].SystemAttrs[k]
}
return n, nil
}
func (s *BlackHoleStorage) checkStudyID(studyID int) bool {
return studyID == InMemoryStorageStudyID
}
func (s *BlackHoleStorage) getTrialIndex(trialID int) int {
return trialID % s.nTrials
}
func (s *BlackHoleStorage) checkTrialID(trialID int) error {
// | nTrials | counter | trials |
// | 3 | 0 | [] |
// | 3 | 3 | [0,1,2] |
// | 3 | 4 | [1,2,3] |
if trialID < 0 || trialID >= s.counter {
// counter represents an id for next trial.
return ErrInvalidTrialID
}
if s.counter-s.nTrials < trialID {
return nil
}
return ErrTrialAlreadyDeleted
}
func (s *BlackHoleStorage) isPartiallyDeleted() bool {
// | nTrials | counter | trials |
// | 3 | 0 | [] |
// | 3 | 3 | [0,1,2] |
// | 3 | 4 | [1,2,3] |
return s.counter > s.nTrials
}
func (s *BlackHoleStorage) updateBestTrial(trial FrozenTrial) {
if trial.State != TrialStateComplete {
return
}
if s.bestTrial.State != TrialStateComplete {
s.bestTrial = trial
return
}
if s.direction == StudyDirectionMaximize && trial.Value > s.bestTrial.Value {
s.bestTrial = trial
return
}
if s.direction == StudyDirectionMinimize && trial.Value < s.bestTrial.Value {
s.bestTrial = trial
return
}
}