Ejemplo n.º 1
0
        private int DeleteAndFreeBuffers()
        {
            int res = NonLinLeastSqProbWoConstraintsNative.dtrnlsp_delete(ref solverHandle);

            NonLinLeastSqProbWoConstraintsNative.mkl_free_buffers();
            return(res);
        }
Ejemplo n.º 2
0
        public IEnumerable <double> SolveOptimizationProblem(double precision = 1.0e-8d)
        {
            if (mSpaceVector == null)
            {
                throw new NotSetRequiredParametersException("x space values vector is null");
                return(null);
            }
            if (mFittingValuesVector == null)
            {
                throw new NotSetRequiredParametersException("fitting values vector is null");
                return(null);
            }
            if (nXspacePoint == null)
            {
                throw new NotSetRequiredParametersException("initial guess X vector is null");
                return(null);
            }
            if (fittingFunction == null)
            {
                throw new NotSetRequiredParametersException("fitting function has not been set");
                return(null);
            }

            int m = mFittingValuesVector.Count();
            int n = nXspacePoint.Count();

            double[] eps = new double[6] {
                precision, precision, precision, precision, precision, precision
            };
            int    iter1       = 100000;
            int    iter2       = 10000;
            int    RCI_Request = 0;
            double rs          = 0.0d;
            bool   successful  = false;
            double r1          = 0.0d;
            double r2          = 0.0d;

            int[] check_info = new int[6] {
                0, 0, 0, 0, 0, 0
            };
            double[] x    = nXspacePoint.ToArray();
            double[] fVec = ObjectiveFunctional(x).ToArray();
            double[,] fJacobi = new double[m, n];
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    fJacobi[i, j] = 0.0d;
                }
            }

            unsafe
            {
                fixed(double *xPtr = &x[0], fVecPtr = &fVec[0], fJacPtr = &fJacobi[0, 0])
                {
                    int init_res = NonLinLeastSqProbWoConstraintsNative.dtrnlsp_init(ref solverHandle, ref n, ref m,
                                                                                     (IntPtr)xPtr, eps, ref iter1, ref iter2, ref rs);

                    if (init_res != MKLwrapper.DEFINE.TR_SUCCESS)
                    {
                        DeleteAndFreeBuffers();
                        return(x);
                    }


                    int check_res = NonLinLeastSqProbWoConstraintsNative.dtrnlsp_check(ref solverHandle, ref n, ref m,
                                                                                       (IntPtr)fJacPtr, (IntPtr)fVecPtr, eps, check_info);

                    if (check_res != MKLwrapper.DEFINE.TR_SUCCESS)
                    {
                        DeleteAndFreeBuffers();
                        return(x);
                    }
                    else
                    {
                        if (check_info.Sum() > 0)
                        {
                            resultStatus = "input parameters aren`t valid.";
                            DeleteAndFreeBuffers();
                            return(nXspacePoint);
                        }
                    }



                    while (!successful)
                    {
                        int solve_res = NonLinLeastSqProbWoConstraintsNative.dtrnlsp_solve(ref solverHandle,
                                                                                           (IntPtr)fVecPtr, (IntPtr)fJacPtr, ref RCI_Request);
                        if (solve_res != MKLwrapper.DEFINE.TR_SUCCESS)
                        {
                            resultStatus = "error in dtrnlsp_solve";
                            DeleteAndFreeBuffers();
                            return(nXspacePoint);
                        }

                        if (RCI_Request == -1 || RCI_Request == -2 || RCI_Request == -3 || RCI_Request == -4 || RCI_Request == -5 || RCI_Request == -6)
                        {
                            successful = true;
                        }

                        if (RCI_Request == 1)
                        {
                            double[] newFVecValues = ObjectiveFunctional(x).ToArray();
                            for (int i = 0; i < m; i++)
                            {
                                fVec[i] = newFVecValues[i];
                            }
                            resultStatus = "";
                        }

                        if (RCI_Request == 2)
                        {
                            JacobianMatrixCalc jCalculator = new JacobianMatrixCalc();
                            //jCalculator.mSpaceVector = (new List<double>(mSpaceVector)).ToArray();
                            //jCalculator.mFittingValuesVector = (new List<double>(mFittingValuesVector)).ToArray();
                            jCalculator.mEventsPointsSetLength = mFittingValuesVector.Count();
                            jCalculator.nParametersSpacePoint  = (new List <double>(x)).ToArray();
                            jCalculator.objectiveFunction      = xPoint => ObjectiveFunctional(xPoint).ToArray();
                            double[,] tmpNewJacobi             = jCalculator.SolveJacobianMatrix(precision);

                            for (int i = 0; i < m; i++)
                            {
                                for (int j = 0; j < n; j++)
                                {
                                    fJacobi[i, j] = tmpNewJacobi[i, j];
                                }
                            }
                            resultStatus = "";
                        }
                    }
                }
            }



            int iterationsMade = 0;
            int stopCriterion  = 0;

            if (NonLinLeastSqProbWoConstraintsNative.dtrnlsp_get(ref solverHandle, ref iterationsMade, ref stopCriterion, ref r1, ref r2) != MKLwrapper.DEFINE.TR_SUCCESS)
            {
                resultStatus = "error in dtrnlsp_get";
                DeleteAndFreeBuffers();
                return(x);
            }


            if (NonLinLeastSqProbWoConstraintsNative.dtrnlsp_delete(ref solverHandle) != MKLwrapper.DEFINE.TR_SUCCESS)
            {
                resultStatus = "error in dtrnlsp_delete";
                return(x);
            }


            if (r2 < precision)
            {
                resultStatus = "optimization passed successfully";
                return(x);
            }
            else
            {
                resultStatus = "optimization failed";
                return(x);
            }
        }