使用Gurobi和C ++解决IP问题

时间:2015-09-02 08:35:25

标签: c++ cmake gurobi

我尝试使用gurobi解决一个非常简单的IP,从c ++运行它(来源:http://rmlewi.people.wm.edu/~rmlewi/sciclone/docs/gurobi/quickstart/node5.html):

#include <iostream>
#include "gurobi_c++.h"

using namespace std;

int main(int   argc,
     char *argv[])
{
try {
    GRBEnv env = GRBEnv();

    GRBModel model = GRBModel(env);

    // Create variables

    GRBVar x = model.addVar(0.0, 1.0, -1.0, GRB_BINARY, "x");
    GRBVar y = model.addVar(0.0, 1.0, -1.0, GRB_BINARY, "y");
    GRBVar z = model.addVar(0.0, 1.0, -2.0, GRB_BINARY, "z");

    // Integrate new variables

    model.update();

    // Add constraint: x + 2 y + 3 z <= 4
    GRBLinExpr expression;
    expression += x;
    expression += 2*y;
    expression += 3*z;
    model.addConstr(expression <= 4, "c0");

    // Add constraint: x + y >= 1

    model.addConstr(x + y >= 1, "c1");

    // Optimize model

    model.optimize();

    cout << x.get(GRB_StringAttr_VarName) << " "
    << x.get(GRB_DoubleAttr_X) << endl;
    cout << y.get(GRB_StringAttr_VarName) << " "
    << y.get(GRB_DoubleAttr_X) << endl;
    cout << z.get(GRB_StringAttr_VarName) << " "
    << z.get(GRB_DoubleAttr_X) << endl;

    cout << "Obj: " << model.get(GRB_DoubleAttr_ObjVal) << endl;

} catch(GRBException e) {
    cout << "Error code = " << e.getErrorCode() << endl;
    cout << e.getMessage() << endl;
} catch(...) {
    cout << "Exception during optimization" << endl;
}
cout << "Hello, World!" << endl;
return 0;

为了构建它,我使用以下CMake文件:

cmake_minimum_required(VERSION 3.2)
project(gurobitest)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_executable(gurobitest ${SOURCE_FILES})

if (GUROBI_INCLUDE_DIR)
# in cache already
set(GUROBI_FOUND TRUE)
set(GUROBI_INCLUDE_DIRS "${GUROBI_INCLUDE_DIR}" )
set(GUROBI_LIBRARIES "${GUROBI_LIBRARY};${GUROBI_CXX_LIBRARY}" )
else (GUROBI_INCLUDE_DIR)

find_path(GUROBI_INCLUDE_DIR
    NAMES gurobi_c++.h
    PATHS  "/Library/gurobi604/mac64/include"
            "$ENV{GUROBI_HOME}/include"
           "C:\\libs\\gurobi604\\include"
    #CMAKE_ROOT_PATH "/Library/gurobi604/mac64"
    )

find_library( GUROBI_LIBRARY
        NAMES gurobi
    gurobi45
    gurobi46
  gurobi50
  gurobi51
  gurobi52
  gurobi55
  gurobi56
  gurobi60
  libgurobi_c++.a
  libgurobi60.so
        PATHS "$ENV{GUROBI_HOME}/lib"
              "/Library/gurobi604/mac64/lib"
              "/Library/gurobi604/mac64/lib"
              "C:\\libs\\gurobi604\\lib"
        )

find_library( GUROBI_CXX_LIBRARY
        NAMES gurobi_c++
        libgurobi60.so
        libgurobi_c++.a
        PATHS "$ENV{GUROBI_HOME}/lib"
              "/Library/gurobi604/mac64/lib"
              "C:\\libs\\gurobi604\\lib"

        )

set(GUROBI_INCLUDE_DIRS "${GUROBI_INCLUDE_DIR}" )
set(GUROBI_LIBRARIES "${GUROBI_LIBRARY};${GUROBI_CXX_LIBRARY}" )


endif(GUROBI_INCLUDE_DIR)

include_directories(${GUROBI_INCLUDE_DIR})
target_link_libraries(gurobitest ${GUROBI_CXX_LIBRARY})
target_link_libraries(gurobitest ${GUROBI_LIBRARY})

但是,编译时总是遇到问题。然后出现以下错误消息:

Linking CXX executable gurobitest
Undefined symbols for architecture x86_64:

似乎所有的grub命令都不知道。 如果这很重要,我会使用mac。有用的提示吗?

1 个答案:

答案 0 :(得分:0)

我的一位朋友找到了答案。最后一部分并不完全正确:

include_directories( ${GUROBI_INCLUDE_DIRS} )
target_link_libraries( gurobitest ${GUROBI_LIBRARIES} )
set (CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -stdlib=libstdc++")
set (CMAKE_SHARED_LINKER_FLAGS "-lgurobi_c++ -lgurobi60, --as-needed")

显然,旗帜没有设置好。