-
Notifications
You must be signed in to change notification settings - Fork 22
/
trial_frozen.go
116 lines (105 loc) · 3.5 KB
/
trial_frozen.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
package goptuna
import (
"errors"
"fmt"
"time"
)
// FrozenTrial holds the status and results of a Trial.
type FrozenTrial struct {
ID int `json:"trial_id"`
StudyID int `json:"study_id"`
Number int `json:"number"`
State TrialState `json:"state"`
Value float64 `json:"value"`
IntermediateValues map[int]float64 `json:"intermediate_values"`
DatetimeStart time.Time `json:"datetime_start"`
DatetimeComplete time.Time `json:"datetime_complete"`
InternalParams map[string]float64 `json:"internal_params"`
Params map[string]interface{} `json:"params"`
Distributions map[string]interface{} `json:"distributions"`
UserAttrs map[string]string `json:"user_attrs"`
SystemAttrs map[string]string `json:"system_attrs"`
}
// GetLatestStep returns the latest step in intermediate values.
func (t FrozenTrial) GetLatestStep() (step int, exist bool) {
if len(t.IntermediateValues) == 0 {
return -1, false
}
var maxStep int
for k := range t.IntermediateValues {
if k > maxStep {
maxStep = k
}
}
return maxStep, true
}
// Validate returns error if invalid.
func (t FrozenTrial) validate() error {
if t.DatetimeStart.IsZero() {
return errors.New("`DatetimeStart` is supposed to be set")
}
if t.State.IsFinished() {
if t.DatetimeComplete.IsZero() {
return errors.New("`DatetimeComplete` is supposed to be set for a finished trial")
}
} else {
if !t.DatetimeComplete.IsZero() {
return errors.New("`DatetimeComplete` is supposed to not be set for a finished trial")
}
}
if len(t.InternalParams) != len(t.Distributions) {
return errors.New("`Params` and `Distributions` should be the same length")
}
for name := range t.InternalParams {
if _, ok := t.Distributions[name]; !ok {
return fmt.Errorf("distribution '%s' is not found", name)
}
}
if len(t.Params) != len(t.InternalParams) {
return errors.New("`Params` and `InternalParams` should be the same length")
}
for name := range t.InternalParams {
ir := t.InternalParams[name]
d := t.Distributions[name]
switch typedDistribution := d.(type) {
case UniformDistribution:
if !typedDistribution.Contains(ir) {
return fmt.Errorf("internal param is out of the distribution range")
}
case LogUniformDistribution:
if !typedDistribution.Contains(ir) {
return fmt.Errorf("internal param is out of the distribution range")
}
case IntUniformDistribution:
if !typedDistribution.Contains(ir) {
return fmt.Errorf("internal param is out of the distribution range")
}
case StepIntUniformDistribution:
if !typedDistribution.Contains(ir) {
return fmt.Errorf("internal param is out of the distribution range")
}
case DiscreteUniformDistribution:
if !typedDistribution.Contains(ir) {
return fmt.Errorf("internal param is out of the distribution range")
}
case CategoricalDistribution:
if !typedDistribution.Contains(ir) {
return fmt.Errorf("internal param is out of the distribution range")
}
default:
return errors.New("unsupported distribution")
}
expectedXr, err := ToExternalRepresentation(d, ir)
if err != nil {
return err
}
actualXr, ok := t.Params[name]
if !ok {
return fmt.Errorf("params '%s' is not found", name)
}
if expectedXr != actualXr {
return fmt.Errorf("internal params and external param does not match: %s", name)
}
}
return nil
}