You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.4 KiB
98 lines
2.4 KiB
import model.Customer
|
|
import scala.util.chaining._
|
|
import util._
|
|
import com.typesafe.scalalogging.LazyLogging
|
|
import scala.collection.mutable
|
|
class HHCSim(
|
|
private val epsilonMax: Int,
|
|
private val iterations: Int,
|
|
// private val customers: IndexedSeq[Customer],
|
|
private val WLDMax: Float
|
|
) extends LazyLogging {
|
|
// private val graph: Array[Array[T]]
|
|
def go(
|
|
customers: Either[String, IndexedSeq[Customer]]
|
|
): String Either Map[Int, IndexedSeq[(Int, Double)]] =
|
|
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(logCentroids)
|
|
.tap(logRemovedEdges)
|
|
.map(
|
|
e =>
|
|
e match {
|
|
case (mst, centroids, removed) =>
|
|
Util.findClusters(mst, centroids, removed)
|
|
}
|
|
)
|
|
.tap(logClusters)
|
|
.map(
|
|
e =>
|
|
e match {
|
|
case (centroids, clusters, removed) =>
|
|
Util.groupClusters(centroids, clusters, removed)
|
|
}
|
|
)
|
|
|
|
def checkEmpty(
|
|
customers: IndexedSeq[Customer]
|
|
): Either[String, IndexedSeq[Customer]] =
|
|
customers.length match {
|
|
case 0 => Left("Error input was empty")
|
|
case _ => Right(customers)
|
|
}
|
|
|
|
def logGraph(it: Either[String, Array[Array[Double]]]): Unit =
|
|
it.map(
|
|
mst => {
|
|
logger.whenDebugEnabled {
|
|
mst.foreach { e =>
|
|
e.foreach { d =>
|
|
print(f"$d%.2f, ")
|
|
}
|
|
println
|
|
}
|
|
}
|
|
}
|
|
)
|
|
|
|
def logCentroids(
|
|
e: Either[
|
|
String,
|
|
(Array[Array[Double]], IndexedSeq[Int], IndexedSeq[(Int, Int, Double)])
|
|
]
|
|
) = e.map(f => logger.debug(s"\nCentroids: \n ${f._2.toString}"))
|
|
|
|
def logRemovedEdges(
|
|
e: Either[
|
|
String,
|
|
(Array[Array[Double]], IndexedSeq[Int], IndexedSeq[(Int, Int, Double)])
|
|
]
|
|
) =
|
|
e.map(
|
|
f =>
|
|
logger.whenDebugEnabled {
|
|
println(s"Removed Edges: \n${f._3.toString}")
|
|
}
|
|
)
|
|
|
|
def logClusters(
|
|
e: Either[
|
|
String,
|
|
(
|
|
IndexedSeq[Int],
|
|
Map[Int, mutable.ArrayBuffer[Int]],
|
|
IndexedSeq[(Int, Int, Double)]
|
|
)
|
|
]
|
|
) =
|
|
e.map(
|
|
f => logger.whenDebugEnabled { println(s"Clusters: \n${f._2.toString}") }
|
|
)
|
|
}
|