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