progs/cube.sc
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 04 Aug 2022 16:53:38 +0200
changeset 426 b51467741af2
parent 418 fa7f7144f2bb
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
394
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
// for measuring time
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
def time_needed[T](i: Int, code: => T) = {
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
  val start = System.nanoTime()
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
  for (j <- 1 to i) code
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
  val end = System.nanoTime()
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
  (end - start) / (i * 1.0e9)
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
}
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
// for measuring memory usage
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
val mb = 1024*1024
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
val runtime = Runtime.getRuntime
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
def memory() = {
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
    println(s"  ** Used Memory:  ${(runtime.totalMemory - runtime.freeMemory) / mb}")
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
    println(s"  ** Free Memory:  ${runtime.freeMemory / mb}")
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
    println(s"  ** Total Memory: ${runtime.totalMemory / mb}")
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
    println(s"  ** Max Memory:   ${runtime.maxMemory / mb}")
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
}
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
abstract class Colour
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
case object White extends Colour
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
case object Yellow extends Colour
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
case object Orange extends Colour
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
case object Red extends Colour
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
case object Green extends Colour
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
case object Blue extends Colour
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
//    -------
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
//   |c11 c12|
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
//   |c21 c22|
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
//    -------  
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
case class Face(c11: Colour, c12: Colour,
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
                c21: Colour, c22: Colour)
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
//     +--+
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
//     |f2|
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
//  +--+  +--+
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
//  |f5 f1 f6|
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
//  +--+  +--+
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
//     |f3|
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
//     |f4|
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
//     +--+ 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
case class Cube(f1: Face, f2: Face, f3: Face,
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
                f4: Face, f5: Face, f6: Face)                
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
// pretty printing
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
def pp_col(c: Colour) : String = c match {
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
    case White => "W"
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
    case Yellow => "Y"
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
    case Orange => "O"
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
    case Red => "R"
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
    case Green => "G"
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
    case Blue => "B"
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
}
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
def pp_face(f: Face) : String = 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
  s"${pp_col(f.c11)} ${pp_col(f.c12)}\n${pp_col(f.c21)} ${pp_col(f.c22)}"
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
def pp_cube(c: Cube) : String = 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
  s"${pp_face(c.f1)}\n${pp_face(c.f2)}\n${pp_face(c.f3)}\n${pp_face(c.f4)}\n${pp_face(c.f5)}\n${pp_face(c.f6)}"  
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 394
diff changeset
    64
394
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
val init = 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
    Cube(Face(White, Green, White, White), 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
         Face(Blue, Yellow, Orange, Red), 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
         Face(Red, Blue, Red, Blue),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
         Face(White, Yellow, Red, Orange),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
         Face(Yellow, Green, Blue, Green),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
         Face(Yellow, Green, Orange, Orange))  
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
val solved = 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
    Cube(Face(Yellow, Yellow, Yellow, Yellow), 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
         Face(Orange, Orange, Orange, Orange), 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
         Face(Red, Red, Red, Red),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
         Face(White, White, White, White),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
         Face(Blue, Blue, Blue, Blue),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
         Face(Green, Green, Green, Green))  
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
//println(pp_cube(init))                
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
// actions
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
def up(c: Cube) : Cube =
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
    Cube(Face(c.f1.c11, c.f3.c12, c.f1.c21, c.f3.c22),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
         Face(c.f2.c11, c.f1.c12, c.f2.c21, c.f1.c22),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
         Face(c.f3.c11, c.f4.c12, c.f3.c21, c.f4.c22),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
         Face(c.f4.c11, c.f2.c12, c.f4.c21, c.f2.c22),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
         Face(c.f5.c11, c.f5.c12, c.f5.c21, c.f5.c22),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
         Face(c.f6.c21, c.f6.c11, c.f6.c22, c.f6.c12))
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
def clock(c: Cube) : Cube =
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
    Cube(Face(c.f1.c21, c.f1.c11, c.f1.c22, c.f1.c12),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
         Face(c.f2.c11, c.f2.c12, c.f5.c22, c.f5.c12),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
         Face(c.f6.c21, c.f6.c11, c.f3.c21, c.f3.c22),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
         Face(c.f4.c11, c.f4.c12, c.f4.c21, c.f4.c22),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
         Face(c.f5.c11, c.f3.c11, c.f5.c21, c.f3.c12),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
         Face(c.f2.c21, c.f6.c12, c.f2.c22, c.f6.c22))
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
def right(c: Cube) : Cube =
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
    Cube(Face(c.f5.c11, c.f5.c12, c.f1.c21, c.f1.c22),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
         Face(c.f2.c12, c.f2.c22, c.f2.c11, c.f2.c21),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
         Face(c.f3.c11, c.f3.c12, c.f3.c21, c.f3.c22),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
         Face(c.f4.c11, c.f4.c12, c.f6.c12, c.f6.c11),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
         Face(c.f4.c22, c.f4.c21, c.f5.c21, c.f5.c22),
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
         Face(c.f1.c11, c.f1.c12, c.f6.c21, c.f6.c22))         
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
//println("\n" ++ pp_cube(up(init)))           
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
//println("\n" ++ pp_cube(clock(init)))   
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
//println("\n" ++ pp_cube(right(init))) 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
//println(List(init, up(init), clock(init), right(init)).map(solved))
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
// simple bf-search without solution
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
def actions(c: Cube) : List[Cube] = 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
  List(up(c), clock(c), right(c)) 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
def search(cs: List[Cube], d: Int) : Boolean = { 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
  println(s"Depth: $d    Cands: ${cs.length}")  
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
  //memory()
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
  if (cs.exists(solved == _)) true
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
  else search(cs.flatMap(actions), d + 1)
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
}  
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
//println("List Version")
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
//println(search(List(init), 0))
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
//println(s"${time_needed(1, search(List(init), 0))} secs")  
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
def actionsS(c: Cube) : Set[Cube] = 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
  Set(up(c), clock(c), right(c)) 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
def searchS(cs: Set[Cube], d: Int) : Boolean = { 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
  println(s"Depth: $d    Cands: ${cs.size}")  
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 394
diff changeset
   140
  memory()
394
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
  if (cs.exists(solved == _)) true
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
  else searchS(cs.flatMap(actionsS), d + 1)
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
}  
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
//println("Set Version")
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
//println(searchS(Set(init), 0))
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
//println(s"${time_needed(1, searchS(Set(init), 0))} secs")  
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
// bf-search generating a list of "actions""
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
abstract class Action
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
case object Up extends Action
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
case object Right extends Action
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
case object Clock extends Action
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
type Actions = List[Action]
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
def actions2(c: Cube, act: Actions) : List[(Cube, Actions)] = 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
  List((up(c), Up::act), 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
       (clock(c), Clock::act), 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
       (right(c), Right::act)) 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
def search2(cs: List[(Cube, Actions)], d: Int) : (Cube, Actions) = { 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
  println(s"Depth: $d    Cands: ${cs.length}")  
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 394
diff changeset
   166
  val res = cs.find(init == _._1)
394
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
  if (res.isDefined) res.get
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
  else search2(cs.flatMap((actions2 _).tupled), d + 1)
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
}  
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
//println("List Version with Actions")
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 394
diff changeset
   172
//println(search2(List((solved, Nil)), 0)._2.mkString("\n"))  
394
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
//println(s"${time_needed(1, search2(List((init, Nil)), 0))} secs") 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
// using Maps for recording the moves
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
def actionsM(c: Cube, act: Actions) : Map[Cube, Actions] = 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
  Map(up(c) -> (Up::act), 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
      clock(c) -> (Clock::act), 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
      right(c) -> (Right::act)) 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
def searchM(cs: Map[Cube, Actions], d: Int) : Actions = { 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
  println(s"Depth: $d    Cands: ${cs.keySet.size}")  
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 394
diff changeset
   184
  val res = cs.keySet.find(init == _)
394
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
  if (res.isDefined) cs(res.get)
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
  else searchM(cs.flatMap((actionsM _).tupled), d + 1)
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
}  
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
println("Map Version with actions")
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 394
diff changeset
   190
println(searchM(Map(solved -> Nil), 0).mkString("\n"))  
394
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
println(s"${time_needed(1, searchM(Map(init -> Nil), 0))} secs")
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
// bi-directional search
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
def bsearch(cs: Map[Cube, Actions], 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
            bs: Map[Cube, Actions], d: Int) : (Actions, Actions) = { 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
  println(s"Depth: $d    Cands: ${cs.keySet.size}/${bs.keySet.size}") 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
  val res = cs.keySet intersect bs.keySet
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
  if (!res.isEmpty) (cs(res.head), bs(res.head))
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
  else bsearch(cs.flatMap((actions2 _).tupled), 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
               bs.flatMap((actions2 _).tupled), d + 1)
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
} 
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
println("Bidirectional Version with actions")
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
println(bsearch(Map(init -> Nil), Map(solved -> Nil), 0))  
839ad118e467 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
println(s"${time_needed(1, bsearch(Map(init -> Nil), Map(solved -> Nil), 0))}")
418
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 394
diff changeset
   208
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 394
diff changeset
   209
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 394
diff changeset
   210
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 394
diff changeset
   211
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 394
diff changeset
   212
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 394
diff changeset
   213
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 394
diff changeset
   214
// more memory 
fa7f7144f2bb updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 394
diff changeset
   215
// JAVA_OPTS="-Xmx2g"