Skip to content
Snippets Groups Projects
Commit 89a50a18 authored by msurl's avatar msurl
Browse files

added different constraints and refactored

parent 028296c1
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,7 @@ import datetime
import sys
import matplotlib.pyplot as plt
import lp_to_nx_graph
from itertools import combinations
class DominatingSet:
......@@ -64,9 +65,6 @@ class ConnectedKHopDominatingSet(KHopDominatingSet):
self.G = G
super().__init__(G, k, name)
# self.m.addConstr(gp.quicksum(gp.quicksum(self.nodes[w] for w in G.neighbors(v)) -2*self.nodes[v] for v in G.nodes) >= -2)
if exclude:
self.m.addConstrs(self.nodes[v] <= gp.quicksum(self.nodes[w] for w in G.neighbors(v)) for v in G.nodes if v not in exclude)
else:
......@@ -89,6 +87,7 @@ class ConnectedKHopDominatingSet(KHopDominatingSet):
self.m._G = self.G
self.m.Params.lazyConstraints = 1
self.m.optimize(RootedConnectecKHopDominatingSet.elim_unconnectivity)
# self.m.optimize()
ds = {i for i,x_i in enumerate(self.m.getVars()) if x_i.x > 0.5}
return ds
......@@ -119,6 +118,101 @@ class RootedConnectecKHopDominatingSet(ConnectedKHopDominatingSet):
self.m.addConstr(self.nodes[root] >= 1)
# for i in G.nodes:
# if(i not in G.neighbors(root) and i is not root):
# min_ij_sep = ConnectedKHopDominatingSet.min_ij_separator(G, i, root, {i})
# self.m.addConstr(gp.quicksum(self.nodes[s] for s in min_ij_sep) >= self.nodes[i])
# for i in G.nodes:
# for j in G.nodes:
# if i != j and j not in G.neighbors(i):
# min_ij_sep = ConnectedKHopDominatingSet.min_ij_separator(self.G, i, j, {i})
# self.m.addConstr(gp.quicksum(self.nodes[s] for s in min_ij_sep) >= self.nodes[i] + self.nodes[j] - 1)
# All Neighbor separators
# for v in G.nodes:
# if v is not root and root not in G.neighbors(v) and v not in G.neighbors(root) and not set(G.neighbors(root)).intersection(set(G.neighbors(v))):
# for i in range(2,G.degree[v]):
# V = {w for w in G.neighbors(v)}
# V.update([v])
# # for i_neighborhood in combinations(V, 2):
# for i_neighborhood in combinations(V, i):
# if v in i_neighborhood:
# min_ij_sep = ConnectedKHopDominatingSet.min_ij_separator(G, v, root, set(i_neighborhood))
# self.m.addConstr(gp.quicksum(self.nodes[s] for s in min_ij_sep) >= self.nodes[v])
# All separators from single root
# for v in G.nodes:
# # if v is not root and root not in G.neighbors(v) and v not in G.neighbors(root) and not set(G.neighbors(root)).intersection(set(G.neighbors(v))):
# for i in range(2,len(G.nodes)):
# V = {w for w in G.neighbors(v)}
# V.update([v])
# # for i_neighborhood in combinations(V, 2):
# for i_neighborhood in combinations(V, i):
# if v in i_neighborhood:
# min_ij_sep = ConnectedKHopDominatingSet.min_ij_separator(G, v, root, set(i_neighborhood))
# if min_ij_sep:
# self.m.addConstr(gp.quicksum(self.nodes[s] for s in min_ij_sep) >= self.nodes[v])
# for v in G.nodes:
# # if v is not root and root not in G.neighbors(v) and v not in G.neighbors(root) and not set(G.neighbors(root)).intersection(set(G.neighbors(v))):
# for i in range(2,len(G.nodes)):
# V = {w for w in G.neighbors(v)}
# V.update([v])
# # for i_neighborhood in combinations(V, 2):
# for i_neighborhood in combinations(V, i):
# if v in i_neighborhood:
# for h in G.nodes:
# min_ij_sep = ConnectedKHopDominatingSet.min_ij_separator(G, v, h, set(i_neighborhood))
# if min_ij_sep:
# self.m.addConstr(gp.quicksum(self.nodes[s] for s in min_ij_sep) >= self.nodes[v])
def add_single_root_separators(self):
for i in self.G.nodes:
min_ij_sep = ConnectedKHopDominatingSet.min_ij_separator(G, i, self.root, {i})
if min_ij_sep:
self.m.addConstr(gp.quicksum(self.nodes[s] for s in min_ij_sep) >= self.nodes[i])
# ToDo: refactor!
def add_all_neighborhood_root_separators(self):
for v in self.G.nodes:
if v is not self.root and self.root not in self.G.neighbors(v) and v not in self.G.neighbors(self.root) and not set(self.G.neighbors(self.root)).intersection(set(self.G.neighbors(v))):
for i in range(2,self.G.degree[v]):
V = {w for w in self.G.neighbors(v)}
V.update([v])
for i_neighborhood in combinations(V, i):
if v in i_neighborhood:
min_ij_sep = ConnectedKHopDominatingSet.min_ij_separator(self.G, v, self.root, set(i_neighborhood))
self.m.addConstr(gp.quicksum(self.nodes[s] for s in min_ij_sep) >= self.nodes[v])
def add_all_combinations_root_separators(self):
for v in self.G.nodes:
for i in range(2,len(self.G.nodes)):
V = {w for w in self.G.neighbors(v)}
V.update([v])
for i_neighborhood in combinations(V, i):
if v in i_neighborhood:
min_ij_sep = ConnectedKHopDominatingSet.min_ij_separator(self.G, v, self.root, set(i_neighborhood))
if min_ij_sep:
self.m.addConstr(gp.quicksum(self.nodes[s] for s in min_ij_sep) >= self.nodes[v])
def add_simple_path_length_constraint(self):
self.m.addConstrs((self.nodes[v] * len(nx.algorithms.shortest_path(self.G, self.root, v))) <= gp.quicksum(self.nodes) for v in self.G.nodes)
# self.m.addConstrs((self.nodes[v] * self.nodes[w] * len(nx.algorithms.shortest_path(self.G, v, w))) <= gp.quicksum(self.nodes) for v in self.G.nodes for w in self.G.nodes)
def add_gausian_sum_formula_constraint(self):
self.m.addConstr(gp.quicksum(self.nodes[v] * len(nx.algorithms.shortest_path(self.G, self.root, v)) for v in self.G.nodes) <= (gp.quicksum(self.nodes)+1)*gp.quicksum(self.nodes)/2)
# self.m.addConstr(gp.quicksum(self.nodes[v] * self.nodes[w] * len(nx.algorithms.shortest_path(self.G, v, w)) for v in self.G.nodes for w in self.G.nodes) <= (gp.quicksum(self.nodes)+1)*gp.quicksum(self.nodes)/2)
def add_neighbor_of_neighbors_constraint(self, exclude):
self.m.addConstrs(self.nodes[v] <= gp.quicksum(self.nodes[w] * gp.quicksum(self.nodes[h] for h in self.G.neighbors(w) if h is not v) for w in self.G.neighbors(v)) for v in self.G.nodes if v not in exclude)
def intermediate_node_constraint(self, exclude):
self.m.addConstrs(2 * self.nodes[v] <= gp.quicksum(self.nodes[w] for w in G.neighbors(v)) for v in G.nodes if v not in exclude)
if __name__ == '__main__':
G = lp_to_nx_graph.read(sys.argv[1])
......@@ -128,5 +222,8 @@ if __name__ == '__main__':
else:
k = 1
# G = lp_to_nx_graph.read("/home/mario/Dokumente/Uni/Bachelorarbeit/git_repo/bachelor-mario-surlemont/python/lp graphs/asymmetric.lp")
# k = 2
dsProb = RootedConnectecKHopDominatingSet(G, k, 0)
dsProb.solve_and_draw()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment