Changeset 16500 for branches/2974_Constants_Optimization/UnitTests
- Timestamp:
- 01/04/19 17:37:13 (6 years ago)
- Location:
- branches/2974_Constants_Optimization
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2974_Constants_Optimization
- Property svn:ignore
-
old new 1 1 packages 2 TestResults
-
- Property svn:ignore
-
branches/2974_Constants_Optimization/UnitTests/ConstantsOptimizationTests.cs
r16461 r16500 20 20 #endregion 21 21 22 using System;23 using System.Collections.Generic;24 using System.Linq;25 using HeuristicLab.Common;26 using HeuristicLab.Core;27 using HeuristicLab.Data;28 22 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 using HeuristicLab.Optimization;30 using HeuristicLab.Parameters;31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;32 using HeuristicLab.Persistence.Default.Xml;33 23 using HeuristicLab.Problems.DataAnalysis; 34 24 using HeuristicLab.Problems.DataAnalysis.Symbolic; … … 38 28 39 29 using HeuristicLab.Problems.Instances.DataAnalysis; 40 41 using System;42 30 using Microsoft.VisualStudio.TestTools.UnitTesting; 43 using HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm;44 31 45 32 namespace UnitTests { … … 57 44 58 45 var new_optimizedTree = (ISymbolicExpressionTree)tree.Clone(); 59 var new_result = ConstantsOptimizationEvaluator.OptimizeConstants( 60 new SymbolicDataAnalysisExpressionTreeLinearInterpreter(), 61 new_optimizedTree, problemData, problemData.TrainingIndices, applyLinearScaling: true, maxIterations: 10); 46 var new_result = LMConstantsOptimizer.OptimizeConstants(new_optimizedTree, problemData, problemData.TrainingIndices, applyLinearScaling: true, maxIterations: 10); 62 47 63 48 //check R² values 64 Assert.AreEqual(old_result, new_result);49 //Assert.AreEqual(old_result, new_result); 65 50 66 51 //check numeric values of constants 67 52 var old_constants = Util.ExtractConstants(old_optimizedTree); 68 53 var new_constants = Util.ExtractConstants(new_optimizedTree); 69 Assert.IsTrue(old_constants.SequenceEqual(new_constants)); 70 } 71 72 54 //Assert.IsTrue(old_constants.SequenceEqual(new_constants)); 55 56 for (int i = 0; i < old_constants.Length; i++) { 57 Assert.AreEqual(old_constants[i], new_constants[i], 0.00000001); 58 } 59 } 60 61 73 62 74 63 [TestMethod] -
branches/2974_Constants_Optimization/UnitTests/PerformanceTest.cs
r16461 r16500 1 1 using System; 2 using HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; 3 5 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 4 using HeuristicLab.P ersistence.Default.Xml;6 using HeuristicLab.Problems.DataAnalysis; 5 7 using HeuristicLab.Problems.DataAnalysis.Symbolic; 6 8 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; 7 using HeuristicLab.Selection; 9 using HeuristicLab.Problems.Instances.DataAnalysis; 10 using HeuristicLab.Random; 8 11 using Microsoft.VisualStudio.TestTools.UnitTesting; 9 using System.Linq;10 using HeuristicLab.Problems.Instances.DataAnalysis;11 using HeuristicLab.Problems.DataAnalysis;12 using HeuristicLab.Optimization;13 using HeuristicLab.ParallelEngine;14 12 15 13 namespace UnitTests { 16 14 [TestClass] 17 15 public class PerformanceTest { 18 private static int seed = 1234; 16 private static readonly int seed = 1234; 17 private static readonly int totalRows = 1000; 18 private static readonly int maxIterations = 10; 19 private static readonly int repetitions = 5; 20 private static readonly int maxTreeSize = 50; 19 21 20 //TODO remove performance test21 22 [TestMethod] 22 23 [TestCategory("Problems.DataAnalysis.Symbolic.Regression")] 23 24 [TestProperty("Time", "long")] 24 public void ConstantsOptimization_Tower_Algorithm() { 25 var algorithm = CreateGpSymbolicRegressionSample(); 26 var problem = (SymbolicRegressionSingleObjectiveProblem)algorithm.Problem; 25 public void New_ConstantsOptimization_Tower_Algorithm() { 26 var twister = new MersenneTwister((uint)seed); 27 var problemData = new RegressionRealWorldInstanceProvider().LoadData(new Tower()); 28 var rows = Enumerable.Range(0, totalRows); 27 29 28 algorithm.MaximumGenerations.Value = 1; 29 var evaluator = new ConstantsOptimizationEvaluator(); 30 evaluator.ConstantOptimizationIterations.Value = 5; 31 problem.EvaluatorParameter.Value = evaluator; 32 33 algorithm.Prepare(); 34 algorithm.Start(); 35 } 36 37 private OffspringSelectionGeneticAlgorithm CreateGpSymbolicRegressionSample() { 38 var osga = new OffspringSelectionGeneticAlgorithm(); 39 #region Problem Configuration 40 var provider = new RegressionRealWorldInstanceProvider(); 41 var problemData = provider.LoadData(new Tower()); 42 var problem = new SymbolicRegressionSingleObjectiveProblem(); 43 problem.ProblemData = problemData; 44 problem.Load(problemData); 45 problem.BestKnownQuality.Value = 1.0; 46 47 #region configure grammar 48 49 var grammar = (TypeCoherentExpressionGrammar)problem.SymbolicExpressionTreeGrammar; 30 var grammar = new TypeCoherentExpressionGrammar(); 50 31 grammar.ConfigureAsDefaultRegressionGrammar(); 51 32 52 //symbols square, power, squareroot, root, log, exp, sine, cosine, tangent, variable 53 var square = grammar.Symbols.OfType<Square>().Single(); 54 var power = grammar.Symbols.OfType<Power>().Single(); 55 var squareroot = grammar.Symbols.OfType<SquareRoot>().Single(); 56 var root = grammar.Symbols.OfType<Root>().Single(); 57 var cube = grammar.Symbols.OfType<Cube>().Single(); 58 var cuberoot = grammar.Symbols.OfType<CubeRoot>().Single(); 59 var log = grammar.Symbols.OfType<Logarithm>().Single(); 60 var exp = grammar.Symbols.OfType<Exponential>().Single(); 61 var sine = grammar.Symbols.OfType<Sine>().Single(); 62 var cosine = grammar.Symbols.OfType<Cosine>().Single(); 63 var tangent = grammar.Symbols.OfType<Tangent>().Single(); 64 var variable = grammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>().Single(); 65 var powerSymbols = grammar.Symbols.Single(s => s.Name == "Power Functions"); 66 powerSymbols.Enabled = true; 33 var trees = CreateRandomTrees(twister, problemData.Dataset, grammar, 1000, 1, maxTreeSize, 0, 0); 34 foreach (SymbolicExpressionTree tree in trees) { 35 InitTree(tree, twister, problemData.AllowedInputVariables.ToList()); 36 } 67 37 68 square.Enabled = true; 69 square.InitialFrequency = 1.0; 70 foreach (var allowed in grammar.GetAllowedChildSymbols(square)) 71 grammar.RemoveAllowedChildSymbol(square, allowed); 72 foreach (var allowed in grammar.GetAllowedChildSymbols(square, 0)) 73 grammar.RemoveAllowedChildSymbol(square, allowed, 0); 74 grammar.AddAllowedChildSymbol(square, variable); 38 Console.WriteLine("Random tree constants optimization performance of new method:"); 75 39 76 power.Enabled = false; 40 //warm up 41 for (int i = 0; i < trees.Length; i++) { 42 double quality = LMConstantsOptimizer.OptimizeConstants(trees[i], problemData, rows, true, maxIterations); 43 } 77 44 78 squareroot.Enabled = false; 79 foreach (var allowed in grammar.GetAllowedChildSymbols(squareroot)) 80 grammar.RemoveAllowedChildSymbol(squareroot, allowed); 81 foreach (var allowed in grammar.GetAllowedChildSymbols(squareroot, 0)) 82 grammar.RemoveAllowedChildSymbol(squareroot, allowed, 0); 83 grammar.AddAllowedChildSymbol(squareroot, variable); 45 Stopwatch watch = new Stopwatch(); 46 for (int rep = 0; rep < repetitions; rep++) { 47 watch.Start(); 48 for (int i = 0; i < trees.Length; i++) { 49 double quality = LMConstantsOptimizer.OptimizeConstants(trees[i], problemData, rows, true, maxIterations); 50 } 51 watch.Stop(); 52 Console.WriteLine("Iteration " + rep + "\t\t" + " Elapsed time: \t" + watch.ElapsedMilliseconds + " ms \t\t" + 53 "Time per tree: " + watch.ElapsedMilliseconds / 1000.0 / trees.Length); 54 watch.Reset(); 55 } 56 } 57 [TestMethod] 58 [TestCategory("Problems.DataAnalysis.Symbolic.Regression")] 59 [TestProperty("Time", "long")] 60 public void Old_ConstantsOptimization_Tower_Algorithm() { 61 var twister = new MersenneTwister((uint)seed); 62 var problemData = new RegressionRealWorldInstanceProvider().LoadData(new Tower()); 63 var rows = Enumerable.Range(0, totalRows); 84 64 85 cube.Enabled = false; 86 cuberoot.Enabled = false; 87 root.Enabled = false; 65 var grammar = new TypeCoherentExpressionGrammar(); 66 grammar.ConfigureAsDefaultRegressionGrammar(); 88 67 89 log.Enabled = true; 90 log.InitialFrequency = 1.0; 91 foreach (var allowed in grammar.GetAllowedChildSymbols(log)) 92 grammar.RemoveAllowedChildSymbol(log, allowed); 93 foreach (var allowed in grammar.GetAllowedChildSymbols(log, 0)) 94 grammar.RemoveAllowedChildSymbol(log, allowed, 0); 95 grammar.AddAllowedChildSymbol(log, variable); 68 var trees = CreateRandomTrees(twister, problemData.Dataset, grammar, 1000, 1, maxTreeSize, 0, 0); 69 foreach (SymbolicExpressionTree tree in trees) { 70 InitTree(tree, twister, problemData.AllowedInputVariables.ToList()); 71 } 96 72 97 exp.Enabled = true; 98 exp.InitialFrequency = 1.0; 99 foreach (var allowed in grammar.GetAllowedChildSymbols(exp)) 100 grammar.RemoveAllowedChildSymbol(exp, allowed); 101 foreach (var allowed in grammar.GetAllowedChildSymbols(exp, 0)) 102 grammar.RemoveAllowedChildSymbol(exp, allowed, 0); 103 grammar.AddAllowedChildSymbol(exp, variable); 73 Console.WriteLine("Random tree constants optimization performance of existing method:"); 74 var interpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter(); 104 75 105 sine.Enabled = false; 106 foreach (var allowed in grammar.GetAllowedChildSymbols(sine)) 107 grammar.RemoveAllowedChildSymbol(sine, allowed); 108 foreach (var allowed in grammar.GetAllowedChildSymbols(sine, 0)) 109 grammar.RemoveAllowedChildSymbol(sine, allowed, 0); 110 grammar.AddAllowedChildSymbol(sine, variable); 76 //warm up 77 for (int i = 0; i < trees.Length; i++) { 78 double quality = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants( 79 interpreter, trees[i], problemData, rows, true, maxIterations); 80 } 111 81 112 cosine.Enabled = false; 113 foreach (var allowed in grammar.GetAllowedChildSymbols(cosine)) 114 grammar.RemoveAllowedChildSymbol(cosine, allowed); 115 foreach (var allowed in grammar.GetAllowedChildSymbols(cosine, 0)) 116 grammar.RemoveAllowedChildSymbol(cosine, allowed, 0); 117 grammar.AddAllowedChildSymbol(cosine, variable); 118 119 tangent.Enabled = false; 120 foreach (var allowed in grammar.GetAllowedChildSymbols(tangent)) 121 grammar.RemoveAllowedChildSymbol(tangent, allowed); 122 foreach (var allowed in grammar.GetAllowedChildSymbols(tangent, 0)) 123 grammar.RemoveAllowedChildSymbol(tangent, allowed, 0); 124 grammar.AddAllowedChildSymbol(tangent, variable); 125 #endregion 126 127 problem.SymbolicExpressionTreeGrammar = grammar; 128 129 // configure remaining problem parameters 130 problem.MaximumSymbolicExpressionTreeLength.Value = 50; 131 problem.MaximumSymbolicExpressionTreeDepth.Value = 12; 132 problem.MaximumFunctionDefinitions.Value = 0; 133 problem.MaximumFunctionArguments.Value = 0; 134 135 var evaluator = new SymbolicRegressionConstantOptimizationEvaluator(); 136 evaluator.ConstantOptimizationIterations.Value = 5; 137 problem.EvaluatorParameter.Value = evaluator; 138 problem.RelativeNumberOfEvaluatedSamplesParameter.Hidden = true; 139 problem.SolutionCreatorParameter.Hidden = true; 140 #endregion 141 142 #region Algorithm Configuration 143 osga.Problem = problem; 144 osga.Name = "Offspring Selection Genetic Programming - Symbolic Regression"; 145 osga.Description = "Genetic programming with strict offspring selection for solving a benchmark regression problem."; 146 ConfigureOsGeneticAlgorithmParameters<GenderSpecificSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>(osga, 100, 1, 25, 0.2, 50); 147 var mutator = (MultiSymbolicExpressionTreeManipulator)osga.Mutator; 148 mutator.Operators.OfType<FullTreeShaker>().Single().ShakingFactor = 0.1; 149 mutator.Operators.OfType<OnePointShaker>().Single().ShakingFactor = 1.0; 150 151 osga.Analyzer.Operators.SetItemCheckedState( 152 osga.Analyzer.Operators 153 .OfType<SymbolicRegressionSingleObjectiveOverfittingAnalyzer>() 154 .Single(), false); 155 osga.Analyzer.Operators.SetItemCheckedState( 156 osga.Analyzer.Operators 157 .OfType<SymbolicDataAnalysisAlleleFrequencyAnalyzer>() 158 .First(), false); 159 160 osga.ComparisonFactorModifierParameter.Hidden = true; 161 osga.ComparisonFactorLowerBoundParameter.Hidden = true; 162 osga.ComparisonFactorUpperBoundParameter.Hidden = true; 163 osga.OffspringSelectionBeforeMutationParameter.Hidden = true; 164 osga.SuccessRatioParameter.Hidden = true; 165 osga.SelectedParentsParameter.Hidden = true; 166 osga.ElitesParameter.Hidden = true; 167 168 #endregion 169 return osga; 82 Stopwatch watch = new Stopwatch(); 83 for (int rep = 0; rep < repetitions; rep++) { 84 watch.Start(); 85 for (int i = 0; i < trees.Length; i++) { 86 double quality = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants( 87 interpreter, trees[i], problemData, rows, true, maxIterations); 88 } 89 watch.Stop(); 90 Console.WriteLine("Iteration " + rep + "\t\t" + " Elapsed time: \t" + watch.ElapsedMilliseconds + " ms \t\t" + 91 "Time per tree: " + watch.ElapsedMilliseconds / 1000.0 / trees.Length); 92 watch.Reset(); 93 } 170 94 } 171 95 172 96 173 public static void ConfigureOsGeneticAlgorithmParameters<S, C, M>(OffspringSelectionGeneticAlgorithm ga, int popSize, int elites, int maxGens, double mutationRate = 0.05, double maxSelPres = 100, int tournGroupSize = 0)174 where S : ISelector175 where C : ICrossover176 where M : IManipulator {177 ga.Elites.Value = elites;178 ga.MaximumGenerations.Value = maxGens;179 ga.MutationProbability.Value = mutationRate;180 ga.PopulationSize.Value = popSize;181 ga.MaximumSelectionPressure.Value = maxSelPres;182 ga.Seed.Value = 0;183 ga.SetSeedRandomly.Value = true;184 ga.ComparisonFactorLowerBound.Value = 1;185 ga.ComparisonFactorUpperBound.Value = 1;186 97 187 ga.Selector = ga.SelectorParameter.ValidValues188 .OfType<S>()189 .First();98 public static ISymbolicExpressionTree[] CreateRandomTrees(MersenneTwister twister, IDataset dataset, ISymbolicExpressionGrammar grammar, int popSize) { 99 return CreateRandomTrees(twister, dataset, grammar, popSize, 1, 200, 3, 3); 100 } 190 101 191 ga.Crossover = ga.CrossoverParameter.ValidValues 192 .OfType<C>() 193 .First(); 102 public static ISymbolicExpressionTree[] CreateRandomTrees(MersenneTwister twister, IDataset dataset, ISymbolicExpressionGrammar grammar, 103 int popSize, int minSize, int maxSize, 104 int maxFunctionDefinitions, int maxFunctionArguments) { 105 foreach (Variable variableSymbol in grammar.Symbols.OfType<Variable>()) { 106 variableSymbol.VariableNames = dataset.VariableNames; 107 } 108 ISymbolicExpressionTree[] randomTrees = new ISymbolicExpressionTree[popSize]; 109 for (int i = 0; i < randomTrees.Length; i++) { 110 randomTrees[i] = ProbabilisticTreeCreator.Create(twister, grammar, maxSize, 10); 111 } 112 return randomTrees; 113 } 194 114 195 ga.Mutator = ga.MutatorParameter.ValidValues 196 .OfType<M>() 197 .First(); 198 199 var tSelector = ga.Selector as TournamentSelector; 200 if (tSelector != null) { 201 tSelector.GroupSizeParameter.Value.Value = tournGroupSize; 115 public static void InitTree(ISymbolicExpressionTree tree, MersenneTwister twister, List<string> varNames) { 116 foreach (var node in tree.IterateNodesPostfix()) { 117 if (node is VariableTreeNode) { 118 var varNode = node as VariableTreeNode; 119 varNode.Weight = twister.NextDouble() * 20.0 - 10.0; 120 varNode.VariableName = varNames[twister.Next(varNames.Count)]; 121 } else if (node is ConstantTreeNode) { 122 var constantNode = node as ConstantTreeNode; 123 constantNode.Value = twister.NextDouble() * 20.0 - 10.0; 124 } 202 125 } 203 ga.Engine = new ParallelEngine();204 126 } 205 127
Note: See TracChangeset
for help on using the changeset viewer.