本文整理匯總了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.
#.........這裏部分代碼省略.........