diff --git a/src/lava/lib/optimization/utils/generators/mis.py b/src/lava/lib/optimization/utils/generators/mis.py index 3168f6d0..4775db1c 100644 --- a/src/lava/lib/optimization/utils/generators/mis.py +++ b/src/lava/lib/optimization/utils/generators/mis.py @@ -3,9 +3,8 @@ # See: https://spdx.org/licenses/ -import numpy as np -import numpy.typing as npt import networkx as netx +import numpy as np from lava.lib.optimization.problems.problems import QUBO @@ -65,7 +64,7 @@ def from_random_uniform( n=num_vertices, m=int(0.5 * density * num_vertices**2), directed=False, - seed=seed + seed=seed, ) adjacency = np.array(netx.adjacency_matrix(graph).toarray()) return cls(adjacency_matrix=adjacency) @@ -76,7 +75,7 @@ def from_watts_strogatz( num_vertices: int, num_neighbors: int, connection_prob: float, - seed: int = 0 + seed: int = 0, ) -> "MISProblem": """ Instantiate a new MIS problem, based on a random Watts-Strogatz graph diff --git a/tests/lava/lib/optimization/utils/generators/test_mis.py b/tests/lava/lib/optimization/utils/generators/test_mis.py index 165ac5d6..b68e6796 100644 --- a/tests/lava/lib/optimization/utils/generators/test_mis.py +++ b/tests/lava/lib/optimization/utils/generators/test_mis.py @@ -3,11 +3,9 @@ # See: https://spdx.org/licenses/ import unittest + import numpy as np -from lava.lib.optimization.solvers.generic.solver import ( - OptimizationSolver, SolverConfig -) from lava.lib.optimization.utils.generators.mis import MISProblem @@ -15,9 +13,9 @@ class TestMISProblem(unittest.TestCase): """Unit tests for MISProblem class.""" def setUp(self): - self.adjacency = np.array([ - [0, 1, 1, 0], [1, 0, 0, 0], [1, 0, 0, 1], [0, 0, 1, 0] - ]) + self.adjacency = np.array( + [[0, 1, 1, 0], [1, 0, 0, 0], [1, 0, 0, 1], [0, 0, 1, 0]] + ) self.num_vertices = self.adjacency.shape[0] self.num_edges = np.count_nonzero(self.adjacency) self.problem = MISProblem(adjacency_matrix=self.adjacency) @@ -26,55 +24,38 @@ def test_create_obj(self): """Tests correct instantiation of MISProblem object.""" self.assertIsInstance(self.problem, MISProblem) - def test_num_vertices_prop(self): + def test_num_vertices_property(self): """Tests correct value of num_vertices property.""" self.assertEqual(self.problem.num_vertices, self.num_vertices) - def test_num_edges_prop(self): + def test_num_edges_property(self): """Tests correct value of num_edges property.""" self.assertEqual(self.problem.num_edges, self.num_edges) + def test_adjacency_matrix_property(self): + """Tests correct value of adjacency_matrix.""" + self.assertTrue((self.adjacency == self.problem.adjacency_matrix).all()) + def test_get_graph(self): """Tests that the graph contains the correct number of nodes and edges.""" graph = self.problem.get_graph() - number_of_nodes = 10 - possible_connections = (number_of_nodes ** 2 - number_of_nodes) / 2 - self.assertEqual(graph.number_of_nodes(), number_of_nodes) - self.assertAlmostEqual(graph.number_of_edges() / possible_connections, - self.connection_prob, delta=0.1) - - def test_get_graph_matrix(self): - """Tests the correct adjacency matrix is returned.""" - matrix = self.problem.get_graph_matrix() - correct_matrix = np.array([[0, 0, 1, 1, 1, 1, 1, 0, 1, 1], - [0, 0, 0, 1, 1, 1, 1, 1, 1, 1], - [1, 0, 0, 1, 1, 0, 1, 1, 1, 1], - [1, 1, 1, 0, 0, 0, 1, 1, 1, 1], - [1, 1, 1, 0, 0, 1, 1, 1, 1, 1], - [1, 1, 0, 0, 1, 0, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1, 0, 0, 1, 0], - [0, 1, 1, 1, 1, 1, 0, 0, 1, 1], - [1, 1, 1, 1, 1, 1, 1, 1, 0, 1], - [1, 1, 1, 1, 1, 1, 0, 1, 1, 0]]) - self.assertTrue((correct_matrix == matrix).all()) + self.assertEqual(graph.number_of_nodes(), self.num_vertices) + self.assertEqual(graph.number_of_edges() * 2, self.num_edges) def test_get_complement_graph(self): """Tests that the complement graph contains the correct number of nodes and edges.""" graph = self.problem.get_complement_graph() - number_of_nodes = 10 - number_of_edges = 8 self.assertEqual(graph.number_of_nodes(), self.num_vertices) - self.assertEqual(2 * graph.number_of_edges(), self.num_edges) + self.assertEqual(graph.number_of_edges() * 2, self.num_edges) def test_get_complement_graph_matrix(self): """Tests the correct complement graph adjacency matrix is returned.""" matrix = self.problem.get_complement_graph_matrix() - correct_matrix = np.array([[0, 0, 0, 1], - [0, 0, 1, 1], - [0, 1, 0, 0], - [1, 1, 0, 0]]) + correct_matrix = np.array( + [[0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 0, 0], [1, 1, 0, 0]] + ) self.assertTrue((matrix == correct_matrix).all()) def test_get_as_qubo(self): @@ -82,7 +63,7 @@ def test_get_as_qubo(self): w_diag = 1 w_off = 4 qubo = self.problem.get_as_qubo(w_diag, w_off) - self.assertTrue(np.all(qubo.q[np.diag_indices(10)] == -w_diag)) + self.assertTrue(np.all(qubo.q[np.diag_indices(4)] == -w_diag)) self.assertTrue(np.all(qubo.q[qubo.q > 0] == w_off / 2)) def test_find_maximum_independent_set(self): @@ -91,26 +72,6 @@ def test_find_maximum_independent_set(self): correct_set_size = 2 self.assertEqual(mis.sum(), correct_set_size) - def test_qubo_solution(self): - """Tests that a solution with the optimal cost is found by - OptimizationSolver with the QUBO formulation.""" - optimal_cost = -2 - qubo = self.problem.get_as_qubo(w_diag=1, w_off=4) - - config = SolverConfig( - timeout=1000, - target_cost=optimal_cost, - backend="CPU", - hyperparameters={ - "temperature": 1, - "refract": np.random.randint(2, 8, 10) - } - ) - - solver = OptimizationSolver(qubo) - report = solver.solve(config=config) - self.assertEqual(qubo.evaluate_cost(report.best_state), optimal_cost) - if __name__ == "__main__": unittest.main()