Click or drag to resize

Termination Watcher

This topic contains the following sections:

Termination watcher is a utility class for the optimization algorithm stop conditions customization. This class just allows user to simplify his code.

The termination watcher allows user control next parameters:

  • Total iterations count;

  • Total execution time;

  • Objective function best value improvement.

Implementation

Next most important methods and properties are featured in the class:

Termination conditions properties:

Property

Description

PropertyEvaluationsLimit

Maximum number of objective function evaluations till termination.

PropertyTimeout

The algorithm execution timeout till termination flag.

PropertyEpsilon

Minimal fluctuation of the best objective value for next point evaluation.

PropertyEpsilonInterval

Best objective value fluctuation analysis interval.

Execution properties:

Property

Description

PropertyIsTerminalState

The terminal conditions have met flag.

PropertyEvaluationsCount

The total number of evaluation performed.

PropertyElapsedTime

The total elapsed time from this object creation.

Objective function properties:

Property

Description

PropertyBestObjectiveValue

The best observed objective value.

PropertyBestObjectiveIndex

The best observed objective value index.

PropertyBestObjectiveModel

The optional model, associated with the best observed objective value.

Code sample

The next code generates test objective function and process it with the TerminationWatcher. The results of such processing are presented on the next figure.

TW Best Objective

C#
 1using System;
 2using FinMath.MachineLearning;
 3using FinMath.Statistics;
 4
 5namespace FinMath.Samples
 6{
 7    class TerminationWatcherSample
 8    {
 9        private class RawRandomObjective
10        {
11            private RandomGenerator random = new RandomGenerator(42);
12            private long i = 0;
13
14            public double Next => Math.Exp(i++ / -100.0) + random.NextDouble() + Math.PI;
15        }
16
17        static void Main()
18        {
19            var objective = new RawRandomObjective();
20
21            var tw = new TerminationWatcher(1e-3, 1000, new TimeSpan(0, 1, 0));
22            while (!tw.IsTerminalState)
23            {
24                tw.Update(objective.Next);
25            }
26
27            Console.WriteLine($"Optimization stops on the {tw.EvaluationsCount} iteration in {tw.ElapsedTime} with best objective value {tw.BestObjectiveValue} at {tw.BestObjectiveIndex} step.");
28        }
29    }
30}

See Also