From 992796566125be0f1c0d5078b06f9829644c75c2 Mon Sep 17 00:00:00 2001 From: Dave Date: Wed, 4 Jun 2025 17:06:36 -0600 Subject: [PATCH 1/3] Completed basic version of Vertex-Cover printing script. --- Vertex-Cover/print/print.py | 79 +++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Vertex-Cover/print/print.py diff --git a/Vertex-Cover/print/print.py b/Vertex-Cover/print/print.py new file mode 100644 index 0000000..7c1458d --- /dev/null +++ b/Vertex-Cover/print/print.py @@ -0,0 +1,79 @@ +# Use: +# clingo k_vertex_cover.lp instance.lp -c n= | python3 print/print.py +# clingo min_vertex_cover.lp instance.lp | python3 print/print.py +# where instance.lp contains node and edge predicates. +# +# Note: +# The final pipe and call to dot can be omitted if graphviz is not present. +# To be ran in previous directory where instance.lp, k_clique.lp, and max_clique.lp are. + + +import re + + +output = '''// Graph visualization using dot +strict graph { + node [color=black] + edge [color=black] + +''' + +# Reading in graph +instance = open("instance.lp", "r") +instance_lines = instance.readlines() + +for line in instance_lines: + if line.startswith("node"): + + line = re.sub(r"node\(", " ", line) + line = re.sub(r" ; ", " ", line) + line = re.sub(r"\).", "\n", line) + + output += line + + elif line.startswith("edge"): + + line = re.sub(r"edge\(", " ", line) + line = re.sub(r", ", " -- ", line) + + if " (" in line: + line = re.sub(r" \(", " {", line) + line = re.sub(r"\)\).", "}", line) + + else: + line = re.sub(r"\).", "", line) + + output += line + +instance.close() + +# Reading in clingo output +toks_last = None +toks = None +toks_next = input().split() + +while not (toks_next[0].startswith("SAT") or toks_next[0].startswith("UNSAT") or toks_next[0].startswith("OPT")): + toks_last = toks + toks = toks_next + toks_next = input().split() + +if toks_next[0].startswith("SAT") or toks_next[0].startswith("OPT"): + if toks_next[0].startswith("OPT"): + toks = toks_last + + output += ''' + subgraph cluster_0 { + node [color=red,shape=doubleoctogon] + style=invis + +''' + + for t in toks: + output += " " + t[6:-1] + " [color=red,shape=doubleoctagon]\n" + + output += "\n }\n" + + print(output + "}\n") + +elif toks_next[0].startswith("UNSAT"): + print("No solution found.") From 1db4418123e74ab863f6519c1da4233aa3b045ef Mon Sep 17 00:00:00 2001 From: Dave Date: Thu, 5 Jun 2025 13:50:12 -0600 Subject: [PATCH 2/3] Finished basic vertec_cover.lp printing algorithm. --- Vertex-Cover/print/print.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Vertex-Cover/print/print.py b/Vertex-Cover/print/print.py index 7c1458d..b9548b2 100644 --- a/Vertex-Cover/print/print.py +++ b/Vertex-Cover/print/print.py @@ -1,6 +1,6 @@ # Use: -# clingo k_vertex_cover.lp instance.lp -c n= | python3 print/print.py -# clingo min_vertex_cover.lp instance.lp | python3 print/print.py +# clingo k_vertex_cover.lp instance.lp -c n= | python3 print/print.py | dot -Tpdf -o images/vertex_cover.pdf +# clingo min_vertex_cover.lp instance.lp | python3 print/print.py | dot -Tpdf -o images/vertex_cover.pdf # where instance.lp contains node and edge predicates. # # Note: @@ -63,7 +63,6 @@ output += ''' subgraph cluster_0 { - node [color=red,shape=doubleoctogon] style=invis ''' From 65206e27b308ae29b4ebbbb549f44ef1323eab82 Mon Sep 17 00:00:00 2001 From: "Joshua T. Guerin" Date: Thu, 5 Jun 2025 13:56:35 -0600 Subject: [PATCH 3/3] Removed the directory path--not really worried where the user puts the output. --- Vertex-Cover/print/print.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Vertex-Cover/print/print.py b/Vertex-Cover/print/print.py index b9548b2..cb2b807 100644 --- a/Vertex-Cover/print/print.py +++ b/Vertex-Cover/print/print.py @@ -1,6 +1,6 @@ # Use: -# clingo k_vertex_cover.lp instance.lp -c n= | python3 print/print.py | dot -Tpdf -o images/vertex_cover.pdf -# clingo min_vertex_cover.lp instance.lp | python3 print/print.py | dot -Tpdf -o images/vertex_cover.pdf +# clingo k_vertex_cover.lp instance.lp -c n= | python3 print/print.py | dot -Tpdf -o vertex_cover.pdf +# clingo min_vertex_cover.lp instance.lp | python3 print/print.py | dot -Tpdf -o vertex_cover.pdf # where instance.lp contains node and edge predicates. # # Note: