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


Python Tester.is_valid_solution方法代码示例

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


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

示例1: __init__

# 需要导入模块: from tester import Tester [as 别名]
# 或者: from tester.Tester import is_valid_solution [as 别名]
class Optimizer:
	"""
		Object that implements different
		optimization strategies for a solution.

		Attrs:
			solution: A solution being optimized.
			case: A list of customers with their requirements.
	"""
	def __init__(self, solution, case):
		self.solution = solution
		self.current_mattes = sum([int(x) for x in solution])
		self.case = case
		self.tester = Tester()
		self.valid_solution = False
		self.steps = 0
		self.METHODS = {
			'random_optimizer': self.random_optimizer,
			'matte_minimizer': self.matte_minimizer
		}

	'''
		Generates the optimal naive solution.

		The optimal solution is to produce a batch
		of each color glossy.

		[0, 0, ..., 0]

		Args:
			n: The number of colors.
		Returns:
			A list of n 0s. 
	'''
	def generate_naive_solution(self, n):
		return ['0' for i in range(n)]

	'''
		Modifies a solution.

		Args:
			solution: An array with 0s and 1s.
			i: Position to switch value.
		Returns
			The same solution with the i-th 
			position switched.
	'''
	def change_solution(self, solution, i):
		solution[i] = '1' if solution[i] == '0' else '0'
		return solution

	'''
		Checks if the solution has improved

		Args:
			solution: An array with 0s and 1s.

		Returns:
			True if the solution is still valid
			and the number of 1s has decreased.
			False otherwise.
	'''
	def solution_improved(self, s):
		candidate_mattes = sum([int(x) for x in s])
		if self.valid_solution and candidate_mattes < self.current_mattes:
			self.current_mattes = candidate_mattes
			return True
		return False

	'''
		Uses inspection to optimize randomly a solution.

		The idea is to switch the value of random positions in the
		solution array and check if the solution satisfies. Does
		nothing if the solution already satisfies.

		Args:
			solution: A solution candidate.
		Returns
			The first solution that satisfies
	'''
	def random_optimizer(self, i=1):
		tester = self.tester
		case = self.case
		s = self.solution
		for k in range(i):
			self.valid_solution = self.tester.is_valid_solution(self.solution, case)
			pos = randint(0, len(solution)-1)
			s = self.change_solution(s, pos)			
			if self.solution_improved(s):
				self.solution = s
			else:
				s = self.change_solution(s, pos)
		self.steps += i

	'''
		Tries to improve a solution incrementally.

		This method generates a solution that satisfies, and
		then tries to turn 1s into 0s until it no longer satisfies.
#.........这里部分代码省略.........
开发者ID:jkklapp,项目名称:paintshop,代码行数:103,代码来源:optimizer.py


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