java.util.PriorityQueue Scala Examples
The following examples show how to use java.util.PriorityQueue.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: BruteForceBySignatureSearcher.scala From random-projections-at-berlinbuzzwords with Apache License 2.0 | 5 votes |
package com.stefansavev.randomprojections.implementation import com.stefansavev.randomprojections.tuning.PerformanceCounters object Counter{ var i = 0 } class PointScore(val pointId: Int, val score: Double) extends Comparable[PointScore] { override def compareTo(o: PointScore): Int = { //o.score.compareTo(this.score) this.score.compareTo(o.score) } } class BruteForceBySignatureSearcher(settings: SearcherSettings) extends Searcher{ if (!settings.randomTrees.header.isCompatible(settings.trainingSet.rowStoredView.getColumnHeader)){ throw new IllegalStateException("Index and dataset are not compatible") } val trainingSet = settings.trainingSet val numRows = settings.trainingSet.numRows val signatureVectors = settings.randomTrees.signatureVecs val signatures = settings.randomTrees.invertedIndex.asInstanceOf[IndexImpl].signatures def selectTopKNeighbors(query: Array[Double], k: Int): Array[PointScore] = { val querySig = signatureVectors.computePointSignatures(query) import java.util.PriorityQueue val pq = new PriorityQueue[PointScore]() var j = 0 while(j < numRows) { //TODO: probably you want to process in a randomized order to make sure you don't insert too many itmes in pq val score = signatures.overlap(querySig, j) if (pq.size() < k ){ pq.add(new PointScore(j, score)) } else{//it should be exactly k items in pq val minAcceptedScore = pq.peek().score if (score > minAcceptedScore){ pq.remove() //remove min pq.add(new PointScore(j, score)) } } j += 1 } val sorted = Array.ofDim[PointScore](pq.size()) j = 0 while(pq.size() > 0){ sorted(j) = pq.remove() j += 1 } sorted.sortBy(ps => - ps.score) } override def getNearestNeighborsByVector(query: Array[Double], topNearestNeighbors: Int): KNNS = { val start = System.currentTimeMillis() val topNeighbors = selectTopKNeighbors(query, topNearestNeighbors) val asKNN = topNeighbors.map(ps => KNN(ps.pointId, ps.score.toInt, trainingSet.getLabel(ps.pointId), ps.score)) val end = System.currentTimeMillis() Counter.i += 1 PerformanceCounters.processQuery(end - start) KNNS(10, -1, asKNN) } def getNearestNeighborsById(id: Int, topNearestNeighbors: Int): KNNS = { val query = trainingSet.getPointAsDenseVector(id) val knn = getNearestNeighborsByVector(query, topNearestNeighbors) knn.copy(pointId = id) //must replace the id of the point } override def getSettings(): SearcherSettings = settings }
Example 2
Source File: BucketSearchPriorityQueue.scala From random-projections-at-berlinbuzzwords with Apache License 2.0 | 5 votes |
package com.stefansavev.randomprojections.implementation case class PQEntry(var sumScores: Double, var depth: Int, var node: RandomTree) extends Comparable[PQEntry] { def adjustScore(depth: Int, score: Double): Double = if (depth == 0.0) 0.0 else sumScores / Math.sqrt(depth) //(sumScores*sumScores) /depth var adjScore = adjustScore(depth, sumScores) override def compareTo(o: PQEntry): Int = { o.adjScore.compareTo(this.adjScore) } def copyFrom(other: PQEntry): Unit = { sumScores = other.sumScores depth = other.depth node = other.node adjScore = adjustScore(depth, sumScores) } } abstract class BucketSearchPriorityQueue { def remove(pqEntryOutput: PQEntry): Boolean def add(sumScores: Double, depth: Int, node: RandomTree): Unit } class OptimizedPriorityQueueSimpleImpl extends BucketSearchPriorityQueue { import java.util.PriorityQueue val pq = new PriorityQueue[PQEntry]() override def remove(pqEntryOutput: PQEntry): Boolean = { if (pq.size() > 0) { val entry = pq.remove() pqEntryOutput.copyFrom(entry) true } else { false } } override def add(sumScores: Double, depth: Int, node: RandomTree): Unit = { val entry = PQEntry(sumScores, depth, node) pq.add(entry) } }
Example 3
Source File: BatchSampler.scala From shapenet-viewer with MIT License | 5 votes |
package edu.stanford.graphics.shapenet.util import scala.util.Random import java.util.PriorityQueue import scala.collection.JavaConversions._ class BatchSampler { def random(size: Int, rand: Random = Random): Int = { rand.nextInt(size) } def sampleOne[E](samples: IndexedSeq[E], rand: Random = Random): Option[E] = { val total = samples.size if (total > 0) { val i = rand.nextInt(total) Some(samples(i)) } else { None } } def sampleIndices(sampleSize: Int, totalSize: Int, shuffleOrder: Boolean = true, rand: Random = Random): IndexedSeq[Int] = { if (sampleSize >= totalSize) { val samples = (0 until totalSize).toIndexedSeq if (shuffleOrder) rand.shuffle(samples) else samples } else { val samples = Array.ofDim[Int](sampleSize) var t = 0 // # of total processed var m = 0 // # of samples selected while (m < sampleSize && t < totalSize) { val r = rand.nextFloat() if ( (totalSize - t)*r < sampleSize - m) { samples(m) = t m = m+1 } t = t+1 } if (shuffleOrder) rand.shuffle(samples.toIndexedSeq) else samples.toIndexedSeq } } def sampleWithoutReplacement[E](allSamples: Iterable[E], nSamples: Int, randOrder: Boolean = true, rand: Random = Random): IndexedSeq[E] = { val indexed = allSamples.toIndexedSeq if (nSamples < 10 || nSamples < indexed.size/2) { val indices = sampleIndices(nSamples, indexed.size, randOrder) indices.map( x => indexed(x) ) } else { val permutedList = rand.shuffle(indexed) permutedList.slice(0, nSamples) } } def sampleWithReplacement[E](allSamples: Iterable[E], nSamples: Int, rand: Random = Random): IndexedSeq[E] = { val ordered = allSamples.toIndexedSeq for (i <- 0 until nSamples) yield { val r = rand.nextInt(ordered.size) ordered(r) } } }