marking3/knight2.scala
author Christian Urban <urbanc@in.tum.de>
Sat, 22 Jun 2019 08:39:52 +0100
changeset 265 59779ce322a6
parent 258 ebe71908b13e
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
258
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 3 about finding a single tour using the Warnsdorf Rule
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//=============================================================
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
//object CW8b { // for preparing the jar
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
type Pos = (Int, Int)
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
type Path = List[Pos]
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
// for measuring time in the JAR
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
def time_needed[T](code: => T) : T = {
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  val start = System.nanoTime()
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
  val result = code
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  val end = System.nanoTime()
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  println(f"Time needed: ${(end - start) / 1.0e9}%3.3f secs.")
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  result
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
}
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
def print_board(dim: Int, path: Path): Unit = {
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  println
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  for (i <- 0 until dim) {
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
    for (j <- 0 until dim) {
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
      print(f"${path.reverse.indexOf((i, j))}%4.0f ")
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
    }
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
    println
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
  } 
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
}
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
def add_pair(x: Pos, y: Pos): Pos = 
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
  (x._1 + y._1, x._2 + y._2)
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
def is_legal(dim: Int, path: Path, x: Pos): Boolean = 
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
def moves(x: Pos): List[Pos] = 
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x, _))
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  moves(x).filter(is_legal(dim, path, _))
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
 
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
def ordered_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
  legal_moves(dim, path, x).sortBy((x) => legal_moves(dim, path, x).length)
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
import scala.annotation.tailrec
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
@tailrec
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
  case Nil => None
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  case x::xs => {
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
    val result = f(x)
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
    if (result.isDefined) result else first(xs, f)
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  }
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
}
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
def tfirst_closed_tour_heuristics(dim: Int, path: Path): Option[Path] = {
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
  if (path.length == dim * dim && moves(path.head).contains(path.last)) Some(path)
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  else
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
    first(ordered_moves(dim, path, path.head), (x: Pos) => tfirst_closed_tour_heuristics(dim, x::path))
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
}
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
def first_closed_tour_heuristics(dim: Int, path: Path) =
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
 time_needed(tfirst_closed_tour_heuristics(dim: Int, path: Path))
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
def first_closed_tour_heuristic(dim: Int, path: Path) =
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
 time_needed(tfirst_closed_tour_heuristics(dim: Int, path: Path))
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
// heuristic cannot be used to search for closed tours on 7 x 7 an beyond
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
//for (dim <- 1 to 6) {
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
//  val t = time_needed(0, first_closed_tour_heuristics(dim, List((dim / 2, dim / 2))))
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
//  println(s"${dim} x ${dim} closed: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
//}
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
def tfirst_tour_heuristics(dim: Int, path: Path): Option[Path] = {
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
  if (path.length == dim * dim) Some(path)
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  else
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
    first(ordered_moves(dim, path, path.head), (x: Pos) => tfirst_tour_heuristics(dim, x::path))
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
}
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
def first_tour_heuristics(dim: Int, path: Path) = 
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
  time_needed(tfirst_tour_heuristics(dim: Int, path: Path))
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
def first_tour_heuristic(dim: Int, path: Path) = 
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  time_needed(tfirst_tour_heuristics(dim: Int, path: Path))
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
// will be called with boards up to 30 x 30
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
ebe71908b13e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
//}