Advertisement
Guest User

Untitled

a guest
May 27th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. import Main.{allNodes, board, width, world}
  2.  
  3. import scala.collection.mutable
  4. import scala.io.Source
  5.  
  6.  
  7. object Main extends App {
  8.  
  9. val filename = "C:\\Users\\Sheenah\\IdeaProjects\\untitled3\\TrainingSets\\6.txt"
  10. val lines = Source.fromFile(filename).getLines.toList
  11. val height = lines(0).trim().toInt
  12. val width = lines(1).trim().toInt
  13. val board = lines.drop(2).flatten.filterNot((x: Char) => x.isWhitespace)
  14.  
  15. println(board)
  16. val allNodes = for {node <- board.zipWithIndex } yield
  17. if (node._1 == 'x') WorldNode(node._2%width, node._2/width.floor.toInt, true)
  18. else WorldNode(node._2%width, node._2/width.floor.toInt, false)
  19. println(allNodes)
  20.  
  21. val world = new World(width, height, board)
  22.  
  23. // allNodes.foreach(x => println(x.x.toString + x.y.toString + x.isWall.toString))
  24.  
  25.  
  26. println("START: " + world.start)
  27. doEverything(new mutable.PriorityQueue[WorldNode]() ++= world.getNeighbours(world.start), Set(world.start), 1, Map.empty)
  28. // def worldNodePriority(node: WorldNode) = -node.totalCost
  29. // def Astar(world: World): Unit = {
  30. //
  31. //
  32. // }
  33. def doEverything(openList: mutable.PriorityQueue[WorldNode], closedList: Set[WorldNode], currentGScore: Int, path: Map[WorldNode, WorldNode]): Unit = {
  34. println("*****************")
  35. println("*****************")
  36. println("EXPANSION NUMBER: " + currentGScore)
  37. // println("OPEN LIST: " + openList)
  38. openList.foreach(_.update(currentGScore))
  39. println("OPEN LIST START")
  40. openList.foreach(x => println(x + " " + x.distanceToGoal.toString + " " + x.distanceFromStart.toString + " " + x.totalCost.toString))
  41. println(openList.clone().dequeueAll)
  42. println("OPEN LIST END")
  43. println("OPEN LIST MAX")
  44. println(openList.max)
  45. // println("CLOSED LIST: " + closedList)
  46. if(openList.isEmpty) {
  47. println("NO PATH TO GOAL")
  48. return
  49. }
  50. val currentNode = openList.dequeue()
  51. println("CURRENT NODE:" + currentNode)
  52. println("Distance to goal: " + currentNode.distanceToGoal.toString)
  53. println("Distance from start: " + currentNode.distanceFromStart.toString)
  54. println("Total cost: " + currentNode.totalCost.toString)
  55. if(currentNode == world.goal) println("SUCCESS")
  56. else {
  57. val newElements = world.getNeighbours(currentNode) diff closedList
  58. doEverything(new mutable.PriorityQueue[WorldNode]() ++= (openList.toSet ++ newElements),
  59. closedList + currentNode,
  60. currentGScore+1,
  61. path)
  62. }
  63. }
  64. // val foo = WorldNode(1,0,false)
  65. // foo.totalCost = 10
  66. // val bar = WorldNode(0,1 ,false)
  67. // bar.totalCost = 20
  68. // println("foo(10) compare bar(20)")
  69. // println(foo compare bar)
  70. // println("bar(20) compare foo(10)")
  71. // println(bar compare foo)
  72. // println("TEST: " + test)
  73. // else if (openList.isEmpty) println("NO PATH TO GOAL")
  74. // else {
  75. // }
  76.  
  77. // openList.foreach(println)
  78. // openList.foreach(x => println(x.distanceToGoal))
  79. // println(openList.head)
  80. //
  81. // val openList2 = world.getNeighbours(openList.head) ::: openList
  82. //
  83. // for (node <- openList2) {
  84. // node.distanceToGoal = Math.abs(world.goal.x - node.x) + Math.abs(world.goal.y - node.y)
  85. // }
  86. // val heap = new mutable.PriorityQueue[WorldNode]()
  87. //
  88. // for(node <- openList2) {
  89. // heap.enqueue(node)
  90. // }
  91. // for (node <- heap) {
  92. // println(node + node.distanceToGoal.toString)
  93. // }
  94. // val dxstart = start.x - goal.x
  95. // val dystart = start.y - goal.y
  96. //
  97. // println(start)
  98. // val dx1 = foo1.x - goal.x
  99. // val dy1 = foo1.y - goal.y
  100. // val cross1 = Math.abs(dx1*dystart - dxstart*dy1)
  101. // println(cross1)
  102. // val dx2 = foo2.x - goal.x
  103. // val dy2 = foo2.y - goal.y
  104. // val cross2 = Math.abs(dx1*dystart - dxstart*dy1)
  105. // println(cross2)
  106. }
  107.  
  108.  
  109. class World(width: Int, height: Int, board: List[Char]) {
  110.  
  111. val start: WorldNode = WorldNode(board.indexOf('s')%width, board.indexOf('s')/width.floor.toInt, false)
  112. val goal: WorldNode = WorldNode(board.indexOf('g')%width, board.indexOf('g')/width.floor.toInt, false)
  113.  
  114. val nodes: Set[WorldNode] = (for {node <- board.zipWithIndex } yield
  115. if (node._1 == 'x') WorldNode(node._2%width, node._2/width.floor.toInt, true)
  116. else WorldNode(node._2%width, node._2/width.floor.toInt, false)).toSet
  117.  
  118. for (node <- nodes) {
  119. node.distanceToGoal = Math.abs(goal.x - node.x) + Math.abs(goal.y - node.y)
  120. }
  121.  
  122. def getNeighbours(node: WorldNode): Set[WorldNode] = {
  123. nodes.intersect(Set(WorldNode(node.x-1,node.y, false),
  124. WorldNode(node.x,node.y-1, false),
  125. WorldNode(node.x+1,node.y, false),
  126. WorldNode(node.x,node.y+1, false)))
  127. }
  128.  
  129. def heuristic(): Unit ={
  130.  
  131. }
  132.  
  133. }
  134.  
  135. case class WorldNode(x: Int, y: Int, isWall: Boolean) extends Ordered[WorldNode] {
  136. var distanceFromStart: Int = 0 //g
  137. var distanceToGoal: Int = 0 //h
  138.  
  139. var totalCost = 0 //f
  140.  
  141. def compare(that: WorldNode):Int = this.totalCost compare that.totalCost
  142.  
  143. def update(newDistanceFromStart: Int): Unit = {
  144. if(distanceFromStart == 0) distanceFromStart = newDistanceFromStart
  145. totalCost = distanceFromStart + distanceToGoal
  146. }
  147.  
  148. // def heuristic(): Int = {
  149. // distanceToGoal +
  150. // }
  151. // def heuristic()
  152. // def calculateEuclideanDistance(target: WorldNode): Unit = {
  153. // val dx1 = this.x - target.x
  154. // val dy1 = this.y - target.y
  155. //
  156. // }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement