本文整理汇总了Python中problem.Problem.add_variable方法的典型用法代码示例。如果您正苦于以下问题:Python Problem.add_variable方法的具体用法?Python Problem.add_variable怎么用?Python Problem.add_variable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类problem.Problem
的用法示例。
在下文中一共展示了Problem.add_variable方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: print
# 需要导入模块: from problem import Problem [as 别名]
# 或者: from problem.Problem import add_variable [as 别名]
print("{0: <3} ".format("Q"), end="")
else:
print("{0: <3} ".format("x"), end="")
print()
except (TypeError, KeyError):
print ("No Solution!")
if __name__ == "__main__":
NUM_QUEENS = 8 if len(sys.argv) < 2 else int(sys.argv[1])
variable_names = tuple(((i+1) for i in range(NUM_QUEENS)))
constraints = [
no_queens_check_constraint(variable_names)
]
problem = Problem()
for variable in variable_names:
values = list(range(1, NUM_QUEENS+1))
shuffle(values)
problem.add_variable(variable, values)
problem.add_constraints(constraints)
solver = ConstraintProblemSolver(problem)
assignments = solver.solve()
if assignments != None:
print("Failed Constraints: {}".format(problem.check_constraints(assignments)))
print_assignments(NUM_QUEENS, assignments)