def test_add_terms_cterms(self): problem = Problem(name="test") count = 4 for i in range(count): problem.add_term(c=i, indices=[i, i + 1]) self.assertEqual(ProblemType.ising, problem.problem_type) self.assertEqual(count, len(problem.terms)) self.assertEqual(Term(c=1, indices=[1, 2]), problem.terms[1]) more = [] for i in range(1, count + 1): more.append(Term(c=i, indices=[i - 1, i])) problem.add_terms(more) self.assertEqual(2 * count, len(problem.terms)) self.assertEqual(Term(c=count, indices=[count - 1, count]), problem.terms[-1]) subterms = [Term(c=1, indices=[i]) for i in range(count)] subterms.append(Term(c=-5, indices=[])) problem.add_slc_term(terms=[(1, i) for i in range(count)] + [(-5, None)], c=2) self.assertEqual(2 * count, len(problem.terms)) self.assertEqual(1, len(problem.terms_slc)) self.assertEqual(SlcTerm(subterms, c=2), problem.terms_slc[-1]) problem.add_terms(subterms, term_type=GroupType.squared_linear_combination, c=2) self.assertEqual(2 * count, len(problem.terms)) self.assertEqual(2, len(problem.terms_slc)) self.assertEqual(SlcTerm(subterms, c=2), problem.terms_slc[-1]) problem.add_terms(subterms, term_type=GroupType.combination, c=0.5) self.assertEqual(3 * count + 1, len(problem.terms)) self.assertEqual( [Term(subterm.ids, c=subterm.c) for subterm in subterms], problem.terms[-len(subterms):]) problem.add_slc_term(subterms, c=3) self.assertEqual(3 * count + 1, len(problem.terms)) self.assertEqual(3, len(problem.terms_slc)) self.assertEqual(SlcTerm(subterms, c=3), problem.terms_slc[-1])
def test_problem_large(self): problem = Problem(name="test", terms=[], problem_type=ProblemType.pubo) self.assertTrue(not problem.is_large()) problem.add_term(5.0, []) self.assertTrue(not problem.is_large()) problem.add_term(6.0, list(range(3000))) self.assertTrue(not problem.is_large()) problem.add_terms([Term(indices=[9999], c=1.0)] * int(1e6)) # create 1mil dummy terms self.assertTrue(problem.is_large()) problem = Problem(name="test", terms=[ SlcTerm(terms=[Term(indices=[9999], c=1)], c=1) for i in range(int(1e6)) ], problem_type=ProblemType.pubo) self.assertTrue(not problem.is_large()) problem.add_slc_term([(1.0, i) for i in range(3000)]) self.assertTrue(problem.is_large())