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
No related branches found
No related tags found
1 merge request!1Kleines Framework aufgestellt, bereitgestellter Tester noch nicht getestet,...
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
Showing
with 67 additions and 11 deletions
......@@ -21,7 +21,7 @@ def count_crossings_via_variables(c_vars):
for c_var in c_vars.values():
if c_var.varValue == 1:
crossings += 1
print("Crossings:", crossings)
return(crossings)
def solve_bipartite_minimization(graph_file):
logging.info(f"Prozess für {graph_file} gestartet")
......@@ -57,7 +57,23 @@ def solve_bipartite_minimization(graph_file):
# Zielfunktion, die minimiert werden soll
prob += lpSum(c.values())
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
for (i, j) in edges:
for (k, l) in edges:
......@@ -71,6 +87,7 @@ def solve_bipartite_minimization(graph_file):
if l > j:
prob += c[(i, j, k, l)] == 1 - y[(j, l)]
logging.info("Crossing Constraints aufgestellt.")
"""
initial_crossings = count_initial_crossings(edges)
logging.info(f"Initial crossings edges: {initial_crossings}")
......@@ -135,20 +152,32 @@ def solve_bipartite_minimization(graph_file):
if in_degree[neighbor] == 0:
zero_in_degree_queue.append(neighbor)
for b in sorted_b:
print(f"{b}")
""" # Ausgabe der sortierten Knoten
a = count_crossings_via_variables(c)
# Ausgabe der sortierten Knoten
os.makedirs(os.path.dirname(output_file), exist_ok=True)
with open(output_file, 'w') as f:
for b in sorted_b:
f.write(f"{b}\n")
print(f"{b}")
count_crossings_via_variables(c)
logging.info(f"Ergebnisse in {output_file} gespeichert")"""
f.write(f"Crossings: {a}\n")
print("Crossings: ", a)
logging.info(f"Ergebnisse in {output_file} gespeichert")
else:
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'
solve_bipartite_minimization(test_file)
#solve_bipartite_minimization(test_file)
......@@ -48,7 +48,19 @@ def solve_bipartite_minimization(graph_file):
# Zielfunktion, die minimiert werden soll
prob += lpSum(c.values())
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
for (i, j) in edges:
for (k, l) in edges:
......@@ -62,6 +74,8 @@ def solve_bipartite_minimization(graph_file):
if l > j:
prob += c[(i, j, k, l)] == 1 - y[(j, l)] # Verwende .get() für sichere Zugriffe
logging.info("Crossing Constraints aufgestellt.")
prob.solve()
logging.info(f"Status der Lösung: {LpStatus[prob.status]}")
......@@ -76,7 +90,6 @@ def solve_bipartite_minimization(graph_file):
for key, var in c.items():
print(f"c[{key}] = {var.varValue}")
"""
if prob.status == LpStatusOptimal:
logging.info("Optimale Lösung gefunden. Ergebnisse werden gespeichert.")
......@@ -122,5 +135,5 @@ def solve_bipartite_minimization(graph_file):
#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)
5
4
6
Crossings: 0
......@@ -3,3 +3,4 @@
7
6
5
Crossings: 60
......@@ -2,3 +2,4 @@
7
8
5
Crossings: 4
......@@ -2,3 +2,4 @@
7
6
8
Crossings: 3
......@@ -3,3 +3,4 @@
7
5
6
Crossings: 17
......@@ -2,3 +2,4 @@
8
5
7
Crossings: 11
......@@ -2,3 +2,4 @@
8
7
6
Crossings: 3
......@@ -2,3 +2,4 @@
5
6
8
Crossings: 0
......@@ -2,3 +2,4 @@
6
7
9
Crossings: 6
......@@ -2,3 +2,4 @@
9
6
8
Crossings: 0
......@@ -4,3 +4,4 @@
10
7
8
Crossings: 0
......@@ -4,3 +4,4 @@
4
6
8
Crossings: 0
......@@ -8,3 +8,4 @@
14
15
16
Crossings: 13
......@@ -8,3 +8,4 @@
14
16
15
Crossings: 17
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment