Permalink
Browse files

merge af793da:7f661d3 from branch jbking-py3 (sorry for the awkward m…

…erging)
  • Loading branch information...
takemaru committed Mar 2, 2016
1 parent 6042f97 commit 71bb3a9a4ad029a2de2798ef3a2bbdfabb7a27c1
View
@@ -1,29 +1,6 @@
include CMakeLists.txt README.md LICENSE.txt \
cmake/Modules/FindGMP.cmake \
doc/fig1.png doc/fig10.png doc/fig2.png doc/fig3.png doc/fig4.png \
doc/fig5.png doc/fig6.png doc/fig7.png doc/fig8.png doc/fig9.png \
graphillion/test/__init__.py graphillion/test/graphset.py \
graphillion/test/setset.py graphillion/test/tutorial.py \
src/CMakeLists.txt src/pygraphillion.h src/SAPPOROBDD/BDD.h \
src/SAPPOROBDD/ZBDD.h src/SAPPOROBDD/bddc.h src/graphillion/setset.h \
src/graphillion/type.h src/graphillion/util.h src/graphillion/zdd.h \
src/subsetting/dd/DataTable.hpp src/subsetting/dd/DdBuilder.hpp \
src/subsetting/dd/DdEval.hpp src/subsetting/dd/DdNode.hpp \
src/subsetting/dd/DdNodeId.hpp src/subsetting/dd/DdNodeTable.hpp \
src/subsetting/dd/DdSpec.hpp src/subsetting/dd/PathCounter.hpp \
src/subsetting/dd/ZddStructure.hpp src/subsetting/eval/ToZBDD.hpp \
src/subsetting/spec/BinaryOperation.hpp \
src/subsetting/spec/DegreeConstraint.hpp \
src/subsetting/spec/FrontierBasedSearch.hpp \
src/subsetting/spec/LinearConstraints.hpp \
src/subsetting/spec/Lookahead.hpp \
src/subsetting/spec/SapporoZdd.hpp \
src/subsetting/spec/SizeConstraint.hpp \
src/subsetting/util/BigNumber.hpp src/subsetting/util/Graph.hpp \
src/subsetting/util/IntSubset.hpp src/subsetting/util/MemoryPool.hpp \
src/subsetting/util/MessageHandler.hpp \
src/subsetting/util/MyHashTable.hpp src/subsetting/util/MyList.hpp \
src/subsetting/util/MyVector.hpp \
src/subsetting/util/ResourceUsage.hpp \
src/subsetting/util/demangle.hpp \
src/test/graphset.cc src/test/setset.cc
doc/fig5.png doc/fig6.png doc/fig7.png doc/fig8.png doc/fig9.png
graft graphillion/test
graft src
View
@@ -21,10 +21,13 @@
"""
from functools import partial
from builtins import range, int
from future.utils import viewitems
import _graphillion
from graphillion import setset
import pickle
class GraphSet(object):
"""Represents and manipulates a set of graphs.
@@ -128,15 +131,14 @@ def __init__(self, graphset_or_constraints=None):
obj = l
elif isinstance(obj, dict): # constraints
d = {}
for k, l in obj.iteritems():
for k, l in viewitems(obj):
d[k] = [GraphSet._conv_edge(e) for e in l]
obj = d
self._ss = setset(obj)
methods = ['graphs', 'connected_components', 'cliques', 'trees',
'forests', 'cycles', 'paths']
for method in methods:
expr = 'self.%s = partial(GraphSet.%s, graphset=self)'
exec expr % (method, method)
setattr(self, method, partial(getattr(GraphSet, method), graphset=self))
def copy(self):
"""Returns a new GraphSet with a shallow copy of `self`.
@@ -772,7 +774,7 @@ def __contains__(self, obj):
return obj in self._ss
elif type == 'vertex':
return len([e for e in obj if e in self._ss]) > 0
raise TypeError, obj
raise TypeError(obj)
def add(self, graph_or_edge):
"""Adds a given graph or edge to `self`.
@@ -808,7 +810,7 @@ def add(self, graph_or_edge):
if type == 'graph' or type == 'edge':
self._ss.add(obj)
else:
raise TypeError, graph_or_edge
raise TypeError(graph_or_edge)
def remove(self, obj):
"""Removes a given graph, edge, or vertex from `self`.
@@ -847,7 +849,7 @@ def remove(self, obj):
for edge in obj:
self.remove(edge)
else:
raise TypeError, obj
raise TypeError(obj)
return None
def discard(self, obj):
@@ -887,7 +889,7 @@ def discard(self, obj):
for edge in obj:
self.discard(edge)
else:
raise TypeError, obj
raise TypeError(obj)
return None
def pop(self):
@@ -959,7 +961,7 @@ def flip(self, edge):
if type == 'edge':
self._ss.flip(edge)
else:
raise TypeError, edge
raise TypeError(edge)
def minimal(self):
"""Returns a new GraphSet of minimal graphs.
@@ -1412,7 +1414,7 @@ def included(self, obj):
elif type == 'graph':
return self.included(GraphSet([obj]))
else:
raise TypeError, obj
raise TypeError(obj)
def choice(self):
"""Returns an arbitrary graph from `self`.
@@ -1568,7 +1570,7 @@ def set_universe(universe, traversal='bfs', source=None):
universe = GraphSet.converters['to_edges'](universe)
for e in universe:
if e[:2] in edges or (e[1], e[0]) in edges:
raise KeyError, e
raise KeyError(e)
edges.append(e[:2])
if len(e) > 2:
GraphSet._weights[e[:2]] = e[2]
@@ -1612,7 +1614,7 @@ def graphs(vertex_groups=None, degree_constraints=None, num_edges=None,
Examples: a set of paths from vertex 1 to vertex 6
>>> start = 1
>>> end = 6
>>> zero_or_two = xrange(0, 3, 2)
>>> zero_or_two = range(0, 3, 2)
>>> degree_constraints = {start: 1, end: 1,
... 2: zero_or_two, 3: zero_or_two,
... 4: zero_or_two, 5: zero_or_two}
@@ -1661,7 +1663,7 @@ def graphs(vertex_groups=None, degree_constraints=None, num_edges=None,
graph = []
for e in setset.universe():
assert e[0] in GraphSet._vertices and e[1] in GraphSet._vertices
graph.append((pickle.dumps(e[0]), pickle.dumps(e[1])))
graph.append((pickle.dumps(e[0], protocol=0), pickle.dumps(e[1], protocol=0)))
vg = []
nc = 0
@@ -1672,27 +1674,27 @@ def graphs(vertex_groups=None, degree_constraints=None, num_edges=None,
else:
for v in vs:
if v not in GraphSet._vertices:
raise KeyError, v
vg.append([pickle.dumps(v) for v in vs])
raise KeyError(v)
vg.append([pickle.dumps(v, protocol=0) for v in vs])
if not vg and nc == 0:
nc = -1
dc = None
if degree_constraints is not None:
dc = {}
for v, r in degree_constraints.iteritems():
for v, r in viewitems(degree_constraints):
if v not in GraphSet._vertices:
raise KeyError, v
if isinstance(r, (int, long)):
dc[pickle.dumps(v)] = (r, r + 1, 1)
raise KeyError(v)
if isinstance(r, int):
dc[pickle.dumps(v, protocol=0)] = (r, r + 1, 1)
elif len(r) == 1:
dc[pickle.dumps(v)] = (r[0], r[0] + 1, 1)
dc[pickle.dumps(v, protocol=0)] = (r[0], r[0] + 1, 1)
else:
dc[pickle.dumps(v)] = (r[0], r[-1] + 1, r[1] - r[0])
dc[pickle.dumps(v, protocol=0)] = (r[0], r[-1] + 1, r[1] - r[0])
ne = None
if num_edges is not None:
if isinstance(num_edges, (int, long)):
if isinstance(num_edges, int):
ne = (num_edges, num_edges + 1, 1)
elif len(num_edges) == 1:
ne = (num_edges[0], num_edges[0] + 1, 1)
@@ -1708,8 +1710,8 @@ def graphs(vertex_groups=None, degree_constraints=None, num_edges=None,
for c in linear_constraints:
expr = []
for we in c[0]:
u = pickle.dumps(we[0])
v = pickle.dumps(we[1])
u = pickle.dumps(we[0], protocol=0)
v = pickle.dumps(we[1], protocol=0)
w = float(we[2]) if len(we) >= 3 else 1.0
expr.append((u, v, w))
min = float(c[1][0])
@@ -1763,8 +1765,8 @@ def cliques(k, graphset=None):
"""
dc = {}
for v in GraphSet._vertices:
dc[v] = xrange(0, k, k - 1)
ne = range(k * (k - 1) / 2, k * (k - 1) / 2 + 1)
dc[v] = range(0, k, k - 1)
ne = range(k * (k - 1) // 2, k * (k - 1) // 2 + 1)
return GraphSet.graphs(vertex_groups=[[]], degree_constraints=dc,
num_edges=ne, graphset=graphset)
@@ -1793,7 +1795,7 @@ def trees(root=None, is_spanning=False, graphset=None):
if is_spanning:
dc = {}
for v in GraphSet._vertices:
dc[v] = xrange(1, len(GraphSet._vertices))
dc[v] = range(1, len(GraphSet._vertices))
return GraphSet.graphs(vertex_groups=vg, degree_constraints=dc,
no_loop=True, graphset=graphset)
@@ -1824,7 +1826,7 @@ def forests(roots, is_spanning=False, graphset=None):
dc = {}
for v in GraphSet._vertices:
if v not in roots:
dc[v] = xrange(1, len(GraphSet._vertices))
dc[v] = range(1, len(GraphSet._vertices))
return GraphSet.graphs(vertex_groups=vg, degree_constraints=dc,
no_loop=True, graphset=graphset)
@@ -1848,7 +1850,7 @@ def cycles(is_hamilton=False, graphset=None):
"""
dc = {}
for v in GraphSet._vertices:
dc[v] = 2 if is_hamilton else xrange(0, 3, 2)
dc[v] = 2 if is_hamilton else range(0, 3, 2)
return GraphSet.graphs(vertex_groups=[[]], degree_constraints=dc,
graphset=graphset)
@@ -1874,7 +1876,7 @@ def paths(terminal1, terminal2, is_hamilton=False, graphset=None):
if v in (terminal1, terminal2):
dc[v] = 1
else:
dc[v] = 2 if is_hamilton else xrange(0, 3, 2)
dc[v] = 2 if is_hamilton else range(0, 3, 2)
return GraphSet.graphs(vertex_groups=[[terminal1, terminal2]],
degree_constraints=dc,
no_loop=True, graphset=graphset)
@@ -1947,7 +1949,7 @@ def _conv_arg(obj):
edges = GraphSet.converters['to_edges'](obj)
return 'graph', set([GraphSet._conv_edge(e) for e in edges])
except TypeError: # if fail to convert obj into edge list
raise KeyError, obj
raise KeyError(obj)
@staticmethod
def _conv_graph(obj):
@@ -1956,20 +1958,20 @@ def _conv_graph(obj):
@staticmethod
def _conv_edge(edge):
if not isinstance(edge, tuple) or len(edge) < 2:
raise KeyError, edge
raise KeyError(edge)
if len(edge) > 2:
edge = edge[:2]
if edge in setset._obj2int:
return edge
elif (edge[1], edge[0]) in setset._obj2int:
return (edge[1], edge[0])
raise KeyError, edge
raise KeyError(edge)
@staticmethod
def _conv_ret(obj):
if isinstance(obj, (set, frozenset)): # a graph
return GraphSet.converters['to_graph'](sorted(list(obj)))
raise TypeError, obj
raise TypeError(obj)
converters = { 'to_graph': lambda edges: edges,
'to_edges': lambda graph: graph }
View
@@ -20,6 +20,8 @@
"""Module for a set of sets.
"""
from builtins import range
from future.utils import viewitems
import _graphillion
@@ -61,7 +63,7 @@ def __init__(self, setset_or_constraints=None):
obj = l
elif isinstance(obj, dict): # constraints
d = {}
for k, l in obj.iteritems():
for k, l in viewitems(obj):
d[k] = [setset._conv_elem(e) for e in l]
obj = d
_graphillion.setset.__init__(self, obj)
@@ -120,12 +122,12 @@ def flip(self, elem=None):
def __iter__(self):
i = _graphillion.setset.iter(self)
while (True):
yield setset._conv_ret(i.next())
yield setset._conv_ret(next(i))
def rand_iter(self):
i = _graphillion.setset.rand_iter(self)
while (True):
yield setset._conv_ret(i.next())
yield setset._conv_ret(next(i))
def min_iter(self, weights=None, default=1):
return self._optimize(weights, default, _graphillion.setset.min_iter)
@@ -136,12 +138,12 @@ def max_iter(self, weights=None, default=1):
def _optimize(self, weights, default, generator):
ws = [default] * (_graphillion._num_elems() + 1)
if weights:
for e, w in weights.iteritems():
for e, w in viewitems(weights):
i = setset._obj2int[e]
ws[i] = w
i = generator(self, ws)
while (True):
yield setset._conv_ret(i.next())
yield setset._conv_ret(next(i))
def supersets(self, obj):
if (not isinstance(obj, setset)):
@@ -159,7 +161,7 @@ def choice(self):
def probability(self, probabilities):
ps = [-1] * (_graphillion._num_elems() + 1)
for e, p in probabilities. iteritems():
for e, p in viewitems(probabilities):
i = setset._obj2int[e]
ps[i] = p
assert len([p for p in ps[1:] if p < 0 or 1 < p]) == 0
@@ -176,7 +178,7 @@ def loads(s):
@staticmethod
def set_universe(universe):
if len(universe) != len(set(universe)):
raise ValueError, 'duplicated elements found'
raise ValueError('duplicated elements found')
_graphillion._num_elems(0)
setset._obj2int = {}
setset._int2obj = [None]
@@ -192,9 +194,9 @@ def universe():
@staticmethod
def _check_universe():
assert len(setset._int2obj) == _graphillion._num_elems() + 1
for e, i in setset._obj2int.iteritems():
for e, i in viewitems(setset._obj2int):
assert e == setset._int2obj[i]
for i in xrange(1, len(setset._int2obj)):
for i in range(1, len(setset._int2obj)):
e = setset._int2obj[i]
assert i == setset._obj2int[e]
@@ -203,7 +205,7 @@ def _add_elem(elem):
assert elem not in setset._obj2int
if len(setset._obj2int) >= _graphillion._elem_limit():
m = 'not more than %d elements used' % _graphillion._elem_limit()
raise RuntimeError, m
raise RuntimeError(m)
i = len(setset._int2obj)
_graphillion.setset([set([i])])
setset._obj2int[elem] = i
@@ -232,7 +234,7 @@ def _conv_ret(obj):
for e in obj:
ret.add(setset._int2obj[e])
return ret
raise TypeError, obj
raise TypeError(obj)
_obj2int = {}
_int2obj = [None]
Oops, something went wrong.

0 comments on commit 71bb3a9

Please sign in to comment.