private static Solution Secant(double minX, double maxX, Function f, StopConditions stopConditions) { var utcStart = DateTime.UtcNow; var p0 = f(minX); var p1 = f(maxX); var d = f(p1) * (p1 - p0) / (f(p1) - f(p0)); var p2 = p1 - d; int i; StopReasons stopReasons; for (i = 0; !stopConditions.ShouldStop(i, d, f(p2), utcStart, out stopReasons); i++) { p0 = p1; p1 = p2; d = f(p1) * (p1 - p0) / (f(p1) - f(p0)); p2 = p1 - d; } if ((stopReasons & StopReasons.SolutionFound) == StopReasons.SolutionFound) { return(new Solution(true, p2, i, stopReasons)); } return(new Solution(false, p2, i, stopReasons)); }
/// <summary> /// Bisections the lower solution. /// </summary> /// <param name="minX">The min X.</param> /// <param name="maxX">The max X.</param> /// <param name="f">The f.</param> /// <param name="stopConditions">The stop conditions.</param> /// <param name="numberOfFractions">The number of fractions.</param> /// <returns></returns> public static Solution BisectionLowerSolution(double minX, double maxX, Function f, StopConditions stopConditions, int numberOfFractions) { var x0 = minX; var x1 = maxX; // tamanho do espaço dividido var fraction = (x1 - x0) / numberOfFractions; var a0 = x0; var b0 = f(a0); for (var i = 0; i < numberOfFractions; i++) { // definição dos limites var a1 = a0 + fraction; var b1 = f(a1); // chama bissecção no primeiro que tiver sinal invertido (presença de raiz) if (b0 < 0 ? b1 > 0 : b1 < 0) { return(Bisection(a0, a1, f, stopConditions)); } // proxima iteração a0 = a1; b0 = b1; } return(new Solution(false, x1, numberOfFractions, StopReasons.MaximumNumberOfSteps)); }
public SpecExecution(SpecExecutionRequest request, StopConditions stopConditions, IExecutionLogger logger) { Request = request; StopConditions = stopConditions; Logger = logger; Mode = request.Mode; }
private static Solution Bisection(double minX, double maxX, Function f, StopConditions stopConditions) { var utcStart = DateTime.UtcNow; var x0 = minX; var x1 = maxX; var y0 = f(x0); var y1 = f(x1); // verifica se achou raiz if (y0 == 0) { return(new Solution(true, x0, 0, StopReasons.SolutionFound)); } if (y1 == 0) { return(new Solution(true, x1, 0, StopReasons.SolutionFound)); } // retorna false caso não exista raiz no intervalo (sinais iguais) if (y0 < 0 ? y1 < 0 : y1 > 0) { return(Math.Abs(y0) < Math.Abs(y1) ? new Solution(false, x0, 0, StopReasons.DeltaX) : new Solution(false, x1, 0, StopReasons.DeltaX)); } // iterações da bissecção var i = 0; do { var xi = (x0 + x1) / 2.0; //double y0 = f(x0); // tirei do loop var yi = f(xi); // se sinal invertido if (y0 < 0 ? yi >= 0 : yi < 0) { x1 = xi; // y0 = y0; // continua o mesmo } else { x0 = xi; y0 = yi; } if (stopConditions.ShouldStop(i, Math.Abs(x1 - x0), yi, utcStart, out var stopReasons)) { return(new Solution((stopReasons & StopReasons.SolutionFound) == StopReasons.SolutionFound, x1, i, stopReasons)); } ++i; } while (true); }
public ExecutionRun(ISystem system, Timings timings, SpecExecutionRequest request, StopConditions stopConditions, IExecutionMode mode) { _system = system; _timings = timings; _request = request; _stopConditions = stopConditions; _mode = mode; }
public void Start(StopConditions stopConditions) { _runner.UseStopConditions(stopConditions); _executionQueue.Start(); var recycled = _system.Initialize(startTheConsumingQueues); EventAggregator.SendMessage(recycled); }
public void Start(StopConditions stopConditions) { _runner.UseStopConditions(stopConditions); _executionQueue.Start(); var recycled = tryToStart(); EventAggregator.SendMessage(recycled); }
public void Start(StopConditions stopConditions) { _runner.UseStopConditions(stopConditions); _executionQueue.Start(); if (_running.RecycledMessage.success) { startTheConsumingQueues(_running.Fixtures); } EventAggregator.SendMessage(_running.RecycledMessage); }
public void Run() { switch (StopConditionMode) { case EStopConditionMode.Any: while (!StopConditions.Any(x => x.Satisfied(Population))) { Population.NextGeneration(); CreatedNextGeneration?.Invoke(this, new NewGenerationEventArgs(Population, Population.Generation)); } break; case EStopConditionMode.All: while (!StopConditions.All(x => x.Satisfied(Population))) { Population.NextGeneration(); } break; default: return; } }
public void UseStopConditions(StopConditions conditions) { _stopConditions = conditions; }
/// <summary> /// Solves by the method. /// </summary> /// <param name="method">The method.</param> /// <param name="minX">The min X.</param> /// <param name="maxX">The max X.</param> /// <param name="f">The function</param> /// <param name="stopConditions">The stop conditions.</param> /// <returns>Hopefuly, a solution</returns> public static Solution Solve(Method method, double minX, double maxX, Function f, StopConditions stopConditions) { switch (method) { case Method.Secant: return(Secant(minX, maxX, f, stopConditions)); case Method.Bisection: return(Bisection(minX, maxX, f, stopConditions)); case Method.BisectionLowerSolution: return(BisectionLowerSolution(minX, maxX, f, stopConditions, 20)); case Method.Brent: return(BrentMethod(minX, maxX, f, stopConditions)); default: throw new ArgumentOutOfRangeException(nameof(method)); } }
public StepthroughExecution(SpecExecutionRequest request, StopConditions stopConditions, IUserInterfaceObserver observer, IExecutionObserver executionObserver) : base(request, stopConditions, new InstrumentedLogger(observer)) { _observer = observer; _executionObserver = executionObserver; }
private static Solution BrentMethod(double minX, double maxX, Function f, StopConditions stopConditions) { var utcStart = DateTime.UtcNow; var a = minX; var b = maxX; var iterations = 0; //input a, b, and a pointer to a subroutine for f //calculate f(a) var fa = f(a); //calculate f(b) var fb = f(b); //if f(a) f(b) >= 0 then error-exit end if if (fa * fb >= 0) { return(new Solution(false, 0, 0, StopReasons.BadMinMaxGuess)); } //if |f(a)| < |f(b)| then swap (a,b) end if if (Math.Abs(fa) < Math.Abs(fb)) { Swap(ref a, ref b); Swap(ref fa, ref fb); } //c := a var c = a; var fc = fa; double d = 0; //set mflag var mflag = true; //repeat until f(b) = 0 or |b - a| is small enough (convergence) const double delta = 1e-10; do { iterations++; //if f(a) != f(c) and f(b) != f(c) then // fa = f(a); // fb = f(b); // fc = f(c); double s; if (fa != fc && fb != fc) { // (inverse quadratic interpolation) s = a * fb * fc / ((fa - fb) * (fa - fc)); s += b * fa * fc / ((fb - fa) * (fb - fc)); s += c * fa * fb / ((fc - fa) * (fc - fb)); } else { // (secant rule) s = b - fb * (b - a) / (fb - fa); } //if s is not between (3a + b)/4 and b or (mflag is set and |s-b| >= |b-c| / 2) or (mflag is cleared and |s-b| >= |c-d| / 2) then if (!Between(s, (3 * a + b) / 4.0, b) || mflag && Math.Abs(s - b) >= Math.Abs(b - c) / 2.0 || !mflag && Math.Abs(s - b) >= Math.Abs(c - d) / 2.0) { s = (a + b) / 2; //set mflag mflag = true; } else { //if (mflag is set and |b-c| < |delta|) or (mflag is cleared and |c-d| < |delta|) then if (mflag && Math.Abs(b - c) < delta || !mflag && Math.Abs(c - d) < delta) { // formula... s = (a + b) / 2; //set mflag mflag = true; } else { //clear mflag mflag = false; } //end if //end if } //calculate f(s) var fs = f(s); //d := c d = c; //c := b c = b; fc = fb; //* //if f(a) f(s) < 0 then b := s else a := s end if if (fa * fs < 0) { b = s; fb = fs; } else { a = s; fa = fs; } //if |f(a)| < |f(b)| then swap (a,b) end if if (Math.Abs(fa) < Math.Abs(fb)) { Swap(ref a, ref b); Swap(ref fa, ref fb); } //end repeat if (stopConditions.ShouldStop(iterations, Math.Abs(b - a), fb, utcStart, out var stopReasons)) { return(new Solution((stopReasons & StopReasons.SolutionFound) == StopReasons.SolutionFound, b, iterations, stopReasons)); } } while (true); }
public void setStopCondition(StopConditions condition, double parameter) { if (parameter <= 0) throw new Exception("Stop condition parameter must be greater than 0"); this.stopParameter = parameter; this.stopCondition = condition; }
public SpecificationExecutor(StopConditions stopConditions, IResultObserver observer) { }