Skip to content

Commit

Permalink
Bugfix merge commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessandroPierro committed Jul 27, 2023
1 parent 157a08e commit cc628a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 60 deletions.
7 changes: 3 additions & 4 deletions src/lava/lib/optimization/utils/generators/mis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
73 changes: 17 additions & 56 deletions tests/lava/lib/optimization/utils/generators/test_mis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
# 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


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)
Expand All @@ -26,63 +24,46 @@ 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):
"""Tests the conversion to QUBO returns the correct cost matrix."""
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):
Expand All @@ -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()

0 comments on commit cc628a5

Please sign in to comment.