Skip to content
Snippets Groups Projects
Commit 7f9f98a1 authored by konog98's avatar konog98
Browse files

Directory Verarbeitung und Crossing Constraint Optimierung, .sol beinhaltet...

Directory Verarbeitung und Crossing Constraint Optimierung, .sol beinhaltet jetzt auch die optimalen Crossings
parent 08715aa6
Branches
Tags
1 merge request!1Kleines Framework aufgestellt, bereitgestellter Tester noch nicht getestet,...
Showing
with 67 additions and 11 deletions
...@@ -21,7 +21,7 @@ def count_crossings_via_variables(c_vars): ...@@ -21,7 +21,7 @@ def count_crossings_via_variables(c_vars):
for c_var in c_vars.values(): for c_var in c_vars.values():
if c_var.varValue == 1: if c_var.varValue == 1:
crossings += 1 crossings += 1
print("Crossings:", crossings) return(crossings)
def solve_bipartite_minimization(graph_file): def solve_bipartite_minimization(graph_file):
logging.info(f"Prozess für {graph_file} gestartet") logging.info(f"Prozess für {graph_file} gestartet")
...@@ -57,7 +57,23 @@ def solve_bipartite_minimization(graph_file): ...@@ -57,7 +57,23 @@ def solve_bipartite_minimization(graph_file):
# Zielfunktion, die minimiert werden soll # Zielfunktion, die minimiert werden soll
prob += lpSum(c.values()) prob += lpSum(c.values())
logging.info("Zielfunktion aufgestellt.") logging.info("Zielfunktion aufgestellt.")
# Definieren der Crossing Constraints effizienter
# Crossing Constraints basierend auf Kantenpaaren
edges.sort(key=lambda x: x[0]) # Sortieren der Kanten nach dem Startknoten
for idx, (i, j) in enumerate(edges):
for (k, l) in edges[idx + 1:]:
if k > i: # Da die Liste sortiert ist, brauchen wir nur k > i zu prüfen
if (i, j, k, l) not in c:
c[(i, j, k, l)] = LpVariable(f"c_{i}_{j}_{k}_{l}", 0, 1, cat='Binary')
if j > l:
prob += c[(i, j, k, l)] == y[(l, j)]
elif l > j:
prob += c[(i, j, k, l)] == 1 - y[(j, l)]
"""
# Crossing Constraints basierend auf Kantenpaaren # Crossing Constraints basierend auf Kantenpaaren
for (i, j) in edges: for (i, j) in edges:
for (k, l) in edges: for (k, l) in edges:
...@@ -71,6 +87,7 @@ def solve_bipartite_minimization(graph_file): ...@@ -71,6 +87,7 @@ def solve_bipartite_minimization(graph_file):
if l > j: if l > j:
prob += c[(i, j, k, l)] == 1 - y[(j, l)] prob += c[(i, j, k, l)] == 1 - y[(j, l)]
logging.info("Crossing Constraints aufgestellt.") logging.info("Crossing Constraints aufgestellt.")
"""
initial_crossings = count_initial_crossings(edges) initial_crossings = count_initial_crossings(edges)
logging.info(f"Initial crossings edges: {initial_crossings}") logging.info(f"Initial crossings edges: {initial_crossings}")
...@@ -135,20 +152,32 @@ def solve_bipartite_minimization(graph_file): ...@@ -135,20 +152,32 @@ def solve_bipartite_minimization(graph_file):
if in_degree[neighbor] == 0: if in_degree[neighbor] == 0:
zero_in_degree_queue.append(neighbor) zero_in_degree_queue.append(neighbor)
for b in sorted_b: a = count_crossings_via_variables(c)
print(f"{b}")
""" # Ausgabe der sortierten Knoten # Ausgabe der sortierten Knoten
os.makedirs(os.path.dirname(output_file), exist_ok=True) os.makedirs(os.path.dirname(output_file), exist_ok=True)
with open(output_file, 'w') as f: with open(output_file, 'w') as f:
for b in sorted_b: for b in sorted_b:
f.write(f"{b}\n") f.write(f"{b}\n")
print(f"{b}") print(f"{b}")
count_crossings_via_variables(c) f.write(f"Crossings: {a}\n")
logging.info(f"Ergebnisse in {output_file} gespeichert")""" print("Crossings: ", a)
logging.info(f"Ergebnisse in {output_file} gespeichert")
else: else:
logging.warning("Keine optimale Lösung gefunden.") logging.warning("Keine optimale Lösung gefunden.")
def process_directory(directory_path):
# Durchlaufe alle Dateien im angegebenen Verzeichnis
for filename in os.listdir(directory_path):
if filename.endswith('.gr'): # Überprüfen, ob die Datei eine .gr Datei ist
file_path = os.path.join(directory_path, filename)
solve_bipartite_minimization(file_path) # Rufe die Verarbeitungsfunktion für jede .gr Datei auf
logging.info(f"Verarbeitung abgeschlossen für {file_path}")
directory_path = 'githubtests/tiny_test_set/instances/'
process_directory(directory_path)
test_file = 'githubtests/tiny_test_set/instances/website_20.gr' #test_file = 'githubtests/tiny_test_set/instances/complete_4_5.gr'
#test_file = 'test_instances/0.gr' #test_file = 'test_instances/0.gr'
solve_bipartite_minimization(test_file) #solve_bipartite_minimization(test_file)
...@@ -48,7 +48,19 @@ def solve_bipartite_minimization(graph_file): ...@@ -48,7 +48,19 @@ def solve_bipartite_minimization(graph_file):
# Zielfunktion, die minimiert werden soll # Zielfunktion, die minimiert werden soll
prob += lpSum(c.values()) prob += lpSum(c.values())
logging.info("Zielfunktion aufgestellt.") logging.info("Zielfunktion aufgestellt.")
"""
edges.sort(key=lambda x: x[0]) # Sortieren der Kanten nach dem Startknoten
for idx, (i, j) in enumerate(edges):
for (k, l) in edges[idx + 1:]:
if k > i: # Da die Liste sortiert ist, brauchen wir nur k > i zu prüfen
if (i, j, k, l) not in c:
c[(i, j, k, l)] = LpVariable(f"c_{i}_{j}_{k}_{l}", 0, 1, cat='Binary')
if j > l:
prob += c[(i, j, k, l)] == y[(l, j)]
elif l > j:
prob += c[(i, j, k, l)] == 1 - y[(j, l)]
logging.info("Crossing Constraints aufgestellt.")
"""
# Crossing Constraints basierend auf Kantenpaaren # Crossing Constraints basierend auf Kantenpaaren
for (i, j) in edges: for (i, j) in edges:
for (k, l) in edges: for (k, l) in edges:
...@@ -62,6 +74,8 @@ def solve_bipartite_minimization(graph_file): ...@@ -62,6 +74,8 @@ def solve_bipartite_minimization(graph_file):
if l > j: if l > j:
prob += c[(i, j, k, l)] == 1 - y[(j, l)] # Verwende .get() für sichere Zugriffe prob += c[(i, j, k, l)] == 1 - y[(j, l)] # Verwende .get() für sichere Zugriffe
logging.info("Crossing Constraints aufgestellt.") logging.info("Crossing Constraints aufgestellt.")
prob.solve() prob.solve()
logging.info(f"Status der Lösung: {LpStatus[prob.status]}") logging.info(f"Status der Lösung: {LpStatus[prob.status]}")
...@@ -76,7 +90,6 @@ def solve_bipartite_minimization(graph_file): ...@@ -76,7 +90,6 @@ def solve_bipartite_minimization(graph_file):
for key, var in c.items(): for key, var in c.items():
print(f"c[{key}] = {var.varValue}") print(f"c[{key}] = {var.varValue}")
""" """
if prob.status == LpStatusOptimal: if prob.status == LpStatusOptimal:
logging.info("Optimale Lösung gefunden. Ergebnisse werden gespeichert.") logging.info("Optimale Lösung gefunden. Ergebnisse werden gespeichert.")
...@@ -122,5 +135,5 @@ def solve_bipartite_minimization(graph_file): ...@@ -122,5 +135,5 @@ def solve_bipartite_minimization(graph_file):
#test_file = 'githubtests/tiny_test_set/instances/grid_9_shuffled.gr' #test_file = 'githubtests/tiny_test_set/instances/grid_9_shuffled.gr'
test_file = 'test_instances/0.gr' test_file = 'test_instances/1.gr'
solve_bipartite_minimization(test_file) solve_bipartite_minimization(test_file)
5 5
4 4
6 6
Crossings: 0
...@@ -3,3 +3,4 @@ ...@@ -3,3 +3,4 @@
7 7
6 6
5 5
Crossings: 60
...@@ -2,3 +2,4 @@ ...@@ -2,3 +2,4 @@
7 7
8 8
5 5
Crossings: 4
...@@ -2,3 +2,4 @@ ...@@ -2,3 +2,4 @@
7 7
6 6
8 8
Crossings: 3
...@@ -3,3 +3,4 @@ ...@@ -3,3 +3,4 @@
7 7
5 5
6 6
Crossings: 17
...@@ -2,3 +2,4 @@ ...@@ -2,3 +2,4 @@
8 8
5 5
7 7
Crossings: 11
...@@ -2,3 +2,4 @@ ...@@ -2,3 +2,4 @@
8 8
7 7
6 6
Crossings: 3
...@@ -2,3 +2,4 @@ ...@@ -2,3 +2,4 @@
5 5
6 6
8 8
Crossings: 0
...@@ -2,3 +2,4 @@ ...@@ -2,3 +2,4 @@
6 6
7 7
9 9
Crossings: 6
...@@ -2,3 +2,4 @@ ...@@ -2,3 +2,4 @@
9 9
6 6
8 8
Crossings: 0
...@@ -4,3 +4,4 @@ ...@@ -4,3 +4,4 @@
10 10
7 7
8 8
Crossings: 0
...@@ -4,3 +4,4 @@ ...@@ -4,3 +4,4 @@
4 4
6 6
8 8
Crossings: 0
...@@ -8,3 +8,4 @@ ...@@ -8,3 +8,4 @@
14 14
15 15
16 16
Crossings: 13
...@@ -8,3 +8,4 @@ ...@@ -8,3 +8,4 @@
14 14
16 16
15 15
Crossings: 17
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment