| author | Christian Urban <urbanc@in.tum.de> | 
| Tue, 16 Jan 2018 10:47:29 +0000 | |
| changeset 168 | bb69fdebf05a | 
| parent 89 | fac25de665d2 | 
| permissions | -rw-r--r-- | 
| 86 | 1 | |
| 2 | import scala.concurrent._ | |
| 3 | import scala.concurrent.duration._ | |
| 4 | import ExecutionContext.Implicits.global | |
| 5 | import scala.language.postfixOps | |
| 6 | ||
| 7 | ||
| 8 | def add_pair_urban(x: Pos)(y: Pos): Pos = | |
| 9 | (x._1 + y._1, x._2 + y._2) | |
| 10 | ||
| 11 | def is_legal_urban(dim: Int, path: Path)(x: Pos): Boolean = | |
| 12 | 0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x) | |
| 13 | ||
| 14 | def moves_urban(x: Pos): List[Pos] = | |
| 15 | List(( 1, 2),( 2, 1),( 2, -1),( 1, -2), | |
| 16 | (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair_urban(x)) | |
| 17 | ||
| 18 | def legal_moves_urban(dim: Int, path: Path, x: Pos): List[Pos] = | |
| 19 | moves_urban(x).filter(is_legal_urban(dim, path)) | |
| 20 | ||
| 21 | def correct_urban(dim: Int)(p: Path): Boolean = p match {
 | |
| 22 | case Nil => true | |
| 23 | case x::Nil => true | |
| 24 | case x::y::p => | |
| 25 | if (legal_moves_urban(dim, p, y).contains(x)) correct_urban(dim)(y::p) else false | |
| 26 | } | |
| 27 | ||
| 87 
9384b8c98014
updatd
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
86diff
changeset | 28 | |
| 89 
fac25de665d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
88diff
changeset | 29 | lazy val f1 = Future {
 | 
| 86 | 30 | |
| 89 
fac25de665d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
88diff
changeset | 31 | val ts8 = first_tour_heuristic(8, List((0,0))).get | 
| 
fac25de665d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
88diff
changeset | 32 | assert(correct_urban(8)(ts8) == true) | 
| 86 | 33 | |
| 34 | } | |
| 35 | ||
| 89 
fac25de665d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
88diff
changeset | 36 | Await.result(f1, 360 second) | 
| 
fac25de665d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
88diff
changeset | 37 | |
| 
fac25de665d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
88diff
changeset | 38 | lazy val f2 = Future {
 | 
| 
fac25de665d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
88diff
changeset | 39 | |
| 
fac25de665d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
88diff
changeset | 40 | val ts50 = first_tour_heuristic(50, List((0,0))).get | 
| 
fac25de665d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
88diff
changeset | 41 | assert(correct_urban(50)(ts50) == true) | 
| 
fac25de665d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
88diff
changeset | 42 | |
| 
fac25de665d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
88diff
changeset | 43 | } | 
| 
fac25de665d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
88diff
changeset | 44 | |
| 
fac25de665d2
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
88diff
changeset | 45 | Await.result(f2, 360 second) |