File size: 7,849 Bytes
d5bfab8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
structure kolmo :> kolmo =
struct
open HolKernel Abbrev boolLib
aiLib mlTreeNeuralNetworkAlt smlParallel kernel bloom
val ERR = mk_HOL_ERR "kolmo";
(* -------------------------------------------------------------------------
Datastructures
------------------------------------------------------------------------- *)
val seltemp = ref []
val seld = ref (dempty Int.compare)
val semb = BoolArray.tabulate (bmod, fn _ => false)
val winl = ref []
val wind = ref (eempty seq_compare)
val sem_counter = ref 0
val div_counter = ref 0
fun semtoseq x = first_n 16 x
val quotient_flag = ref true
(* -------------------------------------------------------------------------
Store in selection dictionnary + increment counters
------------------------------------------------------------------------- *)
fun update_wind p sem =
let
val winseql = find_wins p sem
val b = ref false
fun f x = if not (emem x (!wind))
then (wind := eadd x (!wind); b := true) else ()
in
app f winseql;
if !b then winl := p :: !winl else ()
end
fun to_seld f x =
let
val _ = check_timer ()
val p = f x
val f = let val exec = start (mk_exec p) in
(fn x => exec x handle Overflow => error)
end
val semo = SOME (map f entryl) handle Div => (incr div_counter; NONE)
in
case semo of NONE => () | SOME sem =>
(
update_wind p sem;
if !quotient_flag andalso bmem sem semb then incr sem_counter else
(seltemp := zip_prog p :: !seltemp; badd sem semb)
)
end
val piapp_nullop = papp_nullop
fun piapp_binop id (a,b) = papp_binop id (unzip_prog a, unzip_prog b)
fun piapp_ternop id (a,b,c) =
papp_ternop id (unzip_prog a, unzip_prog b, unzip_prog c)
fun papp_nullop_seld x = to_seld piapp_nullop x
fun papp_binop_seld x y = to_seld (piapp_binop x) y
fun papp_cond_seld y = to_seld (piapp_ternop cond_id) y
fun papp_loop_seld y = to_seld (piapp_ternop loop_id) y
(* -------------------------------------------------------------------------
Generating all inputs at depth dep for a function with arity ari.
------------------------------------------------------------------------- *)
fun numpart k n = number_partition k n handle HOL_ERR _ => [];
fun iter_cpl2 f (v1,v2) =
let
val n1 = Vector.length v1
val n2 = Vector.length v2
fun loop i j =
if i >= n1 then loop 0 (j+1)
else if j >= n2 then () else
(f (Vector.sub (v1,i), Vector.sub (v2,j));
loop (i+1) j)
in
loop 0 0
end
fun iter_part2 dep f =
let
val partl1 = map pair_of_list (numpart 2 dep)
fun g i = dfind i (!seld) handle NotFound => (Vector.fromList [])
val partl2 = map (fn (a,b) => (g a, g b)) partl1
in
app (iter_cpl2 f) partl2
end
fun app_all_binop dep operl =
app (fn x => iter_part2 dep (papp_binop_seld x)) operl;
fun iter_cpl3 f (v1,v2,v3) =
let
val n1 = Vector.length v1
val n2 = Vector.length v2
val n3 = Vector.length v3
fun loop i j k =
if i >= n1 then loop 0 (j+1) k
else if j >= n2 then loop 0 0 (k+1)
else if k >= n3 then ()
else
(f (Vector.sub (v1,i), Vector.sub (v2,j), Vector.sub (v3,k));
loop (i+1) j k)
in
loop 0 0 0
end
fun iter_part3 dep f =
let
val partl1 = map triple_of_list (numpart 3 dep)
fun g i = dfind i (!seld) handle NotFound => (Vector.fromList [])
val partl2 = map (fn (a,b,c) => (g a, g b, g c)) partl1
in
app (iter_cpl3 f) partl2
end
(* -------------------------------------------------------------------------
Exhaustive bottom-up search
------------------------------------------------------------------------- *)
fun debug_rl l = debug (String.concatWith " " (map (rts_round 3) l))
fun kolmo_one_aux dep =
let
val _ = seltemp := []
val _ = debug "binop"
val (_,t1) = add_time (app_all_binop dep) binaryidl
val _ = debug "cond"
val (_,t2) = add_time (iter_part3 dep) papp_cond_seld
val _ = debug "loop"
val (_,t3) = add_time (iter_part3 dep) papp_loop_seld
val _ = debug_rl [t1,t2,t3]
val v = Vector.fromList (!seltemp)
val _ = seld := dadd dep v (!seld)
in
debug ("new: " ^ its (Vector.length v));
print_endline
("total: " ^ its (
sum_int (map (Vector.length o snd) (dlist (!seld)))));
print_endline ("solutions: " ^ its (elength (!wind)));
print_endline ("sol prog: " ^ its (length (!winl)))
end
fun kolmo_one dep =
let
val (_,t) = add_time kolmo_one_aux dep
in
debug (String.concatWith ", " [
"step:" ^ its dep ^ " " ^ its (!rti_glob),
"time:" ^ rts_round 3 t])
end
fun init_kolmo () =
(
init_timer ();
seld := dempty Int.compare; seltemp := [];
div_counter := 0;
sem_counter := 0;
app papp_nullop_seld nullaryidl;
seld := dadd 1 (Vector.fromList (!seltemp)) (dempty Int.compare);
print_endline
("total: " ^ its (sum_int (map (Vector.length o snd) (dlist (!seld)))));
print_endline ("solutions: " ^ its (elength (!wind)));
print_endline ("sol prog: " ^ its (length (!winl)))
)
fun kolmo maxdep =
let
val startsize = 2
fun loop n = if n > maxdep then () else (kolmo_one n; loop (n + 1))
in
init_kolmo (); loop startsize
end
fun all_subseq_aux seq = case seq of
[] => [[]]
| a :: m => seq :: all_subseq_aux m
fun all_subseq seq = map rev (all_subseq_aux (rev seq))
fun stats () =
let
val tot = !rti_glob
val divc = !div_counter
val semc = !sem_counter
in
print_endline ("\ntotal: " ^ its tot);
print_endline ("quotiented: " ^ its semc);
print_endline ("exceptions: " ^ its divc);
print_endline ("solutions: " ^ its (elength (!wind)))
end
end
(*
load "kolmo";
PolyML.print_depth 0; open aiLib kernel bloom kolmo; PolyML.print_depth 40;
search_time := Time.fromReal 1000.0;
debug_flag := false;
quotient_flag := false;
time kolmo 5;
stats ();
*)
(*
total: 24293300
quotiented: 18582806
exceptions: 959968
unique: 4750526
unique top: 4750526
solutions long: 4449
solutions short: 315
!overflow_counter; (negative numbers)
total: 23350500
quotiented: 17377311
exceptions: 1322704
unique: 4650485
unique top: 4650485
solutions long: 3389
solutions short: 231
total: 21967400
quotiented: 16518626
exceptions: 1322421
unique: 4126353
unique top: 4126353
solutions long: 3346
solutions short: 268
total: 36663900
quotiented: 24224448
exceptions: 8002150
unique: 4437302
win: 8692
realwin: 3503
7hash
total: 12518700
quotiented: 9140176
exceptions: 1319123
solutions: 2992
2hash
total: 19946500
quotiented: 15103483
exceptions: 1319123
solutions: 3171
7hash + dict
total: 14808000
quotiented: 10954044
exceptions: 1319396
solutions: 3083
7hash + dict + nocollision checking
total: 14808000
quotiented: 10954044
exceptions: 1319396
solutions: 3083
total: 19144100
quotiented: 13785601
exceptions: 3050831
solutions: 1690
total: 15195800
quotiented: 11335770
exceptions: 1319396
solutions: 3083
total: 27254800
quotiented: 21089765
exceptions: 1319416
solutions: 3427
total: 44874200
quotiented: 26292032
exceptions: 13075706
solutions: 3918
total: 45920800
quotiented: 27295461
exceptions: 13075706
solutions: 3919
total: 322207700
quotiented: 255549654
exceptions: 18300507
solutions: 6246
total: 25263500
quotiented: 19100883
exceptions: 1319406
solutions: 3426
total: 290972600
quotiented: 228900780
exceptions: 18300345
solutions: 6157
total: 36450300
quotiented: 23687801
exceptions: 7593816
solutions: 3695
total: 25754300
quotiented: 19584199
exceptions: 1319123
solutions: 3618
*)
|