def test_edge_dir_closeness_with_id(self): dir_u = vft.edge_dir(self.sample_graph, (5, 4), vfmode=vft.CLOSENESS) dir_d = vft.edge_dir(self.sample_graph, (6, 2), vfmode=vft.CLOSENESS) dir_p = vft.edge_dir(self.sample_graph, (5, 6), vfmode=vft.CLOSENESS) self.assertEqual(dir_u, vf_tools.LinkDir.U) self.assertEqual(dir_d, vf_tools.LinkDir.D) self.assertEqual(dir_p, vf_tools.LinkDir.P)
def test_edge_dir_prelabeled_with_id(self): dir_u = vft.edge_dir(self.sample_graph, (5, 4), vfmode=vft.PRELABELED) dir_d = vft.edge_dir(self.sample_graph, (6, 2), vfmode=vft.PRELABELED) dir_p = vft.edge_dir(self.sample_graph, (5, 6), vfmode=vft.PRELABELED) self.assertEqual(dir_u, vf_tools.LinkDir.U) self.assertEqual(dir_d, vf_tools.LinkDir.D) self.assertEqual(dir_p, vf_tools.LinkDir.P)
def test_edge_dir_rank_with_id(self): dir_u = vft.edge_dir(self.sample_graph, (5, 4), vfmode=vft.DEGREE) dir_d = vft.edge_dir(self.sample_graph, (6, 2), vfmode=vft.DEGREE) dir_p = vft.edge_dir(self.sample_graph, (5, 6), vfmode=vft.DEGREE) self.assertEqual(dir_u, vf_tools.LinkDir.U) self.assertEqual(dir_d, vf_tools.LinkDir.D) self.assertEqual(dir_p, vf_tools.LinkDir.P)
def test_edge_dir_closeness_with_name(self): dir_u = vft.edge_dir(self.sample_graph, ('N5', 'N4'), vfmode=vft.CLOSENESS) dir_d = vft.edge_dir(self.sample_graph, ('N6', 'N2'), vfmode=vft.CLOSENESS) dir_p = vft.edge_dir(self.sample_graph, ('N5', 'N6'), vfmode=vft.CLOSENESS) self.assertEqual(dir_u, vf_tools.LinkDir.U) self.assertEqual(dir_d, vf_tools.LinkDir.D) self.assertEqual(dir_p, vf_tools.LinkDir.P)
def test_edge_dir_prelabeled_with_name(self): dir_u = vft.edge_dir(self.sample_graph, ('N5', 'N4'), vfmode=vft.PRELABELED) dir_d = vft.edge_dir(self.sample_graph, ('N6', 'N2'), vfmode=vft.PRELABELED) dir_p = vft.edge_dir(self.sample_graph, ('N5', 'N6'), vfmode=vft.PRELABELED) self.assertEqual(dir_u, vf_tools.LinkDir.U) self.assertEqual(dir_d, vf_tools.LinkDir.D) self.assertEqual(dir_p, vf_tools.LinkDir.P)
def test_edge_dir_rank_with_name(self): dir_u = vft.edge_dir(self.sample_graph, ('N5', 'N4'), vfmode=vft.DEGREE) dir_d = vft.edge_dir(self.sample_graph, ('N6', 'N2'), vfmode=vft.DEGREE) dir_p = vft.edge_dir(self.sample_graph, ('N5', 'N6'), vfmode=vft.DEGREE) self.assertEqual(dir_u, vf_tools.LinkDir.U) self.assertEqual(dir_d, vf_tools.LinkDir.D) self.assertEqual(dir_p, vf_tools.LinkDir.P)
def test_edge_dir_error(self): up = self.sample_graph.es[self.sample_graph.get_eid(4, 5)] error_msg = "igraph's edge type is not supported" with self.assertRaises(AttributeError, msg=error_msg): vft.edge_dir(self.sample_graph, up, vfmode=vft.PRELABELED) with self.assertRaises(AttributeError, msg=error_msg): vft.edge_dir(self.sample_graph, up, vfmode=vft.CLOSENESS) with self.assertRaises(AttributeError, msg=error_msg): vft.edge_dir(self.sample_graph, up, vfmode=vft.DEGREE)
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_nonvf_route(g, s, t, hop_count, path=None, vfmode=None): if path is None: try: if isinstance(s, str): s = g.vs.find(s).index if isinstance(t, str): t = g.vs.find(t).index # 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 hop_count < 1: # print 'HOP COUNT: %d' % hop_count return (False, []) if s == t: return (True, [s, ]) # if s != t then length must be larger than 1 if hop_count == 1: # print 'S: %s, T: %s, HC: %d' % (s, t, hop_count) return (False, []) shortest_route = g.shortest_paths(s, t, mode=i.OUT)[0][0] + 1 if hop_count < shortest_route: # print 'TOO SHORT %d' % (hop_count) return (False, []) path = [s, ] hop_count -= 1 if vfmode is None: vfmode = vft.CLOSENESS if s == t: return (vft.is_valley_free(g, path, vfmode=vfmode), path) logger.debug('Hop count remained: %d' % hop_count) logger.debug('Current node: %s' % s) neighbors = [x for x in g.neighbors(s, mode=i.OUT) if x not in path] distances = [x[0] for x in g.shortest_paths(neighbors, t, mode=i.OUT)] candidates = filter(lambda x: x[1] + 1 <= hop_count, # +, mert az igraph # azt mondja meg, # s-bol hany hopp t zip(neighbors, distances)) weights = [-1 if not vft.is_valley_free(g, path + [x[0], ], vfmode=vfmode) else vft.edge_dir(g, [s, x[0]], vfmode=vfmode).value for x in candidates] # create a list where every columnt is neighbors, distances, weights # respectevly candidates = zip(*(zip(*candidates) + [weights, ])) # sort by weights candidates = sorted(candidates, key=itemgetter(2)) if len(candidates) == 0: return (False, []) logger.debug('Valid candidates: %s' % candidates) first_route = (False, []) # by default there was no route to T for next_hop in candidates: logger.debug('Chosen one: %s' % next_hop[0]) isvf, r = random_nonvf_route(g, next_hop[0], t, hop_count - 1, path + [next_hop[0], ], vfmode) if len(r) == 0: continue if not isvf: # we are done, we found a nonVF route return (isvf, r) # our first guess a vf route. save it for later use (e.g. there is no # nonVF rotue) but lets try again with another candidate if len(first_route[1]) == 0: # first save first_route = (isvf, r) return first_route