-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactions_graph.py
executable file
·184 lines (150 loc) · 5.49 KB
/
interactions_graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
# encoding: utf-8
import os
import sys
import matplotlib.pyplot as plt
import numpy as np
from analyzer import FormulaAnalyzer
from collections import defaultdict, Counter
from graphviz import Graph as GGraph, Digraph
from model import Model
from itertools import permutations
from graph_tool.all import *
styles = {
'graph': {
'label': 'Graph',
'fontsize': '12',
'fontcolor': 'white',
'bgcolor': '#888888',
'overlap': 'prism',
'outputorder': 'edgesfirst'
# 'rankdir': 'BT'
},
'nodes': {
'fontname': 'Helvetica',
'shape': 'hexagon',
'fontcolor': 'white',
'color': 'white',
'style': 'filled',
'fillcolor': '#006699',
},
'edges': {
'color': 'black',
'arrowhead': 'open',
'fontname': 'Courier',
'fontsize': '12',
'fontcolor': 'white',
}
}
def _apply_styles(graph, styles):
graph.graph_attr.update(
('graph' in styles and styles['graph']) or {}
)
graph.node_attr.update(
('nodes' in styles and styles['nodes']) or {}
)
graph.edge_attr.update(
('edges' in styles and styles['edges']) or {}
)
return graph
# Creating interaction graph between variables using clauses
def create_interactions_graph(clauses, f):
dot = GGraph(comment='Interactions graph', engine='sfdp')
seen_vars = set()
edges_between_vars = defaultdict(int)
for clause in clauses:
for lit in clause:
var = f(lit)
if var not in seen_vars:
seen_vars.add(var)
dot.node(str(var), label=str(var))
for clause in clauses:
l = len(clause)
for i in xrange(l):
for j in xrange(i+1, l):
edges_between_vars[(str(f(clause[i])), str(f(clause[j])))] += 1
for interacting_vars, weight in edges_between_vars.iteritems():
dot.edge(interacting_vars[0], interacting_vars[1], weight=str(weight))
print edges_between_vars
dot = _apply_styles(dot, styles)
# print dot.source
dot.render(os.path.join('images', 'interactions_graph.gv'), view=True)
# Creating implications graph
def create_implications_graph(clauses):
dot = Digraph(comment='Implications graph', engine='sfdp')
seen_vars = set()
edges = set()
# for clause in clauses:
# for lit in clause:
# if lit not in seen_vars:
# seen_vars.add(lit)
# dot.node(str(lit), label=str(lit))
for clause in clauses:
for perm in permutations(clause):
l = len(perm)
for i in xrange(l-2):
edges.add((-perm[i], -perm[i+1]))
edges.add((-perm[l-2], perm[l-1]))
for (head, tail) in edges:
dot.edge(str(head).replace('-','~'), str(tail).replace('-', '~'))
dot = _apply_styles(dot, styles)
dot.render(os.path.join('images', 'implications_graph.gv'), view=True)
# If two clauses have conflicting literals then there is an edge between those clauses
def create_conflicts_graph(clauses):
dot = GGraph(comment='Conflicts graph', engine='sfdp')
for i in xrange(len(clauses)):
dot.node(str(i), label=str(i))
for i in xrange(len(clauses)):
for j in xrange(i+1, len(clauses)):
clause_i = clauses[i]
clause_j = clauses[j]
edge_labels = []
for lit in clause_i:
if -lit in clause_j:
var = abs(lit)
edge_labels.append(str(var))
if len(edge_labels) > 0:
dot.edge(str(i), str(j), label=','.join(edge_labels))
dot = _apply_styles(dot, styles)
dot.render(os.path.join('images', 'conflicts_graph.gv'), view=True)
# Creating interactions graphs using graph-tool
def create_interactions_graph_gt(clauses, f):
ug = Graph(directed=False)
labels = ug.new_vertex_property("string")
e_weights = ug.new_edge_property("double")
vertices = {}
edges_between_vars = defaultdict(int)
for clause in clauses:
for lit in clause:
var = f(lit)
if var not in vertices:
v = ug.add_vertex()
vertices[var] = v
labels[v] = str(var)
for clause in clauses:
l = len(clause)
for i in xrange(l):
for j in xrange(i+1, l):
f_clause_i = f(clause[i])
f_clause_j = f(clause[j])
mini = min(f_clause_i, f_clause_j)
maxi = max(f_clause_i, f_clause_j)
edges_between_vars[(mini, maxi)] += 1
for interacting_vars, weight in edges_between_vars.iteritems():
print interacting_vars, weight
e = ug.add_edge(vertices[interacting_vars[0]], vertices[interacting_vars[1]])
e_weights[e] = weight
print 'Planarity:', is_planar(ug)
graph_draw(ug, vertex_text=labels, edge_pen_width = e_weights, vertex_font_size=18, output_size=(800, 800), output="images/interactions_graph.png")
def main():
n = sys.argv[1]
f = Model.parse_dimacs('data/%s.dimacs' % n)
f.unit_propagate()
# create_interactions_graph(f.clauses, abs)
# create_interactions_graph(f.clauses, lambda x: x)
# create_interactions_graph_gt(f.clauses, abs)
# create_interactions_graph_gt(f.clauses, lambda x: x)
create_conflicts_graph(f.clauses)
# create_implications_graph([[1, 2, 3], [1, 2, -3], [1, -2, 3], [1, -2, -3], [-1, 2, 3], [-1, 2, -3], [-1, -2, 3]])
if __name__ == '__main__':
main()