优化 ortools cp-sat 以获得最佳速度

问题描述 投票:0回答:1

我有一个非常简单的 C++ 程序,带有 Or-tools cp-sat 求解器,需要很长时间才能求解。平均而言,我的成绩介于

500-950 randomization per second
之间。

这是代码

#include <absl/log/initialize.h>
#include <stdlib.h>

#include "ortools/base/logging.h"
#include "ortools/sat/cp_model.h"
#include "ortools/sat/cp_model.pb.h"
#include "ortools/sat/cp_model_solver.h"
#include "ortools/util/sorted_interval_list.h"

void SimpleSatProgram() {
  const operations_research::Domain domain(0, 100);
  operations_research::sat::CpSolverResponse response;

  operations_research::sat::CpModelBuilder cp_model;
  operations_research::sat::IntVar a = cp_model.NewIntVar(domain);
  operations_research::sat::IntVar b = cp_model.NewIntVar(domain);
  operations_research::sat::IntVar c = cp_model.NewIntVar(domain);
  cp_model.AddEquality(a + b + c, 200);

  for (int x=0; x < 10000; x++) {
    response = operations_research::sat::Solve(cp_model.Build());

    if (response.status() == operations_research::sat::CpSolverStatus::OPTIMAL ||
        response.status() == operations_research::sat::CpSolverStatus::FEASIBLE) {
      // for (auto i : all)
      //   std::cout << std::setfill('0') << std::setw(2)
      //             << SolutionIntegerValue(response, i) << " ";
      // std::cout << "\n";
    } else {
      LOG(INFO) << "No solution found.";
    }
  }
}

int main() {
  // absl::InitializeLog();
  SimpleSatProgram();
  return EXIT_SUCCESS;
}

我有一个专有工具,在相同的设置中,其效果远高于

120k randomizations per second

我可以做些什么来优化或遵循编码中的最佳实践以获得最佳性能吗?

c++ constraints or-tools cp-sat-solver
1个回答
2
投票

您可以尝试添加

SatParameters params;
params.set_num_workers(1);
params.set_cp_model_presolve(false);

并将

Solve()
替换为
Solvewithparameters()

请注意,cp-sat 经过调整以解决有意义的非平凡模型。

© www.soinside.com 2019 - 2024. All rights reserved.