Random Search |
This topic contains the following sections:
The algorithm randomly combines factor values for the best objective value search. As the search grid can be extremely large, there is no mechanism of parameters combinations duplicate prevention.
The algorithm supports simple (one search point at once) and block-oriented (multiple search point points at once) objective function estimation. Compared with simple search the block oriented call allows user to perform multiple search point optimization in parallel or use shared information for calculation speedup.
Next most important methods and properties are featured in the class:
1using System; 2using System.Collections.Generic; 3using System.Text; 4using System.Threading.Tasks; 5using FinMath.MachineLearning; 6using FinMath.MachineLearning.EvolutionaryAlgorithms; 7using FinMath.Statistics; 8 9namespace FinMath.Samples 10{ 11 internal class RandomSearchSample 12 { 13 static readonly SearchAlgorithm.ICountableFactor[] Factors = 14 { 15 new SearchAlgorithm.DiscreteFactor("DiscreteFactor1", Math.PI, -Math.Exp(1.0), false, 5), 16 new SearchAlgorithm.DiscreteFactor("DiscreteFactor1", 2.7, 3.1, false, 0.1), 17 new SearchAlgorithm.CategoricalFactor("CategoricalFactor1", new double[] {3, 2, -1}), 18 new SearchAlgorithm.CategoricalFactor("CategoricalFactor2", new double[] {-1, 1, 45}), 19 }; 20 21 public static void SimpleProcessing() 22 { 23 TerminationWatcher tw = new TerminationWatcher(timeout: new TimeSpan(0, 0, 5)); 24 25 var searchAlgo = new RandomSearch(Factors, 26 factorsPoint => 27 { 28 var objective = factorsPoint.SumMagnitudes(); 29 tw.Update(objective); 30 return objective; 31 }); 32 searchAlgo.ExitDelegate = () => tw.IsTerminalState; 33 34 searchAlgo.Run(); 35 36 Console.WriteLine($"RandomSearchSample.SimpleProcessing: Best objective: {searchAlgo.MinimumObjectiveValue} at [{String.Join(", ", searchAlgo.MinimumObjectivePoint)}]"); 37 } 38 39 public static void BlockProcessing() 40 { 41 TerminationWatcher tw = new TerminationWatcher(timeout: new TimeSpan(0, 0, 5)); 42 Object twLock = new Object(); 43 44 var searchAlgo = new RandomSearch(Factors, 10, 45 (factorPoints, objectives) => Parallel.For(0, factorPoints.Length, 46 i => 47 { 48 objectives[i] = factorPoints[i].SumMagnitudes(); 49 lock (twLock) 50 { 51 tw.Update(objectives[i]); 52 } 53 })); 54 searchAlgo.ExitDelegate = () => tw.IsTerminalState; 55 56 searchAlgo.Run(); 57 58 Console.WriteLine($"RandomSearchSample.BlockProcessing: Best objective: {searchAlgo.MinimumObjectiveValue} at [{String.Join(", ", searchAlgo.MinimumObjectivePoint)}]"); 59 } 60 } 61}