File: epsilon_value_calculators.go

package info (click to toggle)
golang-github-hailocab-go-hostpool 0.0~git20160125.0.e80d13c-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch, stretch-backports
  • size: 88 kB
  • ctags: 64
  • sloc: makefile: 2
file content (40 lines) | stat: -rw-r--r-- 1,539 bytes parent folder | download | duplicates (2)
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
package hostpool

// --- Value Calculators -----------------

import (
	"math"
)

// --- Definitions -----------------------

// Structs implementing this interface are used to convert the average response time for a host
// into a score that can be used to weight hosts in the epsilon greedy hostpool. Lower response
// times should yield higher scores (we want to select the faster hosts more often) The default
// LinearEpsilonValueCalculator just uses the reciprocal of the response time. In practice, any
// decreasing function from the positive reals to the positive reals should work.
type EpsilonValueCalculator interface {
	CalcValueFromAvgResponseTime(float64) float64
}

type LinearEpsilonValueCalculator struct{}
type LogEpsilonValueCalculator struct{ LinearEpsilonValueCalculator }
type PolynomialEpsilonValueCalculator struct {
	LinearEpsilonValueCalculator
	Exp float64 // the exponent to which we will raise the value to reweight
}

// -------- Methods -----------------------

func (c *LinearEpsilonValueCalculator) CalcValueFromAvgResponseTime(v float64) float64 {
	return 1.0 / v
}

func (c *LogEpsilonValueCalculator) CalcValueFromAvgResponseTime(v float64) float64 {
	// we need to add 1 to v so that this will be defined on all positive floats
	return c.LinearEpsilonValueCalculator.CalcValueFromAvgResponseTime(math.Log(v + 1.0))
}

func (c *PolynomialEpsilonValueCalculator) CalcValueFromAvgResponseTime(v float64) float64 {
	return c.LinearEpsilonValueCalculator.CalcValueFromAvgResponseTime(math.Pow(v, c.Exp))
}