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