public void TraverseChildren(OnIteration callback) { foreach (Node n in m_children) { callback(n); } }
KmeansTrainResult <T> Training() { var ThisGen = FirstGeneration; for (int Iteration = 0; Iteration < IterationLimit; Iteration++) { ClassifyUntrainedData(ThisGen); var NextGen = GenerateNextIteration(ThisGen); OnIteration?.Invoke(ThisGen, Iteration); float LongestDistance = CalcLongestDistance(ThisGen, NextGen); if (LongestDistance < ConvDistance) { Console.WriteLine("Iteration:{0}, LongestDistance:{1} < {2}, Done", Iteration, LongestDistance, ConvDistance); break; } else { Console.WriteLine("Iteration:{0}, LongestDistance:{1} > {2}, Continue...", Iteration, LongestDistance, ConvDistance); ThisGen = NextGen; } } foreach (var vectorCollection in ThisGen) { vectorCollection.Tag = vectorCollection.GetMostTag(); } var trainResult = new KmeansTrainResult <T>(ThisGen.Select(x => x.Center)); return(trainResult); }
private (double LeftBound, double RightBound) GetBoundInternal(double startPoint, double step, bool minimization, int maxIterations) { // Get initial range var innerPoint = startPoint; var leftPoint = startPoint - step; var rightPoint = startPoint + step; DirectionDelegate getDirection; if (minimization) { // Min function on left side getDirection = GetDirectionForMinimization; } else { // Max function on right side getDirection = GetDirectionForMaximization; } var direction = getDirection(leftPoint, innerPoint); for (int iteration = 0; iteration < maxIterations; iteration++) { // If function changed direction, then found extremum if (direction != getDirection(leftPoint, rightPoint)) { return(LeftBound : leftPoint, RightBound : rightPoint); } // Calculating step to shift range bounds var h = Math.Pow(2, iteration + 1) * step; // Set new bounds, depended of direction // WARNING!!! Do not change position of set range if (direction == Direction.Right) { // Shift to right leftPoint = innerPoint; innerPoint = rightPoint; rightPoint = innerPoint + h; } else { // Shift to left rightPoint = innerPoint; innerPoint = leftPoint; leftPoint = innerPoint - h; } OnIteration?.Invoke(this, new IterationInfoEventArgs(leftPoint, rightPoint, iteration)); } throw new Exception($"Maximum number of iterations ({maxIterations}) reached"); }
public bool IsAccepted(IEnumerable <Terminal> terminals, OnIteration onIteration = null) { onIteration = onIteration ?? ((_, __) => { }); var terminalsList = terminals.Cast <Symbol>().ToList(); terminalsList.Add(new End()); var stack = new Stack <Symbol>(); stack.Push(new Start()); var i = 0; while (i < terminalsList.Count && stack.Any()) { onIteration(stack, terminalsList.Skip(i)); var current = terminalsList[i]; var pair = (stack.Peek(), current); if (grammar.IsEqual(pair) || grammar.IsLess(pair)) { stack.Push(current); i++; } else if (grammar.IsGreater(pair)) { if (!rules.TryGetByLongestPrefix(stack, out var rule)) { if (stack.Peek() == grammar.Grammar.Axiom && current == new End()) { stack.Push(current); onIteration(stack, new Symbol[0]); break; } return(false); } stack.PopCount(rule.Right.Count); stack.Push(rule.Left); } else { return(false); } } return(stack.Count == 3 && stack.Pop() == new End() && stack.Pop() == grammar.Grammar.Axiom && stack.Pop() == new Start()); }
public List <TimeStep> Integrate(DAEProblem problem, ILogger logger) { StepSizeMax = Math.Abs(EndTime - StartTime) / 50.0; var results = new List <TimeStep>(); problem.Time.SetValue(StartTime); problem.TimeStep.SetValue(StepSize); if (UseDulmageMendelsohnSolver) { Solver = new DecompositionSolver(logger); } else { Solver = new BasicNewtonSolver(logger); } double rho = 0; double eps = 0; Vector y1h = new Vector(problem.AlgebraicVariables.Count); Vector y2h = new Vector(problem.AlgebraicVariables.Count); Vector y0 = new Vector(problem.AlgebraicVariables.Count); Vector yt_1 = new Vector(problem.AlgebraicVariables.Count); Vector yt_2 = new Vector(problem.AlgebraicVariables.Count); Vector dummy = new Vector(problem.AlgebraicVariables.Count); double lastProgress = 0; double currentProgress = 0; while (problem.Time.InternalValue <= EndTime) { var x = problem.AlgebraicVariables.Select(v => v.InternalValue).ToArray(); var y = problem.DifferentialVariables.Select(v => v.InternalValue).ToArray(); var t0 = problem.Time.InternalValue; var currentStep = new TimeStep() { Time = t0, AlgebraicStates = x, DifferentialStates = y }; results.Add(currentStep); rho = 0; fillVector(y0, yt_1, yt_2, problem); var dt = 0.0; int iter = 0; while (rho < 1) { dt = 2 * StepSize; resetVariables(y0, yt_1, yt_2, problem); problem.Time.SetValue(t0); TakeStep(problem, 2 * StepSize); fillVector(y2h, dummy, dummy, problem); resetVariables(y0, yt_1, yt_2, problem); problem.Time.SetValue(t0); TakeStep(problem, StepSize); TakeStep(problem, StepSize); fillVector(y1h, dummy, dummy, problem); eps = (y1h - y2h).GetNorm() / 6.0; rho = StepSize * eps / Tolerance; StepSize = Math.Min(StepSize * Math.Pow(1.0 / rho, 1.0 / 3.0), 2 * StepSize); if (problem.Time.InternalValue + 2 * StepSize > EndTime) { StepSize = (EndTime - problem.Time.InternalValue) / 2.0; } if (StepSize < MinStepSize) { StepSize = MinStepSize; } problem.Time.SetValue(t0); if (iter > 10) { break; } iter++; } problem.Time.AddDelta(dt); currentProgress = problem.Time.InternalValue / EndTime; if (currentProgress - lastProgress > 0.01) { OnIteration?.Invoke(problem.Time.InternalValue / EndTime); lastProgress = currentProgress; } } return(results); }
public List <TimeStep> Integrate(DAEProblem problem, ILogger logger) { var results = new List <TimeStep>(); TotalSteps = (int)((EndTime - StartTime) / StepSize); problem.Time.SetValue(StartTime); problem.TimeStep.SetValue(StepSize); if (UseDulmageMendelsohnSolver) { Solver = new DecompositionSolver(logger); } else { Solver = new BasicNewtonSolver(logger); } var x = problem.AlgebraicVariables.Select(v => v.InternalValue).ToArray(); var y = problem.DifferentialVariables.Select(v => v.InternalValue).ToArray(); var currentStep = new TimeStep() { Time = StartTime, AlgebraicStates = x, DifferentialStates = y }; results.Add(currentStep); var reportingInveral = TotalSteps / 100; for (int i = 0; i < TotalSteps; i++) { problem.Time.AddDelta(StepSize); problem.Step(Solver); foreach (var dery in problem.DifferentialVariables) { var cury = problem.Mapping[dery]; var yt_1 = PreMap[cury]; var yt_2 = PreMap[yt_1]; yt_2.SetValue(yt_1.InternalValue); yt_1.SetValue(cury.InternalValue); } x = problem.AlgebraicVariables.Select(v => v.InternalValue).ToArray(); y = problem.DifferentialVariables.Select(v => v.InternalValue).ToArray(); currentStep = new TimeStep() { Time = problem.Time.InternalValue, AlgebraicStates = x, DifferentialStates = y }; results.Add(currentStep); logger.Log(currentStep.ToString()); if (i % reportingInveral == 0) { OnIteration?.Invoke(problem.Time.InternalValue / EndTime); } //System.Threading.Thread.Sleep(2); } return(results); }
public void Solve(EquationSystem system) { if (!SuppressLogging) { Log(String.Format("{0} {1,15} {2,15} {3,7} {4}", "Iter", "Step Length", "Infeasibility", "Damping", "Algorithm")); } if (!system.IsSquare()) { Status = "Newton-Solver can only solve square problems."; IsAborted = true; IsConverged = false; return; } ProblemData = system; ProblemData.CreateIndex(); ProblemData.GenerateJacobian(); Evaluator eval = new Evaluator(); _watch = System.Diagnostics.Stopwatch.StartNew(); var algorithm = "Newton"; var status = false; var scalingLogSum = new MatrixScalingLogSum(2); double[] U = null, V = null; Iterations = 0; IsConverged = false; IsAborted = false; double varNorm = 0; double eqNorm = 0; Vector delta = new Vector(system.NumberOfVariables); var lambda = BrakeFactor; for (int i = 0; i < system.NumberOfVariables; i++) { delta[i] = system.Variables[i].ValueInSI; } while (!IsConverged && !IsAborted) { string flags = ""; #region Jacobian and Residuals eval.Reset(); var b = CSparseWrapper.FillResiduals(system, eval); var A = CSparseWrapper.FillJacobian(system, eval); #endregion varNorm = delta.GetNorm(); eqNorm = 0;// string debugString = ""; for (int i = 0; i < b.Size; i++) { var babs = Math.Abs(b[i]); if (babs > eqNorm) { eqNorm = babs; if (DebugMode) { debugString = ProblemData.Equations[i].ToString(); } } } //b.ToDouble().Max((s) => Math.Abs(s)); //if(DebugMode) //debugString= ProblemData.Equations.Max(eq=> Math.Abs(eq.Residual())) if (!SuppressLogging) { Log(String.Format(" {0:000} {1,15} {2,15} {3,7} {4,4} {5} {6} {7}", Iterations, varNorm.ToString("G2", CultureInfo.InvariantCulture), eqNorm.ToString("G8", CultureInfo.InvariantCulture), lambda, "NEWTON", algorithm, flags, debugString)); } OnIteration?.Invoke(system); CheckAbortionCriteria(Iterations, MaximumIterations, eqNorm, Tolerance); if (IsAborted || IsConverged) { break; } #region Scaling //TODO: Only rescale when necessary? Maybe decide based on condition number? // Current version only scales once at the beginning. if (ScalingFrequency == MatrixScalingFrequency.Once && (U == null || V == null) || ScalingFrequency == MatrixScalingFrequency.Always) { scalingLogSum.GetMatrixScalingFactors(A, out U, out V); } if (DoScaling) { for (int i = 0; i < system.NumberOfEquations; i++) { b[i] = b[i] * U[i]; } var UM = CSparseWrapper.CreateDiagonal(system.NumberOfEquations, U); var VM = CSparseWrapper.CreateDiagonal(system.NumberOfEquations, V); A = UM.Multiply(A); A = A.Multiply(VM); } #endregion delta = CSparseWrapper.SolveLinearSystem(A, delta, -b, out status, out algorithm); #region Scaling 2 if (DoScaling) { for (int i = 0; i < delta.Size; i++) { delta[i] = delta[i] * V[i]; } } #endregion lambda = BrakeFactor; if (delta.GetNorm() > _maximumNewtonStep) { //LogWarning("Variable step above limit. Truncating Newton step"); flags += "T"; delta /= delta.GetNorm() / _maximumNewtonStep; } else { flags += "_"; } if (status == false) { LogWarning("Error during factorization. Performing steepest descent step"); //delta = new Vector(system.NumberOfVariables, 1e-6); //Force rescaling when now direction could be found if (ScalingFrequency == MatrixScalingFrequency.OnDemand) { U = null; V = null; } flags += "D"; lambda *= 0.1; A.TransposeMultiply((-b).ToDouble(), delta.ToDouble()); } else { flags += "_"; } Func <Vector, Double> FuncLineSearch = ((r) => r.ToDouble().Max(s => Math.Abs(s))); var F0 = FuncLineSearch(b); var x0 = system.Variables.Select(v => v.ValueInSI).ToArray(); var lineSearchIter = 0; var currentStepLength = lambda; var lambdaMin = _lambdaMin; if (DoLinesearch) { while (lineSearchIter < 10) { for (int i = 0; i < delta.Size; i++) { var vari = system.Variables[i]; vari.ValueInSI = x0[i]; } ApplyNewtonStep(system, lambda, delta); eval.Reset(); b = CSparseWrapper.FillResiduals(system, eval); if (DoScaling) { for (int i = 0; i < system.NumberOfEquations; i++) { b[i] = b[i] * U[i]; } } var F1 = FuncLineSearch(b); if (F1 >= F0) { lambda *= 0.5; if (lambda < lambdaMin) { lambda = lambdaMin; } currentStepLength = lambda; if (!flags.Contains("L")) { flags += "L"; } } else { if (!flags.Contains("L")) { flags += "_"; } currentStepLength = lambda; break; } lineSearchIter++; } } else { flags += "_"; ApplyNewtonStep(system, lambda, delta); } Iterations++; } }
protected void Notify(double leftBound, double rightBound, int iteration) { OnIteration?.Invoke(this, new IterationInfoEventArgs(leftBound, rightBound, iteration)); Iterations++; }
/// <summary> /// Safely invoke the iteration event /// </summary> /// <param name="iteration"></param> /// <param name="objVal"></param> /// <param name="rmsDeriv"></param> protected internal void RaiseIterationEvent(int iteration, double objVal, double rmsDeriv) { OnIteration?.Invoke(this, new OptimiserIterationEventArgs(iteration, objVal, rmsDeriv)); }