Skip to content

Commit

Permalink
Combinations #23 Subsets #25
Browse files Browse the repository at this point in the history
  • Loading branch information
vsuthichai committed Aug 18, 2016
1 parent d770cbc commit 21b8ed1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,20 @@ abstract class AbstractCombinations[T](

protected val values = iterable.toSeq

assert(k > 0, "k must be greater than 0")
assert(k <= values.length, s"k must be less than or equal to length of the iterable, ${values.length}")

def sample(rng: Random): Iterable[T] = {
if (replacement) sampleWithReplacement(rng)
else sampleNoReplacement(rng)
}

def sampleWithReplacement(rng: Random) = {
val combo = mutable.SortedSet[T]()
val combo = new mutable.PriorityQueue[T]()

while (combo.size < k) {
val index = rng.nextInt(values.length)
val element = values(rng.nextInt(values.length))
combo.add(element)
combo += values(index)
}

combo.toSeq
combo.toIndexedSeq
}

def sampleNoReplacement(rng: Random) = {
Expand All @@ -49,7 +45,7 @@ abstract class AbstractCombinations[T](
}
}

combo.toSeq
combo.toIndexedSeq
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,39 @@ import scala.util.Random
abstract class AbstractSubset[T](iterable: Iterable[T], k: Int, replacement: Boolean = false)(implicit ord: Ordering[T]) extends Serializable {
protected val values = iterable.toIndexedSeq

def sample(rng: Random): Iterable[T] = {
def sampleWithReplacement(rng: Random): Iterable[T] = {
val sampleSize = rng.nextInt(k) + 1
val subset = new mutable.PriorityQueue[T]()

while (subset.size < k) {
val index = rng.nextInt(values.length)
subset += values(index)
}

subset.toIndexedSeq
}

def sampleNoReplacement(rng: Random): Iterable[T] = {
val sampleSize = rng.nextInt(k) + 1
val subset = mutable.SortedSet[T]()
val indices = mutable.Set[Int]()

while (subset.size < sampleSize) {
val index = rng.nextInt(values.size)

if (replacement) {
subset.add(values(index))
} else if (!indices.contains(index)) {
if (!indices.contains(index)) {
indices.add(index)
subset.add(values(index))
}
}

subset.toIndexedSeq
}

def sample(rng: Random): Iterable[T] = {
if (replacement) sampleWithReplacement(rng)
else sampleNoReplacement(rng)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ trait VwHoldout extends AbstractVwHoldout {
trait VwHoldoutRandomSearch extends VwHoldout {
val space = Map(
("l", UniformDouble(0, 1)),
("q", Combinations(List("a", "b", "c", "d"), k = 2, x = 2))
("interactions", Combinations('a' to 'z', k = 4, x = 7))
)

def main(args: Array[String]) {
Expand Down

0 comments on commit 21b8ed1

Please sign in to comment.