Changeset 16500 for branches/2974_Constants_Optimization
- Timestamp:
- 01/04/19 17:37:13 (6 years ago)
- Location:
- branches/2974_Constants_Optimization
- Files:
-
- 2 added
- 1 deleted
- 7 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/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj
r16461 r16500 192 192 <Compile Include="Plugin.cs" /> 193 193 <Compile Include="SingleObjective\ConstantOptimizationAnalyzer.cs" /> 194 <Compile Include="SingleObjective\Evaluators\ConstantsOptimizationEvaluator.cs" />195 194 <Compile Include="SingleObjective\Evaluators\SymbolicRegressionMeanRelativeErrorEvaluator.cs" /> 196 195 <Compile Include="SingleObjective\SymbolicRegressionSolutionsAnalyzer.cs" /> -
branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Constants Optimization/Util.cs
r16461 r16500 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 27 using static HeuristicLab.Problems.DataAnalysis.Symbolic.TreeToAutoDiffTermConverter; … … 48 49 } 49 50 50 public static Dictionary<DataForVariable, AutoDiff.Variable> ExtractParameters(IDataset dataset) {51 var parameters = new Dictionary<DataForVariable, AutoDiff.Variable>();51 public static List<DataForVariable> GenerateVariables(IDataset dataset) { 52 var variables = new List<DataForVariable>(); 52 53 foreach (var doubleVariable in dataset.DoubleVariables) { 53 54 var data = new DataForVariable(doubleVariable, string.Empty, 0); 54 var param = new AutoDiff.Variable(); 55 parameters.Add(data, param); 55 variables.Add(data); 56 56 } 57 57 … … 59 59 foreach (var stringValue in dataset.GetStringValues(stringVariable).Distinct()) { 60 60 var data = new DataForVariable(stringVariable, stringValue, 0); 61 var param = new AutoDiff.Variable(); 62 parameters.Add(data, param); 61 variables.Add(data); 63 62 } 64 63 } 65 return parameters; 64 return variables; 65 } 66 67 public static List<DataForVariable> ExtractLaggedVariables(ISymbolicExpressionTree tree) { 68 var variables = new HashSet<DataForVariable>(); 69 foreach (var laggedNode in tree.IterateNodesPrefix().OfType<ILaggedTreeNode>()) { 70 var laggedVariableTreeNode = laggedNode as LaggedVariableTreeNode; 71 if (laggedVariableTreeNode != null) { 72 var data = new DataForVariable(laggedVariableTreeNode.VariableName, string.Empty, laggedVariableTreeNode.Lag); 73 if (!variables.Contains(data)) variables.Add(data); 74 } 75 } 76 return variables.ToList(); 66 77 } 67 78 … … 79 90 for (int j = 0; j < factorVarTreeNode.Weights.Length; j++) 80 91 constants.Add(factorVarTreeNode.Weights[j]); 81 } 92 } else throw new NotSupportedException(string.Format("Terminal nodes of type {0} are not supported.", node.GetType().GetPrettyName())); 82 93 } 83 94 return constants.ToArray(); … … 97 108 for (int j = 0; j < factorVarTreeNode.Weights.Length; j++) 98 109 factorVarTreeNode.Weights[j] = constants[i++]; 99 } 110 } else throw new NotSupportedException(string.Format("Terminal nodes of type {0} are not supported.", node.GetType().GetPrettyName())); 100 111 } 101 112 } -
branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToAutoDiffTermConverter.cs
r16463 r16500 131 131 } 132 132 133 public static bool TryConvertToAutoDiff(ISymbolicExpressionTree tree, bool makeVariableWeightsVariable, bool addLinearScalingTerms, Dictionary<DataForVariable, AutoDiff.Variable> parameters, 134 out ParametricFunction func, 135 out ParametricFunctionGradient func_grad, 136 out double[] initialConstants 137 ) { 133 public static bool TryConvertToAutoDiff(ISymbolicExpressionTree tree, bool addLinearScalingTerms, IEnumerable<DataForVariable> variables, 134 out IParametricCompiledTerm autoDiffTerm, out double[] initialConstants) { 138 135 // use a transformator object which holds the state (variable list, parameter list, ...) for recursive transformation of the tree 139 var transformator = new TreeToAutoDiffTermConverter(makeVariableWeightsVariable, parameters); 136 //TODO change ctor 137 var transformator = new TreeToAutoDiffTermConverter(true); 138 var parameters = new AutoDiff.Variable[variables.Count()]; 139 140 int i = 0; 141 foreach(var variable in variables) { 142 var autoDiffVar = new AutoDiff.Variable(); 143 transformator.parameters.Add(variable, autoDiffVar); 144 parameters[i] = autoDiffVar; 145 i++; 146 } 147 140 148 AutoDiff.Term term; 141 149 try { 142 143 150 term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0)); 144 151 if (addLinearScalingTerms) { 145 // scaling variables α, β are given at the beginningof the parameter vector152 // scaling variables α, β are given at the end of the parameter vector 146 153 var alpha = new AutoDiff.Variable(); 147 154 var beta = new AutoDiff.Variable(); 155 156 term = term * alpha + beta; 157 158 transformator.variables.Add(alpha); 148 159 transformator.variables.Add(beta); 149 transformator.variables.Add(alpha); 150 term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0)); 151 term = term * alpha + beta; 152 } else { 153 term = transformator.ConvertToAutoDiff(tree.Root.GetSubtree(0)); 154 } 155 156 var compiledTerm = term.Compile(transformator.variables.ToArray(), parameters.Values.ToArray()); 157 func = (vars, @params) => compiledTerm.Evaluate(vars, @params); 158 func_grad = (vars, @params) => compiledTerm.Differentiate(vars, @params); 160 161 transformator.initialConstants.Add(1.0); 162 transformator.initialConstants.Add(0.0); 163 } 164 165 var compiledTerm = term.Compile(transformator.variables.ToArray(), parameters); 166 autoDiffTerm = compiledTerm; 159 167 initialConstants = transformator.initialConstants.ToArray(); 160 168 161 169 return true; 162 170 } catch (ConversionException) { 163 func = null; 164 func_grad = null; 171 autoDiffTerm = null; 165 172 initialConstants = null; 166 173 } … … 174 181 private readonly bool makeVariableWeightsVariable; 175 182 176 private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable , Dictionary<DataForVariable, AutoDiff.Variable> parameters = null) {183 private TreeToAutoDiffTermConverter(bool makeVariableWeightsVariable) { 177 184 this.makeVariableWeightsVariable = makeVariableWeightsVariable; 178 185 this.initialConstants = new List<double>(); 179 if (parameters == null) 180 this.parameters = new Dictionary<DataForVariable, AutoDiff.Variable>(); 181 else 182 this.parameters = parameters; 186 this.parameters = new Dictionary<DataForVariable, AutoDiff.Variable>(); 183 187 this.variables = new List<AutoDiff.Variable>(); 184 188 } -
branches/2974_Constants_Optimization/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r16460 r16500 213 213 <SubType>Code</SubType> 214 214 </Compile> 215 <Compile Include="Constants Optimization\IConstantsOptimizer.cs" /> 216 <Compile Include="Constants Optimization\LMConstantsOptimizer.cs" /> 215 217 <Compile Include="Constants Optimization\Util.cs" /> 216 218 <Compile Include="Converters\LinearModelToTreeConverter.cs" /> -
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.