Compare commits
No commits in common. "actorbintree" and "kmeans" have entirely different histories.
actorbintr
...
kmeans
@ -25,7 +25,7 @@ grade:
|
|||||||
tags:
|
tags:
|
||||||
- cs206
|
- cs206
|
||||||
image:
|
image:
|
||||||
name: smarter3/moocs:reactive-actorbintree-2020-04-15
|
name: smarter3/moocs:parprog1-kmeans-2020-02-24-2
|
||||||
entrypoint: [""]
|
entrypoint: [""]
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
before_script:
|
before_script:
|
||||||
|
|||||||
@ -1,4 +1,9 @@
|
|||||||
// Student tasks (i.e. submit, packageSubmission)
|
// Student tasks (i.e. submit, packageSubmission)
|
||||||
enablePlugins(StudentTasks)
|
enablePlugins(StudentTasks)
|
||||||
|
|
||||||
|
courseraId := ch.epfl.lamp.CourseraId(
|
||||||
|
key = "UJmFEtoIEeWJwRKcpT8ChQ",
|
||||||
|
itemId = "Olt0g",
|
||||||
|
premiumItemId = Some("akLxD"),
|
||||||
|
partId = "mz8iL"
|
||||||
|
)
|
||||||
|
|||||||
29
build.sbt
29
build.sbt
@ -1,24 +1,13 @@
|
|||||||
course := "reactive"
|
course := "parprog1"
|
||||||
assignment := "actorbintree"
|
assignment := "kmeans"
|
||||||
|
|
||||||
testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "-a", "-v", "-s")
|
|
||||||
parallelExecution in Test := false
|
|
||||||
|
|
||||||
val akkaVersion = "2.6.0"
|
|
||||||
|
|
||||||
scalaVersion := "0.23.0-bin-20200211-5b006fb-NIGHTLY"
|
scalaVersion := "0.23.0-bin-20200211-5b006fb-NIGHTLY"
|
||||||
|
scalacOptions ++= Seq("-language:implicitConversions", "-deprecation")
|
||||||
scalacOptions ++= Seq(
|
|
||||||
"-feature",
|
|
||||||
"-deprecation",
|
|
||||||
"-encoding", "UTF-8",
|
|
||||||
"-unchecked",
|
|
||||||
"-language:implicitConversions"
|
|
||||||
)
|
|
||||||
|
|
||||||
libraryDependencies ++= Seq(
|
libraryDependencies ++= Seq(
|
||||||
"com.typesafe.akka" %% "akka-actor" % akkaVersion,
|
"com.storm-enroute" %% "scalameter-core" % "0.19",
|
||||||
"com.typesafe.akka" %% "akka-testkit" % akkaVersion % Test,
|
"org.scala-lang.modules" %% "scala-parallel-collections" % "0.2.0",
|
||||||
"com.novocode" % "junit-interface" % "0.11" % Test
|
"com.novocode" % "junit-interface" % "0.11" % Test
|
||||||
).map(_.withDottyCompat(scalaVersion.value))
|
).map(_.withDottyCompat(scalaVersion.value))
|
||||||
testSuite := "actorbintree.BinaryTreeSuite"
|
|
||||||
|
testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "-a", "-v", "-s")
|
||||||
|
testSuite := "kmeans.KMeansSuite"
|
||||||
|
|||||||
Binary file not shown.
BIN
src/main/resources/kmeans/epfl-view.jpg
Normal file
BIN
src/main/resources/kmeans/epfl-view.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 322 KiB |
@ -1,189 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
|
|
||||||
*/
|
|
||||||
package actorbintree
|
|
||||||
|
|
||||||
import akka.actor._
|
|
||||||
import scala.collection.immutable.Queue
|
|
||||||
|
|
||||||
object BinaryTreeSet {
|
|
||||||
|
|
||||||
trait Operation {
|
|
||||||
def requester: ActorRef
|
|
||||||
def id: Int
|
|
||||||
def elem: Int
|
|
||||||
}
|
|
||||||
|
|
||||||
trait OperationReply {
|
|
||||||
def id: Int
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Request with identifier `id` to insert an element `elem` into the tree.
|
|
||||||
* The actor at reference `requester` should be notified when this operation
|
|
||||||
* is completed.
|
|
||||||
*/
|
|
||||||
case class Insert(requester: ActorRef, id: Int, elem: Int) extends Operation
|
|
||||||
|
|
||||||
/** Request with identifier `id` to check whether an element `elem` is present
|
|
||||||
* in the tree. The actor at reference `requester` should be notified when
|
|
||||||
* this operation is completed.
|
|
||||||
*/
|
|
||||||
case class Contains(requester: ActorRef, id: Int, elem: Int) extends Operation
|
|
||||||
|
|
||||||
/** Request with identifier `id` to remove the element `elem` from the tree.
|
|
||||||
* The actor at reference `requester` should be notified when this operation
|
|
||||||
* is completed.
|
|
||||||
*/
|
|
||||||
case class Remove(requester: ActorRef, id: Int, elem: Int) extends Operation
|
|
||||||
|
|
||||||
/** Request to perform garbage collection */
|
|
||||||
case object GC
|
|
||||||
|
|
||||||
/** Holds the answer to the Contains request with identifier `id`.
|
|
||||||
* `result` is true if and only if the element is present in the tree.
|
|
||||||
*/
|
|
||||||
case class ContainsResult(id: Int, result: Boolean) extends OperationReply
|
|
||||||
|
|
||||||
/** Message to signal successful completion of an insert or remove operation. */
|
|
||||||
case class OperationFinished(id: Int) extends OperationReply
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class BinaryTreeSet extends Actor {
|
|
||||||
import BinaryTreeSet._
|
|
||||||
import BinaryTreeNode._
|
|
||||||
|
|
||||||
def createRoot: ActorRef = context.actorOf(BinaryTreeNode.props(0, initiallyRemoved = true))
|
|
||||||
|
|
||||||
var root = createRoot
|
|
||||||
|
|
||||||
// optional
|
|
||||||
var pendingQueue = Queue.empty[Operation]
|
|
||||||
|
|
||||||
// optional
|
|
||||||
def receive = normal
|
|
||||||
|
|
||||||
// optional
|
|
||||||
/** Accepts `Operation` and `GC` messages. */
|
|
||||||
val normal: Receive = {
|
|
||||||
case op:Operation => root ! op
|
|
||||||
case GC => {
|
|
||||||
val newRoot = createRoot;
|
|
||||||
root ! CopyTo(newRoot)
|
|
||||||
context.become(garbageCollecting(newRoot))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// optional
|
|
||||||
/** Handles messages while garbage collection is performed.
|
|
||||||
* `newRoot` is the root of the new binary tree where we want to copy
|
|
||||||
* all non-removed elements into.
|
|
||||||
*/
|
|
||||||
def garbageCollecting(newRoot: ActorRef): Receive = {
|
|
||||||
case op:Operation => pendingQueue = pendingQueue.enqueue(op)
|
|
||||||
case CopyFinished =>
|
|
||||||
pendingQueue.foreach(newRoot ! _) //foreach preserves order of a queue (same as dequeueing)
|
|
||||||
root ! PoisonPill //Will also stop all of its children
|
|
||||||
pendingQueue = Queue.empty
|
|
||||||
root = newRoot;
|
|
||||||
context.become(normal)
|
|
||||||
//Ignore GC messages here
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
object BinaryTreeNode {
|
|
||||||
trait Position
|
|
||||||
|
|
||||||
case object Left extends Position
|
|
||||||
case object Right extends Position
|
|
||||||
|
|
||||||
case class CopyTo(treeNode: ActorRef)
|
|
||||||
case object CopyFinished
|
|
||||||
|
|
||||||
def props(elem: Int, initiallyRemoved: Boolean) = Props(classOf[BinaryTreeNode], elem, initiallyRemoved)
|
|
||||||
}
|
|
||||||
|
|
||||||
class BinaryTreeNode(val elem: Int, initiallyRemoved: Boolean) extends Actor {
|
|
||||||
import BinaryTreeNode._
|
|
||||||
import BinaryTreeSet._
|
|
||||||
|
|
||||||
var subtrees = Map[Position, ActorRef]()
|
|
||||||
var removed = initiallyRemoved
|
|
||||||
|
|
||||||
// optional
|
|
||||||
def receive = normal
|
|
||||||
|
|
||||||
def goDownTo(elem : Int) : Position = if(elem < this.elem) Left else Right
|
|
||||||
// optional
|
|
||||||
/** Handles `Operation` messages and `CopyTo` requests. */
|
|
||||||
val normal: Receive = {
|
|
||||||
case Insert (requester, id, elem) =>
|
|
||||||
if(elem == this.elem && !removed){
|
|
||||||
requester ! OperationFinished(id)
|
|
||||||
}else{
|
|
||||||
val nextPos = goDownTo(elem)
|
|
||||||
|
|
||||||
subtrees get nextPos match{
|
|
||||||
case Some(node) => node ! Insert(requester, id, elem)
|
|
||||||
case None => {
|
|
||||||
val newActorSubtree = (nextPos, context.actorOf(BinaryTreeNode.props(elem, false)))
|
|
||||||
subtrees = subtrees + newActorSubtree
|
|
||||||
requester ! OperationFinished(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case Contains(requester, id, elem) =>
|
|
||||||
if(elem == this.elem && !removed)
|
|
||||||
requester ! ContainsResult(id, true)
|
|
||||||
else{
|
|
||||||
//Need to search subtrees
|
|
||||||
subtrees get goDownTo(elem) match{
|
|
||||||
case Some(node) => node ! Contains(requester, id, elem)
|
|
||||||
case None => requester ! ContainsResult(id, false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case Remove (requester, id, elem) =>
|
|
||||||
if(elem == this.elem && !removed){
|
|
||||||
removed = true
|
|
||||||
requester ! OperationFinished(id)
|
|
||||||
}else{
|
|
||||||
subtrees get goDownTo(elem) match{
|
|
||||||
case Some(node) => node ! Remove(requester, id, elem)
|
|
||||||
case None => requester ! OperationFinished(id) // (elem isn't in the tree)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
case CopyTo(newRoot) =>
|
|
||||||
//We are already done, nothing to do
|
|
||||||
if(removed && subtrees.isEmpty) context.parent ! CopyFinished
|
|
||||||
else{
|
|
||||||
if(!removed) newRoot ! Insert(self, elem, elem)
|
|
||||||
subtrees.values foreach(_ ! CopyTo(newRoot)) //Copy subtrees elems
|
|
||||||
//val insertConfirmed = if(removed) true else false, hence we can simply pass removed
|
|
||||||
context.become(copying(subtrees.values.toSet, removed))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// optional
|
|
||||||
/** `expected` is the set of ActorRefs whose replies we are waiting for,
|
|
||||||
* `insertConfirmed` tracks whether the copy of this node to the new tree has been confirmed.
|
|
||||||
*/
|
|
||||||
def copying(expected: Set[ActorRef], insertConfirmed: Boolean): Receive = {
|
|
||||||
//To catch the insert of this node into the new tree beeing finished
|
|
||||||
case OperationFinished(_) => {
|
|
||||||
if(expected.isEmpty) context.parent ! CopyFinished
|
|
||||||
else context.become(copying(expected, true))
|
|
||||||
}
|
|
||||||
|
|
||||||
case CopyFinished => {
|
|
||||||
val newExp = expected-sender
|
|
||||||
if(insertConfirmed && newExp.isEmpty){
|
|
||||||
context.parent ! CopyFinished
|
|
||||||
}else{
|
|
||||||
context.become(copying(newExp, insertConfirmed))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
147
src/main/scala/kmeans/KMeans.scala
Normal file
147
src/main/scala/kmeans/KMeans.scala
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
package kmeans
|
||||||
|
|
||||||
|
import scala.annotation.tailrec
|
||||||
|
import scala.collection.{Map, Seq, mutable}
|
||||||
|
import scala.collection.parallel.CollectionConverters._
|
||||||
|
import scala.collection.parallel.{ForkJoinTaskSupport, ParMap, ParSeq}
|
||||||
|
import scala.util.Random
|
||||||
|
import org.scalameter._
|
||||||
|
import java.util.concurrent.ForkJoinPool
|
||||||
|
|
||||||
|
class KMeans extends KMeansInterface {
|
||||||
|
|
||||||
|
def generatePoints(k: Int, num: Int): ParSeq[Point] = {
|
||||||
|
val randx = new Random(1)
|
||||||
|
val randy = new Random(3)
|
||||||
|
val randz = new Random(5)
|
||||||
|
(0 until num)
|
||||||
|
.map({ i =>
|
||||||
|
val x = ((i + 1) % k) * 1.0 / k + randx.nextDouble() * 0.5
|
||||||
|
val y = ((i + 5) % k) * 1.0 / k + randy.nextDouble() * 0.5
|
||||||
|
val z = ((i + 7) % k) * 1.0 / k + randz.nextDouble() * 0.5
|
||||||
|
new Point(x, y, z)
|
||||||
|
}).to(mutable.ArrayBuffer).par
|
||||||
|
}
|
||||||
|
|
||||||
|
def initializeMeans(k: Int, points: ParSeq[Point]): ParSeq[Point] = {
|
||||||
|
val rand = new Random(7)
|
||||||
|
(0 until k).map(_ => points(rand.nextInt(points.length))).to(mutable.ArrayBuffer).par
|
||||||
|
}
|
||||||
|
|
||||||
|
def findClosest(p: Point, means: IterableOnce[Point]): Point = {
|
||||||
|
val it = means.iterator
|
||||||
|
assert(it.nonEmpty)
|
||||||
|
var closest = it.next()
|
||||||
|
var minDistance = p.squareDistance(closest)
|
||||||
|
while (it.hasNext) {
|
||||||
|
val point = it.next()
|
||||||
|
val distance = p.squareDistance(point)
|
||||||
|
if (distance < minDistance) {
|
||||||
|
minDistance = distance
|
||||||
|
closest = point
|
||||||
|
}
|
||||||
|
}
|
||||||
|
closest
|
||||||
|
}
|
||||||
|
|
||||||
|
def classify(points: ParSeq[Point], means: ParSeq[Point]): ParMap[Point, ParSeq[Point]] = {
|
||||||
|
val pointsMeanMap = points.par.groupBy(findClosest(_, means))
|
||||||
|
// So iterate over means get (empty) list and return map
|
||||||
|
means.par.map(mean => mean -> pointsMeanMap.getOrElse(mean, ParSeq())).toMap
|
||||||
|
}
|
||||||
|
|
||||||
|
def findAverage(oldMean: Point, points: ParSeq[Point]): Point = if (points.isEmpty) oldMean else {
|
||||||
|
var x = 0.0
|
||||||
|
var y = 0.0
|
||||||
|
var z = 0.0
|
||||||
|
points.seq.foreach { p =>
|
||||||
|
x += p.x
|
||||||
|
y += p.y
|
||||||
|
z += p.z
|
||||||
|
}
|
||||||
|
new Point(x / points.length, y / points.length, z / points.length)
|
||||||
|
}
|
||||||
|
|
||||||
|
def update(classified: ParMap[Point, ParSeq[Point]], oldMeans: ParSeq[Point]): ParSeq[Point] = {
|
||||||
|
oldMeans.par.map(oldMean => findAverage(oldMean, classified(oldMean)))
|
||||||
|
}
|
||||||
|
|
||||||
|
def converged(eta: Double, oldMeans: ParSeq[Point], newMeans: ParSeq[Point]): Boolean = {
|
||||||
|
(oldMeans zip newMeans).forall{
|
||||||
|
case (oldMean, newMean) => oldMean.squareDistance(newMean) <= eta
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@tailrec
|
||||||
|
final def kMeans(points: ParSeq[Point], means: ParSeq[Point], eta: Double): ParSeq[Point] = {
|
||||||
|
val classified = classify(points, means)
|
||||||
|
val newMeans = update(classified, means)
|
||||||
|
|
||||||
|
if (!converged(eta, means, newMeans)) kMeans(points, newMeans, eta) else newMeans
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Describes one point in three-dimensional space.
|
||||||
|
*
|
||||||
|
* Note: deliberately uses reference equality.
|
||||||
|
*/
|
||||||
|
class Point(val x: Double, val y: Double, val z: Double) {
|
||||||
|
private def square(v: Double): Double = v * v
|
||||||
|
def squareDistance(that: Point): Double = {
|
||||||
|
square(that.x - x) + square(that.y - y) + square(that.z - z)
|
||||||
|
}
|
||||||
|
private def round(v: Double): Double = (v * 100).toInt / 100.0
|
||||||
|
override def toString = s"(${round(x)}, ${round(y)}, ${round(z)})"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
object KMeansRunner {
|
||||||
|
|
||||||
|
val standardConfig = config(
|
||||||
|
Key.exec.minWarmupRuns -> 20,
|
||||||
|
Key.exec.maxWarmupRuns -> 40,
|
||||||
|
Key.exec.benchRuns -> 25,
|
||||||
|
Key.verbose -> false
|
||||||
|
) withWarmer(new Warmer.Default)
|
||||||
|
|
||||||
|
def main(args: Array[String]): Unit = {
|
||||||
|
val kMeans = new KMeans()
|
||||||
|
|
||||||
|
val numPoints = 500000
|
||||||
|
val eta = 0.01
|
||||||
|
val k = 32
|
||||||
|
val points = kMeans.generatePoints(k, numPoints)
|
||||||
|
val means = kMeans.initializeMeans(k, points)
|
||||||
|
|
||||||
|
val seqtime = {
|
||||||
|
val parTasksupport = points.tasksupport
|
||||||
|
val seqPool = new ForkJoinPool(1)
|
||||||
|
val seqTasksupport = new ForkJoinTaskSupport(seqPool)
|
||||||
|
try {
|
||||||
|
points.tasksupport = seqTasksupport
|
||||||
|
means.tasksupport = seqTasksupport
|
||||||
|
standardConfig measure {
|
||||||
|
kMeans.kMeans(points, means, eta)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
points.tasksupport = parTasksupport
|
||||||
|
means.tasksupport = parTasksupport
|
||||||
|
seqPool.shutdown()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val partime = standardConfig measure {
|
||||||
|
kMeans.kMeans(points, means, eta)
|
||||||
|
}
|
||||||
|
|
||||||
|
println(s"sequential time: $seqtime")
|
||||||
|
println(s"parallel time: $partime")
|
||||||
|
println(s"speedup: ${seqtime.value / partime.value}")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Workaround Dotty's handling of the existential type KeyValue
|
||||||
|
implicit def keyValueCoerce[T](kv: (Key[T], T)): KeyValue = {
|
||||||
|
kv.asInstanceOf[KeyValue]
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/main/scala/kmeans/KMeansInterface.scala
Normal file
15
src/main/scala/kmeans/KMeansInterface.scala
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package kmeans
|
||||||
|
|
||||||
|
import scala.collection.{Map, Seq}
|
||||||
|
import scala.collection.parallel.{ParMap, ParSeq}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The interface used by the grading infrastructure. Do not change signatures
|
||||||
|
* or your submission will fail with a NoSuchMethodError.
|
||||||
|
*/
|
||||||
|
trait KMeansInterface {
|
||||||
|
def classify(points: ParSeq[Point], means: ParSeq[Point]): ParMap[Point, ParSeq[Point]]
|
||||||
|
def update(classified: ParMap[Point, ParSeq[Point]], oldMeans: ParSeq[Point]): ParSeq[Point]
|
||||||
|
def converged(eta: Double, oldMeans: ParSeq[Point], newMeans: ParSeq[Point]): Boolean
|
||||||
|
def kMeans(points: ParSeq[Point], means: ParSeq[Point], eta: Double): ParSeq[Point]
|
||||||
|
}
|
||||||
117
src/main/scala/kmeans/fun/IndexedColors.scala
Normal file
117
src/main/scala/kmeans/fun/IndexedColors.scala
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
package kmeans
|
||||||
|
package fun
|
||||||
|
|
||||||
|
import scala.collection.Seq
|
||||||
|
import scala.collection.parallel.ParSeq
|
||||||
|
import scala.collection.parallel.CollectionConverters._
|
||||||
|
|
||||||
|
abstract sealed trait InitialSelectionStrategy
|
||||||
|
case object RandomSampling extends InitialSelectionStrategy
|
||||||
|
case object UniformSampling extends InitialSelectionStrategy
|
||||||
|
case object UniformChoice extends InitialSelectionStrategy
|
||||||
|
|
||||||
|
abstract sealed trait ConvergenceStrategy
|
||||||
|
case class ConvergedWhenSNRAbove(x: Double) extends ConvergenceStrategy
|
||||||
|
case class ConvergedAfterNSteps(n: Int) extends ConvergenceStrategy
|
||||||
|
case class ConvergedAfterMeansAreStill(eta: Double) extends ConvergenceStrategy
|
||||||
|
|
||||||
|
|
||||||
|
class IndexedColorFilter(initialImage: Img,
|
||||||
|
colorCount: Int,
|
||||||
|
initStrategy: InitialSelectionStrategy,
|
||||||
|
convStrategy: ConvergenceStrategy) extends KMeans {
|
||||||
|
|
||||||
|
private var steps = 0
|
||||||
|
|
||||||
|
val points = imageToPoints(initialImage).par
|
||||||
|
val means = initializeIndex(colorCount, points).par
|
||||||
|
|
||||||
|
/* The work is done here: */
|
||||||
|
private val newMeans = kMeans(points, means, 0.01)
|
||||||
|
|
||||||
|
/* And these are the results exposed */
|
||||||
|
def getStatus() = s"Converged after $steps steps."
|
||||||
|
def getResult() = indexedImage(initialImage, newMeans)
|
||||||
|
|
||||||
|
private def imageToPoints(img: Img): Seq[Point] =
|
||||||
|
for (x <- 0 until img.width; y <- 0 until img.height) yield {
|
||||||
|
val rgba = img(x, y)
|
||||||
|
new Point(red(rgba), green(rgba), blue(rgba))
|
||||||
|
}
|
||||||
|
|
||||||
|
private def indexedImage(img: Img, means: ParSeq[Point]) = {
|
||||||
|
val dst = new Img(img.width, img.height)
|
||||||
|
val pts = collection.mutable.Set[Point]()
|
||||||
|
|
||||||
|
for (x <- 0 until img.width; y <- 0 until img.height) yield {
|
||||||
|
val v = img(x, y)
|
||||||
|
var point = new Point(red(v), green(v), blue(v))
|
||||||
|
point = findClosest(point, means)
|
||||||
|
pts += point
|
||||||
|
dst(x, y) = rgba(point.x, point.y, point.z, 1d)
|
||||||
|
}
|
||||||
|
|
||||||
|
dst
|
||||||
|
}
|
||||||
|
|
||||||
|
private def initializeIndex(numColors: Int, points: ParSeq[Point]): Seq[Point] = {
|
||||||
|
val initialPoints: Seq[Point] =
|
||||||
|
initStrategy match {
|
||||||
|
case RandomSampling =>
|
||||||
|
val d: Int = points.size / numColors
|
||||||
|
(0 until numColors) map (idx => points(d * idx))
|
||||||
|
case UniformSampling =>
|
||||||
|
val sep: Int = 32
|
||||||
|
(for (r <- 0 until 255 by sep; g <- 0 until 255 by sep; b <- 0 until 255 by sep) yield {
|
||||||
|
def inside(p: Point): Boolean =
|
||||||
|
(p.x >= (r.toDouble / 255)) &&
|
||||||
|
(p.x <= ((r.toDouble + sep) / 255)) &&
|
||||||
|
(p.y >= (g.toDouble / 255)) &&
|
||||||
|
(p.y <= ((g.toDouble + sep) / 255)) &&
|
||||||
|
(p.z >= (b.toDouble / 255)) &&
|
||||||
|
(p.z <= ((b.toDouble + sep) / 255))
|
||||||
|
|
||||||
|
val pts = points.filter(inside(_))
|
||||||
|
val cnt = pts.size * 3 * numColors / points.size
|
||||||
|
if (cnt >= 1) {
|
||||||
|
val d = pts.size / cnt
|
||||||
|
(0 until cnt) map (idx => pts(d * idx))
|
||||||
|
} else
|
||||||
|
Seq()
|
||||||
|
}).flatten
|
||||||
|
case UniformChoice =>
|
||||||
|
val d: Int = math.max(1, (256 / math.cbrt(numColors.toDouble).ceil).toInt)
|
||||||
|
for (r <- 0 until 256 by d; g <- 0 until 256 by d; b <- 0 until 256 by d) yield
|
||||||
|
new Point(r.toDouble / 256,g.toDouble / 256, b.toDouble / 256)
|
||||||
|
}
|
||||||
|
|
||||||
|
val d2 = initialPoints.size.toDouble / numColors
|
||||||
|
(0 until numColors) map (idx => initialPoints((idx * d2).toInt))
|
||||||
|
}
|
||||||
|
|
||||||
|
private def computeSNR(points: ParSeq[Point], means: ParSeq[Point]): Double = {
|
||||||
|
var sound = 0.0
|
||||||
|
var noise = 0.0
|
||||||
|
|
||||||
|
for (point <- points) {
|
||||||
|
import math.{pow, sqrt}
|
||||||
|
val closest = findClosest(point, means)
|
||||||
|
sound += sqrt(pow(point.x, 2) + pow(point.y, 2) + pow(point.z, 2))
|
||||||
|
noise += sqrt(pow(point.x - closest.x, 2) + pow(point.y - closest.y, 2) + pow(point.z - closest.z, 2))
|
||||||
|
}
|
||||||
|
sound/noise
|
||||||
|
}
|
||||||
|
|
||||||
|
override def converged(eta: Double, oldMeans: ParSeq[Point], newMeans: ParSeq[Point]): Boolean = {
|
||||||
|
steps += 1
|
||||||
|
convStrategy match {
|
||||||
|
case ConvergedAfterNSteps(n) =>
|
||||||
|
steps >= n
|
||||||
|
case ConvergedAfterMeansAreStill(eta) =>
|
||||||
|
super.converged(eta, oldMeans, newMeans)
|
||||||
|
case ConvergedWhenSNRAbove(snr_desired) =>
|
||||||
|
val snr_computed = computeSNR(points.par, newMeans)
|
||||||
|
snr_computed >= snr_desired
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
95
src/main/scala/kmeans/fun/PhotoCanvas.scala
Normal file
95
src/main/scala/kmeans/fun/PhotoCanvas.scala
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
package kmeans
|
||||||
|
package fun
|
||||||
|
|
||||||
|
import java.awt._
|
||||||
|
import java.awt.event._
|
||||||
|
import java.awt.image._
|
||||||
|
import java.io._
|
||||||
|
import javax.imageio._
|
||||||
|
import javax.swing._
|
||||||
|
import javax.swing.event._
|
||||||
|
|
||||||
|
class PhotoCanvas extends JComponent {
|
||||||
|
|
||||||
|
var imagePath: Option[String] = None
|
||||||
|
|
||||||
|
var image = loadEPFLImage()
|
||||||
|
|
||||||
|
val timerDelay = 100
|
||||||
|
val timer =
|
||||||
|
new Timer(timerDelay, new ActionListener() {
|
||||||
|
def actionPerformed(e: ActionEvent): Unit = repaint()
|
||||||
|
})
|
||||||
|
|
||||||
|
override def getPreferredSize = {
|
||||||
|
new Dimension(image.width, image.height)
|
||||||
|
}
|
||||||
|
|
||||||
|
private def loadEPFLImage(): Img = {
|
||||||
|
val stream = this.getClass.getResourceAsStream("/kmeans/epfl-view.jpg")
|
||||||
|
try {
|
||||||
|
loadImage(stream)
|
||||||
|
} finally {
|
||||||
|
stream.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private def loadFileImage(path: String): Img = {
|
||||||
|
val stream = new FileInputStream(path)
|
||||||
|
try {
|
||||||
|
loadImage(stream)
|
||||||
|
} finally {
|
||||||
|
stream.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private def loadImage(inputStream: InputStream): Img = {
|
||||||
|
val bufferedImage = ImageIO.read(inputStream)
|
||||||
|
val width = bufferedImage.getWidth
|
||||||
|
val height = bufferedImage.getHeight
|
||||||
|
val img = new Img(width, height)
|
||||||
|
for (x <- 0 until width; y <- 0 until height)
|
||||||
|
img(x, y) = bufferedImage.getRGB(x, y)
|
||||||
|
img
|
||||||
|
}
|
||||||
|
|
||||||
|
def reload(): Unit = {
|
||||||
|
image = imagePath match {
|
||||||
|
case Some(path) => loadFileImage(path)
|
||||||
|
case None => loadEPFLImage()
|
||||||
|
}
|
||||||
|
repaint()
|
||||||
|
}
|
||||||
|
|
||||||
|
def loadFile(path: String): Unit = {
|
||||||
|
imagePath = Some(path)
|
||||||
|
reload()
|
||||||
|
}
|
||||||
|
|
||||||
|
def saveFile(path: String): Unit = {
|
||||||
|
reload()
|
||||||
|
val stream = new FileOutputStream(path)
|
||||||
|
val bufferedImage = new BufferedImage(image.width, image.height, BufferedImage.TYPE_INT_ARGB)
|
||||||
|
for (x <- 0 until image.width; y <- 0 until image.height) bufferedImage.setRGB(x, y, image(x, y))
|
||||||
|
ImageIO.write(bufferedImage, "png", stream)
|
||||||
|
}
|
||||||
|
|
||||||
|
def applyIndexedColors(colorCount: Int, initStrategy: InitialSelectionStrategy, convStrategy: ConvergenceStrategy): String = {
|
||||||
|
val filter = new IndexedColorFilter(image, colorCount, initStrategy, convStrategy)
|
||||||
|
image = filter.getResult()
|
||||||
|
repaint()
|
||||||
|
filter.getStatus()
|
||||||
|
}
|
||||||
|
|
||||||
|
override def paintComponent(gcan: Graphics) = {
|
||||||
|
super.paintComponent(gcan)
|
||||||
|
|
||||||
|
val width = image.width
|
||||||
|
val height = image.height
|
||||||
|
val bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB)
|
||||||
|
for (x <- 0 until width; y <- 0 until height) bufferedImage.setRGB(x, y, image(x, y))
|
||||||
|
|
||||||
|
gcan.drawImage(bufferedImage, 0, 0, null)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
218
src/main/scala/kmeans/fun/ScalaShop.scala
Normal file
218
src/main/scala/kmeans/fun/ScalaShop.scala
Normal file
@ -0,0 +1,218 @@
|
|||||||
|
package kmeans
|
||||||
|
package fun
|
||||||
|
|
||||||
|
import java.awt._
|
||||||
|
import java.awt.event._
|
||||||
|
import javax.swing._
|
||||||
|
import javax.swing.event._
|
||||||
|
import scala.collection.mutable.ArrayBuffer
|
||||||
|
import scala.reflect.ClassTag
|
||||||
|
import org.scalameter._
|
||||||
|
|
||||||
|
object ScalaShop {
|
||||||
|
|
||||||
|
class ScalaShopFrame extends JFrame("ScalaShop\u2122") {
|
||||||
|
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
|
||||||
|
setSize(800, 500)
|
||||||
|
setLayout(new BorderLayout)
|
||||||
|
|
||||||
|
val rightpanel = new JPanel
|
||||||
|
rightpanel.setBorder(BorderFactory.createEtchedBorder(border.EtchedBorder.LOWERED))
|
||||||
|
rightpanel.setLayout(new BorderLayout)
|
||||||
|
add(rightpanel, BorderLayout.EAST)
|
||||||
|
|
||||||
|
val allControls = new JPanel
|
||||||
|
allControls.setLayout(new BoxLayout(allControls, BoxLayout.Y_AXIS))
|
||||||
|
rightpanel.add(allControls, BorderLayout.NORTH)
|
||||||
|
|
||||||
|
// Color count selection
|
||||||
|
val colorControls = new JPanel
|
||||||
|
colorControls.setLayout(new GridLayout(0, 2))
|
||||||
|
allControls.add(colorControls)
|
||||||
|
|
||||||
|
val colorCountLabel = new JLabel("Colors")
|
||||||
|
colorControls.add(colorCountLabel)
|
||||||
|
|
||||||
|
val colorCountSpinner = new JSpinner(new SpinnerNumberModel(32, 16, 512, 16))
|
||||||
|
colorControls.add(colorCountSpinner)
|
||||||
|
|
||||||
|
// Initial selection
|
||||||
|
val initSelectionControls = new JPanel
|
||||||
|
initSelectionControls.setLayout(new GridLayout(0, 1))
|
||||||
|
allControls.add(initSelectionControls)
|
||||||
|
|
||||||
|
val initialSelectionGroup = new ButtonGroup()
|
||||||
|
|
||||||
|
val initSelectionLabel = new JLabel("Initial Color Selection:")
|
||||||
|
initSelectionControls.add(initSelectionLabel)
|
||||||
|
|
||||||
|
val uniformSamplingButton = new JRadioButton("Uniform Sampling")
|
||||||
|
uniformSamplingButton.setSelected(true);
|
||||||
|
initSelectionControls.add(uniformSamplingButton)
|
||||||
|
|
||||||
|
val randomSamplingButton = new JRadioButton("Random Sampling")
|
||||||
|
initSelectionControls.add(randomSamplingButton)
|
||||||
|
|
||||||
|
val uniformChoiceButton = new JRadioButton("Uniform Choice")
|
||||||
|
initSelectionControls.add(uniformChoiceButton)
|
||||||
|
|
||||||
|
initialSelectionGroup.add(randomSamplingButton)
|
||||||
|
initialSelectionGroup.add(uniformSamplingButton)
|
||||||
|
initialSelectionGroup.add(uniformChoiceButton)
|
||||||
|
|
||||||
|
// Initial means selection
|
||||||
|
val convergenceControls = new JPanel
|
||||||
|
convergenceControls.setLayout(new BoxLayout(convergenceControls, BoxLayout.Y_AXIS))
|
||||||
|
allControls.add(convergenceControls)
|
||||||
|
|
||||||
|
val convergenceGroup = new ButtonGroup()
|
||||||
|
|
||||||
|
val convergenceLabel = new JLabel("Convergence criteria:")
|
||||||
|
initSelectionControls.add(convergenceLabel)
|
||||||
|
|
||||||
|
val criteriaControls = new JPanel
|
||||||
|
criteriaControls.setLayout(new GridLayout(0, 2))
|
||||||
|
convergenceControls.add(criteriaControls)
|
||||||
|
|
||||||
|
val stepConvergenceButton = new JRadioButton("Steps")
|
||||||
|
criteriaControls.add(stepConvergenceButton)
|
||||||
|
|
||||||
|
val stepCountSpinner = new JSpinner(new SpinnerNumberModel(5, 1, 50, 1))
|
||||||
|
criteriaControls.add(stepCountSpinner)
|
||||||
|
|
||||||
|
val etaConvergenceButton = new JRadioButton("Eta")
|
||||||
|
etaConvergenceButton.setSelected(true);
|
||||||
|
criteriaControls.add(etaConvergenceButton)
|
||||||
|
|
||||||
|
val etaCountSpinner = new JSpinner(new SpinnerNumberModel(0.001, 0.00001, 0.01, 0.00001))
|
||||||
|
criteriaControls.add(etaCountSpinner)
|
||||||
|
|
||||||
|
val snrConvergenceButton = new JRadioButton("Sound-to-noise")
|
||||||
|
criteriaControls.add(snrConvergenceButton)
|
||||||
|
|
||||||
|
val snrCountSpinner = new JSpinner(new SpinnerNumberModel(40, 10, 80, 1))
|
||||||
|
criteriaControls.add(snrCountSpinner)
|
||||||
|
|
||||||
|
convergenceGroup.add(stepConvergenceButton)
|
||||||
|
convergenceGroup.add(etaConvergenceButton)
|
||||||
|
convergenceGroup.add(snrConvergenceButton)
|
||||||
|
|
||||||
|
// Action Buttons
|
||||||
|
val actionControls = new JPanel
|
||||||
|
actionControls.setLayout(new GridLayout(0, 2))
|
||||||
|
allControls.add(actionControls)
|
||||||
|
|
||||||
|
val stepbutton = new JButton("Apply filter")
|
||||||
|
stepbutton.addActionListener(new ActionListener {
|
||||||
|
def actionPerformed(e: ActionEvent): Unit = {
|
||||||
|
var status = ""
|
||||||
|
val time = measure {
|
||||||
|
status = canvas.applyIndexedColors(getColorCount, getInitialSelectionStrategy, getConvergenceStragegy)
|
||||||
|
}
|
||||||
|
updateInformationBox(status, time.value)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
actionControls.add(stepbutton)
|
||||||
|
|
||||||
|
val clearButton = new JButton("Reload")
|
||||||
|
clearButton.addActionListener(new ActionListener {
|
||||||
|
def actionPerformed(e: ActionEvent): Unit = {
|
||||||
|
canvas.reload()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
actionControls.add(clearButton)
|
||||||
|
|
||||||
|
val info = new JTextArea(" ")
|
||||||
|
info.setBorder(BorderFactory.createLoweredBevelBorder)
|
||||||
|
rightpanel.add(info, BorderLayout.SOUTH)
|
||||||
|
|
||||||
|
val mainMenuBar = new JMenuBar()
|
||||||
|
|
||||||
|
val fileMenu = new JMenu("File")
|
||||||
|
val openMenuItem = new JMenuItem("Open...")
|
||||||
|
openMenuItem.addActionListener(new ActionListener {
|
||||||
|
def actionPerformed(e: ActionEvent): Unit = {
|
||||||
|
val fc = new JFileChooser()
|
||||||
|
if (fc.showOpenDialog(ScalaShopFrame.this) == JFileChooser.APPROVE_OPTION) {
|
||||||
|
canvas.loadFile(fc.getSelectedFile.getPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
fileMenu.add(openMenuItem)
|
||||||
|
val saveMenuItem = new JMenuItem("Save...")
|
||||||
|
saveMenuItem.addActionListener(new ActionListener {
|
||||||
|
def actionPerformed(e: ActionEvent): Unit = {
|
||||||
|
val fc = new JFileChooser("epfl-view.png")
|
||||||
|
if (fc.showSaveDialog(ScalaShopFrame.this) == JFileChooser.APPROVE_OPTION) {
|
||||||
|
canvas.saveFile(fc.getSelectedFile.getPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
fileMenu.add(saveMenuItem)
|
||||||
|
val exitMenuItem = new JMenuItem("Exit")
|
||||||
|
exitMenuItem.addActionListener(new ActionListener {
|
||||||
|
def actionPerformed(e: ActionEvent): Unit = {
|
||||||
|
sys.exit(0)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
fileMenu.add(exitMenuItem)
|
||||||
|
|
||||||
|
mainMenuBar.add(fileMenu)
|
||||||
|
|
||||||
|
val helpMenu = new JMenu("Help")
|
||||||
|
val aboutMenuItem = new JMenuItem("About")
|
||||||
|
aboutMenuItem.addActionListener(new ActionListener {
|
||||||
|
def actionPerformed(e: ActionEvent): Unit = {
|
||||||
|
JOptionPane.showMessageDialog(null, "ScalaShop, the ultimate image manipulation tool\nBrought to you by EPFL, 2015")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
helpMenu.add(aboutMenuItem)
|
||||||
|
|
||||||
|
mainMenuBar.add(helpMenu)
|
||||||
|
|
||||||
|
setJMenuBar(mainMenuBar)
|
||||||
|
|
||||||
|
val canvas = new PhotoCanvas
|
||||||
|
|
||||||
|
val scrollPane = new JScrollPane(canvas)
|
||||||
|
|
||||||
|
add(scrollPane, BorderLayout.CENTER)
|
||||||
|
setVisible(true)
|
||||||
|
|
||||||
|
def updateInformationBox(status: String, time: Double): Unit = {
|
||||||
|
info.setText(s"$status\nTime: ${time.toInt} ms.")
|
||||||
|
}
|
||||||
|
|
||||||
|
def getColorCount: Int =
|
||||||
|
colorCountSpinner.getValue.asInstanceOf[Int]
|
||||||
|
|
||||||
|
def getInitialSelectionStrategy: InitialSelectionStrategy =
|
||||||
|
if (randomSamplingButton.isSelected())
|
||||||
|
RandomSampling
|
||||||
|
else if (uniformSamplingButton.isSelected())
|
||||||
|
UniformSampling
|
||||||
|
else
|
||||||
|
UniformChoice
|
||||||
|
|
||||||
|
def getConvergenceStragegy: ConvergenceStrategy =
|
||||||
|
if (stepConvergenceButton.isSelected())
|
||||||
|
ConvergedAfterNSteps(stepCountSpinner.getValue.asInstanceOf[Int])
|
||||||
|
else if (etaConvergenceButton.isSelected())
|
||||||
|
ConvergedAfterMeansAreStill(etaCountSpinner.getValue.asInstanceOf[Double])
|
||||||
|
else
|
||||||
|
ConvergedWhenSNRAbove(snrCountSpinner.getValue.asInstanceOf[Int])
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
|
||||||
|
} catch {
|
||||||
|
case _: Exception => println("Cannot set look and feel, using the default one.")
|
||||||
|
}
|
||||||
|
|
||||||
|
val frame = new ScalaShopFrame
|
||||||
|
|
||||||
|
def main(args: Array[String]): Unit = {
|
||||||
|
frame.repaint()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
41
src/main/scala/kmeans/fun/package.scala
Normal file
41
src/main/scala/kmeans/fun/package.scala
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package kmeans
|
||||||
|
|
||||||
|
package object fun {
|
||||||
|
|
||||||
|
/** The value of every pixel is represented as a 32 bit integer. */
|
||||||
|
type RGBA = Int
|
||||||
|
|
||||||
|
/** Returns the alpha component. */
|
||||||
|
def alpha(c: RGBA): Double = ((0xff000000 & c) >>> 24).toDouble / 256
|
||||||
|
|
||||||
|
/** Returns the red component. */
|
||||||
|
def red(c: RGBA): Double = ((0x00ff0000 & c) >>> 16).toDouble / 256
|
||||||
|
|
||||||
|
/** Returns the green component. */
|
||||||
|
def green(c: RGBA): Double = ((0x0000ff00 & c) >>> 8).toDouble / 256
|
||||||
|
|
||||||
|
/** Returns the blue component. */
|
||||||
|
def blue(c: RGBA): Double = ((0x000000ff & c) >>> 0).toDouble / 256
|
||||||
|
|
||||||
|
/** Used to create an RGBA value from separate components. */
|
||||||
|
def rgba(r: Double, g: Double, b: Double, a: Double): RGBA = {
|
||||||
|
(clamp((a * 256).toInt, 0, 255) << 24) |
|
||||||
|
(clamp((r * 256).toInt, 0, 255) << 16) |
|
||||||
|
(clamp((g * 256).toInt, 0, 255) << 8) |
|
||||||
|
(clamp((b * 256).toInt, 0, 255) << 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Restricts the integer into the specified range. */
|
||||||
|
def clamp(v: Int, min: Int, max: Int): Int = {
|
||||||
|
if (v < min) min
|
||||||
|
else if (v > max) max
|
||||||
|
else v
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Image is a two-dimensional matrix of pixel values. */
|
||||||
|
class Img(val width: Int, val height: Int, private val data: Array[RGBA]) {
|
||||||
|
def this(w: Int, h: Int) = this(w, h, new Array(w * h))
|
||||||
|
def apply(x: Int, y: Int): RGBA = data(y * width + x)
|
||||||
|
def update(x: Int, y: Int, c: RGBA): Unit = data(y * width + x) = c
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,126 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com>
|
|
||||||
*/
|
|
||||||
package actorbintree
|
|
||||||
|
|
||||||
import akka.actor.{ActorRef, ActorSystem, Props, actorRef2Scala, scala2ActorRef}
|
|
||||||
import akka.testkit.{ImplicitSender, TestKit, TestProbe}
|
|
||||||
import org.junit.Test
|
|
||||||
import org.junit.Assert._
|
|
||||||
|
|
||||||
import scala.util.Random
|
|
||||||
import scala.concurrent.duration._
|
|
||||||
|
|
||||||
class BinaryTreeSuite extends TestKit(ActorSystem("BinaryTreeSuite")) with ImplicitSender {
|
|
||||||
|
|
||||||
import actorbintree.BinaryTreeSet._
|
|
||||||
|
|
||||||
def receiveN(requester: TestProbe, ops: Seq[Operation], expectedReplies: Seq[OperationReply]): Unit =
|
|
||||||
requester.within(5.seconds) {
|
|
||||||
val repliesUnsorted = for (i <- 1 to ops.size) yield try {
|
|
||||||
requester.expectMsgType[OperationReply]
|
|
||||||
} catch {
|
|
||||||
case ex: Throwable if ops.size > 10 => sys.error(s"failure to receive confirmation $i/${ops.size}\n$ex")
|
|
||||||
case ex: Throwable => sys.error(s"failure to receive confirmation $i/${ops.size}\nRequests:" + ops.mkString("\n ", "\n ", "") + s"\n$ex")
|
|
||||||
}
|
|
||||||
val replies = repliesUnsorted.sortBy(_.id)
|
|
||||||
if (replies != expectedReplies) {
|
|
||||||
val pairs = (replies zip expectedReplies).zipWithIndex filter (x => x._1._1 != x._1._2)
|
|
||||||
fail("unexpected replies:" + pairs.map(x => s"at index ${x._2}: got ${x._1._1}, expected ${x._1._2}").mkString("\n ", "\n ", ""))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def verify(probe: TestProbe, ops: Seq[Operation], expected: Seq[OperationReply]): Unit = {
|
|
||||||
val topNode = system.actorOf(Props[BinaryTreeSet])
|
|
||||||
|
|
||||||
ops foreach { op =>
|
|
||||||
topNode ! op
|
|
||||||
}
|
|
||||||
|
|
||||||
receiveN(probe, ops, expected)
|
|
||||||
// the grader also verifies that enough actors are created
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test def `proper inserts and lookups (5pts)`(): Unit = {
|
|
||||||
val topNode = system.actorOf(Props[BinaryTreeSet])
|
|
||||||
|
|
||||||
topNode ! Contains(testActor, id = 1, 1)
|
|
||||||
expectMsg(ContainsResult(1, false))
|
|
||||||
|
|
||||||
topNode ! Insert(testActor, id = 2, 1)
|
|
||||||
topNode ! Contains(testActor, id = 3, 1)
|
|
||||||
|
|
||||||
expectMsg(OperationFinished(2))
|
|
||||||
expectMsg(ContainsResult(3, true))
|
|
||||||
()
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test def `instruction example (5pts)`(): Unit = {
|
|
||||||
val requester = TestProbe()
|
|
||||||
val requesterRef = requester.ref
|
|
||||||
val ops = List(
|
|
||||||
Insert(requesterRef, id=100, 1),
|
|
||||||
Contains(requesterRef, id=50, 2),
|
|
||||||
Remove(requesterRef, id=10, 1),
|
|
||||||
Insert(requesterRef, id=20, 2),
|
|
||||||
Contains(requesterRef, id=80, 1),
|
|
||||||
Contains(requesterRef, id=70, 2)
|
|
||||||
)
|
|
||||||
|
|
||||||
val expectedReplies = List(
|
|
||||||
OperationFinished(id=10),
|
|
||||||
OperationFinished(id=20),
|
|
||||||
ContainsResult(id=50, false),
|
|
||||||
ContainsResult(id=70, true),
|
|
||||||
ContainsResult(id=80, false),
|
|
||||||
OperationFinished(id=100)
|
|
||||||
)
|
|
||||||
|
|
||||||
verify(requester, ops, expectedReplies)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test def `behave identically to built-in set (includes GC) (40pts)`(): Unit = {
|
|
||||||
val rnd = new Random()
|
|
||||||
def randomOperations(requester: ActorRef, count: Int): Seq[Operation] = {
|
|
||||||
def randomElement: Int = rnd.nextInt(100)
|
|
||||||
def randomOperation(requester: ActorRef, id: Int): Operation = rnd.nextInt(4) match {
|
|
||||||
case 0 => Insert(requester, id, randomElement)
|
|
||||||
case 1 => Insert(requester, id, randomElement)
|
|
||||||
case 2 => Contains(requester, id, randomElement)
|
|
||||||
case 3 => Remove(requester, id, randomElement)
|
|
||||||
}
|
|
||||||
|
|
||||||
for (seq <- 0 until count) yield randomOperation(requester, seq)
|
|
||||||
}
|
|
||||||
|
|
||||||
def referenceReplies(operations: Seq[Operation]): Seq[OperationReply] = {
|
|
||||||
var referenceSet = Set.empty[Int]
|
|
||||||
def replyFor(op: Operation): OperationReply = op match {
|
|
||||||
case Insert(_, seq, elem) =>
|
|
||||||
referenceSet = referenceSet + elem
|
|
||||||
OperationFinished(seq)
|
|
||||||
case Remove(_, seq, elem) =>
|
|
||||||
referenceSet = referenceSet - elem
|
|
||||||
OperationFinished(seq)
|
|
||||||
case Contains(_, seq, elem) =>
|
|
||||||
ContainsResult(seq, referenceSet(elem))
|
|
||||||
}
|
|
||||||
|
|
||||||
for (op <- operations) yield replyFor(op)
|
|
||||||
}
|
|
||||||
|
|
||||||
val requester = TestProbe()
|
|
||||||
val topNode = system.actorOf(Props[BinaryTreeSet])
|
|
||||||
val count = 1000
|
|
||||||
|
|
||||||
val ops = randomOperations(requester.ref, count)
|
|
||||||
val expectedReplies = referenceReplies(ops)
|
|
||||||
|
|
||||||
ops foreach { op =>
|
|
||||||
topNode ! op
|
|
||||||
if (rnd.nextDouble() < 0.1) topNode ! GC
|
|
||||||
}
|
|
||||||
receiveN(requester, ops, expectedReplies)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
28
src/test/scala/kmeans/KMeansSuite.scala
Normal file
28
src/test/scala/kmeans/KMeansSuite.scala
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package kmeans
|
||||||
|
|
||||||
|
import java.util.concurrent._
|
||||||
|
import scala.collection.{mutable, Map, Seq}
|
||||||
|
import scala.collection.parallel.{ParMap, ParSeq}
|
||||||
|
import scala.collection.parallel.CollectionConverters._
|
||||||
|
import scala.math._
|
||||||
|
import org.junit._
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
|
||||||
|
class KMeansSuite {
|
||||||
|
object KM extends KMeans
|
||||||
|
import KM._
|
||||||
|
|
||||||
|
def checkParClassify(points: ParSeq[Point], means: ParSeq[Point], expected: ParMap[Point, ParSeq[Point]]): Unit = {
|
||||||
|
assertEquals(s"classify($points, $means) should equal to $expected", expected, classify(points, means))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test def `'classify' should work for empty 'points' and empty 'means'`: Unit = {
|
||||||
|
val points: ParSeq[Point] = IndexedSeq().par
|
||||||
|
val means: ParSeq[Point] = IndexedSeq().par
|
||||||
|
val expected = ParMap[Point, ParSeq[Point]]()
|
||||||
|
checkParClassify(points, means, expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user