ortool 整数规划之车间调度提前停止搜索

这篇博客介绍了如何使用ortools解决大规模作业车间调度问题,并在短时间内找到解决方案。通过设置求解个数或时间限制,可以在满足条件时提前停止搜索。示例代码展示了如何创建解决方案回调函数以限制求解数量或时间,从而优化搜索过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ortool解决作业车间调度问题

当问题规模比较大时短时间无法求得解,可以设置条件提前停止求解

  • 1,以设置的求解个数来作为终止条件
"""Code sample that solves a model and displays a small number of solutions."""

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('  objective value = %i' % self.ObjectiveValue())
    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."""
  ###设置停止条件
  stop_early_count=2
  # 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')
  model.Add(x != y)
  model.Maximize(x + 2 * y + 3 * z)
  # Create a solver and solve.
  solver = cp_model.CpSolver()
  solution_printer = VarArraySolutionPrinterWithLimit([x, y, z], stop_early_count)
  # Enumerate all solutions.
  solver.parameters.enumerate_all_solutions = True
  # Solve.
  status = solver.SolveWithSolutionCallback(model, solution_printer)
  print('Status = %s' % solver.StatusName(status))
  print('Number of solutions found: %i' % solution_printer.solution_count())
  assert solution_printer.solution_count() == stop_early_count
  print(solver.ObjectiveValue())


StopAfterNSolutionsSampleSat()

  • 2.设置时间为停止条件
from ortools.sat.python import cp_model
from time import time

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

  def __init__(self,  limit):
    cp_model.CpSolverSolutionCallback.__init__(self)
    self.__solution_count = time()
    self.__solution_limit = limit

  def on_solution_callback(self):
    t1=time()
    if (t1-self.__solution_count) >= self.__solution_limit:
      self.StopSearch()



def StopAfterNSolutionsSampleSat():
  """Showcases calling the solver to search for small number of solutions."""
  ###设置停止条件
  stop_early_time=10
  # 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')
  model.Add(x != y)
  model.Maximize(x + 2 * y + 3 * z)
  # Create a solver and solve.
  solver = cp_model.CpSolver()
  solution_printer = VarArraySolutionPrinterWithLimit( stop_early_time)
  # Enumerate all solutions.
  solver.parameters.enumerate_all_solutions = True
  # Solve.
  status = solver.SolveWithSolutionCallback(model, solution_printer)
  print('Status = %s' % solver.StatusName(status))
  print(solver.ObjectiveValue())


StopAfterNSolutionsSampleSat()

引用

https://github.com/google/or-tools/blob/master/ortools/sat/doc/solver.md#stopping-search-early

https://github.com/google/or-tools/issues/651

Python可以使用多种库来实现线性规划和混合整数规划,其中比较常用的是docplex和ortool库。这两个库都提供了简单易用的API来定义和求解线性规划和混合整数规划问题。具体实现步骤如下: 1. 定义问题:使用库提供的API定义问题的目标函数、约束条件和决策变量类型等。 2. 求解问题:使用库提供的API求解问题,得到最优解和最优解对应的目标函数值。 下面以docplex库为例,介绍如何实现线性规划和混合整数规划: 1. 线性规划LP的实现: ``` # 导入库 from docplex.mp.model import Model # 创建模型 model = Model(name='LP') # 定义决策变量 x = model.continuous_var(name='x') y = model.continuous_var(name='y') # 定义目标函数 model.maximize(3*x + 2*y) # 添加约束条件 model.add_constraint(2*x + y <= 10) model.add_constraint(x + y <= 8) # 求解问题 solution = model.solve() # 输出结果 print('最优解为:x = {}, y = {}'.format(solution.get_value(x), solution.get_value(y))) print('最优解对应的目标函数值为:{}'.format(solution.get_objective_value())) ``` 2. 混合整数规划MIP的实现: ``` # 导入库 from docplex.mp.model import Model # 创建模型 model = Model(name='MIP') # 定义决策变量 x = model.integer_var(name='x') y = model.integer_var(name='y') # 定义目标函数 model.maximize(3*x + 2*y) # 添加约束条件 model.add_constraint(2*x + y <= 10) model.add_constraint(x + y <= 8) # 求解问题 solution = model.solve() # 输出结果 print('最优解为:x = {}, y = {}'.format(solution.get_value(x), solution.get_value(y))) print('最优解对应的目标函数值为:{}'.format(solution.get_objective_value())) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值