math.go 687 Bytes
Newer Older
1
package strategy
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
2 3 4

import (
	"math"
Jeromy's avatar
Jeromy committed
5
	"math/rand"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6 7
)

8
type strategyFunc func(*ledger) bool
Jeromy's avatar
Jeromy committed
9

10 11 12
// TODO avoid using rand.Float64 method. it uses a singleton lock and may cause
// performance issues. Instead, instantiate a rand struct and use that to call
// Float64()
13
func standardStrategy(l *ledger) bool {
14
	return rand.Float64() <= probabilitySend(l.Accounting.Value())
Jeromy's avatar
Jeromy committed
15 16
}

17
func yesManStrategy(l *ledger) bool {
Jeromy's avatar
Jeromy committed
18 19 20
	return true
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34
func probabilitySend(ratio float64) float64 {
	x := 1 + math.Exp(6-3*ratio)
	y := 1 / x
	return 1 - y
}

type debtRatio struct {
	BytesSent uint64
	BytesRecv uint64
}

func (dr *debtRatio) Value() float64 {
	return float64(dr.BytesSent) / float64(dr.BytesRecv+1)
}