Changeset 1935 in genesis
- Timestamp:
- Mar 28, 2004, 9:34:21 PM (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
nodes/channelga.py
r1929 r1935 23 23 24 24 # deze twee zijn nodig voor de evaluatie van een configuratie 25 nodes = {} # lijst van Node instanties26 groups = [] # lijst van Group instanties25 nodes = {} # nodename -> Node 26 groups = {} # essid -> WIGroup 27 27 28 28 population = [] … … 34 34 d[e] = 1 35 35 return d.keys() 36 37 def multiget(d, ks): 38 return map(lambda k: d[k], ks) 36 39 37 40 class Node: … … 86 89 self.wis.append(wi) 87 90 wi.setGroup(self) 91 def uniq(self): 92 self.wis = uniq(self.wis) 88 93 89 94 class Configuration: … … 93 98 def copy(self): 94 99 c = Configuration() 95 c.conf = self.conf [:]100 c.conf = self.conf.copy() 96 101 c.calculated_score = None 97 102 return c 98 103 def randomize(self): 99 self.conf = [random.randint(1, 13) for i in range(len(groups))] 104 self.conf = {} 105 for essid in groups.keys(): 106 self.conf[essid] = random.randint(1, 13) 100 107 self.calculated_score = None 101 108 def score(self): … … 103 110 if self.calculated_score != None: 104 111 return self.calculated_score 105 for i in range(len(self.conf)):106 groups[ i].channel = self.conf[i]112 for essid in groups.keys(): 113 groups[essid].channel = self.conf[essid] 107 114 score = 0 108 115 for node in nodes.values(): … … 111 118 return score 112 119 def mutate(self, rate): 113 for i in range(len(groups)):120 for essid in groups.keys(): 114 121 if random.random() <= rate: 115 self.conf[ i] = random.randint(1, 13)122 self.conf[essid] = random.randint(1, 13) 116 123 def crossover(self, partner, numpoints): 117 124 # pick the crossover points 118 points = [random.randint(0, len(groups) - 1) for i in range(numpoints)] 125 essids = groups.keys() 126 points = [random.randint(0, len(essids) - 1) for i in range(numpoints)] 119 127 points.sort() 120 128 conf = [] … … 125 133 if who == 0: 126 134 l = self.conf[lastpoint:point] 127 who = 1128 135 else: 129 136 l = partner.conf[lastpoint:point] 130 who = 0137 who = 1 - who 131 138 conf.extend(l) 132 139 lastpoint = point … … 142 149 # BEGIN UGLY PARSING CODE 143 150 144 ipdict = {} 145 ipnr_node = {} 146 node_ipnr = {} 147 node_links = {} 151 to_resolve = [] # list of (essid, WI) tuples 148 152 149 153 def parse_wleiden_conf(lines): 150 ipnr= None154 essid = None 151 155 wi = None 152 peer = None 153 omni = 0 156 wis = [] 154 157 for l in lines: 155 158 if wi != None: 156 match = re.match("^\EW.*", l) 157 if match != None: 159 match = re.match("^MODE=master.*", l) 160 if match != None: 161 master = 1 162 match = re.match("^ESSID=(.*)", l) 163 if match != None: 164 essid = match.group(1) 165 match = re.match("^EW[0-9]*", l) 166 if match != None: 167 if master == 1: 168 group = WIGroup(essid) 169 groups[essid] = group 170 group.add_wi(wi) 171 else: 172 # defer, may come way later 173 to_resolve.append( (essid, wi) ) 174 master = 0 158 175 wi = None 159 ipnr = None 160 peer = None 161 continue 162 match = re.match("^SDESC=omni.*", l) 163 if match != None: 164 omni = 1 165 match = re.match("^IP=([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*).*", l) 166 if match != None: 167 ipnr = match.group(1) 168 if not node_ipnr.has_key(nodename): 169 node_ipnr[nodename] = [] 170 node_ipnr[nodename].append(ipnr) 171 ipnr_node[ipnr] = nodename 172 match = re.match("^POINT_TO_POINT=([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*).*", l) 173 if match != None: 174 peer = match.group(1) 175 if not node_links.has_key(nodename): 176 node_links[nodename] = [] 177 node_links[nodename].append( (peer, wi) ) 176 essid = None 178 177 else: 179 178 match = re.match("^\$nodename='([^']*)';.*", l) 180 179 if match != None: 181 180 nodename = match.group(1).lower() 181 match = re.match("^\$config{'wi[0-9]*:[0-9].*", l) 182 if match != None: 183 continue 182 184 match = re.match("^\$config{'(wi[0-9]*).*", l) 183 185 if match != None: 184 wi = match.group(1) 185 omni = 0 186 wi = WI(match.group(1)) 187 wis.append(wi) 188 master = 0 189 node = Node(nodename, wis) 190 nodes[nodename] = node 186 191 187 192 # gegeven een file met filenames van wleiden.conf's, parseer ze allemaal en … … 190 195 for fname in open(filename).readlines(): 191 196 parse_wleiden_conf(open(fname[:-1]).readlines()) 192 for nodename in node_ipnr.keys(): 193 wis = [] 194 if node_links.has_key(nodename): 195 d = {} 196 for peer, wi in node_links[nodename]: 197 if not d.has_key(wi): 198 wis.append(WI(wi)) 199 d[wi] = 1 200 node = Node(nodename, wis) 201 nodes[nodename] = node 202 203 for nodename in node_links.keys(): 204 node = nodes[nodename] 205 206 for peerip, winame in node_links[nodename]: 207 wi = node.wi_byname(winame) 208 if not ipnr_node.has_key(peerip): 209 print "Warning: node %s links to phantom IP %s" % (nodename, peerip) 210 continue 211 peername = ipnr_node[peerip] 212 bail = 0 213 for peerspeerip, peerswiname in node_links[peername]: 214 if not ipnr_node.has_key(peerspeerip): 215 print "Warning: node %s links to phantom IP %s" % (nodename, peerspeerip) 216 bail = 1 217 break 218 if ipnr_node[peerspeerip] == nodename: 219 break 220 if bail: 221 continue 222 peer = nodes[peername] 223 peerwi = peer.wi_byname(peerswiname) 224 # komt wi voor deze node al voor in de bestaande 225 # groepen? 226 group = None 227 for i in range(len(groups)): 228 if filter(lambda wi: wi.node.name == nodename and wi.name == winame, groups[i].wis) != []: 229 group = groups[i] 230 break 231 if group == None: 232 for i in range(len(groups)): 233 if filter(lambda wi: wi.node.name == peername and wi.name == peerswiname, groups[i].wis) != []: 234 group = groups[i] 235 break 236 if group == None: 237 # nee. maak een groep en voeg beide 238 # endpoints toe 239 group = WIGroup("group%d" % len(groups)) 240 group.add_wi(wi) 241 groups.append(group) 242 group.add_wi(peerwi) 243 for group in groups: 244 group.wis = uniq(group.wis) 197 for essid, wi in to_resolve: 198 if not groups.has_key(essid): 199 print "Warning: node %s, %s refers to unknown essid %s" % (wi.node.name, wi.name, essid) 200 continue 201 groups[essid].add_wi(wi) 202 for group in groups.values(): 203 group.uniq() 245 204 246 205 # END UGLY PARSING CODE … … 248 207 def plot_configuration(conf, out): 249 208 out.write("digraph plot {\n") 250 for i in range(len(groups)):251 out.write(" group%s [label=\"%d\"]\n" % (i, conf[i]))209 for essid in groups: 210 out.write("\"%s\" [label=\"%s\\n(%d)\"]\n" % (essid, essid, groups[essid].channel)) 252 211 for nodename in nodes.keys(): 253 212 for wi in nodes[nodename].wis: 254 213 if wi.group == None: 255 214 continue 256 out.write(" %s -> %s\n" % (nodename, wi.group.name))215 out.write("\"%s\" -> \"%s\"\n" % (nodename, wi.group.name)) 257 216 out.write("}") 258 217 259 218 parse_metafile('l') 219 220 for essid in groups.keys(): 221 print essid, map(lambda wi: wi.node.name, groups[essid].wis) 260 222 261 223 for i in range(20): … … 267 229 for i in range(0, 9): 268 230 p = population[i].copy() 269 population[i].crossover(population[i + 10], random.randint(1, 4) * 2)231 #population[i].crossover(population[i + 10], random.randint(1, 4) * 2) 270 232 population[i + 10] = p 271 233 population[i + 10].mutate(0.05) 272 234 population.sort(lambda a, b: cmp(b.score(), a.score())) 273 print population[0].score()235 print population[0].score(), map(lambda g: g.channel, groups.values()) 274 236 plot_configuration(population[0].conf, open("foo.dot", 'w')) 275 237 os.system('neato -Gstart=foo -Goverlap=false/scale -Gsplines=true -Gsep=2 -Gratio=fill -Gnslimit=50.0 -Grotate=90 -Gsize="11,7.5" -Tps -o channelga.ps foo.dot')
Note:
See TracChangeset
for help on using the changeset viewer.