美文网首页Python与OR
设置解答器约束

设置解答器约束

作者: 白晓寒星 | 来源:发表于2019-08-11 23:11 被阅读0次

·运行时间约束
·解约束

1.为解答器设置运行时间约束
如果程序的运行时间太长了,我们可以设置程序的合理运行时间。调用函数为 solver.parameters.max_time_in_seconds 。以下是一个十秒运行时间的限制:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from ortools.sat.python import cp_model


def SolveWithTimeLimitSampleSat():
    """Minimal CP-SAT example to showcase calling the solver."""
    # Creates the model.
    model = cp_model.CpModel()
    # Creates the variables.
    num_vals = 3
    x = model.NewIntVar(0, num_vals - 1, 'x')
    y = model.NewIntVar(0, num_vals - 1, 'y')
    z = model.NewIntVar(0, num_vals - 1, 'z')
    # Adds an all-different constraint.
    model.Add(x != y)

    # Creates a solver and solves the model.
    solver = cp_model.CpSolver()

    # Sets a time limit of 10 seconds.
    solver.parameters.max_time_in_seconds = 10.0

    status = solver.Solve(model)

    if status == cp_model.FEASIBLE:
        print('x = %i' % solver.Value(x))
        print('y = %i' % solver.Value(y))
        print('z = %i' % solver.Value(z))


SolveWithTimeLimitSampleSat()

2.给定解的数量终止运行
例子如下:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from ortools.sat.python import cp_model


class VarArraySolutionPrinterWithLimit(cp_model.CpSolverSolutionCallback):
    """Print intermediate solutions."""

    def __init__(self, variables, limit):
        cp_model.CpSolverSolutionCallback.__init__(self)
        self.__variables = variables
        self.__solution_count = 0
        self.__solution_limit = limit

    def on_solution_callback(self):
        self.__solution_count += 1
        for v in self.__variables:
            print('%s=%i' % (v, self.Value(v)), end=' ')
        print()
        if self.__solution_count >= self.__solution_limit:
            print('Stop search after %i solutions' % self.__solution_limit)
            self.StopSearch()

    def solution_count(self):
        return self.__solution_count


def StopAfterNSolutionsSampleSat():
    """Showcases calling the solver to search for small number of solutions."""
    # Creates the model.
    model = cp_model.CpModel()
    # Creates the variables.
    num_vals = 3
    x = model.NewIntVar(0, num_vals - 1, 'x')
    y = model.NewIntVar(0, num_vals - 1, 'y')
    z = model.NewIntVar(0, num_vals - 1, 'z')

    # Create a solver and solve.
    solver = cp_model.CpSolver()
    solution_printer = VarArraySolutionPrinterWithLimit([x, y, z], 5)
    status = solver.SearchForAllSolutions(model, solution_printer)
    print('Status = %s' % solver.StatusName(status))
    print('Number of solutions found: %i' % solution_printer.solution_count())
    assert solution_printer.solution_count() == 5


StopAfterNSolutionsSampleSat()

相关文章

  • 设置解答器约束

    ·运行时间约束·解约束 1.为解答器设置运行时间约束如果程序的运行时间太长了,我们可以设置程序的合理运行时间。调用...

  • 高速PCB设计系列基础知识38|线宽设置的操作步骤

    本节继续讲解PCB设计的约束管理器之线宽设置。 (1)设置默认约束 点选 Physical Constraint ...

  • attempting to add unsupported at

    使用masonry, 设置约束时 约束冲突或约束不全. 而我遇到的问题时,在设置一个view约束时, 这个view...

  • 使用 xib 设置约束

    0.使用代码设置约束和使用 xib 设置约束,两种方式各有好处吧.代码约束便于维护和修改, xib 约束则可快速搭...

  • iOS NSLayoutConstraint 设置 multip

    使用以下方法设置宽度约束实现一个进度指示器,进度从0到1,设置multiplier从0到1,当设置为0时再次更新为...

  • mas_remakeConstraints

    如果要重新设置一个控件的约束最好使用mas_remakeConstraints,清除原来的约束,重新设置约束。

  • tableHeaderView使用Masonry自动计算高度

    操作步骤 1.设置tableHeaderView及其约束 2.设置tableHeaderView最底部子视图的约束...

  • 约束个人见解

    一般设置控件约束的方法步骤: 先确定你需要固定哪几个约束,最常见的是固定四个约束. 在设置约束的时候先固定好约束,...

  • MySQL服务器变量

    服务器变量SQL_MODE SQL_MODE: 对其设置可以完成一些约束检查的工作,可分别进行全局的设置或当前会话...

  • 05-xib创建cell

    1、创建cell 2、在cell.xib中拖入你要的控件,设置约束 3、如需修改控件约束,拖出该约束,设置.con...

网友评论

    本文标题:设置解答器约束

    本文链接:https://www.haomeiwen.com/subject/aoedjctx.html