3
$\begingroup$

I am using the cvxpy library in python to solve a quadratic programming problem and the solver used is scip. I found that when the amount of data becomes large, the solution process will be particularly slow. Therefore, I am thinking about whether C++ can speed up the solution, such as some numerical calculation libraries in C++,alglib,nplot and so on.

But I'm really not sure whether C++ will be faster. Because most of the time is consumed in the solving process, rather than the speed of code execution, I think the solvers used are similar, such as SCIP, OSQP and so on.

If I want to switch to C++ to implement, then I should need a lot of time to learn those libraries, but I am not sure whether they are really useful, because I am afraid that I have done useless work, so I raised this question and wanted to know their speed is there really a big difference.

$\endgroup$
1
  • $\begingroup$ This is a comment as it may only add to other more complete answers. The python library cython allows writing python syntax (with some extras) to create c or c++ complied python extensions. Speedup over python is incremental and up to you how deep you want to go in learning cython. Depending on the optimization library you are using and the nature of your python code, the speedup could be significant. If you have for loops over arrays, cython is sure to give big improvements. $\endgroup$
    – blarg
    Dec 5, 2022 at 0:24

2 Answers 2

3
$\begingroup$

As far as I know, the core of almost all of the optimization solvers has been written in C/C++ and other their available APIs are playing as a thin layer to exchange information on the both sides, unless one would like to write some specific callbacks or routines to deal solver, usually on the fly, that may change this too much. Also, you would find the same useful topics by searching in the community. I hope it helps.

$\endgroup$
4
  • $\begingroup$ So they are already as fast as C plus the little overhead of calling the function from python? Is it right? $\endgroup$
    – happy
    Dec 4, 2022 at 12:43
  • $\begingroup$ @happy, what I said back to the solving process and did not refer to the pre-processing or data manipulation. I work a bit with python and java and at least for data manipulation, they are different. $\endgroup$
    – A.Omidi
    Dec 4, 2022 at 12:49
  • $\begingroup$ I see, I mean just for the solving process time, prob.solve(solver='SCIP') , should they be the same? Because actually in my case, the pre-processing or data manipulation time is little. Almost all the time is consumed in the solving process. $\endgroup$
    – happy
    Dec 4, 2022 at 12:54
  • $\begingroup$ @happy, as far as I know, yes. Without any external functionality, they should be the same. $\endgroup$
    – A.Omidi
    Dec 4, 2022 at 13:02
2
$\begingroup$

Agree with Omidi.
One need to timeit and test timing of model loading/updating using one or two constraints using loops. Like Numpy/pandas in python allows vars to be listed in array/dataframe. That may vectorize it and may be faster than using, say, the sum function call from the solver API.

$\endgroup$
2
  • $\begingroup$ I see, vectorizing the problem can be faster, sum all the cost and constrains can be slower. But what if my problem can't be vectorized modeled. So I have to sum all and then solve. $\endgroup$
    – happy
    Dec 5, 2022 at 1:20
  • $\begingroup$ In that case have a look at hyperparameters of the solver- tolerance, optimality gap, #iterations etc to make sure these are not having an impact. Also if same version of solver is being distributed and if it's compatible with version of programming language. $\endgroup$ Dec 5, 2022 at 1:42

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.