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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. import model.Customer
  2. import scala.util.chaining._
  3. import util._
  4. import com.typesafe.scalalogging.LazyLogging
  5. import scala.collection.mutable
  6. class HHCSim(
  7. private val epsilonMax: Int,
  8. private val iterations: Int,
  9. // private val customers: IndexedSeq[Customer],
  10. private val WLDMax: Float
  11. ) extends LazyLogging {
  12. // private val graph: Array[Array[T]]
  13. def go(
  14. customers: Either[String, IndexedSeq[Customer]]
  15. ): String Either Map[Int, IndexedSeq[(Int, Double)]] =
  16. customers
  17. .flatMap(checkEmpty)
  18. .map(Util.formAdjMatrix)
  19. .tap(_ => logger.debug("Edge matrix: "))
  20. .tap(logGraph)
  21. .map(edges => Util.mstUsingPrims(edges))
  22. .tap(_ => logger.debug("MST: "))
  23. .tap(logGraph)
  24. .map(mst => Util.findCentroids(mst))
  25. .tap(logCentroids)
  26. .tap(logRemovedEdges)
  27. .map(
  28. e =>
  29. e match {
  30. case (mst, centroids, removed) =>
  31. Util.findClusters(mst, centroids, removed)
  32. }
  33. )
  34. .tap(logClusters)
  35. .map(
  36. e =>
  37. e match {
  38. case (centroids, clusters, removed) =>
  39. Util.groupClusters(centroids, clusters, removed)
  40. }
  41. )
  42. def checkEmpty(
  43. customers: IndexedSeq[Customer]
  44. ): Either[String, IndexedSeq[Customer]] =
  45. customers.length match {
  46. case 0 => Left("Error input was empty")
  47. case _ => Right(customers)
  48. }
  49. def logGraph(it: Either[String, Array[Array[Double]]]): Unit =
  50. it.map(
  51. mst => {
  52. logger.whenDebugEnabled {
  53. mst.foreach { e =>
  54. e.foreach { d =>
  55. print(f"$d%.2f, ")
  56. }
  57. println
  58. }
  59. }
  60. }
  61. )
  62. def logCentroids(
  63. e: Either[
  64. String,
  65. (Array[Array[Double]], IndexedSeq[Int], IndexedSeq[(Int, Int, Double)])
  66. ]
  67. ) = e.map(f => logger.debug(s"\nCentroids: \n ${f._2.toString}"))
  68. def logRemovedEdges(
  69. e: Either[
  70. String,
  71. (Array[Array[Double]], IndexedSeq[Int], IndexedSeq[(Int, Int, Double)])
  72. ]
  73. ) =
  74. e.map(
  75. f =>
  76. logger.whenDebugEnabled {
  77. println(s"Removed Edges: \n${f._3.toString}")
  78. }
  79. )
  80. def logClusters(
  81. e: Either[
  82. String,
  83. (
  84. IndexedSeq[Int],
  85. Map[Int, mutable.ArrayBuffer[Int]],
  86. IndexedSeq[(Int, Int, Double)]
  87. )
  88. ]
  89. ) =
  90. e.map(
  91. f => logger.whenDebugEnabled { println(s"Clusters: \n${f._2.toString}") }
  92. )
  93. }