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.

114 lines
2.7 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
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(logGraph)
  20. .map(edges => Util.mstUsingPrims(edges))
  21. .tap(logMST)
  22. .map(
  23. e => Util.findCentroids(mst = e._1, epsilon = e._2)
  24. )
  25. .tap(logCentroids)
  26. .tap(logRemovedEdges)
  27. .map(
  28. e =>
  29. e match {
  30. case (mst, centroids, removed) => {
  31. printGraph(mst)
  32. Util.findClusters(mst, centroids, removed)
  33. }
  34. }
  35. )
  36. .tap(logClusters)
  37. .map(
  38. e =>
  39. e match {
  40. case (centroids, clusters, removed) =>
  41. Util.groupClusters(centroids, clusters, removed)
  42. }
  43. )
  44. def checkEmpty(
  45. customers: IndexedSeq[Customer]
  46. ): Either[String, IndexedSeq[Customer]] =
  47. customers.length match {
  48. case 0 => Left("Error input was empty")
  49. case _ => Right(customers)
  50. }
  51. def printGraph(graph: Array[Array[Double]]) = graph.foreach { e =>
  52. e.foreach { d =>
  53. print(f"$d%.2f, ")
  54. }
  55. println
  56. }
  57. def logGraph(it: Either[String, Array[Array[Double]]]): Unit =
  58. it.map(
  59. graph => {
  60. logger.whenDebugEnabled {
  61. println("Graph: ")
  62. printGraph(graph)
  63. }
  64. }
  65. )
  66. def logMST(it: Either[String, (Array[Array[Double]], Double)]): Unit =
  67. it.map(
  68. t => {
  69. logger.whenDebugEnabled {
  70. println(s"Epsilon = ${t._2}")
  71. println("MST: ")
  72. printGraph(graph = t._1)
  73. }
  74. }
  75. )
  76. def logCentroids(
  77. e: Either[
  78. String,
  79. (Array[Array[Double]], IndexedSeq[Int], IndexedSeq[(Int, Int, Double)])
  80. ]
  81. ) = e.map(f => logger.debug(s"\nCentroids: \n ${f._2.toString}"))
  82. def logRemovedEdges(
  83. e: Either[
  84. String,
  85. (Array[Array[Double]], IndexedSeq[Int], IndexedSeq[(Int, Int, Double)])
  86. ]
  87. ) =
  88. e.map(
  89. f =>
  90. logger.whenDebugEnabled {
  91. println(s"Removed Edges: \n${f._3.toString}")
  92. }
  93. )
  94. def logClusters(
  95. e: Either[
  96. String,
  97. (
  98. IndexedSeq[Int],
  99. Map[Int, mutable.ArrayBuffer[Int]],
  100. IndexedSeq[(Int, Int, Double)]
  101. )
  102. ]
  103. ) =
  104. e.map(
  105. f => logger.whenDebugEnabled { println(s"Clusters: \n${f._2.toString}") }
  106. )
  107. }