epsilon is now calculated

This commit is contained in:
Rohan Sircar 2020-04-12 14:01:20 +05:30
parent 0c39eed5d9
commit 872ab85c74
3 changed files with 36 additions and 18 deletions

View File

@ -16,12 +16,12 @@ class HHCSim(
customers
.flatMap(checkEmpty)
.map(Util.formAdjMatrix)
.tap(_ => logger.debug("Edge matrix: "))
.tap(logGraph)
.map(edges => Util.mstUsingPrims(edges))
.tap(_ => logger.debug("MST: "))
.tap(logGraph)
.map(mst => Util.findCentroids(mst))
.tap(logMST)
.map(
e => Util.findCentroids(mst = e._1, epsilon = e._2)
)
.tap(logCentroids)
.tap(logRemovedEdges)
.map(
@ -48,16 +48,30 @@ class HHCSim(
case _ => Right(customers)
}
def printGraph(graph: Array[Array[Double]]) = graph.foreach { e =>
e.foreach { d =>
print(f"$d%.2f, ")
}
println
}
def logGraph(it: Either[String, Array[Array[Double]]]): Unit =
it.map(
mst => {
graph => {
logger.whenDebugEnabled {
mst.foreach { e =>
e.foreach { d =>
print(f"$d%.2f, ")
}
println
}
println("Graph: ")
printGraph(graph)
}
}
)
def logMST(it: Either[String, (Array[Array[Double]], Double)]): Unit =
it.map(
t => {
logger.whenDebugEnabled {
println(s"Epsilon = ${t._2}")
println("MST: ")
printGraph(graph = t._1)
}
}
)

View File

@ -102,7 +102,8 @@ object Main {
e.toArray
}).toArray
val mst = Util.mstUsingPrims(edges)
val (mst,eps) = Util.mstUsingPrims(edges)
println(s"Epsilon: $eps")
// mst
// 0, 9 , 0 , 0 , 0
@ -168,7 +169,7 @@ object Main {
// 0, 0, 51, 0 , 31
// 0, 0, 0 , 0 , 0
val (mst2, centr, removed) = Util.findCentroids(mst)
val (mst2, centr, removed) = Util.findCentroids(mst, eps)
// val (centr2, eds2) = Util.findCentroids(edges2)
println()

View File

@ -93,7 +93,7 @@ object Util extends LazyLogging {
def mstUsingPrims[T: ClassTag](
edges: Array[Array[T]]
)(implicit num: Numeric[T]): Array[Array[T]] = {
)(implicit num: Numeric[T]): (Array[Array[T]], T) = {
val n = edges.length
val selected: ArrayBuffer[Boolean] = ArrayBuffer.fill(n)(false)
@ -101,6 +101,8 @@ object Util extends LazyLogging {
val mst: Array[Array[T]] = Array.ofDim[T](n, n)
var sum = 0
for (_ <- 0 until n - 1) {
var min = 999999
var x = 0
@ -121,10 +123,10 @@ object Util extends LazyLogging {
// println(s"Edge selected $x - $y : ${edges(x)(y)}")
mst(x)(y) = edges(x)(y)
mst(y)(x) = edges(x)(y)
sum += num.toInt(edges(x)(y))
selected(y) = true
}
mst
(mst, num.fromInt(sum / n))
}
def findClusters[T](
@ -166,7 +168,8 @@ object Util extends LazyLogging {
}
def findCentroids[T](
mst: Array[Array[T]]
mst: Array[Array[T]],
epsilon: T
)(
implicit ev: Numeric[T]
): (Array[Array[T]], IndexedSeq[Int], IndexedSeq[(Int, Int, T)]) = {
@ -175,7 +178,7 @@ object Util extends LazyLogging {
val removed: ArrayBuffer[(Int, Int, T)] = ArrayBuffer.empty
for (i <- 0 until n) {
for (j <- 0 until n) {
if (ev.gt(mst(i)(j), ev.fromInt(20)) && mst(i)(j) != 0) {
if (ev.gt(mst(i)(j), epsilon) && mst(i)(j) != 0) {
// println(s" $i $j = ${mst(i)(j)}")
centroids += i
centroids += j