Commit 5e0123f4 by msurl

### first codebase

parent b9fe8f5a
 #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat May 9 23:55:40 2020 @author: mario """ import k_transitive_closure import k_dominating_set import networkx as nx import matplotlib.pyplot as plt import gurobipy as gp from gurobipy import GRB def solve(G, k): m, nodes = model(G, k, "MINkCDS") add_constraints(G, k, m) # return solve_iteratively(G, k, m, nodes) def min_ij_separator(G, i, j, C_i): N_ci = {v for c in C_i for v in G.neighbors(c)} G_prime = nx.Graph(G) C_i_prime = C_i.copy() C_i_prime.update(N_ci) G_prime.remove_edges_from(G.subgraph(C_i_prime).edges) # dijkstra R_j = nx.algorithms.dag.descendants(G_prime, j) return R_j.intersection(N_ci) def model(G, k, name): m, nodes = k_dominating_set.model(G,k,name) add_constraints(G, m, nodes) return m, nodes def add_base_connectivity_constraint(G, m, nodes): m.addConstrs(nodes[v] <= gp.quicksum(nodes[w] for w in G.neighbors(v)) for v in G.nodes) def add_constraints(G, m, nodes): add_base_connectivity_constraint(G, m, nodes) def solve_iteratively(G, k, m, nodes, maxIterations): iterations = 0 m.optimize() ds = {i for i,x_i in enumerate(m.getVars()) if x_i.x == 1} G_prime_prime = G.subgraph(ds) while(not nx.is_connected(G_prime_prime)) and iterations < maxIterations: iterations+=1 C = [c for c in nx.algorithms.components.connected_components(G_prime_prime)] for i in range(len(C)-1): C_i = C[i] for j in range(i+1, len(C)): C_j = C[j] h = next(iter(C_i)) l = next(iter(C_j)) min_ij_sep = min_ij_separator(G, h, l, C_i) m.addConstr(gp.quicksum(nodes[s] for s in min_ij_sep) >= nodes[h] + nodes[l] - 1) m.optimize() ds = {i for i,x_i in enumerate(m.getVars()) if x_i.x == 1} G_prime_prime = G.subgraph(ds) return ds, iterations \ No newline at end of file
 #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat May 9 23:53:13 2020 @author: mario """ import networkx as nx import gurobipy as gp from gurobipy import GRB def add_constraints(G, m, nodes): m.addConstrs(((gp.quicksum(nodes[n] for n in G.neighbors(v)) + nodes[v] )>= 1) for v in G.nodes) def model(G, name): m = gp.Model(name) nodes = m.addVars(G.nodes, vtype = GRB.BINARY, name = "nodes") m.setObjective(gp.quicksum(nodes), GRB.MINIMIZE) add_constraints(G,m,nodes) return m, nodes def solve(G): m, nodes = model(G,'DS') m.optimize() # ds = {v for j,x_j in enumerate(m.getVars()) for i,v in enumerate(G.nodes) if j == i} ds = {i for i,x_i in enumerate(m.getVars()) if x_i.x == 1} return ds \ No newline at end of file
 #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat May 9 23:55:30 2020 @author: mario """ import dominating_set import k_transitive_closure import networkx as nx def solve(G,k): G_prime = k_transitive_closure.make_closure(G,k) dominating_set.solve(G_prime) def model(G, k, name): G_prime = k_transitive_closure.make_closure(G,k) return dominating_set.model(G_prime, name) \ No newline at end of file
 #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat May 9 19:00:59 2020 @author: mario """ import networkx as nx import matplotlib.pyplot as plt # G is supposed to be a undirected networx.Graph def make_closure(G, k): G_prime = nx.Graph(G) for v in G.nodes: neighbors = set(G.neighbors(v)) for i in range(k-1): neighbors.update([w for n in neighbors for w in G.neighbors(n)]) G_prime.add_edges_from((v,n) for n in neighbors) return G_prime if __name__ == '__main__': G = nx.Graph() G.add_nodes_from(range(10)) G.add_edges_from((i,i+1) for i in range(9)) G_prime = make_closure(G,10) nx.draw(G_prime) plt.show()
 #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed May 13 17:03:41 2020 @author: mario """ import networkx as nx import re # def lp_to_nx_graph(strInput): # nodePattern = re.compile(r'node\([0-9]+\.\.[0-9]+\)\.') # print(nodePattern.findall("node(0..195).")) # lp_to_nx_graph("") def cleanLP(lpSTR): lpSTR = lpSTR.replace(" ", "") lpSTR = lpSTR.replace("\t", "") newLPList = [] for line in lpSTR.split("\n"): toAdd = line if "%" in line: toAdd = line[:line.index("%")] if toAdd != "": newLPList.append(toAdd) return newLPList def lpStrToGraphNX(lpSTR): lpList = cleanLP(lpSTR) graph = nx.empty_graph(0) for part in lpList: if ".." in part and "node" in part: startrange = int(part[5:part.index("..")]) endrange = int(part[part.index("..")+2:part.index(").")]) graph.add_nodes_from(range(startrange, endrange)) elif "." in part and ".." not in part: for i in part.split("."): if "node(" in i: graph.add_node(int(i[5:-1])) elif "edge(" in i: first = int(i[5:i.index(",")]) second = int(i[i.index(",")+1:-1]) graph.add_edge(first, second) if(first == 13 or first == 9): if(second == 13 or second == 9): print("Hello") return graph def read(filenameRaw): fileIn = open(filenameRaw,'r') graph = lpStrToGraphNX(fileIn.read()) return graph \ No newline at end of file