Пример #1
0
        public bool Solve(EquationSystem problem)
        {
            if (Subproblems != null)
            {
                Subproblems.Clear();
            }

            this.ProblemData = problem;

            ProblemData.CreateIndex();
            ProblemData.GenerateJacobian();

            Decompose(problem);

            if (IsOverconstrained && IsInsufficientRank)
            {
                return(false);
            }

            Newton newtonSubsolver = new Newton();

            newtonSubsolver.MaximumIterations = NewtonMaxIter;
            newtonSubsolver.Tolerance         = NewtonTolerance;
            newtonSubsolver.DoLinesearch      = DoLinesearch;
            newtonSubsolver.LambdaMin         = MinNewtonLambda;
            //newtonSubsolver.BrakeFactor = 0.99;

            //newtonSubsolver.OnLog += OnLog;
            // newtonSubsolver.OnLogSuccess += OnLogSuccess;
            newtonSubsolver.OnLogError   += OnLogError;
            newtonSubsolver.OnLogWarning += OnLogWarning;

            int i = 1;
            // LogInfo("Solving decomposed sub-problems...");
            bool hasError = false;
            var  watch    = System.Diagnostics.Stopwatch.StartNew();

            foreach (var decomposedNlp in _decomposedNlps)
            {
                if (decomposedNlp.Variables.Count == 0)
                {
                    continue;
                }

                var statusmessage = "Solving problem " + i + " of " + _decomposedNlps.Count + " (Size: " + decomposedNlp.Variables.Count + ")";
                //   PublishProgress(statusmessage, (int)((i / (double)_decomposedNlps.Count) * 100));
                //  PublishStatus(statusmessage);
                newtonSubsolver.Solve(decomposedNlp);

                if (!newtonSubsolver.IsConverged)
                {
                    var eval = new Evaluator();
                    // PublishStatus("Solving problem " + decomposedNlp.Name + " failed!");
                    hasError = true;
                    LogError("Solving problem " + decomposedNlp.Name + " (Size: " + decomposedNlp.Variables.Count + ") failed!");
                    LogError("The 20 most problematic constraints are:");
                    foreach (var eq in decomposedNlp.Equations.OrderByDescending(c => Math.Abs(c.Residual(eval))).Take(Math.Min(20, decomposedNlp.Equations.Count)))
                    {
                        LogError(String.Format("{2,-20} {3,-10} {4,-15} {0,20} ( {1} )", eq.Residual(eval).ToString("G8"), eq, eq.ModelClass, eq.ModelName, eq.Group));
                    }
                    LogError("");

                    break;
                }
                else
                {
                    //LogSuccess("Problem " + decomposedNlp.Name + " solved in " + subsolver.Iterations + " iterations.");
                }

                i++;
            }
            watch.Stop();

            if (!hasError)
            {
                LogSuccess("Problem " + problem.Name + " was successfully solved (" + watch.Elapsed.TotalSeconds.ToString("0.00") + " seconds)");
                return(true);
            }
            else
            {
                LogError("Problem " + problem.Name + " was not successfully solved (Result = " + ")");
                return(false);
            }
            //PublishStatus("Solution finished...");
            //  PublishProgress("No background work", 0);
        }