Commit 7270d3ff authored by msurl's avatar msurl
Browse files

pushed progress

parent 27987454
......@@ -11,7 +11,7 @@ from gurobipy import GRB
import datetime
import sys
import matplotlib.pyplot as plt
#import lp_to_nx_graph
import lp_to_nx_graph
from itertools import combinations
from enum import Enum
......@@ -37,9 +37,10 @@ class KHopDominatingSet():
def solve(self):
# TODO: refactor, maybe use self.nodes directly
ds = {i for i,x_i in enumerate(self.m.getVars()) if x_i.x == 1}
self.m.optimize()
ds = {v for v in self.G.nodes if self.nodes[v].x > 0.5}
return ds
def solve_and_draw(self):
starttime = datetime.datetime.now()
ds = self.solve()
......@@ -68,6 +69,7 @@ class ConnectedKHopDominatingSet(KHopDominatingSet):
def __init__(self, G, k, name = "CkDS", exclude = {}, constraints = ConnectivityConstraint.SEPARATORS):
self.G = G
super().__init__(G, k, name)
self.m._rooted = False
self.constraints = constraints
......@@ -154,15 +156,13 @@ class ConnectedKHopDominatingSet(KHopDominatingSet):
self.m._G = self.G
self.m.Params.lazyConstraints = 1
self.m.optimize(ConnectedKHopDominatingSet.elim_unconnectivity)
elif ConnectivityConstraint.INDEGREE == self.constraints:
self.m.optimize()
# self.m.optimize()
elif ConnectivityConstraint.MILLER_TUCKER_ZEMLIN == self.constraints:
self.m.optimize()
elif ConnectivityConstraint.MARTIN == self.constraints:
self.m.optimize()
ds = {i for i,x_i in enumerate(self.m.getVars()) if x_i.x > 0.5}
ds = {v for v in self.G.nodes if self.nodes[v].x > 0.5}
return ds
# Probably uncorrect version
......@@ -203,25 +203,72 @@ class ConnectedKHopDominatingSet(KHopDominatingSet):
# model.cbLazy(gp.quicksum(model._vars[s] for s in min_ij_sep) >= model._vars[h] + model._vars[l] - 1)
# V2
def elim_unconnectivity(model, where):
if where == GRB.Callback.MIPSOL:
vals = model.cbGetSolution(model._vars)
ds = {i for i in model._vars.keys() if vals[i] > 0.5}
# def elim_unconnectivity(model, where):
# if where == GRB.Callback.MIPSOL:
# vals = model.cbGetSolution(model._vars)
# ds = {i for i in model._vars.keys() if vals[i] > 0.5}
G_prime_prime = model._G.subgraph(ds)
if(not nx.is_connected(G_prime_prime)):
C = [c for c in nx.algorithms.components.connected_components(G_prime_prime)]
for i in range(len(C)):
C_i = C[i]
for j in range(len(C)):
C_j = C[j]
h = next(iter(C_i))
l = next(iter(C_j))
min_ij_sep = ConnectedKHopDominatingSet.min_ij_separator(model._G, h, l, C_i)
if min_ij_sep:
for w in C_i:
model.cbLazy(gp.quicksum(model._vars[s] for s in min_ij_sep) >= model._vars[w] + model._vars[l] - 1)
# G_prime_prime = model._G.subgraph(ds)
# if(not nx.is_connected(G_prime_prime)):
# C = [c for c in nx.algorithms.components.connected_components(G_prime_prime)]
# for i in range(len(C)):
# C_i = C[i]
# for j in range(len(C)):
# C_j = C[j]
# h = next(iter(C_i))
# l = next(iter(C_j))
# min_ij_sep = ConnectedKHopDominatingSet.min_ij_separator(model._G, h, l, C_i)
# if min_ij_sep:
# for w in C_i:
# model.cbLazy(gp.quicksum(model._vars[s] for s in min_ij_sep) >= model._vars[w] + model._vars[l] - 1)
# V3
def elim_unconnectivity(model, where):
if where == GRB.Callback.MIPSOL:
if model._rooted:
vals = model.cbGetSolution(model._vars)
ds = {i for i in model._vars.keys() if vals[i] > 0.5}
G_prime_prime = model._G.subgraph(ds)
if(not nx.is_connected(G_prime_prime)):
C = [c for c in nx.algorithms.components.connected_components(G_prime_prime)]
C_root = [c for c in C if model._root in c].pop() # this is really awkward. Try to find something better!
for i in range(len(C)):
C_i = C[i]
if C_i != C_root:
h = next(iter(C_i))
l = model._root
min_ij_sep = ConnectedKHopDominatingSet.min_ij_separator(model._G, h, l, C_i)
if min_ij_sep:
for w in C_i:
model.cbLazy(gp.quicksum(model._vars[s] for s in min_ij_sep) >= model._vars[w] + model._vars[l] - 1)
else:
for j in range(len(C)):
C_j = C[j]
h = model._root
l = next(iter(C_j))
min_ij_sep = ConnectedKHopDominatingSet.min_ij_separator(model._G, h, l, C_i)
if min_ij_sep:
for w in C_i:
model.cbLazy(gp.quicksum(model._vars[s] for s in min_ij_sep) >= model._vars[w] + model._vars[l] - 1)
else:
vals = model.cbGetSolution(model._vars)
ds = {i for i in model._vars.keys() if vals[i] > 0.5}
G_prime_prime = model._G.subgraph(ds)
if(not nx.is_connected(G_prime_prime)):
C = [c for c in nx.algorithms.components.connected_components(G_prime_prime)]
for i in range(len(C)):
C_i = C[i]
for j in range(len(C)):
C_j = C[j]
h = next(iter(C_i))
l = next(iter(C_j))
min_ij_sep = ConnectedKHopDominatingSet.min_ij_separator(model._G, h, l, C_i)
if min_ij_sep:
for w in C_i:
model.cbLazy(gp.quicksum(model._vars[s] for s in min_ij_sep) >= model._vars[w] + model._vars[l] - 1)
class RootedConnectecKHopDominatingSet(ConnectedKHopDominatingSet):
def __init__(self, G, k, root = 0, name = "RCkDS", constraints = ConnectivityConstraint.SEPARATORS):
......@@ -230,12 +277,20 @@ class RootedConnectecKHopDominatingSet(ConnectedKHopDominatingSet):
self.m.addConstr(self.nodes[root] >= 1)
# self.m.addConstr(gp.quicksum(self.nodes) >= 12)
# self.add_all_combinations_root_separators()
# print("neighborhood separators:")
# self.add_all_neighborhood_root_separators()
# print("single node separators:")
# self.add_single_root_separators()
def solve(self):
self.m._root = self.root
self.m._rooted = True
return super().solve()
def add_single_root_separators(self):
number_of_found_separators = 0
for i in self.G.nodes:
......@@ -297,9 +352,11 @@ class RootedConnectecKHopDominatingSet(ConnectedKHopDominatingSet):
if __name__ == '__main__':
G = lp_to_nx_graph.read(sys.argv[1])
G = G.to_undirected()
filename = sys.argv[1]
if filename.endswith(".lp"):
G = lp_to_nx_graph.read(sys.argv[1])
else:
G = nx.read_graphml(sys.argv[1])
if(len(sys.argv) > 2):
k = int(sys.argv[2])
else:
......@@ -308,5 +365,7 @@ if __name__ == '__main__':
# G = lp_to_nx_graph.read("/home/mario/Dokumente/Uni/Bachelorarbeit/git_repo/bachelor-mario-surlemont/python/lp graphs/small-leaf.lp")
# k = 1
dsProb = RootedConnectecKHopDominatingSet(G, k, 0, constraints = ConnectivityConstraint.SEPARATORS)
dsProb = RootedConnectecKHopDominatingSet(G, k, next(iter(G.nodes)), constraints = ConnectivityConstraint.SEPARATORS)
dsProb.solve_and_draw()
# ds = KHopDominatingSet(G,k)
# ds.solve_and_draw()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment