forum.alglib.net

ALGLIB forum
It is currently Sun Jul 03, 2022 10:58 pm

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: NullReferenceException when running minqpoptimize
PostPosted: Tue Feb 15, 2022 3:46 pm 
Offline

Joined: Tue Feb 15, 2022 3:25 pm
Posts: 3
Hi,

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

Code:
// 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
                alglib.minqpsetscaleautodiag(state);
                // optimize using BLEIC-based QP solver
                alglib.minqpsetalgobleic(state, 0.0, 1e-5, 0.0, 10000);
                alglib.minqpoptimize(state);


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:

Quote:
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?

Ɓukasz


Top
 Profile  
 
 Post subject: Re: NullReferenceException when running minqpoptimize
PostPosted: Wed Feb 16, 2022 12:15 pm 
Offline

Joined: Tue Feb 15, 2022 3:25 pm
Posts: 3
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.


Top
 Profile  
 
 Post subject: Re: NullReferenceException when running minqpoptimize
PostPosted: Wed Feb 16, 2022 12:43 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 874
Hi!

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...


Top
 Profile  
 
 Post subject: Re: NullReferenceException when running minqpoptimize
PostPosted: Thu Feb 17, 2022 10:37 pm 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 874
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.


Top
 Profile  
 
 Post subject: Re: NullReferenceException when running minqpoptimize
PostPosted: Mon Feb 21, 2022 10:59 am 
Offline

Joined: Tue Feb 15, 2022 3:25 pm
Posts: 3
Thank you for the response. I'm running managed code as far as I know. I imported alglib.net 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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group