This repository has been archived by the owner on Nov 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
termination.go
119 lines (110 loc) · 2.78 KB
/
termination.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
// Copyright ©2014 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package optimize
import "errors"
// Status represents the status of the optimization. Programs
// should not rely on the underlying numeric value of the Status being constant.
type Status int
const (
NotTerminated Status = iota
Success
FunctionThreshold
FunctionConvergence
GradientThreshold
StepConvergence
FunctionNegativeInfinity
Failure
IterationLimit
RuntimeLimit
FunctionEvaluationLimit
GradientEvaluationLimit
HessianEvaluationLimit
)
func (s Status) String() string {
return statuses[s].name
}
// Early returns true if the status indicates the optimization ended before a
// minimum was found. As an example, if the maximum iterations was reached, a
// minimum was not found, but if the gradient norm was reached then a minimum
// was found.
func (s Status) Early() bool {
return statuses[s].early
}
// Err returns the error associated with an early ending to the minimization. If
// Early returns false, Err will return nil.
func (s Status) Err() error {
return statuses[s].err
}
var statuses = []struct {
name string
early bool
err error
}{
{
name: "NotTerminated",
},
{
name: "Success",
},
{
name: "FunctionThreshold",
},
{
name: "FunctionConvergence",
},
{
name: "GradientThreshold",
},
{
name: "StepConvergence",
},
{
name: "FunctionNegativeInfinity",
},
{
name: "Failure",
early: true,
err: errors.New("optimize: termination ended in failure"),
},
{
name: "IterationLimit",
early: true,
err: errors.New("optimize: maximum number of major iterations reached"),
},
{
name: "RuntimeLimit",
early: true,
err: errors.New("optimize: maximum runtime reached"),
},
{
name: "FunctionEvaluationLimit",
early: true,
err: errors.New("optimize: maximum number of function evaluations reached"),
},
{
name: "GradientEvaluationLimit",
early: true,
err: errors.New("optimize: maximum number of gradient evaluations reached"),
},
{
name: "HessianEvaluationLimit",
early: true,
err: errors.New("optimize: maximum number of Hessian evaluations reached"),
},
}
// NewStatus returns a unique Status variable to represent a custom status.
// NewStatus is intended to be called only during package initialization, and
// calls to NewStatus are not thread safe.
//
// NewStatus takes in three arguments, the string that should be output from
// Status.String, a boolean if the status indicates early optimization conclusion,
// and the error to return from Err (if any).
func NewStatus(name string, early bool, err error) Status {
statuses = append(statuses, struct {
name string
early bool
err error
}{name, early, err})
return Status(len(statuses) - 1)
}