epsilon_value_calculators.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package hostpool
  2. // --- Value Calculators -----------------
  3. import (
  4. "math"
  5. )
  6. // --- Definitions -----------------------
  7. // Structs implementing this interface are used to convert the average response time for a host
  8. // into a score that can be used to weight hosts in the epsilon greedy hostpool. Lower response
  9. // times should yield higher scores (we want to select the faster hosts more often) The default
  10. // LinearEpsilonValueCalculator just uses the reciprocal of the response time. In practice, any
  11. // decreasing function from the positive reals to the positive reals should work.
  12. type EpsilonValueCalculator interface {
  13. CalcValueFromAvgResponseTime(float64) float64
  14. }
  15. type LinearEpsilonValueCalculator struct{}
  16. type LogEpsilonValueCalculator struct{ LinearEpsilonValueCalculator }
  17. type PolynomialEpsilonValueCalculator struct {
  18. LinearEpsilonValueCalculator
  19. Exp float64 // the exponent to which we will raise the value to reweight
  20. }
  21. // -------- Methods -----------------------
  22. func (c *LinearEpsilonValueCalculator) CalcValueFromAvgResponseTime(v float64) float64 {
  23. return 1.0 / v
  24. }
  25. func (c *LogEpsilonValueCalculator) CalcValueFromAvgResponseTime(v float64) float64 {
  26. // we need to add 1 to v so that this will be defined on all positive floats
  27. return c.LinearEpsilonValueCalculator.CalcValueFromAvgResponseTime(math.Log(v + 1.0))
  28. }
  29. func (c *PolynomialEpsilonValueCalculator) CalcValueFromAvgResponseTime(v float64) float64 {
  30. return c.LinearEpsilonValueCalculator.CalcValueFromAvgResponseTime(math.Pow(v, c.Exp))
  31. }