OR-Tools 是一个用于优化的开源软件套件,专为解决世界上最棘手的车辆路线规划、流程、整数和线性规划以及约束规划问题而调整。
本文将介绍如何用CLion(CMake)构建工程并能debug源码。
由于OR-Tools更新后源码构建的方法也略有不同,现在更新到OR-Tools v9.6——@小猪快跑
相关教程
- 【从零开始】在Windows中使用Linux——在WSL使用CLion、IDEA、PyCharm(安装到建立工程)——更新于2021.12
- WSL2 运行 CLion/IDEA/PyCharm等GUI应用——更新于2022.01
- 【从零开始】coin-or/CoinUtils Osi Clp Cgl Cbc源码构建debug(CLion/CMake)
相关文献
方法一:CMake使用打包后的OR-Tools:
大部分时候我们只是希望使用已经打包后的源码进行debug,而非修改源码,但我们希望他能实现CMake自动打包所有依赖。于是新建工程,然后CMakeLists.txt:
cmake_minimum_required(VERSION 3.21)
project(orTools)
set(CMAKE_CXX_STANDARD 20)
# Build OR-tools dependencies.
set(BUILD_DEPS ON)
# Disable SCIP solver.
set(USE_SCIP OFF)
# Fetch OR-tools library and create the alias ortools::ortools.
include(FetchContent)
# 填充期间的日志输出可能非常冗长,使得配置阶段非常嘈杂。此缓存选项(ON默认情况下)隐藏所有填充输出,除非遇到错误。如果遇到挂起下载的问题,
# 暂时关闭此选项可能有助于诊断导致问题的内容群体。
set(FETCHCONTENT_QUIET OFF)
# 避免每次编译or-tools及其所有依赖
get_filename_component(_deps "../_deps" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
# 在大多数情况下,保存的详细信息没有指定与用于内部子构建、最终源和构建区域的目录相关的任何选项。通常最好将这些决定留给FetchContent 模块来代表项目处理。
# 缓存变量控制收集所有内容填充目录的FETCHCONTENT_BASE_DIR 点,但在大多数情况下,开发人员不需要更改它。
# 默认位置是${CMAKE_BINARY_DIR}/_deps,但如果开发人员更改此值,他们应该致力于保持路径短且刚好低于构建树的顶层,以避免在 Windows 上遇到路径长度问题。
set(FETCHCONTENT_BASE_DIR ${_deps})
FetchContent_Declare(
or-tools
GIT_REPOSITORY https://github.com/google/or-tools.git
GIT_TAG v9.6
)
FetchContent_MakeAvailable(or-tools)
# Create a main calling operations_research::BasicExample() and link the or-tools library.
add_executable(orTools main.cpp)
target_link_libraries(orTools ortools::ortools)
main函数就以官网的例子为例:
// Copyright 2010-2021 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Constraint programming example that shows how to use the API.
#include "ortools/base/logging.h"
#include "ortools/constraint_solver/constraint_solver.h"
namespace operations_research {
void RunConstraintProgrammingExample() {
// Instantiate the solver.
Solver solver("ConstraintProgrammingExample");
const int64_t numVals = 3;
// Define decision variables.
IntVar* const x = solver.MakeIntVar(0, numVals - 1, "x");
IntVar* const y = solver.MakeIntVar(0, numVals - 1, "y");
IntVar* const z = solver.MakeIntVar(0, numVals - 1, "z");
// Define constraints.
std::vector<IntVar*> xyvars = {x, y};
solver.AddConstraint(solver.MakeAllDifferent(xyvars));
LOG(INFO) << "Number of constraints: " << solver.constraints();
// Create decision builder to search for solutions.
std::vector<IntVar*> allvars = {x, y, z};
DecisionBuilder* const db = solver.MakePhase(
allvars, Solver::CHOOSE_FIRST_UNBOUND, Solver::ASSIGN_MIN_VALUE);
solver.NewSearch(db);
while (solver.NextSolution()) {
LOG(INFO) << "Solution"
<< ": x = " << x->Value() << "; y = " << y->Value()
<< "; z = " << z->Value();
}
solver.EndSearch();
LOG(INFO) << "Number of solutions: " << solver.solutions();
LOG(INFO) << "";
LOG(INFO) << "Advanced usage:";
LOG(INFO) << "Problem solved in " << solver.wall_time() << "ms";
LOG(INFO) << "Memory usage: " << Solver::MemoryUsage() << " bytes";
}
} // namespace operations_research
int main(int argc, char** argv) {
// google::InitGoogleLogging(argv[0]);
// absl::SetFlag(&FLAGS_logtostderr, 1);
operations_research::RunConstraintProgrammingExample();
return EXIT_SUCCESS;
}
点击run即可:
方法二:
安装OR-Tools
博主的环境是Ubuntu 22.04 LTS。官网可参考在 Linux 上从源代码 OR-Tools C++ 构建 | Google for Developers。
要安装 C++ 工具,请打开终端窗口并输入:
sudo apt update
sudo apt install -y build-essential cmake lsb-release
通过输入克隆稳定的分支
git clone https://github.com/google/or-tools
下载先前的发布版本
您可以通过以下任一方式获取先前版本的源代码:
-
从 GitHub 发布页面下载先前的发布版本。
-
假设您已经(通过
git clone
)创建了本地代码库,则可以使用 Git 标记签出特定版本。例如,如需使用 v9.6 版本(而不是main
分支),请在本地代码库中输入以下命令:git fetch --all --tags --prune git checkout tags/v9.6 -v v9.6
配置构建
在构建 OR-Tools 之前,您需要配置 CMake 构建系统生成器。
打开终端,然后转到提取文件的目录。 然后输入以下命令:
cmake -S . -B cmake-build-debug -DBUILD_DEPS=ON
我们用CLion打开刚才克隆的工程(注意这里选择cmake-build-debug):
打开Settings(快捷键 Ctrl + Alt + S),设置成如图这样的参数
随便找个examples试下debug: