Skip to content

Instantly share code, notes, and snippets.

@vmironovich
Created April 10, 2017 13:27
Show Gist options
  • Save vmironovich/6dff0e261ea02105fce1cf118cb5d1e6 to your computer and use it in GitHub Desktop.
Save vmironovich/6dff0e261ea02105fce1cf118cb5d1e6 to your computer and use it in GitHub Desktop.
Power-law apache.commons
import org.apache.commons.math3.distribution.ZipfDistribution
var zipfGenerator : ZipfDistribution = null
/*
assume that B never changes, thus we need to initialize the zipfGenerator only once
we do it the first time we try to get a value for zipfDistribution
x0 -> from (1)
x1 -> to (n/2)
b -> beta (1.5) // in paper it's -1.5 but here it should be positive and produces same values
can be rewritten with specific (init) function which takes x0, x1, beta and creates generator
later getZipf can be completely replaced by zipfGenerator.sample()
*/
def getZipf(x0 : Int, x1 : Int, b : Double) : Int = {
if (zipfGenerator == null) {
initZipf(x0, x1, b)
}
zipfGenerator.sample()
}
def initZipf(x0: Int, x1 : Int, b : Double) : Unit = {
zipfGenerator = new ZipfDistribution(1+x1-x0, b)
}
def fastMutate(source : MatrixGraph, beta : Double) : MatrixGraph = {
/*...*/
val len = source.capacities.size()
val fastNumber = MathUtil.getZipf(1, len, beta)
val numberToMutate = MathUtil.getBinomial(len, 1.0*fastNumber/len)
val positions = MathUtil.getChangePositions(numberToMutate, source.capacities.size)
/*...*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment