def test_exceptions(self): qubo = [[-1.2, 1.1], [1.1, -1.2]] timeout = 10 restarts = 100 # Wrong length for init_solution with self.assertRaises(RuntimeError): init = [1, 1, 1] tenure = len(init) - 1 search = tabu.TabuSearch(qubo, init, tenure, timeout, restarts) # Tenure out of bounds with self.assertRaises(RuntimeError): init = [1, 1] tenure = 3 search = tabu.TabuSearch(qubo, init, tenure, timeout, restarts)
def test_correctness(self): qubo = [[-1.2, 1.1], [1.1, -1.2]] init = [1, 1] tenure = len(init) - 1 timeout = 20 restarts = 100 search = tabu.TabuSearch(qubo, init, tenure, timeout, restarts) solution = list(search.bestSolution()) energy = search.bestEnergy() self.assertEqual(solution, [0, 1]) self.assertEqual(energy, -1.2)
def test_trivial(self): qubo = [[1.0]] init = [1] tenure = len(init) - 1 timeout = 1 restarts = 100 search = tabu.TabuSearch(qubo, init, tenure, timeout, restarts) solution = list(search.bestSolution()) energy = search.bestEnergy() self.assertEqual(solution, [0]) self.assertEqual(energy, 0.0)
def test_correctness(self): qubo = [[2, 1, 1], [1, 2, 1], [1, 1, 2]] init = [1, 1, 1] tenure = len(init) - 1 scale = 1 timeout = 20 search = tabu.TabuSearch(qubo, init, tenure, scale, timeout) solution = list(search.bestSolution()) energy = search.bestEnergy() self.assertEqual(solution, [0, 0, 0]) self.assertEqual(energy, 0.0)
def test_trivial(self): qubo = [[1]] init = [1] tenure = len(init) - 1 scale = 1 timeout = 1 search = tabu.TabuSearch(qubo, init, tenure, scale, timeout) solution = list(search.bestSolution()) energy = search.bestEnergy() self.assertEqual(solution, [0]) self.assertEqual(energy, 0.0)
def test_float(self): n = 20 init = [1] * n tenure = len(init) - 1 timeout = 20 restarts = 100 bqm = dimod.generators.random.uniform(n, 'BINARY', low=-100, high=100, seed=123) Q, _ = tabu.TabuSampler._bqm_to_tabu_qubo(bqm) search = tabu.TabuSearch(Q, init, tenure, timeout, restarts) self.assertAlmostEqual(search.bestEnergy(), -1465.9867898) bqm = dimod.generators.random.uniform(n, 'BINARY', low=-1, high=1, seed=123) Q, _ = tabu.TabuSampler._bqm_to_tabu_qubo(bqm) search = tabu.TabuSearch(Q, init, tenure, timeout, restarts) self.assertAlmostEqual(search.bestEnergy(), -14.65986790)
def search(timeout, restarts=int(1e6)): return tabu.TabuSearch([[1.0]], [1], 0, timeout, restarts).bestEnergy()
def search(timeout): return tabu.TabuSearch([[1]], [1], 0, 1, timeout).bestEnergy()