微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将 OR-Tools 链接到我的 CMake 项目?

如何解决如何将 OR-Tools 链接到我的 CMake 项目?

下面是一个关于如何将 OR-Tools 链接到 CMake 项目的小示例。

感谢 mizuxkamilcuk 的帮助。

此外,mizux 最好更新文档以指定可能需要“USE_SCIP=OFF”来解决使用 FetchContent 构建时出现的错误

解决方案:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)
project(my_proj VERSION 1.0 LANGUAGES CXX)

# 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)
FetchContent_Declare(
        or-tools
        GIT_REPOSITORY https://github.com/google/or-tools.git
        GIT_TAG        master
)
FetchContent_MakeAvailable(or-tools)

# Create a main calling operations_research::BasicExample() and link the or-tools library.
add_executable(myapp main.cpp)
target_link_libraries(myapp ortools::ortools)

main.cpp:

#include "ortools/linear_solver/linear_solver.h"

namespace operations_research {

    void BasicExample() {
        // Create the linear solver with the GLOP backend.
        std::unique_ptr<MPSolver> solver(MPSolver::CreateSolver("GLOP"));

        // Create the variables x and y.
        MPVariable* const x = solver->MakeNumVar(0.0,1,"x");
        MPVariable* const y = solver->MakeNumVar(0.0,2,"y");

        LOG(INFO) << "Number of variables = " << solver->NumVariables();

        // Create a linear constraint,0 <= x + y <= 2.
        MPConstraint* const ct = solver->MakeRowConstraint(0.0,2.0,"ct");
        ct->SetCoefficient(x,1);
        ct->SetCoefficient(y,1);

        LOG(INFO) << "Number of constraints = " << solver->NumConstraints();

        // Create the objective function,3 * x + y.
        MPObjective* const objective = solver->MutableObjective();
        objective->SetCoefficient(x,3);
        objective->SetCoefficient(y,1);
        objective->Setmaximization();

        solver->Solve();

        LOG(INFO) << "Solution:" << std::endl;
        LOG(INFO) << "Objective value = " << objective->Value();
        LOG(INFO) << "x = " << x->solution_value();
        LOG(INFO) << "y = " << y->solution_value();
    }

}

int main() {
    operations_research::BasicExample();
    return EXIT_SUCCESS;
}

输出

$> myapp
WARNING: Logging before InitGoogleLogging() is written to STDERR
I0721 17:54:37.269460 1170927 main.cpp:17] Number of variables = 2
I0721 17:54:37.270126 1170927 main.cpp:24] Number of constraints = 1
W0721 17:54:37.273723 1170927 lp_solver.cc:163] 
******************************************************************
* WARNING: Glop will be very slow because it will use DCHECKs    *
* to verify the results and the precision of the solver.         *
* You can gain at least an order of magnitude speedup by         *
* compiling with optimizations enabled and by defining NDEBUG.   *
******************************************************************
I0721 17:54:37.277882 1170927 main.cpp:34] Solution:
I0721 17:54:37.277948 1170927 main.cpp:35] Objective value = 4
I0721 17:54:37.278002 1170927 main.cpp:36] x = 1
I0721 17:54:37.278023 1170927 main.cpp:37] y = 1

Process finished with exit code 0

解决方法

几点(OR-Tools dev here):

基本上我们提供了一个别名库 ortools::ortools,你应该依赖它。 源代码:https://github.com/google/or-tools/blob/b37d9c786b69128f3505f15beca09e89bf078a89/cmake/cpp.cmake#L134

否则,请注意 or-tools 依赖于 abseil-cpp 并使用 C++17,因此您必须使用相同的语言方言,因为 abseil-cpp 取决于语言版本。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。