Changeset 1999 in genesis


Ignore:
Timestamp:
Apr 13, 2004, 11:10:18 PM (21 years ago)
Author:
lodewijk
Message:
  • n-point crossover operator toegevoegd
  • die operator in de 'recombine' functie gehangen, geconstateerd dat de oplossingen er niet beter van werden en het geheel wel een stuk trager werd, dus maar weer weggecommentaard.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • nodes/channelea.ml

    r1998 r1999  
    119119(** Shorthand for List.tail *)
    120120let tail = List.tl
     121let even x = (x mod 2) == 0
     122(*let shuffle = Array.sort (fun _ _ -> 1 - Random.int(2))*)
    121123let just x = match x with
    122124               Nothing -> assert false
     
    208210        let print_node n = print_string (n.node_name ^ ": " ^ (wis n) ^ "\n") in
    209211        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 *)
     216let 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 }
    210232
    211233(** Generalized evolutionary algorithm driver.
     
    223245        let generation = ref 0 in
    224246        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
    226249        while !iterations_since_new_high_score < max_stagnant_iterations do
    227250                let newpop = iterate population in
    228                 Array.sort (fun a b -> compare b.score a.score) newpop;
     251                assert ((Array.length newpop) == population_size);
    229252                copyarray newpop population;
    230253                let high_score = population.(0).score in
     
    337360        let evaluate_hash = score_configuration all_nodes in
    338361        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 *)
    340370        let maxchannel essid =
    341371                let group = Hashtbl.find groups essid in
     
    357387                Array.iter (fun c -> c.score <- evaluate_hash c.conf) population;
    358388                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
    360393        let best = evolutionary_algorithm initialize recombine mutate evaluate select in
    361394        print_conf best.conf;;
Note: See TracChangeset for help on using the changeset viewer.