progs/knight3.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 12 Nov 2019 10:47:27 +0000
changeset 319 b84ea52bfd8f
parent 213 f968188d4a9b
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
     1
// Part 3 about finding a single tour using the Warnsdorf Rule
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
     2
//=============================================================
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
     3
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     4
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     5
type Pos = (Int, Int)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     6
type Path = List[Pos]
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     7
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     8
// for measuring time
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     9
def time_needed[T](n: Int, code: => T) : T = {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    10
  val start = System.nanoTime()
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    11
  for (i <- 0 until n) code
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    12
  val result = code
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    13
  val end = System.nanoTime()
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    14
  println(f"Time needed: ${(end - start) / 1.0e9}%3.3f secs.")
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    15
  result
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    16
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    17
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    18
def print_board(dim: Int, path: Path): Unit = {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    19
  println
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    20
  for (i <- 0 until dim) {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    21
    for (j <- 0 until dim) {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    22
      print(f"${path.reverse.indexOf((i, j))}%4.0f ")
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    23
    }
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    24
    println
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    25
  } 
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    26
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    27
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    28
def add_pair(x: Pos, y: Pos): Pos = 
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    29
  (x._1 + y._1, x._2 + y._2)
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    31
def is_legal(dim: Int, path: Path, x: Pos): Boolean = 
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    32
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    33
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    34
def moves(x: Pos): List[Pos] = 
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    35
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    36
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x, _))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    37
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    38
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    39
  moves(x).filter(is_legal(dim, path, _))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    40
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    41
def ordered_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    42
  legal_moves(dim, path, x).sortBy((x) => legal_moves(dim, path, x).length)
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    43
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    44
import scala.annotation.tailrec
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    45
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    46
@tailrec
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    47
def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    48
  case Nil => None
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    49
  case x::xs => {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    50
    val result = f(x)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    51
    if (result.isDefined) result else first(xs, f)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    52
  }
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    53
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    54
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    55
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    56
//def first[A, B](xs: List[A], f: A => Option[B]): Option[B] =
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    57
//  xs.flatMap(f(_)).headOption
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    58
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    59
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    60
def first_closed_tour_heuristics(dim: Int, path: Path): Option[Path] = {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    61
  if (path.length == dim * dim && moves(path.head).contains(path.last)) Some(path)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    62
  else
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    63
    first(ordered_moves(dim, path, path.head), (x: Pos) => first_closed_tour_heuristics(dim, x::path))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    64
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    65
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    66
// heuristic cannot be used to search for closed tours on 7 x 7
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    67
for (dim <- 1 to 6) {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    68
  val t = time_needed(0, first_closed_tour_heuristics(dim, List((dim / 2, dim / 2))))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    69
  println(s"${dim} x ${dim} closed: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    70
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    71
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    73
//@tailrec
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    74
/*
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    75
def first_tour_heuristics(dim: Int, path: Path): Option[Path] = {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    76
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    77
  @tailrec
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    78
  def aux(dim: Int, path: Path, moves: List[Pos]): Option[Path] = 
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    79
  if (path.length == dim * dim) Some(path)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    80
  else
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    81
    moves match {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    82
      case Nil => None
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    83
      case x::xs => {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    84
        val r = first_tour_heuristics(dim, x::path)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    85
        if (r.isDefined) r else aux(dim, path, xs)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    86
      }    
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    87
    }
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    88
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    89
  aux(dim, path, ordered_moves(dim, path, path.head)) 
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    90
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    91
*/ 
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
    92
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    93
@tailrec
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    94
def tour_on_mega_board(dim: Int, paths: List[Path]): Option[Path] = paths match {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    95
  case Nil => None
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    96
  case (path::rest) =>
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    97
    if (path.length == dim * dim) Some(path)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    98
    else tour_on_mega_board(dim, ordered_moves(dim, path, path.head).map(_::path) ::: rest)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    99
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   100
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   101
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   103
/*
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   104
def first_tour_heuristics(dim: Int, path: Path): Option[Path] = {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   105
  if (path.length == dim * dim) Some(path)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   106
  else
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   107
    for (p <- ordered_moves(dim, path, path.head))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   108
      val r = first_tour_heuristics(dim, x::path)
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   109
    //first(ordered_moves(dim, path, path.head), (x: Pos) => first_tour_heuristics(dim, x::path))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   110
    ordered_moves(dim, path, path.head).view.flatMap((x: Pos) => first_tour_heuristics(dim, x::path)).headOption
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   111
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   112
*/ 
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 4
diff changeset
   113
213
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   114
/*
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   115
for (dim <- 1 to 50) {
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   116
  val t = first_tour_heuristics(dim, List((dim / 2, dim / 2)))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   117
  println(s"${dim} x ${dim}: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   118
}
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   119
*/
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   120
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   121
val dim = 70
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   122
println(s"${dim} x ${dim}:")
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   123
print_board(dim, time_needed(0, tour_on_mega_board(dim, List(List((0, 0)))).get))
f968188d4a9b updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   124