Changeset 1998 in genesis
- Timestamp:
- Apr 13, 2004, 12:17:06 AM (21 years ago)
- Location:
- nodes
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
nodes/channelea.ml
r1997 r1998 64 64 wi_name: string; 65 65 wi_nodename: string; 66 wi_essid: string ;67 } ;;66 wi_essid: string 67 } 68 68 type group = { 69 69 group_essid: string; 70 mutable group_wis: wi list ;71 } ;;70 mutable group_wis: wi list 71 } 72 72 type node = { 73 73 node_name: string; 74 node_wis: wi list ;75 } ;;74 node_wis: wi list 75 } 76 76 (** A configuration is an assignment of groups, identified by essid, to a 77 77 channel, plus a score. The code should be careful not to use the score … … 79 79 type configuration = { 80 80 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 } 83 type 'a maybe = Nothing | Just of 'a 84 84 85 85 (** The global nodes hash, mapping from node name to node struct. *) … … 152 152 else if (in_list nointerference wi1.wi_name) && 153 153 (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 of157 the scores of all the combinations of interfaces, written down as a fold for158 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 *) 159 159 let node_score c n = 160 160 let nointerference_ = try Hashtbl.find nointerference n.node_name … … 162 162 let unusable_ = try Hashtbl.find unusable n.node_name 163 163 with Not_found -> [] in 164 let f oldfunc acc (wi1, wi2) = acc+ (wi_score c unusable_ nointerference_ wi1 wi2) in165 let base_score = List.fold_left f oldfunc0 (combinations n.node_wis) in164 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 166 166 base_score * (List.length n.node_wis) 167 167 … … 169 169 configuration *) 170 170 let 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 173 176 let channel1 = c wi1.wi_essid in 174 177 let channel2 = c wi2.wi_essid in … … 182 185 let score_configuration ns c = 183 186 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 188 192 189 193 (** Return a random configuration. For some reason, if this function accesses … … 196 200 197 201 let 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 202 210 203 211 (** Generalized evolutionary algorithm driver. … … 215 223 let generation = ref 0 in 216 224 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 219 228 Array.sort (fun a b -> compare b.score a.score) newpop; 220 229 copyarray newpop population; … … 233 242 incr iterations_since_new_high_score; 234 243 incr generation 235 done in236 population.(0) ;;244 done; 245 population.(0) 237 246 238 247 (** BEGIN PARSING CODE *) … … 253 262 let parse_file fname = 254 263 let spacere = Str.regexp " " in 255 (** Given the name of the node currently being parsed, parse the given tuple256 t hat 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. *) 257 266 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 260 271 let group = Hashtbl.find groups essid in 261 272 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 264 276 Hashtbl.add groups essid group in 265 277 new_wi in 266 278 let parse_fields fields = 267 279 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 273 287 let sorted_wis = List.sort compare wis in 274 288 let node = { node_name = nodename; node_wis = sorted_wis } in 275 289 Hashtbl.add nodes nodename node in 276 290 List.iter (parse_fields $ (Str.split spacere)) (snarf_lines fname) 277 ;;278 291 279 292 (* the parsers for the special case components *) … … 325 338 let initialize () = Array.init population_size (fun _ -> random_configuration groups evaluate_hash) in 326 339 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 327 344 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 341 359 let select p = Array.sub p 0 ((Array.length p) / 2) in 342 360 let best = evolutionary_algorithm initialize recombine mutate evaluate select in -
nodes/special.conf
r1996 r1998 3 3 supernode jorg jorg jorg2 4 4 supernode jacob jacob jacob2 5 supernode unigor unigor unigorn 6 supernode kaag kaag kaag2
Note:
See TracChangeset
for help on using the changeset viewer.