3
votes

I just inherited a piece of code, which solves a mixed integer programming problem using OR Tools. Integrating OR Tools into VS 2019 was a headache of its own (well, I'm not that used to C++). Now the program is running, but I am getting:

Access violation reading location 0x0000000000000020

According to the debugger, it looks like solver.MutableObjective() is returning null.

Other functions like MakeRowConstraint seem to work fine though, and they don't return null. Any clue what's going on? Could it be something I misconfigured when installing the library maybe?

MPSolver solver("simple_mip_program", MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING);
//...
MPObjective* FObj = solver.MutableObjective(); // <-- NULL

//therefore the below throws exception.
//The arrays `variables` and `F` are populated as expected
for (int i = 0; i < n; i++) FObj->SetCoefficient(variables[i], F[i]);
FObj->SetMaximization();

Edit: A little more details, I am using ORTools 64bit_v8.1.8487, on Windows 10 and Visual Studio 2019.

I also tried running the sample MIP problem from GitHub, and same error occurred on line 54.

4
did it work if you are using the glop solver ? pretty sure you didn't have the USE_SCIP define defined... - Mizux

4 Answers

1
votes

I have the exactly same settings: ORTools 64bit_v8.1.8487, on Windows 10 and Visual Studio 2019.

To reproduce the question, run the sample code "examples/cpp/linear_programming_example.cc" in Visual Studio 2019. (with .lib and includes/ included into the property.)

I solve the problem by-

  1. changing the C++ compiler to C++17 (default: 14) and
  2. adding flags " /D_SILENCE_CXX17_OLD_ALLOCATOR_MEMBERS_DEPRECATION_WARNING /D_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS " when compiling.
0
votes

You need to call Minimize() or Maximize() before to create the objective.

0
votes

What I ended up doing is more of a workaround.

The ORTools library download comes with example programs and a Makefile. So, I replaced the contents of one of the sample files with the code I need, and used the supplied Makefile to compile/run the program.

This way, the exact same code I had is not showing errors anymore. So, I guess the problem was a linking/import issue somewhere.

0
votes

If you want to integrate the lib in your own project you also need the correct flags

 CXXFLAGS = /std:c++17 /EHsc /MD /nologo /D_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS -nologo $(DEBUG) \ 
   /DPSAPI_VERSION=1 /D__WIN32__ /DNOMINMAX /DWIN32_LEAN_AND_MEAN=1 /D_CRT_SECURE_NO_WARNINGS \ 
   /DGOOGLE_GLOG_DLL_DECL= \ 
   /I$(INC_DIR)\\src\\windows /I$(INC_DIR) /I. \ 
   /DUSE_BOP /DUSE_GLOP \ 
   /DUSE_CBC /DUSE_CLP \ 
   /DUSE_SCIP 

ref: https://github.com/google/or-tools/blob/fa84bc05e72641dddfbb98164d81b8bc9bef6ea5/tools/Makefile.cc.java.dotnet#L126-L132