Changeset 1998 in genesis


Ignore:
Timestamp:
Apr 13, 2004, 12:17:06 AM (21 years ago)
Author:
lodewijk
Message:

syntax cleanup, overbodige ;;'s weg en wat regels die wrappen met a2ps herlayout.

Location:
nodes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • nodes/channelea.ml

    r1997 r1998  
    6464        wi_name: string;
    6565        wi_nodename: string;
    66         wi_essid: string;
    67 };;
     66        wi_essid: string
     67}
    6868type group = {
    6969        group_essid: string;
    70         mutable group_wis: wi list;
    71 };;
     70        mutable group_wis: wi list
     71}
    7272type node = {
    7373        node_name: string;
    74         node_wis: wi list;
    75 };;
     74        node_wis: wi list
     75}
    7676(** A configuration is an assignment of groups, identified by essid, to a
    7777    channel, plus a score. The code should be careful not to use the score
     
    7979type configuration = {
    8080        mutable score: int;
    81         conf: (string, int) Hashtbl.t;
    82 };;
    83 type 'a maybe = Nothing | Just of 'a;;
     81        conf: (string, int) Hashtbl.t
     82}
     83type 'a maybe = Nothing | Just of 'a
    8484
    8585(** The global nodes hash, mapping from node name to node struct. *)
     
    152152        else if (in_list nointerference wi1.wi_name) &&
    153153                (in_list nointerference wi2.wi_name) then 1
    154         else runtable scoretable diff;;
    155 
    156 (** Given a configuration and a node, return the score. this is simply the sum of
    157    the scores of all the combinations of interfaces, written down as a fold for
    158    efficiency *)
     154        else runtable scoretable diff
     155
     156(** Given a configuration and a node, return the score. this is simply the sum
     157    of the scores of all the combinations of interfaces, written down as a fold
     158    for efficiency *)
    159159let node_score c n =
    160160        let nointerference_ = try Hashtbl.find nointerference n.node_name
     
    162162        let unusable_ = try Hashtbl.find unusable n.node_name
    163163                        with Not_found -> [] in
    164         let foldfunc acc (wi1, wi2) = acc + (wi_score c unusable_ nointerference_ wi1 wi2) in
    165         let base_score = List.fold_left foldfunc 0 (combinations n.node_wis) in
     164        let f a (wi1, wi2) = a + (wi_score c unusable_ nointerference_ wi1 wi2) in
     165        let base_score = List.fold_left f 0 (combinations n.node_wis) in
    166166        base_score * (List.length n.node_wis)
    167167
     
    169169    configuration *)
    170170let score_interference c (nodename1, winame1, nodename2, winame2) =
    171         let wi1 = List.find (fun wi -> (compare wi.wi_name winame1) == 0) (Hashtbl.find nodes nodename1).node_wis in
    172         let wi2 = List.find (fun wi -> (compare wi.wi_name winame2) == 0) (Hashtbl.find nodes nodename2).node_wis in
     171        let node1 = Hashtbl.find nodes nodename1 in
     172        let node2 = Hashtbl.find nodes nodename2 in
     173        let f winame = fun wi -> (compare wi.wi_name winame) == 0 in
     174        let wi1 = List.find (f winame1) node1.node_wis in
     175        let wi2 = List.find (f winame2) node2.node_wis in
    173176        let channel1 = c wi1.wi_essid in
    174177        let channel2 = c wi2.wi_essid in
     
    182185let score_configuration ns c =
    183186        let mapper = Hashtbl.find c in
    184         let foldfunc acc n = acc + (node_score mapper n) in
    185         let nodescores = List.fold_left foldfunc 0 ns in
    186         let interference_score = List.fold_left (fun a i -> a + score_interference mapper i) 0 !interference in
    187         nodescores + interference_score;;
     187        let f1 a n = a + (node_score mapper n) in
     188        let nodescores = List.fold_left f1 0 ns in
     189        let f2 a i = a + (score_interference mapper i) in
     190        let interference_score = List.fold_left f2 0 !interference in
     191        nodescores + interference_score
    188192
    189193(** Return a random configuration. For some reason, if this function accesses
     
    196200
    197201let print_conf conf =
    198         let print_wi wi = wi.wi_name ^ ": " ^ (string_of_int (Hashtbl.find conf wi.wi_essid)) in
    199         let wis node = List.fold_left (fun acc wi -> acc ^ " " ^ (print_wi wi)) "" node.node_wis in
    200         let sorted_nodes = List.sort (fun a b -> compare (a.node_name) (b.node_name)) (values nodes) in
    201         List.iter (fun n -> print_string (n.node_name ^ ":" ^ (wis n) ^ "\n")) sorted_nodes
     202        let channel wi = string_of_int (Hashtbl.find conf wi.wi_essid) in
     203        let print_wi wi = wi.wi_name ^ ": " ^ (channel wi) in
     204        let wis node = List.fold_left (fun a wi -> a ^ " " ^ (print_wi wi))
     205                                      "" node.node_wis in
     206        let cmpnode a b = compare (a.node_name) (b.node_name) in
     207        let sorted_nodes = List.sort cmpnode (values nodes) in
     208        let print_node n = print_string (n.node_name ^ ": " ^ (wis n) ^ "\n") in
     209        List.iter print_node sorted_nodes
    202210
    203211(** Generalized evolutionary algorithm driver.
     
    215223        let generation = ref 0 in
    216224        let all_nodes = values nodes in
    217         let _ = while !iterations_since_new_high_score < max_stagnant_iterations do
    218                 let newpop = (recombine $ mutate $ evaluate $ select) population in
     225        let iterate = recombine $ mutate $ evaluate $ select in
     226        while !iterations_since_new_high_score < max_stagnant_iterations do
     227                let newpop = iterate population in
    219228                Array.sort (fun a b -> compare b.score a.score) newpop;
    220229                copyarray newpop population;
     
    233242                incr iterations_since_new_high_score;
    234243                incr generation
    235         done in
    236         population.(0);;
     244        done;
     245        population.(0)
    237246
    238247(** BEGIN PARSING CODE *)
     
    253262let parse_file fname =
    254263        let spacere = Str.regexp " " in
    255         (** Given the name of the node currently being parsed, parse the given tuple
    256             that consists of a wi name and an essid. *)
     264        (** Given the name of the node currently being parsed, parse the given
     265            tuple that consists of a wi name and an essid. *)
    257266        let parse_pair nodename (wname, essid) =
    258                 let new_wi = { wi_name = wname; wi_nodename = nodename; wi_essid = essid} in
    259                 let foo = try
     267                let new_wi = { wi_name = wname;
     268                               wi_nodename = nodename;
     269                               wi_essid = essid} in
     270                let _ = try
    260271                                let group = Hashtbl.find groups essid in
    261272                                group.group_wis <- new_wi::group.group_wis;
    262                           with Not_found ->
    263                                 let group = { group_essid = essid; group_wis = [ new_wi ] } in
     273                        with Not_found ->
     274                                let group = { group_essid = essid;
     275                                              group_wis = [ new_wi ] } in
    264276                                Hashtbl.add groups essid group in
    265277                new_wi in
    266278        let parse_fields fields =
    267279                let nodename = head fields in
    268                 let rec makepairs l = match l with
    269                                         []              -> []
    270                                       | x::[]           -> assert false
    271                                       | a::b::xs        -> (a, b)::(makepairs xs) in
    272                 let wis = List.map (parse_pair nodename) (makepairs (tail fields)) in
     280                let rec makepairs l =
     281                        match l with
     282                          []            -> []
     283                        | x::[]         -> assert false
     284                        | a::b::xs      -> (a, b)::(makepairs xs) in
     285                let wis = List.map (parse_pair nodename)
     286                                   (makepairs (tail fields)) in
    273287                let sorted_wis = List.sort compare wis in
    274288                let node = { node_name = nodename; node_wis = sorted_wis } in
    275289                Hashtbl.add nodes nodename node in
    276290        List.iter (parse_fields $ (Str.split spacere)) (snarf_lines fname)
    277         ;;
    278291
    279292(* the parsers for the special case components *)
     
    325338        let initialize () = Array.init population_size (fun _ -> random_configuration groups evaluate_hash) in
    326339        let recombine x = x in
     340        let maxchannel essid =
     341                let group = Hashtbl.find groups essid in
     342                if (List.length group.group_wis) == 1 then 11
     343                else 13 in
    327344        let mutate_conf conf =
    328                 Hashtbl.iter (fun essid _ -> let f = Random.float 1.0 in
    329                                              let group = Hashtbl.find groups essid in
    330                                              let maxchannel = if (List.length group.group_wis) == 1 then 11
    331                                                               else 13 in
    332                                              if (f < mutation_rate) then
    333                                                      Hashtbl.replace conf essid (1 + (Random.int maxchannel))) conf in
    334         let mutate population = let mutants = Array.map (fun c -> let hash = Hashtbl.copy c.conf in
    335                                                                   mutate_conf hash;
    336                                                                   { score = evaluate_hash hash;
    337                                                                     conf = hash}) population in
    338                                 Array.append population mutants in
    339         let evaluate population = Array.iter (fun c -> c.score <- evaluate_hash c.conf) population;
    340                                   population in
     345                Hashtbl.iter (fun essid _ ->
     346                                let f = Random.float 1.0 in
     347                                if (f < mutation_rate) then
     348                                        let channel = 1 + (Random.int (maxchannel essid)) in
     349                                        Hashtbl.replace conf essid channel) conf in
     350        let mutate population =
     351                let mutants = Array.map (fun c -> let hash = Hashtbl.copy c.conf in
     352                                                  mutate_conf hash;
     353                                                  { score = evaluate_hash hash;
     354                                                    conf = hash}) population in
     355                Array.append population mutants in
     356        let evaluate population =
     357                Array.iter (fun c -> c.score <- evaluate_hash c.conf) population;
     358                population in
    341359        let select p = Array.sub p 0 ((Array.length p) / 2) in
    342360        let best = evolutionary_algorithm initialize recombine mutate evaluate select in
  • nodes/special.conf

    r1996 r1998  
    33supernode jorg jorg jorg2
    44supernode jacob jacob jacob2
     5supernode unigor unigor unigorn
     6supernode kaag kaag kaag2
Note: See TracChangeset for help on using the changeset viewer.