progs/knight1.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 09 Nov 2016 13:36:13 +0000
changeset 24 66b97f9a40f8
parent 4 f31c22f4f104
child 50 9891c9fac37e
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
import scala.util._
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
class Computation[A,B](value: A, function: A => B) {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
  lazy val result = function(value)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
def print_board(n: Int)(steps: List[(Int, Int)]): Unit = {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
  println
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  for (i <- 0 until n) {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
    for (j <- 0 until n) {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
      print(f"${steps.indexOf((i, j))}%3.0f ")
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
    }
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
    println
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  } 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
def add_pair(x: (Int, Int))(y: (Int, Int)) = 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  (x._1 + y._1, x._2 + y._2)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
def is_legal(n: Int)(x: (Int, Int)) = 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  0 <= x._1 && 0 <= x._2 && x._1 < n && x._2 < n
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
def moves(n: Int)(steps: List[(Int, Int)])(x: (Int, Int)): List[(Int, Int)] = {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  List((1, 2),(2, 1),(2, -1),(1, -2),
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
       (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)).filter(is_legal(n)).filterNot(steps.contains(_))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
def ordered_moves(n: Int)(steps: List[(Int, Int)])(x : (Int, Int)): List[(Int, Int)] = 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
  moves(n)(steps)(x).sortBy(moves(n)(steps)(_).length)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
moves(8)(Nil)(1,3)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
ordered_moves(8)(Nil)(1,3)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
ordered_moves(8)(List((2, 4), (2, 6)))(1,3)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
def first[A, B](xs: List[A], f: A => Set[B]): Set[B] = xs match {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  case Nil => Set()
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  case x::xs => {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
    val result = f(x)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
    if (result == Set()) first(xs, f) else result
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  }
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
// non-circular tour
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
def tour(n: Int)(steps: List[(Int, Int)]): Option[List[(Int, Int)]] = {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
  if (steps.length ==  n * n) Some(steps) 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  else 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
    { val list = moves(n)(steps)(steps.head) map (x => new Computation(x, ((x:(Int, Int)) => tour(n)(x::steps))))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
      val found = list.par find (_.result.isDefined)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
      found map (_.result.get)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
    }
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
val n = 6
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
println(s"simple tour: n = $n")
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
val starts = for (i <- (0 until n).toList; 
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
                  j <- (0 until n).toList) yield new Computation ((i, j), ((x:(Int, Int)) => tour(n)(x::Nil)))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
val found = starts.par find (_.result.isDefined)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
print_board(n)((found map (_.result.get)).get)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
//for measuring time
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
def time_needed[T](i: Int, code: => T) = {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  val start = System.nanoTime()
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  for (j <- 1 to i) code
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
  val end = System.nanoTime()
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
  (end - start)/(i * 1.0e9)
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
//for (i <- 1 to 20) {
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
//  println(i + ": " + "%.5f".format(time_needed(2, matches(EVIL1(i), "a" * i))))
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
//}
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
f31c22f4f104 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76