当前位置: 首页>>代码示例>>Python>>正文


Python Solver.compute_solutions方法代码示例

本文整理汇总了Python中solver.Solver.compute_solutions方法的典型用法代码示例。如果您正苦于以下问题:Python Solver.compute_solutions方法的具体用法?Python Solver.compute_solutions怎么用?Python Solver.compute_solutions使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在solver.Solver的用法示例。


在下文中一共展示了Solver.compute_solutions方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: read_and_solve_case

# 需要导入模块: from solver import Solver [as 别名]
# 或者: from solver.Solver import compute_solutions [as 别名]
def read_and_solve_case(input, out=None, best=False, first=False):
	parser = Parser(input)
	solver = Solver(0, [], first)
	if out:
		output_file = open(out, 'w')
	for i in range(parser.c):
		solver.customers = parser.read_next_case()
		solver.length = parser.current_n
		solver.compute_solutions()
		if best:
			sol = solver.get_optimal_solution()
		else:
			sol = solver.get_random_solution()
		if sol != "IMPOSSIBLE":
			solution_string = "Case #" + str(i + 1) + ": " + " ".join(sol)
		else:
			solution_string = "Case #" + str(i + 1) + ": " + sol
		print solution_string
		if out:
			output_file.write(solution_string + '\n')
	if out:
		output_file.close()
开发者ID:jkklapp,项目名称:paintshop,代码行数:24,代码来源:main.py

示例2: TestSolver

# 需要导入模块: from solver import Solver [as 别名]
# 或者: from solver.Solver import compute_solutions [as 别名]
class TestSolver(unittest.TestCase):

	def setUp(self):
		self.solver = Solver(0, [])

	def test_solver_small(self):
		self.solver.length = 2
		self.solver.customers = [['1 1', '2 0'], ['1 0', '2 1']]
		s1 = ['0', '0']
		s2 = ['1', '1']
		self.solver.compute_solutions()
		sols = self.solver.solutions
		self.assertIn(s1, sols)
		self.assertIn(s2, sols)
		optimal = self.solver.get_optimal_solution()
		self.assertEqual(optimal[0], '0')
		self.assertEqual(optimal[1], '0')
		

	def test_solver_medium3(self):
		self.solver.length = 5
		self.solver.customers = [['1 1'], ['1 0', '2 0'], ['5 0'], ['2 1', '3 0']]
		self.solver.compute_solutions()
		sols = self.solver.solutions
		s = ['1', '0', '0', '0', '0']
		self.assertEqual(len(sols), 1)
		self.assertIn(s, sols)

	def test_solver_medium1(self):
		self.solver.customers = [['1 0', '6 1', '3 0', '4 0'],
			['5 1', '2 0', '3 0', '6 0'],
			['1 0', '5 1', '3 0', '4 0', '6 0'],
			['2 1'], ['2 1', '1 0', '6 0', '3 0']]
		self.solver.length = 6
		self.solver.compute_solutions()
		sols = self.solver.solutions
		self.assertEqual(len(sols), 3)
		optimal = self.solver.get_optimal_solution()
		self.assertEqual(optimal[0], '0')
		self.assertEqual(optimal[1], '1')
		self.assertEqual(optimal[2], '0')
		self.assertEqual(optimal[3], '0')
		self.assertEqual(optimal[4], '0')
		self.assertEqual(optimal[5], '0')


	def test_solver_medium2(self):
		self.solver.length = 5
		self.solver.customers = [['1 1', '2 0', '4 0'],
								 ['2 1', '1 0'],
								 ['2 1', '3 0'],
								 ['3 1', '2 0'],
								 ['4 1', '5 0'],
								 ['5 1']]
		self.solver.compute_solutions()
		self.assertTrue(self.solver.impossible)


	def test_solver_medium4(self):
		self.solver.length = 5
		self.solver.customers = [['1 1', '2 0', '4 0'],
			['2 1', '3 0'], ['3 1', '1 0'],
			['4 1', '5 0'], ['5 1']]
		s1 = ['1', '1', '1', '1', '1']
		s2 = ['0', '0', '0', '1', '1']
		self.solver.compute_solutions()
		sols = self.solver.solutions
		self.assertEqual(len(sols), 2)
		self.assertIn(s1, sols)
		self.assertIn(s2, sols)
		optimal = self.solver.get_optimal_solution()
		self.assertEqual(s2, optimal)


	def test_expansion(self):
		expand = self.solver.expand_a_candidate
		self.solver.length = 5
		candidate1 = ['1 1', '1 0', '5 0', '1 1', '3 1']
		self.assertIsNone(expand(candidate1))
		candidate2 = ['1 1', '2 0', '5 0']
		self.assertEqual(expand(candidate2), ['1 1', '2 0', '3 0', '4 0', '5 0'])
		candidate3 = ['1 1', '1 0', '5 0']
		self.assertIsNone(expand(candidate3))
		candidate4 = ['1 1', '1 0', '5 0', '2 1']
		self.assertIsNone(expand(candidate4))
开发者ID:jkklapp,项目名称:paintshop,代码行数:87,代码来源:solver_test.py


注:本文中的solver.Solver.compute_solutions方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。