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