def upwalker_counter(g, meta, vf_g, arguments): N = g.vcount() progress = progressbar1.DummyProgressBar(end=10, width=15) if arguments.progressbar: progress = progressbar1.AnimatedProgressBar(end=len(meta), width=15) # trace_up_map = {} # random_up_map = {} trace_up_counter = [] random_up_counter = [] for m in meta: progress += 1 progress.show_progress() trace = m[helpers.TRACE] trace_dir = vft.trace_to_string(g, trace) trace_up_count = trace_dir.count('U') trace_up_counter.append(trace_up_count) # trace_up_map[trace_up_count] = trace_up_map.get(trace_up_count, 0) + # 1 s, t = trace[0], trace[-1] s_idx, t_idx = vft.node_to_nodeid(g, s), vft.node_to_nodeid(g, t) random_vf_route = helpers.random_route_walk(vf_g, s_idx, t_idx + N, len(trace), named=False, weight_field='VFweight') random_vf_route = vft.vf_route_converter(random_vf_route, N) random_vf_dir = vft.trace_to_string(g, random_vf_route) random_vf_count = random_vf_dir.count('U') random_up_counter.append(random_vf_count) # random_up_map[random_vf_count] = random_up_map.get(random_vf_count, # 0) + 1 real_counter = collections.Counter(trace_up_counter) real_up = ' '.join( ['%s: %s' % (k, real_counter[k]) for k in sorted(list(real_counter))]) random_counter = collections.Counter(random_up_counter) random_up = ' '.join([ '%s: %s' % (k, random_counter[k]) for k in sorted(list(random_counter)) ]) logger.info('') logger.info('Real trace UP counter: %s' % real_up) logger.info('Random vf trace up counter: %s' % random_up) helpers.save_to_json(arguments.out, { 'REAL': dict(real_counter), 'RANDOM': dict(random_counter) }) keys = sorted(set(list(real_counter) + list(random_counter))) logger.info('IDX;REAL;RANDOM') for k in keys: logger.info('%s;%d;%d' % (k, real_counter[k], random_counter[k]))
def trace_dir(g, meta, vf_g, arguments): N = g.vcount() vf_sum_lp_w = sum([e['LPweight'] for e in vf_g.es]) out_instead_core = 0 core_instead_out = 0 in_customer = 0 in_customer_but_use_up = 0 progress = progressbar1.DummyProgressBar(end=10, width=15) if arguments.progressbar: progress = progressbar1.AnimatedProgressBar(end=len(meta), width=15) for m in meta: progress += 1 progress.show_progress() trace = m[helpers.TRACE] first_edge = [trace[0], trace[1]] first_hop_type = vft.edge_dir(g, first_edge, vfmode=vft.CLOSENESS) s_idx = vft.node_to_nodeid(g, trace[0]) t_idx = vft.node_to_nodeid(g, trace[-1]) sh_vf = vf_g.get_all_shortest_paths(s_idx + N, t_idx + N) if len(sh_vf) > 0: in_customer += 1 if first_hop_type != LinkDir.D: in_customer_but_use_up += 1 if first_hop_type == LinkDir.D: # 'remove' all downward path: for neighbor in vf_g.neighbors(s_idx + N, mode=igraph.OUT): down_edge = vf_g.get_eid(s_idx + N, neighbor, directed=True) vf_g.es[down_edge]['LPweight_old'] = vf_g.es[down_edge][ 'LPweight'] vf_g.es[down_edge]['LPweight'] = vf_sum_lp_w + 1 lp_sh = vft.get_shortest_vf_route(g, s_idx, t_idx, mode='lp', vf_g=vf_g, vfmode=vft.CLOSENESS) pretty_plotter.pretty_plot(g, trace, lp_sh, []) first_new_edge = [lp_sh[0], lp_sh[1]] first_new_hop_type = vft.edge_dir(g, first_new_edge, vfmode=vft.CLOSENESS) if first_hop_type != first_new_hop_type: out_instead_core += 1 print '' print "Original trace: %s" % trace print 'Original trace dir: %s' % vft.trace_to_string(g, trace) print 'Original closeness: %s' % [ g.vs.find(x)['closeness'] for x in trace ] print 'LP trace: %s' % [g.vs[x]['name'] for x in lp_sh] print 'LP dir: %s' % vft.trace_to_string(g, lp_sh) print 'LP closeness: %s' % [ g.vs.find(x)['closeness'] for x in lp_sh ] # raw_input() for neighbor in vf_g.neighbors(s_idx + N, mode=igraph.OUT): down_edge = vf_g.get_eid(s_idx + N, neighbor, directed=True) vf_g.es[down_edge]['LPweight'] = vf_g.es[down_edge][ 'LPweight_old'] elif first_hop_type == LinkDir.U: lp_sh = vft.get_shortest_vf_route(g, s_idx, t_idx, mode='lp', vf_g=vf_g, vfmode=vft.CLOSENESS) lp_dir = vft.trace_to_string(g, lp_sh) # if lp_dir[1:].startswith('U') or lp_dir[1:].startswith('D'): # continue first_new_edge = [lp_sh[0], lp_sh[1]] first_new_hop_type = vft.edge_dir(g, first_new_edge, vfmode=vft.CLOSENESS) if first_hop_type != first_new_hop_type: core_instead_out += 1 print '' print "Original trace: %s" % trace print 'Original trace dir: %s' % vft.trace_to_string(g, trace) print 'Original closeness: %s' % [ g.vs.find(x)['closeness'] for x in trace ] print 'LP trace: %s' % [g.vs[x]['name'] for x in lp_sh] print 'LP dir: %s' % vft.trace_to_string(g, lp_sh) print 'LP closeness: %s' % [ g.vs.find(x)['closeness'] for x in lp_sh ] pretty_plotter.pretty_plot(g, trace, lp_sh, []) # raw_input() logger.info('Core instead down: %d' % core_instead_out) logger.info('Down instead core: %d' % out_instead_core) logger.info('In customer cone: %d' % in_customer) logger.info('In customer but use UP: %d' % in_customer_but_use_up)
def random_route_walk(g, s, t, limit, named=False, weight_field=None): try: s = vft.node_to_nodeid(g, s) t = vft.node_to_nodeid(g, t) # check if s and t are valid inidecies _, _ = g.vs[s], g.vs[t] except (ValueError, IndexError): raise IndexError('Vertex index out of range or not exists') # some special case if limit <= 0: # print 'HOP COUNT: %d' % hop_count return [] if s == t: return [s, ] # # if s != t then length must be larger than 1 # # but only in hop count mode if weight_field is None and limit == 1: # print 'S: %s, T: %s, HC: %d' % (s, t, hop_count) return [] shortest_route = g.shortest_paths(s, t, weights=weight_field, mode=i.OUT) shortest_route = shortest_route[0][0] logger.debug('Shortes: %f, Limit: %f' % (shortest_route, limit)) if weight_field is None: # in hop count mode convert hop count to node count shortest_route += 1 if limit < shortest_route: # print 'TOO SHORT %d' % (hop_count) return [] def edge_weight(s, t): eid = g.get_eid(s, t) if weight_field is None: # +1 ha nincs suly, mert az igraph azt mondja meg, # s-bol hany hopp t w = 1 else: w = g.es[eid][weight_field] return w path = [s, ] weights = [0, ] visited_node_list = [[], ] actual_node = s # In hop count mode decrement, becase s already added to the path if weight_field is None: limit -= 1 while limit > 0 and actual_node != t: logger.debug('Limit remained: %d' % limit) logger.debug('Current node: %s' % actual_node) visited_nodes = visited_node_list[-1] last_weight = weights[-1] logger.debug('Visited nodes: %s' % visited_nodes) neighbors = [x for x in g.neighbors(actual_node, mode=i.OUT) if x not in visited_nodes and x not in path] neighbor_distances = [edge_weight(actual_node, x) for x in neighbors] next_hop_distances = [x[0] for x in g.shortest_paths(neighbors, t, weights=weight_field, mode=i.OUT)] distances = [x[0] + x[1] for x in zip(neighbor_distances, next_hop_distances)] candidates = filter(lambda x: x[1] <= limit, zip(neighbors, distances)) if len(candidates) == 0: limit += last_weight path.pop() visited_node_list.pop() weights.pop() actual_node = path[-1] continue logger.debug('Valid candidates: %s' % candidates) next_hop = random.choice(candidates)[0] logger.debug('Chosen one: %s' % next_hop) visited_nodes.append(next_hop) path.append(next_hop) visited_node_list.append([]) last_weight = edge_weight(actual_node, next_hop) weights.append(last_weight) limit -= last_weight actual_node = next_hop if named: path = [g.vs[x]['name'] for x in path] logger.debug('random path between {s} and {t}: {p}'.format(s=s, t=t, p=path)) return path