sigmoid strat placeholder

This commit is contained in:
Juan Batiz-Benet 2014-08-20 18:29:00 -07:00
parent 668a90f6b6
commit e658ade600
2 changed files with 37 additions and 0 deletions

20
bitswap/strategy.go Normal file
View File

@ -0,0 +1,20 @@
package bitswap
import (
"math"
)
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)
}

17
bitswap/strategy_test.go Normal file
View File

@ -0,0 +1,17 @@
package bitswap
import (
"testing"
)
func TestProbabilitySendDecreasesAsRatioIncreases(t *testing.T) {
grateful := debtRatio{BytesSent: 0, BytesRecv: 10000}
pWhenGrateful := probabilitySend(grateful.Value())
abused := debtRatio{BytesSent: 10000, BytesRecv: 0}
pWhenAbused := probabilitySend(abused.Value())
if pWhenGrateful < pWhenAbused {
t.Fail()
}
}