testing2/knight3.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 10 Jul 2018 15:12:14 +0100
changeset 190 8d78255026e8
parent 161 bb43401d82c3
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
161
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
     1
// Part 3 about finding a single tour using the Warnsdorf Rule
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
     2
//=============================================================
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
     3
145
155a7e41615e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
object CW7c {
161
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
     5
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
     6
type Pos = (Int, Int)
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
     7
type Path = List[Pos]
145
155a7e41615e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
161
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
     9
def print_board(dim: Int, path: Path): Unit = {
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    10
  println
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    11
  for (i <- 0 until dim) {
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    12
    for (j <- 0 until dim) {
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    13
      print(f"${path.reverse.indexOf((i, j))}%3.0f ")
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    14
    }
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    15
    println
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    16
  } 
145
155a7e41615e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
}
155a7e41615e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
161
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    19
def add_pair(x: Pos)(y: Pos): Pos = 
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    20
  (x._1 + y._1, x._2 + y._2)
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    21
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    22
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    23
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    24
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    25
def moves(x: Pos): List[Pos] = 
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    26
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    27
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    28
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    29
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    30
  moves(x).filter(is_legal(dim, path))
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    31
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    32
def ordered_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    33
  legal_moves(dim, path, x).sortBy((x) => legal_moves(dim, path, x).length)
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    34
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    35
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    36
import scala.annotation.tailrec
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    37
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    38
@tailrec
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    39
def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    40
  case Nil => None
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    41
  case x::xs => {
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    42
    val result = f(x)
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    43
    if (result.isDefined) result else first(xs, f)
160
250e1d7df9ff updated
Christian Urban <urbanc@in.tum.de>
parents: 145
diff changeset
    44
  }
145
155a7e41615e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
}
155a7e41615e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
155a7e41615e updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
161
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    48
def first_closed_tour_heuristic(dim: Int, path: Path): Option[Path] = {
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    49
  if (path.length == dim * dim && moves(path.head).contains(path.last)) Some(path)
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    50
  else
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    51
    first(ordered_moves(dim, path, path.head), (x: Pos) => first_closed_tour_heuristic(dim, x::path))
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    52
}
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    53
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    54
/*
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    55
for (dim <- 1 to 6) {
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    56
  val t = first_closed_tour_heuristic(dim, List((dim / 2, dim / 2)))
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    57
  println(s"${dim} x ${dim} closed: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    58
}*/
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    59
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    60
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    61
def first_tour_heuristic(dim: Int, path: Path): Option[Path] = {
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    62
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    63
  @tailrec
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    64
  def aux(dim: Int, path: Path, moves: List[Pos]): Option[Path] =  
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    65
  if (path.length == dim * dim) Some(path)
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    66
  else
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    67
    moves match {
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    68
      case Nil => None
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    69
      case x::xs => {
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    70
        val r = first_tour_heuristic(dim, x::path)
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    71
        if (r.isDefined) r else aux(dim, path, xs)
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    72
    }
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    73
  }    
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    74
 
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    75
  aux(dim, path, ordered_moves(dim, path, path.head)) 
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    76
}
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    77
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    78
/*
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    79
def first_tour_heuristic(dim: Int, path: Path): Option[Path] = {
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    80
  if (path.length == dim * dim) Some(path)
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    81
  else
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    82
    first(ordered_moves(dim, path, path.head), (x: Pos) => first_tour_heuristic(dim, x::path))
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    83
}
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    84
*/
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    85
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    86
/*
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    87
for (dim <- 1 to 50) {
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    88
  val t = first_tour_heuristic(dim, List((dim / 2, dim / 2)))
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    89
  println(s"${dim} x ${dim}: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    90
}
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    91
*/
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    92
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    93
}
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    94
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    95
bb43401d82c3 updated
Christian Urban <urbanc@in.tum.de>
parents: 160
diff changeset
    96
//CW7c.first_tour_heuristic(50, List((0,0))).get