epsilon is now calculated
This commit is contained in:
parent
0c39eed5d9
commit
872ab85c74
@ -16,12 +16,12 @@ class HHCSim(
|
|||||||
customers
|
customers
|
||||||
.flatMap(checkEmpty)
|
.flatMap(checkEmpty)
|
||||||
.map(Util.formAdjMatrix)
|
.map(Util.formAdjMatrix)
|
||||||
.tap(_ => logger.debug("Edge matrix: "))
|
|
||||||
.tap(logGraph)
|
.tap(logGraph)
|
||||||
.map(edges => Util.mstUsingPrims(edges))
|
.map(edges => Util.mstUsingPrims(edges))
|
||||||
.tap(_ => logger.debug("MST: "))
|
.tap(logMST)
|
||||||
.tap(logGraph)
|
.map(
|
||||||
.map(mst => Util.findCentroids(mst))
|
e => Util.findCentroids(mst = e._1, epsilon = e._2)
|
||||||
|
)
|
||||||
.tap(logCentroids)
|
.tap(logCentroids)
|
||||||
.tap(logRemovedEdges)
|
.tap(logRemovedEdges)
|
||||||
.map(
|
.map(
|
||||||
@ -48,16 +48,30 @@ class HHCSim(
|
|||||||
case _ => Right(customers)
|
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 =
|
def logGraph(it: Either[String, Array[Array[Double]]]): Unit =
|
||||||
it.map(
|
it.map(
|
||||||
mst => {
|
graph => {
|
||||||
logger.whenDebugEnabled {
|
logger.whenDebugEnabled {
|
||||||
mst.foreach { e =>
|
println("Graph: ")
|
||||||
e.foreach { d =>
|
printGraph(graph)
|
||||||
print(f"$d%.2f, ")
|
}
|
||||||
}
|
}
|
||||||
println
|
)
|
||||||
}
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -102,7 +102,8 @@ object Main {
|
|||||||
e.toArray
|
e.toArray
|
||||||
}).toArray
|
}).toArray
|
||||||
|
|
||||||
val mst = Util.mstUsingPrims(edges)
|
val (mst,eps) = Util.mstUsingPrims(edges)
|
||||||
|
println(s"Epsilon: $eps")
|
||||||
|
|
||||||
// mst
|
// mst
|
||||||
// 0, 9 , 0 , 0 , 0
|
// 0, 9 , 0 , 0 , 0
|
||||||
@ -168,7 +169,7 @@ object Main {
|
|||||||
// 0, 0, 51, 0 , 31
|
// 0, 0, 51, 0 , 31
|
||||||
// 0, 0, 0 , 0 , 0
|
// 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)
|
// val (centr2, eds2) = Util.findCentroids(edges2)
|
||||||
|
|
||||||
println()
|
println()
|
||||||
|
@ -93,7 +93,7 @@ object Util extends LazyLogging {
|
|||||||
|
|
||||||
def mstUsingPrims[T: ClassTag](
|
def mstUsingPrims[T: ClassTag](
|
||||||
edges: Array[Array[T]]
|
edges: Array[Array[T]]
|
||||||
)(implicit num: Numeric[T]): Array[Array[T]] = {
|
)(implicit num: Numeric[T]): (Array[Array[T]], T) = {
|
||||||
val n = edges.length
|
val n = edges.length
|
||||||
val selected: ArrayBuffer[Boolean] = ArrayBuffer.fill(n)(false)
|
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)
|
val mst: Array[Array[T]] = Array.ofDim[T](n, n)
|
||||||
|
|
||||||
|
var sum = 0
|
||||||
|
|
||||||
for (_ <- 0 until n - 1) {
|
for (_ <- 0 until n - 1) {
|
||||||
var min = 999999
|
var min = 999999
|
||||||
var x = 0
|
var x = 0
|
||||||
@ -121,10 +123,10 @@ object Util extends LazyLogging {
|
|||||||
// println(s"Edge selected $x - $y : ${edges(x)(y)}")
|
// println(s"Edge selected $x - $y : ${edges(x)(y)}")
|
||||||
mst(x)(y) = edges(x)(y)
|
mst(x)(y) = edges(x)(y)
|
||||||
mst(y)(x) = edges(x)(y)
|
mst(y)(x) = edges(x)(y)
|
||||||
|
sum += num.toInt(edges(x)(y))
|
||||||
selected(y) = true
|
selected(y) = true
|
||||||
}
|
}
|
||||||
mst
|
(mst, num.fromInt(sum / n))
|
||||||
}
|
}
|
||||||
|
|
||||||
def findClusters[T](
|
def findClusters[T](
|
||||||
@ -166,7 +168,8 @@ object Util extends LazyLogging {
|
|||||||
}
|
}
|
||||||
|
|
||||||
def findCentroids[T](
|
def findCentroids[T](
|
||||||
mst: Array[Array[T]]
|
mst: Array[Array[T]],
|
||||||
|
epsilon: T
|
||||||
)(
|
)(
|
||||||
implicit ev: Numeric[T]
|
implicit ev: Numeric[T]
|
||||||
): (Array[Array[T]], IndexedSeq[Int], IndexedSeq[(Int, Int, 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
|
val removed: ArrayBuffer[(Int, Int, T)] = ArrayBuffer.empty
|
||||||
for (i <- 0 until n) {
|
for (i <- 0 until n) {
|
||||||
for (j <- 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)}")
|
// println(s" $i $j = ${mst(i)(j)}")
|
||||||
centroids += i
|
centroids += i
|
||||||
centroids += j
|
centroids += j
|
||||||
|
Loading…
Reference in New Issue
Block a user