Advertisement
Guest User

Untitled

a guest
Jun 14th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.39 KB | None | 0 0
  1.  
  2. /**
  3.  * User: Martin
  4.  * Date: 06.06.2014
  5.  * Time: 14:59
  6.  */
  7. case class Edge(from: Int, to: Int, weight: Double) extends Ordered[Edge] {
  8.   override def compare(that: Edge): Int = weight.compareTo(that.weight)
  9.  
  10.   def reverse = Edge(to = from, from = to, weight = weight)
  11. }
  12.  
  13.  
  14. class NNHeuristic(val allLocations: List[Location], val constraints: List[PrecedenceConstraint]) {
  15.   def makeCompleteGraph(ls: List[Location]) = {
  16.     for (x <- ls; y <- ls; if x.getCityId != y.getCityId) yield Edge(x.getCityId, y.getCityId, x.distanceTo(y))
  17.   }
  18.  
  19.   def findFailingConstraints(tour: Seq[Location]): Seq[PrecedenceConstraint] = {
  20.     for {pc <- this.constraints
  21.          first = pc.getFirst
  22.          second = pc.getSecond
  23.          i = tour.indexWhere(_.getCityId == first)
  24.          j = tour.indexWhere(_.getCityId == second)
  25.          if i != -1 && j != -1 && i >= j} yield new PrecedenceConstraint(first, second)
  26.   }
  27.  
  28.   def nearestNeighborTour(tour: Vector[Location], edgesNotTaken: Set[Edge], relevantLocations: Map[Int, Location]): Vector[Location] = {
  29.     if (tour.containsSlice(relevantLocations.values.toSeq)) {
  30.       tour
  31.     }
  32.  
  33.     else {
  34.       val lastId = tour.head.getCityId
  35.       val nextEdgeCandidates = edgesNotTaken.filter(edge => edge.from == lastId)
  36.       val nextEdge = nextEdgeCandidates.filter(edge => relevantLocations.keySet.contains(edge.to)).toSeq.sorted.head
  37.       val nextNode = relevantLocations(nextEdge.to)
  38.       val newTour = tour :+ nextNode
  39.       val failingConstraints = findFailingConstraints(newTour)
  40.  
  41.       if (failingConstraints.size == 0) {
  42.         nearestNeighborTour(newTour, edgesNotTaken - nextEdge, relevantLocations - nextNode.getCityId)
  43.       }
  44.       else {
  45.         val pc = failingConstraints.head
  46.         val firstIndex = newTour.indexWhere(_.getCityId == pc.getFirst)
  47.         val secondIndex = newTour.indexWhere(_.getCityId == pc.getSecond)
  48.         val tourSwapped = newTour.updated(firstIndex, allLocations(pc.getSecond)).updated(secondIndex, allLocations(pc.getFirst))
  49.         nearestNeighborTour(tourSwapped, edgesNotTaken - nextEdge, relevantLocations - nextNode.getCityId)
  50.       }
  51.     }
  52.   }
  53. }
  54.  
  55. object Main {
  56.   def calcObjectiveValue(solution: Seq[Location]): Double = {
  57.     var sumDistance: Double = 0
  58.     var last: Location = null
  59.     import scala.collection.JavaConversions._
  60.     for (c <- solution) {
  61.       if (last != null && c != null) {
  62.         sumDistance += last.distanceTo(c)
  63.       }
  64.       last = c
  65.     }
  66.     if (last != null && (solution.get(0) ne last)) {
  67.       sumDistance += last.distanceTo(solution.get(0))
  68.     }
  69.     sumDistance
  70.   }
  71.  
  72.   def main(args: Array[String]) {
  73.     val tspInstance = new TspLibReader("C:\\Users\\Martin\\Desktop\\asd2\\public_instances\\0001").readInstance()
  74.     import scala.collection.JavaConverters._
  75.     val nodeMap = tspInstance.getAllLocations.asScala.map {
  76.       case (a, b) => (a.intValue(), b)
  77.     }.toMap
  78.  
  79.     val start = System.nanoTime
  80.  
  81.     val nodes = tspInstance.getAllLocations.asScala.values.toList
  82.     val constraints = tspInstance.getConstraints.asScala.toList
  83.     val nn = new NNHeuristic(nodes, constraints)
  84.  
  85.     val tour = nn.nearestNeighborTour(Vector(nodeMap(2)), nn.makeCompleteGraph(nodes).toSet, nodeMap - 2)
  86.  
  87.     val end = System.nanoTime
  88.  
  89.     println(tour.map(_.getCityId))
  90.     println(calcObjectiveValue(tour))
  91.  
  92.     println("t = " + (end - start) / 1e6 + " ms")
  93.   }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement