def run(nodes,edges): """ execute the Floyd Warshall algorithm """ dist = init_dist(nodes) # set matrix identity elements to 0 for v in nodes: dist[v][v] = 0 # fill matrix with intial edge weights for e in edges: # shorter in our case means with stronger connections, hence: w = gi.get_weight(e) if w > 0: d = 1.0 / w else: d = 99999 # "Inf" # inizialization of v->u and u->v to same value because the graph is undirected dist[e.source][e.target] = d dist[e.target][e.source] = d # the core of the algorithm for v1 in nodes: for v2 in nodes: for v3 in nodes: if dist[v2][v1] + dist[v1][v3] < dist[v2][v3]: dist[v2][v3] = dist[v2][v1] + dist[v1][v3] return dist
def compute_edges(): """ actions performed by the environment Alzeihmer desease: select randomly a certain percentage of edges. The weights of the edges will be reduced of a certain percentage """ num_edges_selected = int(math.ceil(gi.num_edges() * config.frac_edges )) selected=gi.random_edges(num_edges_selected) damaged = set() thisdamage=[] for e in selected: # each of these edges will be assigned a damage prev_weight = gi.get_weight(e) gi.mult_weight(e, config.frac_damage) diff = prev_weight - e.weight e.damage = diff # if the edge is too light, destroy it thisdamage.append((e.source, e.target, e.damage)) if gi.get_weight(e) < 1.0: #del e # FIXME!! careful!!! pass damaged.add(e) return damaged
def total_weight(): ret = 0.0 for e in gi.edges(): ret += gi.get_weight(e) return ret