/
solver.go
229 lines (190 loc) · 5.41 KB
/
solver.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
package solver
import (
"errors"
"math"
"github.com/dimchansky/ebsl-go/opinion"
"github.com/dimchansky/ebsl-go/trust/equations"
)
var (
ErrEpochMustBePositiveNumber = errors.New("solver: epoch must be positive number")
)
type DistanceFun func(prevValue *opinion.Type, newValue *opinion.Type) float64
type DistanceAggregator interface {
Reset()
Add(float64)
Result() float64
}
type EpochStartFun func(epoch uint) error
type EpochEndFun func(epoch uint, aggregatedDistance float64) error
type options struct {
epochs uint
distanceFun DistanceFun
distanceAggregator DistanceAggregator
tolerance float64
onEpochStart EpochStartFun
onEpochEnd EpochEndFun
}
type Options func(opts *options) (*options, error)
func UseMaxEpochs(epochs uint) Options {
return func(opts *options) (*options, error) {
if epochs < 1 {
return nil, ErrEpochMustBePositiveNumber
}
opts.epochs = epochs
return opts, nil
}
}
func UseManhattanDistance() Options { return UseDistanceFunction(manhattanDistance) }
func UseChebyshevDistance() Options { return UseDistanceFunction(chebyshevDistance) }
func UseEuclideanDistance() Options { return UseDistanceFunction(euclideanDistance) }
func UseMaxDistanceAggregator() Options { return UseDistanceAggregator(&maxDistanceAggregator{}) }
func UseSumDistanceAggregator() Options { return UseDistanceAggregator(&sumDistanceAggregator{}) }
func UseDistanceFunction(distanceFun DistanceFun) Options {
return func(opts *options) (*options, error) {
opts.distanceFun = distanceFun
return opts, nil
}
}
func UseDistanceAggregator(distanceAggregator DistanceAggregator) Options {
return func(opts *options) (*options, error) {
opts.distanceAggregator = distanceAggregator
return opts, nil
}
}
func UseTolerance(tolerance float64) Options {
return func(opts *options) (*options, error) {
opts.tolerance = tolerance
return opts, nil
}
}
func UseOnEpochStartCallback(onEpochStart EpochStartFun) Options {
return func(opts *options) (*options, error) {
opts.onEpochStart = onEpochStart
return opts, nil
}
}
func UseOnEpochEndCallback(onEpochEnd EpochEndFun) Options {
return func(opts *options) (*options, error) {
opts.onEpochEnd = onEpochEnd
return opts, nil
}
}
func SolveFinalReferralTrustEquations(
context equations.FinalReferralTrustEquationContext,
eqs equations.IterableFinalReferralTrustEquations,
opts ...Options,
) (err error) {
solverOpts := &options{
epochs: 100,
distanceFun: manhattanDistance,
distanceAggregator: &maxDistanceAggregator{},
tolerance: 0.0,
onEpochStart: func(epoch uint) error {
return nil
},
onEpochEnd: func(epoch uint, aggregatedDistance float64) error {
return nil
},
}
// apply solver options
for _, applyOption := range opts {
solverOpts, err = applyOption(solverOpts)
if err != nil {
return
}
}
epochs := solverOpts.epochs
distanceFun := solverOpts.distanceFun
distanceAggregator := solverOpts.distanceAggregator
tolerance := solverOpts.tolerance
onEpochStart := solverOpts.onEpochStart
onEpochEnd := solverOpts.onEpochEnd
for epoch := uint(1); epoch <= epochs; epoch++ {
if err := onEpochStart(epoch); err != nil {
return err
}
distanceAggregator.Reset()
foreachEquation := eqs.GetFinalReferralTrustEquationIterator()
err = foreachEquation(func(eq *equations.FinalReferralTrustEquation) error {
prevValue := context.GetFinalReferralTrust(eq.R)
newValue, err := eq.EvaluateFinalReferralTrust(context)
if err != nil {
return err
}
dist := distanceFun(&prevValue, newValue)
// fmt.Printf("R[%v,%v]: prev: %v new: %v dist: %v\n", eq.R.From, eq.R.To, prevValue, newValue, dist) // TODO: add callback
distanceAggregator.Add(dist)
return nil
})
if err != nil {
return
}
distError := distanceAggregator.Result()
if err := onEpochEnd(epoch, distError); err != nil {
return err
}
if distError <= tolerance {
return nil
}
}
return nil
}
func manhattanDistance(prevValue *opinion.Type, newValue *opinion.Type) float64 {
return math.Abs(prevValue.B-newValue.B) +
math.Abs(prevValue.D-newValue.D) +
math.Abs(prevValue.U-newValue.U)
}
func chebyshevDistance(prevValue *opinion.Type, newValue *opinion.Type) float64 {
return math.Max(
math.Max(
math.Abs(prevValue.B-newValue.B),
math.Abs(prevValue.D-newValue.D),
),
math.Abs(prevValue.U-newValue.U),
)
}
func euclideanDistance(prevValue *opinion.Type, newValue *opinion.Type) float64 {
return math.Sqrt(square(prevValue.B-newValue.B) +
square(prevValue.D-newValue.D) +
square(prevValue.U-newValue.U),
)
}
func square(x float64) float64 { return x * x }
type maxDistanceAggregator struct {
nonEmpty bool
maxDistance float64
}
func (a *maxDistanceAggregator) Reset() {
a.nonEmpty = false
a.maxDistance = math.MaxFloat64
}
func (a *maxDistanceAggregator) Add(v float64) {
if a.nonEmpty {
a.maxDistance = math.Max(a.maxDistance, v)
} else {
a.nonEmpty = true
a.maxDistance = v
}
}
func (a *maxDistanceAggregator) Result() float64 {
return a.maxDistance
}
type sumDistanceAggregator struct {
nonEmpty bool
sumDistance float64
}
func (a *sumDistanceAggregator) Reset() {
a.nonEmpty = false
a.sumDistance = math.MaxFloat64
}
func (a *sumDistanceAggregator) Add(v float64) {
if a.nonEmpty {
a.sumDistance += v
} else {
a.nonEmpty = true
a.sumDistance = v
}
}
func (a *sumDistanceAggregator) Result() float64 {
return a.sumDistance
}