def test_graph_coloring_2(self):
		"""
		Tests the translation of the graph coloring to a SAT problem for a given graph G and color b.
		"""
		b = 3
		G = [("a", "b"),("a", "c"),("c", "b")]
		self.assertIsNotNone(gc.graph_coloring(G, b), "Invalid graph coloring for G and b = 3, expected not None.")
Пример #2
0
def run_example(G, b):
	"""
	Helper method that converts the graph G to a boolean formula and runs the DPLL algorithm on the specified graph G
	and number of colors b.
	Graph must be defined as a list of connections between nodes, where each connection is represented by a tuple
	(example: G = [("v1", "v2"),("v1", "v3"),("v2", "v3")])
	"""
	print "RUNNING EXAMPLE FOR NUMBER OF COLORS b = " + str(b) + " AND GRAPH G = " + str(G)

	translation_start = time.time()
	translation = gc.graph_coloring(G, b)
	translation_end = time.time() - translation_start
	print "TRANSLATION OF GRAPH: "
	print translation
	print "TIME NEEDED FOR TRANSLATION: " + str(translation_end)

	dpll_start = time.time()
	solution = dpll.DPLL(translation)
	dpll_end = time.time() - dpll_start
	if solution == False:
		print "Problem is not solvable"
	else:
		print "SOLUTION: " + str(solution)
	print "TIME NEEDED FOR DPLL: " + str(dpll_end)
	print "TOTAL TIME NEEDED: " + str(translation_end + dpll_end)
	print "-----------------------------------------------------------------------------------------------------------------\n"
	def test_graph_coloring_1(self):
		"""
		Tests the translation of the graph coloring to a SAT problem for a given graph G and color b.
		"""
		b = 2
		G = [("a", "b"), ("a", "c"), ("b", "c"), ("d", "e"), ("e", "a")]
		g1 = bf.And([bf.Or([bf.Var("a1"), bf.Var("a2")]), bf.Or([bf.Var("c1"), bf.Var("c2")]), bf.Or([bf.Var("b1"), bf.Var("b2")]), bf.Or([bf.Var("e1"), bf.Var("e2")]), bf.Or([bf.Var("d1"), bf.Var("d2")])])
		g2 = bf.And([bf.Not(bf.And([bf.Var("a1"), bf.Var("a2")])), bf.Not(bf.And([bf.Var("c1"), bf.Var("c2")])), bf.Not(bf.And([bf.Var("b1"), bf.Var("b2")])), bf.Not(bf.And([bf.Var("e1"), bf.Var("e2")])), bf.Not(bf.And([bf.Var("d1"), bf.Var("d2")]))])
		g3 = bf.And([bf.Not(bf.And([bf.Var("a1"), bf.Var("b1")])), bf.Not(bf.And([bf.Var("a2"), bf.Var("b2")])), bf.Not(bf.And([bf.Var("a1"), bf.Var("c1")])), bf.Not(bf.And([bf.Var("a2"), bf.Var("c2")])), bf.Not(bf.And([bf.Var("b1"), bf.Var("c1")])),
					bf.Not(bf.And([bf.Var("b2"), bf.Var("c2")])), bf.Not(bf.And([bf.Var("d1"), bf.Var("e1")])), bf.Not(bf.And([bf.Var("d2"), bf.Var("e2")])), bf.Not(bf.And([bf.Var("e1"), bf.Var("a1")])), bf.Not(bf.And([bf.Var("e2"), bf.Var("a2")]))])

		result = bf.And([g1, g2, g3])
		self.assertEqual(result, gc.graph_coloring(G, b), "Invalid graph coloring for G and b = 2, expected the same as result.")