Skip to content

Commit

Permalink
some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SHoltzen committed Feb 22, 2019
1 parent 8b27906 commit b4b071e
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 25 deletions.
116 changes: 116 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
103 changes: 85 additions & 18 deletions orbitgen.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
from sage.all import *
import cProfile, pstats, StringIO
import itertools
from sage.groups.perm_gps.partn_ref.refinement_graphs import isomorphic

### foldrep
def findsubsets(S,m):
return set(itertools.combinations(S, m))

### foldrep: applies f to each canonical representative, maintains an
### accumulator
### graph: a sage graph
### colors: a list of lists which partitions the vertices in G into colors
### colors[0]: True vertices
### colors[1]: False vertices
### colors[3] is reserved for the algorithm
### fold: apply a fold method to each orbit
def foldrep(graph, colors, acc, f):
# TODO: reduce number of isomorphism calls if possible
acc = f(acc, graph, colors)
if len(colors[0]) >= (len(graph.vertices()) / 2):
return acc
Expand Down Expand Up @@ -36,15 +49,74 @@ def foldrep(graph, colors, acc, f):
return acc

### genrep: generates a representative of each orbit class of a graph
### graph: a sage graph
### colors: a list of lists which partitions the vertices in G into colors
### colors[0]: True vertices
### colors[1]: False vertices
### colors[3] is reserved for the algorithm
### fold: apply a fold method to each orbit
def genrep(G):
colors = [[], [i for i in G.vertices()]]
return foldrep(G, colors, [], lambda acc, g, color: acc + [(g, color)])
# folding function
def add_vertex(acc, g, color):
# check if inversion is isomorphic to the current graph
if len(color[0]) < len(g.vertices()) / 2.0:
return acc + [color] + [color[::-1]]

# we know it's a 50/50 color split, check if this is an isomorphic
# inversion or not
g2 = Graph(g)
g.canonical_label(partition=color)
g2.canonical_label(partition=color[::-1])
if g == g2:
# inversions are isomorphic, return one
return acc + [color]
else:
# inversion are non-isomorphic, return both
print("non isomorphic inversion: %s, %s" % (color, color[::-1]))
return acc + [color] + [color[::-1]]
return foldrep(G, colors, [], add_vertex)

### generates a friends and smokers graph with n people
def gen_friends_smokers(n):
g = Graph(sparse=True)
# make n smoker vertices
smokers = [x for x in range(0,n)]
# connect all the smokers
smokeredges = findsubsets(smokers, 2)
# make friends
friends = []
friendedges = []
count = n
for (s1,s2) in findsubsets(smokers, 2):
friends += [count]
friendedges += [(s1, count), (s2, count)]
count += 1

g.add_vertices(smokers)
g.add_vertices(friends)
g.add_edges(friendedges)
g.add_edges(smokeredges)
return g

# generates a graph which is fully-connected in m and connected across n
def gen_pigeonhole(n,m):
g = Graph()
v = []
e = []
# generate vertices
for x in range(0,n):
for y in range(0,m):
v += [x*m + y]

# generate edges
# generate fully connected graph in n
for x in range(0,n):
for y in findsubsets(range(0, m), 2):
e += [(x*m + y[0], x*m + y[1])]

# connect between n and m
for x in range(0,n):
for y in range (0,m):
e += [(x*m + y, ((x + 1) % n)*m + y)]

g.add_vertices(v)
g.add_edges(e)
return g

# given a graph g, count the number of distinct 2-colorings using Polya's
# theorem.
Expand All @@ -58,19 +130,14 @@ def main():
# pr = cProfile.Profile()
# pr.enable()

G = graphs.CycleGraph(4)
# G = gen_friends_smokers(3)
G = graphs.CycleGraph(8)
num_vert = len(G.vertices())
r = genrep(G)
tot = 0
for k in r:
if len(k[1][0]) == num_vert/2.0:
tot += 1
else:
tot+= 2
print(k)
print("Found configurations: %d" % tot)
for x in r:
print(x)
print("Found configurations: %d" % len(r))
print("Expected configurations: %d" % count_num_distinct(G))
# print("size: %d" % len(r))

# pr.disable()
# s = StringIO.StringIO()
Expand Down
Binary file removed orbitgen.pyc
Binary file not shown.
8 changes: 1 addition & 7 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ def count_num_generated(G):
colors = [[], [i for i in G.vertices()]]
num_vert = len(G.vertices())
l = orbitgen.genrep(G)
tot = 0
for k in l:
if len(k[1][0]) == num_vert/2.0:
tot += 1
else:
tot+= 2
return tot
return len(l)

class TestGen(unittest.TestCase):
def test_complete(self):
Expand Down

0 comments on commit b4b071e

Please sign in to comment.