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.

239 lines
5.6 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
4 years ago
  1. import model.Coord
  2. import model.Customer
  3. import model.HHCEdge
  4. import scala.collection.mutable.ArrayBuffer
  5. import util.Util
  6. import config.Conf
  7. import java.io.File
  8. import scala.io.Source
  9. object Main {
  10. def main(args: Array[String]): Unit = {
  11. val conf = new Conf(args.toIndexedSeq)
  12. val bufferedSource = Source.fromFile(conf.infile())
  13. val customers = Util.getCustomers(bufferedSource)
  14. bufferedSource.close()
  15. val hhc = new HHCSim(epsilonMax = 40, iterations = 10, WLDMax = 10)
  16. val adjList2 =
  17. customers
  18. .map(Util.formAdjMatrix)
  19. .map(e => Util.mstUsingPrims2(e))
  20. .map(_._1)
  21. // .map(e => Util.makeAdjacencyList2(e._1))
  22. adjList2.map(adjl => {
  23. for (i <- 0 until adjl.size) {
  24. print(s"$i -> ")
  25. adjl(i).foreach(e => {
  26. print(f"(${e._1}, ${e._2}%.2f), ")
  27. })
  28. println
  29. }
  30. })
  31. // val fnl2 = hhc.go(customers)
  32. // customers.map(println(_))
  33. // val edges3 = customers.map(Util.formAdjMatrix)
  34. // val fnl2 = go(customers)
  35. // fnl2 match {
  36. // case Left(value) => println(value)
  37. // case Right(groups) =>
  38. // groups.foreach(c => {
  39. // print(s"${c._1} -> ")
  40. // c._2.foreach(e => {
  41. // print(f"(${e._1}%d, ${e._2}%.2f), ")
  42. // })
  43. // println
  44. // })
  45. // }
  46. // edges3 match {
  47. // case Right(e) =>
  48. // e.foreach { d =>
  49. // d.foreach(g => print(f"$g%.2f, "))
  50. // println()
  51. // }
  52. // case Left(e) =>
  53. // println(s"Oops, an error occured. The error message was: $e")
  54. // }
  55. val coord1 = Coord(3, 4)
  56. println(s"Distance from origin = ${coord1.distance}")
  57. val cust1 = Customer(coord1, 5)
  58. val cust2 = Customer(Coord(2, 5), 7)
  59. println(s"Customer 1 = ${cust1}")
  60. println(s"Customer 2 = ${cust2}")
  61. val cust3 = Customer(Coord(3, 5), 6)
  62. val cust4 = Customer(Coord(6, 8), 9)
  63. val cust5 = Customer(Coord(5, 4), 6)
  64. val edge1 = HHCEdge(cust1, cust2)
  65. val edge2 = HHCEdge(cust3, cust4)
  66. println(s"Edge 1 = ${edge1}")
  67. println(s"Edge 1 weight = ${edge1.weight}")
  68. val V = Array(cust1, cust2, cust3, cust4, cust5)
  69. val E = Array(edge1, edge2)
  70. V.foreach(println(_))
  71. // prints
  72. // Customer(Coord(3, 4), 5)
  73. // Customer(Coord(2, 5), 7)
  74. // Customer(Coord(3, 5), 6)
  75. // Customer(Coord(6, 8), 9)
  76. // Customer(Coord(5, 4), 6)
  77. E.foreach(println(_))
  78. //prints
  79. // HHCEdge(Customer(Coord(3, 4), 5), Customer(Coord(2, 5), 7))
  80. // HHCEdge(Customer(Coord(3, 5), 6), Customer(Coord(6, 8), 9))
  81. // sample adjacency
  82. // format: off
  83. val edgesSeq = Seq(
  84. Seq(0 , 9 , 75, 0 , 0),
  85. Seq(9 , 0 , 95, 19, 42),
  86. Seq(75, 95, 0 , 51, 66),
  87. Seq(0 , 19, 51, 0 , 31),
  88. Seq(0 , 42, 66, 31, 0)
  89. );
  90. // adjacency list form
  91. // 0 -> 1 -> 2
  92. // 1 -> 0 -> 2 -> 3 -> 4
  93. // 2 -> 0 -> 1 -> 3 -> 4
  94. // 3 -> 1 -> 2 -> 4
  95. // 4 -> 1 -> 2 -> 3
  96. val edges = edgesSeq.map(e => {
  97. e.toArray
  98. }).toArray
  99. val (mst,eps) = Util.mstUsingPrims(edges)
  100. println(s"Epsilon: $eps")
  101. // mst
  102. // 0, 9 , 0 , 0 , 0
  103. // 9, 0 , 0 , 19, 0
  104. // 0, 0 , 0 , 51, 0
  105. // 0, 19, 51, 0 , 31
  106. // 0, 0 , 0 , 31, 0
  107. // format: on
  108. // Prim's algorithm
  109. // Prim's algorithm result
  110. // Edge selected 0 - 1: 9
  111. // Edge selected 1 - 3: 19
  112. // Edge selected 3 - 4: 31
  113. // Edge selected 3 - 2: 51
  114. // Verify the result with the one at https://www.programiz.com/dsa/prim-algorithm
  115. val edges2: Array[Array[Double]] = Array.ofDim(5, 5)
  116. // create adjacency matrix from given customers
  117. for (i <- 0 to 4) {
  118. for (j <- 0 to 4) {
  119. edges2(i)(j) = Util.getHaversineDistance(V(i).location, V(j).location)
  120. }
  121. }
  122. edges2.foreach { e =>
  123. e.foreach { d =>
  124. print(f"$d%.2f, ")
  125. }
  126. println()
  127. }
  128. // prints
  129. // 0.00 , 159.64, 112.79, 563.55, 225.88
  130. // 159.64, 0.00 , 112.94, 564.16, 357.08
  131. // 112.79, 112.94, 0.00 , 478.40, 252.42
  132. // 563.55, 564.16, 478.40, 0.00 , 463.64
  133. // 225.88, 357.08, 252.42, 463.64, 0.00
  134. println()
  135. println("Initial graph:")
  136. edgesSeq.foreach { e =>
  137. e.foreach { d =>
  138. print(s"$d, ")
  139. }
  140. println()
  141. }
  142. println("MST: ")
  143. mst.foreach { e =>
  144. e.foreach { d =>
  145. print(s"$d, ")
  146. }
  147. println()
  148. }
  149. // 0, 9, 0 , 0 , 0
  150. // 0, 0, 0 , 19, 0
  151. // 0, 0, 0 , 0 , 0
  152. // 0, 0, 51, 0 , 31
  153. // 0, 0, 0 , 0 , 0
  154. val (mst2, centr, removed) = Util.findCentroids(mst, eps)
  155. // val (centr2, eds2) = Util.findCentroids(edges2)
  156. println()
  157. println(s"Centroids: \n$centr")
  158. println(s"Removed: \n$removed")
  159. val (_, clust, _) = Util.findClusters(mst, centr, removed)
  160. val adjList = Util.makeAdjacencyList(edges, centr)
  161. println(s"Clusters:")
  162. clust.foreach(c => {
  163. val (e, d) = c
  164. print(s"$e: ")
  165. d.foreach(f => {
  166. print(s"-> $f ")
  167. })
  168. println()
  169. })
  170. println()
  171. val fnl = Util.groupClusters(centr, clust, removed)
  172. println(s"Final cluster groups: \n$fnl")
  173. // Output
  174. //
  175. // Initial graph:
  176. // 0, 9, 75, 0, 0,
  177. // 9, 0, 95, 19, 42,
  178. // 75, 95, 0, 51, 66,
  179. // 0, 19, 51, 0, 31,
  180. // 0, 42, 66, 31, 0,
  181. // MST:
  182. // 0, 9, 0, 0, 0,
  183. // 9, 0, 0, 19, 0,
  184. // 0, 0, 0, 51, 0,
  185. // 0, 19, 51, 0, 31,
  186. // 0, 0, 0, 31, 0,
  187. // Centroids:
  188. // Vector(2, 3, 4)
  189. // Removed:
  190. // Vector((2,3,51), (3,2,51), (3,4,31), (4,3,31))
  191. // Clusters:
  192. // 2: -> 2
  193. // 3: -> 3 -> 1 -> 0
  194. // 4: -> 4
  195. // Final cluster groups:
  196. // Map(2 -> Vector((3,51)), 3 -> Vector((4,31), (2,51)), 4 -> Vector((3,31)))
  197. }
  198. }