NullReferenceException when running minqpoptimize
Page 1 of 1

Author:  lukaszms [ Tue Feb 15, 2022 3:46 pm ]
Post subject:  NullReferenceException when running minqpoptimize


I'm getting a NullReferenceException when running the following
code multiple times:

// Using ALGLIB to solve a quadratic program
                // cf. example_minqp_d_lc1 example on ALGLIBs page
                double[,] a = C.ToArray(); // <-- quadratic term in objective function
                double[] b = new double[C.RowCount]; // <-- no linear term in objective function
                double[] bndl = new double[C.RowCount]; // <-- lower box bound
                double[] bndu = Enumerable.Repeat<double>(Double.PositiveInfinity, C.RowCount).ToArray();
                double[] cnstrRow = gainPredictorsAt.Append(1.0).ToArray(); // <-- expected gain = 1 constraint coeffs
                double[,] cnstr = new double[1, cnstrRow.Length]; // <-- expected gain = 1 constraint
                for (var i = 0; i < cnstrRow.Length; i++) cnstr[0, i] = cnstrRow[i];
                int[] cnstrType = new int[] { 0 }; // <-- linear constraint type - equality
                double[] ret;
                alglib.minqpstate state;
                alglib.minqpreport rep;
                // create solver, set quadratic/linear terms
                alglib.minqpcreate(C.RowCount, out state);
                alglib.minqpsetquadraticterm(state, a);
                alglib.minqpsetlinearterm(state, b);
                alglib.minqpsetbc(state, bndl, bndu);
                alglib.minqpsetlc(state, cnstr, cnstrType);
                // set scale
                // optimize using BLEIC-based QP solver
                alglib.minqpsetalgobleic(state, 0.0, 1e-5, 0.0, 10000);

C is a MathNet matrix.

The crash is not deterministic with respect to the inputs. Several first input data sets give
me a definite answer, then the exception is thrown. If I permute the input data sets, again a
few first input data sets give me a definite answer, though these often correspond to inputs
that have previously yielded the exception.

The code runs on a single thread as far as I know.

Stack trace looks the following way:

at alglib.ablasf.rgemv(Int32 m, Int32 n, Double alpha, Double[,] a, Int32 opa, Double[] x, Double beta, Double[] y, xparams _params)
at alglib.optserv.feasibilityerror(Double[,] ce, Double[] x, Int32 nmain, Int32 nslack, Int32 k, Double[]& tmp0, xparams _params)
at alglib.optserv.findfeasiblepoint(Double[]& x, Double[] bndl, Boolean[] havebndl, Double[] bndu, Boolean[] havebndu, Int32 nmain, Int32 nslack, Double[,] ce, Int32 k, Double epsi, Int32& qpits, Int32& gpaits, xparams _params)
at alglib.sactivesets.sasstartoptimization(sactiveset state, Double[] x, xparams _params)
at alglib.minbleic.minbleiciteration(minbleicstate state, xparams _params)
at alglib.qpbleicsolver.qpbleicoptimize(convexquadraticmodel a, sparsematrix sparsea, Int32 akind, Boolean sparseaupper, Double absasum, Double absasum2, Double[] b, Double[] bndl, Double[] bndu, Double[] s, Double[] xorigin, Int32 n, Double[,] cleic, Int32 nec, Int32 nic, qpbleicsettings settings, qpbleicbuffers sstate, Boolean& firstcall, Double[]& xs, Int32& terminationtype, xparams _params)
at alglib.minqp.minqpoptimize(minqpstate state, xparams _params)
at alglib.minqpoptimize(minqpstate state)
at QuantTools.Portfolios.PortfolioWeightAllocationMethods.InverseCovarianceMatrixAlloc

Is there any important detail I'm missing about how alglib should be used?


Author:  lukaszms [ Wed Feb 16, 2022 12:15 pm ]
Post subject:  Re: NullReferenceException when running minqpoptimize

An additional word of explanation: Just checked this error only shows when compiling my code using dotnet compiler on Linux. On my colleague's Windows machine all the calculations run successfully.

Author:  Sergey.Bochkanov [ Wed Feb 16, 2022 12:43 pm ]
Post subject:  Re: NullReferenceException when running minqpoptimize


I don't see any obvious mistakes in your coding. Just need some time to think about possible reasons why it happens so unpredictably. The code was tested at both Windows and Linux, by the way.

I will come back tomorrow, need some time to study the sources...

Author:  Sergey.Bochkanov [ Thu Feb 17, 2022 10:37 pm ]
Post subject:  Re: NullReferenceException when running minqpoptimize

What exactly ALGLIB version you used - fully managed NET5/etc or NET5 with SIMD intrinsics?

I want to add debug logging to rgemv() - a function that raises an exception - so we will be able to know more about its arguments. The unpredictable nature of this fault is still completely unexplainable. I do not pretend to be a perfect programmer, but heisenbugs like this are unlikely to appear in this specific part of the program.

Author:  lukaszms [ Mon Feb 21, 2022 10:59 am ]
Post subject:  Re: NullReferenceException when running minqpoptimize

Thank you for the response. I'm running managed code as far as I know. I imported 3.18.2 using NuGet, and my dotnet SDK is 3.1.416, installed using snap (dotnet-sdk package). By the way, I'm not a C#/programming expert - I might be missing some important point.

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group