Skip to content

Commit

Permalink
Update CMake configuration for Gurobi 9.5
Browse files Browse the repository at this point in the history
  • Loading branch information
fdabrandao committed Sep 30, 2022
1 parent 589978b commit a630790
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 17 deletions.
20 changes: 18 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
cmake_minimum_required(VERSION 3.1...3.19)
cmake_minimum_required(VERSION 3.3...3.19)

project(
VPSolver
VERSION 3.0
LANGUAGES CXX)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_CXX_STANDARD 11)

# Visual Studio compiler with static runtime libraries
if(MSVC AND MT)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
endif()

find_package(GUROBI REQUIRED)

add_library(vpsolver-lib OBJECT
src/instance.cpp src/instance.hpp
src/graph.cpp src/graph.hpp
src/arcflow.cpp src/arcflow.hpp
src/arcflowsol.cpp src/arcflowsol.hpp
src/common.cpp src/common.hpp)

add_executable(vpsolver src/vpsolver.cpp $<TARGET_OBJECTS:vpsolver-lib>)
target_include_directories(vpsolver PRIVATE ${GUROBI_INCLUDE_DIRS})
target_link_libraries(vpsolver ${GUROBI_CXX_LIBRARY} ${GUROBI_LIBRARY})

add_executable(vbp2afg src/vbp2afg.cpp $<TARGET_OBJECTS:vpsolver-lib>)
add_executable(afg2mps src/afg2mps.cpp $<TARGET_OBJECTS:vpsolver-lib>)
add_executable(afg2lp src/afg2lp.cpp $<TARGET_OBJECTS:vpsolver-lib>)
add_executable(vbpsol src/vbpsol.cpp src/arcflowsol.cpp src/arcflowsol.hpp $<TARGET_OBJECTS:vpsolver-lib>)
add_executable(vbpsol src/vbpsol.cpp $<TARGET_OBJECTS:vpsolver-lib>)

# CPack
set(CPACK_GENERATOR ZIP)
Expand Down
53 changes: 53 additions & 0 deletions FindGUROBI.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
find_path(
GUROBI_INCLUDE_DIRS
NAMES gurobi_c.h
HINTS ${GUROBI_DIR} $ENV{GUROBI_HOME}
PATH_SUFFIXES include)

find_library(
GUROBI_LIBRARY
NAMES gurobi gurobi81 gurobi90 gurobi95
HINTS ${GUROBI_DIR} $ENV{GUROBI_HOME}
PATH_SUFFIXES lib)

get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
if("CXX" IN_LIST languages)
if(MSVC)
# determine Visual Studio year
if(MSVC_TOOLSET_VERSION EQUAL 142)
set(MSVC_YEAR "2019")
elseif(MSVC_TOOLSET_VERSION EQUAL 141)
set(MSVC_YEAR "2017")
elseif(MSVC_TOOLSET_VERSION EQUAL 140)
set(MSVC_YEAR "2015")
endif()

if(MT)
set(M_FLAG "mt")
else()
set(M_FLAG "md")
endif()

find_library(
GUROBI_CXX_LIBRARY
NAMES gurobi_c++${M_FLAG}${MSVC_YEAR}
HINTS ${GUROBI_DIR} $ENV{GUROBI_HOME}
PATH_SUFFIXES lib)
find_library(
GUROBI_CXX_DEBUG_LIBRARY
NAMES gurobi_c++${M_FLAG}d${MSVC_YEAR}
HINTS ${GUROBI_DIR} $ENV{GUROBI_HOME}
PATH_SUFFIXES lib)
else()
find_library(
GUROBI_CXX_LIBRARY
NAMES gurobi_c++
HINTS ${GUROBI_DIR} $ENV{GUROBI_HOME}
PATH_SUFFIXES lib)
set(GUROBI_CXX_DEBUG_LIBRARY ${GUROBI_CXX_LIBRARY})
endif()
endif()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(GUROBI DEFAULT_MSG GUROBI_LIBRARY
GUROBI_INCLUDE_DIRS)
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ For more details, please refer to the [project wiki](https://github.com/fdabrand

* MIP solver: Gurobi, CPLEX, GLPK, COIN-OR, SCIP, lp_solve, ...
* UNIX-like operating system or a UNIX-like environment such as [Cygwin](https://www.cygwin.com/)
* `g++ >= 4.8`; `make >= 3.0`; `bash >= 3.0`
* `g++ >= 4.8`; `cmake >= 3.3`; `bash >= 3.0`

#### Optional

For the [Python API](https://github.com/fdabrandao/vpsolver/wiki/Python-API) and Web App:
Expand All @@ -53,11 +53,12 @@ It has been successfully compiled and run on the following platforms:
Without the python interface:

```bash
$ ./configure CXXFLAGS="" LDFLAGS=""
$ make
$ sudo make install
$ mkdir build
$ cd build/
$ cmake ..
$ cmake --build .
```
Note: In order to compile only the components that do not require Gurobi, use `./configure GUROBI_HOME=""`. In order to link the optional components that require Gurobi, the environment variable `$GUROBI_HOME` must be set, and some additional flags may also need to be set (e.g., `./configure LDFLAGS="-L${GUROBI_HOME}/lib/ -lgurobi_stdc++"`).
Note: In order to compile the components that require Gurobi, you need to have set the environment variable `GUROBI_HOME` or specify the location of the Gurobi installation in the third step (e.g., `cmake .. -DGUROBI_DIR=/Library/gurobi952/macos_universal2/`).

With the python interface:

Expand Down
17 changes: 8 additions & 9 deletions src/vpsolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Copyright (C) 2013-2021, Filipe Brandao <[email protected]>
#include <map>
#include <vector>
#include <algorithm>
#include "gurobi_c.h"
#include "gurobi_c++.h"
#include "config.hpp"
#include "common.hpp"
Expand Down Expand Up @@ -36,12 +35,12 @@ void solve(const Instance &inst, bool print_inst = false, bool pyout = false) {
// model.getEnv().set(GRB_DoubleParam_ImproveStartTime, 60);
// model.getEnv().set(GRB_DoubleParam_ImproveStartGap, 1);

vector <Arc> As(afg.A);
sort(all(As));
map <Arc, GRBVar> va;
std::vector <Arc> As(afg.A);
std::sort(all(As));
std::map <Arc, GRBVar> va;
int lastv = afg.Ts[0] - 1;
for (int i = 0; i < inst.nbtypes; i++) {
lastv = min(lastv, afg.Ts[i] - 1);
lastv = std::min(lastv, afg.Ts[i] - 1);
}
for (int i = 0; i < 3; i++) {
for (const Arc &a : As) {
Expand Down Expand Up @@ -72,9 +71,9 @@ void solve(const Instance &inst, bool print_inst = false, bool pyout = false) {
}
}

vector <vector<Arc>> Al(inst.nsizes);
vector <vector<Arc>> in(afg.NV);
vector <vector<Arc>> out(afg.NV);
std::vector <std::vector<Arc>> Al(inst.nsizes);
std::vector <std::vector<Arc>> in(afg.NV);
std::vector <std::vector<Arc>> out(afg.NV);

for (const Arc &a : As) {
if (a.label != afg.LOSS) {
Expand Down Expand Up @@ -123,7 +122,7 @@ void solve(const Instance &inst, bool print_inst = false, bool pyout = false) {
printf("Total run time: %.2f seconds\n", tg + pre);

if (inst.vtype == 'I') {
map<Arc, int> flow;
std::map<Arc, int> flow;
for (const auto &a : va) {
double x = a.second.get(GRB_DoubleAttr_X);
int rx = static_cast<int>(round(x));
Expand Down

0 comments on commit a630790

Please sign in to comment.