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.
Next most important methods and properties are featured in the class:
Method | Description |
---|---|
TerminationWatcher(NullableDouble, NullableInt32, NullableTimeSpan, NullableInt64) | The TerminationWatcher constructor that allows to setup termination conditions. |
Updates the TerminationWatcher state and properties. The NaN values are ignored without errors but the EvaluationsLimit and Timeout are checked. |
Termination conditions properties:
Property | Description |
---|---|
Maximum number of objective function evaluations till termination. | |
The algorithm execution timeout till termination flag. | |
Minimal fluctuation of the best objective value for next point evaluation. | |
Best objective value fluctuation analysis interval. |
Execution properties:
Property | Description |
---|---|
The terminal conditions have met flag. | |
The total number of evaluation performed. | |
The total elapsed time from this object creation. |
Objective function properties:
Property | Description |
---|---|
The best observed objective value. | |
The best observed objective value index. | |
The optional model, associated with the best observed objective value. |
The next code generates test objective function and process it with the TerminationWatcher. The results of such processing are presented on the next figure.
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}