Skip to content

Commit

Permalink
Adding tests for the 3D case Hamiltonian pathsu
Browse files Browse the repository at this point in the history
  • Loading branch information
emma58 committed Aug 12, 2024
1 parent 61bc6fa commit d467bd2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
22 changes: 13 additions & 9 deletions pyomo/contrib/piecewise/ordered_3d_j1_triangulation_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@
# This software is distributed under the 3-clause BSD License.
# ___________________________________________________________________________


"""
This code was used to generate the data structure in this file. It should never
need to be run again, but is here for the sake of documentation:
import networkx as nx
from pyomo.common.dependencies import networkx as nx
import itertools

# Get a list of 60 hamiltonian paths used in the 3d version of the ordered J1
# triangulation, and dump it to stdout.
if __name__ == '__main__':
def _get_double_cube_graph():
# Graph of a double cube
sign_vecs = list(itertools.product((-1, 1), repeat=3))
permutations = itertools.permutations(range(1, 4))
Expand All @@ -46,6 +39,17 @@
neighbor_simplex = (tuple(neighbor_sign), simplex[1])
G.add_edge(simplex, neighbor_simplex)

return G

"""
This code was used to generate the data structure in this file. It should never
need to be run again, but is here for the sake of documentation:
# Get a list of 60 hamiltonian paths used in the 3d version of the ordered J1
# triangulation, and dump it to stdout.
if __name__ == '__main__':
G = _get_double_cube_graph()
# Each of these simplices has an outward face in the specified direction; also,
# the +x simplex of one cube is adjacent to the -x simplex of a cube adjacent in
# the x direction, and similarly for the others.
Expand Down
19 changes: 17 additions & 2 deletions pyomo/contrib/piecewise/tests/test_triangulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@
from unittest import skipUnless
import pyomo.common.unittest as unittest
from pyomo.contrib.piecewise.ordered_3d_j1_triangulation_data import (
get_hamiltonian_paths
get_hamiltonian_paths,
_get_double_cube_graph,
)
from pyomo.contrib.piecewise.triangulations import (
get_unordered_j1_triangulation,
get_ordered_j1_triangulation,
_get_Gn_hamiltonian,
_get_grid_hamiltonian,
)
from pyomo.common.dependencies import numpy as np, numpy_available
from pyomo.common.dependencies import numpy as np, numpy_available, networkx_available
from math import factorial
import itertools

Expand Down Expand Up @@ -224,6 +225,20 @@ def test_grid_hamiltonian_paths(self):
self.check_grid_hamiltonian(3, 5)
self.check_grid_hamiltonian(4, 3)

@unittest.skipUnless(networkx_available, "Networkx is not available")
class TestHamiltonianPaths(unittest.TestCase):
def test_hamiltonian_paths(self):
G = _get_double_cube_graph()

paths = get_hamiltonian_paths()
self.assertEqual(len(paths), 60)

for ((s1, t1), (s2, t2)), path in paths.items():
# ESJ: I'm not quite sure how to check this is *the right* path
# given the key?

# Check it's Hamiltonian
self.assertEqual(len(path), 48)
# Check it's a path
for idx in range(1, 48):
self.assertTrue(G.has_edge(path[idx - 1], path[idx]))

0 comments on commit d467bd2

Please sign in to comment.