0
$\begingroup$

I am working on the scaling of a MIP using C++. I use Gurobi within CLion. The Gurobi model I use now in Clion require me to enter everything manually (model.addVar, model.addConstr...). However, in a large integer program with more than 1000 constraints, it is not feasible to define each constraint. So I need to make CLion read the Gurobi file(.lp) and solve without using things like model.addVar, model.addConstr...

How to make CLion read the .lp file and make Gurobi solve that file within Clion?

Update:

So far I have tried to GRBModel in Clion in the following form below but it gives an error and cannot solve the model.

GRBEnv env = GRBEnv();
GRBModel model = GRBModel(env,"coins.lp");

I have used coins.lp to test the model via Command line using $gurobi_cl coins.lp and Gurobi is able to solve it without an issue there.

$\endgroup$
6
  • $\begingroup$ 1. CLion is an IDE and does not compile your C/C++ code - it merely tells a compiler like gcc or clang to do that. 2. LP format is not well suited in a data transformation approach because it may cut off coefficients. You should better use MPS to write out and read in the problems. Although, the correct way of doing it would be completely in code without writing anything to disk. 3. 1000 constraints is anything but large by today's standards. Modern solvers are capable of optimizing models with several million constraints. $\endgroup$
    – mattmilten
    Commented Feb 27, 2022 at 14:16
  • $\begingroup$ Clion has built-in the compiler so in Linux/Windows or in any kind of environment, I do not install compiler separately, so I stick with what Clion use and so I wrote it. What does MPS stand for? Yes, I know they are capable of solving but without reading files automatically, we cannot even solve more than 1000 since we cannot define all of them one by one. $\endgroup$
    – asdf
    Commented Feb 27, 2022 at 16:24
  • $\begingroup$ You can read more about file types supported by Gurobi (including MPS): gurobi.com/documentation/9.5/refman/model_file_formats.html I still don't understand where your data is coming from and how you generate those LP files in the first place but this is clearly beyond the scope of this comment section. $\endgroup$
    – mattmilten
    Commented Feb 27, 2022 at 19:06
  • $\begingroup$ .lp files are already given. I need to create Scaler app that will take those .lp files scale it and solve the scaled model. Referance manual has every function, but it is so limited on use cases in C++. I think how to integrate all those functions to the C++ program that uses Gurobi could be more clearly explained $\endgroup$
    – asdf
    Commented Mar 1, 2022 at 17:57
  • $\begingroup$ You should probably develop a prototype with Python first and only later convert the code to C++. To see how the Gurobi API is used, you should check out the examples: gurobi.com/documentation/9.5/examples/cpp_examples.html $\endgroup$
    – mattmilten
    Commented Mar 2, 2022 at 11:28

1 Answer 1

4
$\begingroup$

I'm assuming you have the .lp file. Check the example in the Gurobi webpage lp_c++.cpp, where they show how to create a model based on a .lp file, and optimize it (respectively, lines 23 and 25 in the code, I believe).

The example above should be run from the terminal (after compiled) using the syntax below, and indicated in line 17 (else, that'll show up as an error).

lp_c++.exe my_model.lp

Now, to specify the .lp file inside your code:

  1. Change line 23 to GRBModel model = GRBModel(env, "my_model.lp"); and make sure you put my_model.lp in the same folder or provide the path to it inside the quotes.
  2. Comment lines 16-19 (or you'll see the error "Usage:..." again)

Edit: I've never used CLion, so my answer is not specific to it, but I don't know how it would change if it was.

$\endgroup$
8
  • $\begingroup$ I could not see any place that I can make this code read my .lp file. Could describe how can I feed this code with my lp file as input? $\endgroup$
    – asdf
    Commented Feb 20, 2022 at 16:50
  • $\begingroup$ The .lp file name will be provided when you run the .exe file (in the command line, for example) after you compile the .cpp file. The syntax is explained in line 17. Can you compile the .cpp file? Also, can you please add more details regarding what you have attempted so far? $\endgroup$ Commented Feb 21, 2022 at 8:39
  • $\begingroup$ I could not compile the .cpp file, since I am using Terminal and not Clion when I try to $ make, it gives an error, you can see terminal output below, sandboxGurobi g++ sandboxGurobi.cpp -o sandboxGurobi sandboxGurobi.cpp:9:10: fatal error: gurobi_c++.h: No such file or directory 9 | #include "gurobi_c++.h" | ^~~~~~~~~~~~~~ compilation terminated. $\endgroup$
    – asdf
    Commented Feb 21, 2022 at 13:10
  • $\begingroup$ I could run this code on Clion without any problems but (it gives an output: Usage: lp_c++ filename), whereas I would like to solve .lp file with it, how can I both make it work and solve the lp, one side of this thing is using terminal and another side is using Clion (due to Gurobi dependencies and others), what should I do? @Nara Begnini $\endgroup$
    – asdf
    Commented Feb 21, 2022 at 19:21
  • $\begingroup$ When running via Terminal, it'll be something like "lp_c++.exe my_model.lp" (no quotes). This syntax is what the output "Usage: ..." means. It'll read and optimize your model. $\endgroup$ Commented Feb 21, 2022 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.