-
Notifications
You must be signed in to change notification settings - Fork 22
/
storage.go
594 lines (510 loc) · 15.8 KB
/
storage.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
package goptuna
import (
"errors"
"sync"
"time"
)
// DefaultStudyNamePrefix is a prefix of the default study name.
var DefaultStudyNamePrefix = "no-name-"
// Storage interface abstract a backend database and provide library
// internal interfaces to read/write history of studies and trials.
// This interface is not supposed to be directly accessed by library users.
type Storage interface {
// Basic study manipulation
CreateNewStudy(name string) (int, error)
DeleteStudy(studyID int) error
SetStudyDirection(studyID int, direction StudyDirection) error
SetStudyUserAttr(studyID int, key string, value string) error
SetStudySystemAttr(studyID int, key string, value string) error
// Basic study access
GetStudyIDFromName(name string) (int, error)
GetStudyIDFromTrialID(trialID int) (int, error)
GetStudyNameFromID(studyID int) (string, error)
GetStudyDirection(studyID int) (StudyDirection, error)
GetStudyUserAttrs(studyID int) (map[string]string, error)
GetStudySystemAttrs(studyID int) (map[string]string, error)
GetAllStudySummaries() ([]StudySummary, error)
// Basic trial manipulation
CreateNewTrial(studyID int) (int, error)
CloneTrial(studyID int, baseTrial FrozenTrial) (int, error)
SetTrialValue(trialID int, value float64) error
SetTrialIntermediateValue(trialID int, step int, value float64) error
SetTrialParam(trialID int, paramName string, paramValueInternal float64,
distribution interface{}) error
SetTrialState(trialID int, state TrialState) error
SetTrialUserAttr(trialID int, key string, value string) error
SetTrialSystemAttr(trialID int, key string, value string) error
// Basic trial access
GetTrialNumberFromID(trialID int) (int, error)
GetTrialParam(trialID int, paramName string) (float64, error)
GetTrial(trialID int) (FrozenTrial, error)
GetAllTrials(studyID int) ([]FrozenTrial, error)
GetBestTrial(studyID int) (FrozenTrial, error)
GetTrialParams(trialID int) (map[string]interface{}, error)
GetTrialUserAttrs(trialID int) (map[string]string, error)
GetTrialSystemAttrs(trialID int) (map[string]string, error)
}
var _ Storage = &InMemoryStorage{}
// InMemoryStorageStudyID is a study id for in memory storage backend.
const InMemoryStorageStudyID = 1
// InMemoryStorageStudyUUID is a UUID for in memory storage backend
const InMemoryStorageStudyUUID = "00000000-0000-0000-0000-000000000000"
var (
// ErrInvalidStudyID represents invalid study id.
ErrInvalidStudyID = errors.New("invalid study id")
// ErrInvalidTrialID represents invalid trial id.
ErrInvalidTrialID = errors.New("invalid trial id")
// ErrTrialCannotBeUpdated represents trial cannot be updated.
ErrTrialCannotBeUpdated = errors.New("trial cannot be updated")
// ErrNoCompletedTrials represents no trials are completed yet.
ErrNoCompletedTrials = errors.New("no trials are completed yet")
)
// NewInMemoryStorage returns new memory storage.
func NewInMemoryStorage() *InMemoryStorage {
return &InMemoryStorage{
direction: StudyDirectionMinimize,
trials: make([]FrozenTrial, 0, 128),
userAttrs: make(map[string]string, 8),
systemAttrs: make(map[string]string, 8),
studyName: DefaultStudyNamePrefix + InMemoryStorageStudyUUID,
}
}
// InMemoryStorage stores data in memory of the Go process.
type InMemoryStorage struct {
direction StudyDirection
trials []FrozenTrial
userAttrs map[string]string
systemAttrs map[string]string
studyName string
mu sync.RWMutex
}
// CreateNewStudy creates study and returns studyID.
func (s *InMemoryStorage) CreateNewStudy(name string) (int, error) {
if name != "" {
s.studyName = name
}
return InMemoryStorageStudyID, nil
}
// DeleteStudy deletes a study.
func (s *InMemoryStorage) 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 *InMemoryStorage) 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 *InMemoryStorage) 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 *InMemoryStorage) 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 *InMemoryStorage) 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 *InMemoryStorage) GetStudyIDFromTrialID(trialID int) (int, error) {
s.mu.RLock()
defer s.mu.RUnlock()
for i := range s.trials {
if s.trials[i].ID == trialID {
return InMemoryStorageStudyID, nil
}
}
return -1, errors.New("study not found")
}
// GetStudyNameFromID return the study name from study id.
func (s *InMemoryStorage) 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
}
// GetStudyUserAttrs to restore the attributes for the user.
func (s *InMemoryStorage) 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 *InMemoryStorage) 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 *InMemoryStorage) GetAllStudySummaries() ([]StudySummary, error) {
s.mu.RLock()
defer s.mu.RUnlock()
var datetimeStart time.Time
var foundBestTrial bool
var bestTrial FrozenTrial
for i, t := range s.trials {
if i == 0 {
datetimeStart = t.DatetimeStart
}
if datetimeStart.Unix() > t.DatetimeStart.Unix() {
datetimeStart = t.DatetimeStart
}
if t.State != TrialStateComplete {
continue
}
if !foundBestTrial {
bestTrial = t
foundBestTrial = true
continue
}
if s.direction == StudyDirectionMaximize {
if t.Value > bestTrial.Value {
bestTrial = t
}
} else {
if t.Value < bestTrial.Value {
bestTrial = t
}
}
}
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: bestTrial,
UserAttrs: ua,
SystemAttrs: sa,
DatetimeStart: datetimeStart,
},
}, nil
}
func (s *InMemoryStorage) checkStudyID(studyID int) bool {
return studyID == InMemoryStorageStudyID
}
// CreateNewTrial creates trial and returns trialID.
func (s *InMemoryStorage) CreateNewTrial(studyID int) (int, error) {
if !s.checkStudyID(studyID) {
return -1, ErrInvalidStudyID
}
s.mu.Lock()
defer s.mu.Unlock()
number := len(s.trials)
// trialID equals the number because InMemoryStorage has only 1 study.
trialID := number
s.trials = append(s.trials, 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, nil
}
// CloneTrial creates new Trial from the given base Trial.
func (s *InMemoryStorage) CloneTrial(studyID int, baseTrial FrozenTrial) (int, error) {
if !s.checkStudyID(studyID) {
return -1, ErrInvalidStudyID
}
s.mu.Lock()
defer s.mu.Unlock()
number := len(s.trials)
// trialID equals the number because InMemoryStorage has only 1 study.
trialID := number
s.trials = append(s.trials, 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, nil
}
// SetTrialValue sets the value of trial.
func (s *InMemoryStorage) SetTrialValue(trialID int, value float64) error {
s.mu.Lock()
defer s.mu.Unlock()
if !s.validateTrialID(trialID) {
return ErrInvalidTrialID
}
trial := s.trials[trialID]
if trial.State.IsFinished() {
return ErrTrialCannotBeUpdated
}
trial.Value = value
s.trials[trialID] = trial
return nil
}
// SetTrialIntermediateValue sets the intermediate value of trial.
func (s *InMemoryStorage) SetTrialIntermediateValue(trialID int, step int, value float64) error {
s.mu.Lock()
defer s.mu.Unlock()
if !s.validateTrialID(trialID) {
return ErrInvalidTrialID
}
trial := s.trials[trialID]
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[trialID] = trial
return nil
}
// SetTrialParam sets the sampled parameters of trial.
func (s *InMemoryStorage) SetTrialParam(
trialID int,
paramName string,
paramValueInternal float64,
distribution interface{}) error {
s.mu.Lock()
defer s.mu.Unlock()
// Check param has not been set; otherwise, return error
if !s.validateTrialID(trialID) {
return ErrInvalidTrialID
}
trial := s.trials[trialID]
// 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[trialID] = trial
return nil
}
// SetTrialState sets the state of trial.
func (s *InMemoryStorage) SetTrialState(trialID int, state TrialState) error {
s.mu.Lock()
defer s.mu.Unlock()
if !s.validateTrialID(trialID) {
return ErrInvalidTrialID
}
trial := s.trials[trialID]
if trial.State.IsFinished() {
return ErrTrialCannotBeUpdated
}
trial.State = state
if trial.State.IsFinished() {
trial.DatetimeComplete = time.Now()
}
s.trials[trialID] = trial
return nil
}
// SetTrialUserAttr to store the value for the user.
func (s *InMemoryStorage) SetTrialUserAttr(trialID int, key string, value string) error {
s.mu.Lock()
defer s.mu.Unlock()
if !s.validateTrialID(trialID) {
return ErrInvalidTrialID
}
if s.trials[trialID].State.IsFinished() {
return ErrTrialCannotBeUpdated
}
s.trials[trialID].UserAttrs[key] = value
return nil
}
// SetTrialSystemAttr to store the value for the system.
func (s *InMemoryStorage) SetTrialSystemAttr(trialID int, key string, value string) error {
s.mu.Lock()
defer s.mu.Unlock()
if !s.validateTrialID(trialID) {
return ErrInvalidTrialID
}
if s.trials[trialID].State.IsFinished() {
return ErrTrialCannotBeUpdated
}
s.trials[trialID].SystemAttrs[key] = value
return nil
}
// GetTrialNumberFromID returns the trial's number.
func (s *InMemoryStorage) GetTrialNumberFromID(trialID int) (int, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if !s.validateTrialID(trialID) {
return -1, ErrInvalidTrialID
}
return trialID, nil
}
// GetTrialParam returns the internal parameter of the trial
func (s *InMemoryStorage) GetTrialParam(trialID int, paramName string) (float64, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if !s.validateTrialID(trialID) {
return -1, ErrInvalidTrialID
}
ir, ok := s.trials[trialID].InternalParams[paramName]
if !ok {
return -1.0, errors.New("param doesn't exist")
}
return ir, nil
}
// GetTrialParams returns the external parameters in the trial
func (s *InMemoryStorage) GetTrialParams(trialID int) (map[string]interface{}, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if !s.validateTrialID(trialID) {
return nil, ErrInvalidTrialID
}
return s.trials[trialID].Params, nil
}
// GetTrialUserAttrs to restore the attributes for the user.
func (s *InMemoryStorage) GetTrialUserAttrs(trialID int) (map[string]string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if !s.validateTrialID(trialID) {
return nil, ErrInvalidTrialID
}
n := make(map[string]string, len(s.trials[trialID].UserAttrs))
for k := range s.trials[trialID].UserAttrs {
n[k] = s.trials[trialID].UserAttrs[k]
}
return n, nil
}
// GetTrialSystemAttrs to restore the attributes for the system.
func (s *InMemoryStorage) GetTrialSystemAttrs(trialID int) (map[string]string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if !s.validateTrialID(trialID) {
return nil, ErrInvalidTrialID
}
n := make(map[string]string, len(s.trials[trialID].SystemAttrs))
for k := range s.trials[trialID].SystemAttrs {
n[k] = s.trials[trialID].SystemAttrs[k]
}
return n, nil
}
// GetBestTrial returns the best trial.
func (s *InMemoryStorage) GetBestTrial(studyID int) (FrozenTrial, error) {
if !s.checkStudyID(studyID) {
return FrozenTrial{}, ErrInvalidStudyID
}
s.mu.RLock()
defer s.mu.RUnlock()
var bestTrial FrozenTrial
var bestTrialIsSet bool
for i := range s.trials {
if s.trials[i].State != TrialStateComplete {
continue
}
if s.direction == StudyDirectionMaximize {
if !bestTrialIsSet {
bestTrial = s.trials[i]
bestTrialIsSet = true
} else if s.trials[i].Value > bestTrial.Value {
bestTrial = s.trials[i]
}
} else if s.direction == StudyDirectionMinimize {
if !bestTrialIsSet {
bestTrial = s.trials[i]
bestTrialIsSet = true
} else if s.trials[i].Value < bestTrial.Value {
bestTrial = s.trials[i]
}
}
}
if !bestTrialIsSet {
return FrozenTrial{}, ErrNoCompletedTrials
}
return bestTrial, nil
}
// GetAllTrials returns the all trials.
func (s *InMemoryStorage) GetAllTrials(studyID int) ([]FrozenTrial, error) {
s.mu.RLock()
defer s.mu.RUnlock()
trials := make([]FrozenTrial, 0, len(s.trials))
for i := range s.trials {
trials = append(trials, s.trials[i])
}
return trials, nil
}
// GetStudyDirection returns study direction of the objective.
func (s *InMemoryStorage) GetStudyDirection(studyID int) (StudyDirection, error) {
if !s.checkStudyID(studyID) {
return StudyDirectionMinimize, ErrInvalidStudyID
}
s.mu.RLock()
defer s.mu.RUnlock()
return s.direction, nil
}
// GetTrial returns Trial.
func (s *InMemoryStorage) GetTrial(trialID int) (FrozenTrial, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if !s.validateTrialID(trialID) {
return FrozenTrial{}, ErrInvalidTrialID
}
return s.trials[trialID], nil
}
func (s *InMemoryStorage) validateTrialID(trialID int) bool {
return trialID >= 0 && trialID < len(s.trials)
}