Changeset 1999 in genesis
- Timestamp:
- Apr 13, 2004, 11:10:18 PM (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
nodes/channelea.ml
r1998 r1999 119 119 (** Shorthand for List.tail *) 120 120 let tail = List.tl 121 let even x = (x mod 2) == 0 122 (*let shuffle = Array.sort (fun _ _ -> 1 - Random.int(2))*) 121 123 let just x = match x with 122 124 Nothing -> assert false … … 208 210 let print_node n = print_string (n.node_name ^ ": " ^ (wis n) ^ "\n") in 209 211 List.iter print_node sorted_nodes 212 213 (** n-point crossover operator. pick n points along the length of the parents, 214 produce a child by copying from one parent, switching parents when hitting a 215 chosen crossover point *) 216 let crossover n c1 c2 = 217 let keys1 = keys (c1.conf) in 218 let numkeys1 = List.length keys1 in 219 let pickpoint i = (if even i then c1.conf else c2.conf), 220 (if i < n then (Random.int numkeys1) else numkeys1) in 221 let crosspoints = Array.init (n + 1) pickpoint in 222 let result = Hashtbl.create (List.length keys1) in 223 let i = ref 0 in 224 Array.sort (fun a b -> compare (snd a) (snd b)) crosspoints; 225 Array.iter (fun (h, p) -> while !i < p do 226 let key = List.nth keys1 !i in 227 Hashtbl.add result key (Hashtbl.find h key); 228 incr i 229 done) crosspoints; 230 assert ((List.length (keys result)) == (List.length keys1)); 231 { score = 0; conf = result } 210 232 211 233 (** Generalized evolutionary algorithm driver. … … 223 245 let generation = ref 0 in 224 246 let all_nodes = values nodes in 225 let iterate = recombine $ mutate $ evaluate $ select in 247 (*let iterate = recombine $ mutate $ evaluate $ select in*) 248 let iterate = select $ evaluate $ mutate $ recombine in 226 249 while !iterations_since_new_high_score < max_stagnant_iterations do 227 250 let newpop = iterate population in 228 Array.sort (fun a b -> compare b.score a.score) newpop;251 assert ((Array.length newpop) == population_size); 229 252 copyarray newpop population; 230 253 let high_score = population.(0).score in … … 337 360 let evaluate_hash = score_configuration all_nodes in 338 361 let initialize () = Array.init population_size (fun _ -> random_configuration groups evaluate_hash) in 339 let recombine x = x in 362 let recombine pop = pop in 363 (* 364 let numoffspring = Random.int population_size in 365 let children = Array.init numoffspring (fun _ -> 366 let father = pop.(Random.int population_size) in 367 let mother = pop.(Random.int population_size) in 368 crossover 2 father mother) in 369 Array.append pop children in *) 340 370 let maxchannel essid = 341 371 let group = Hashtbl.find groups essid in … … 357 387 Array.iter (fun c -> c.score <- evaluate_hash c.conf) population; 358 388 population in 359 let select p = Array.sub p 0 ((Array.length p) / 2) in 389 let select p = 390 Array.sort (fun a b -> compare b.score a.score) p; 391 (*shuffle p;*) 392 Array.sub p 0 population_size in 360 393 let best = evolutionary_algorithm initialize recombine mutate evaluate select in 361 394 print_conf best.conf;;
Note:
See TracChangeset
for help on using the changeset viewer.