Changeset 16988 for branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Timestamp:
- 05/25/19 08:43:16 (5 years ago)
- Location:
- branches/2925_AutoDiffForDynamicalModels
- Files:
-
- 1 deleted
- 20 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/2925_AutoDiffForDynamicalModels
- Property svn:mergeinfo changed
-
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Problems.DataAnalysis.Symbolic merged: 16905,16978-16981,16983
- Property svn:mergeinfo changed
-
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers
-
Property
svn:ignore
set to
SymbolicDataAnalysisBuildingBlockAnalyzer.cs
SymbolicDataAnalysisHashBasedDiversityAnalyzer.cs
-
Property
svn:ignore
set to
-
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/DerivativeCalculator.cs
r16892 r16988 143 143 var f = (ISymbolicExpressionTreeNode)branch.Clone(); 144 144 var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone(); 145 return Product(Div(CreateConstant(1.0), Product(CreateConstant(3.0), Square(f))), Derive(u, variableName)); 145 return Product(Div(CreateConstant(1.0), Product(CreateConstant(3.0), Square(f))), Derive(u, variableName)); // 1/3 1/cbrt(f(x))^2 d/dx f(x) 146 146 } 147 147 if (branch.Symbol is Cube) { -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToAutoDiffTermConverter.cs
r16662 r16988 92 92 ); 93 93 94 private static readonly Func<Term, UnaryFunc> cbrt = UnaryFunc.Factory( 95 eval: x => x < 0 ? -Math.Pow(-x, 1.0 / 3) : Math.Pow(x, 1.0 / 3), 96 diff: x => { var cbrt_x = x < 0 ? -Math.Pow(-x, 1.0 / 3) : Math.Pow(x, 1.0 / 3); return 1.0 / (3 * cbrt_x * cbrt_x); } 97 ); 98 99 100 94 101 #endregion 95 102 … … 250 257 } 251 258 if (node.Symbol is CubeRoot) { 252 return AutoDiff.TermBuilder.Power( 253 ConvertToAutoDiff(node.GetSubtree(0)), 1.0/3.0); 259 return cbrt(ConvertToAutoDiff(node.GetSubtree(0))); 254 260 } 255 261 if (node.Symbol is Sine) { -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDiversityPreservingCrossover.cs
r16662 r16988 1 using System; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 * 5 * This file is part of HeuristicLab. 6 * 7 * HeuristicLab is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * HeuristicLab is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 #endregion 21 22 using System; 2 23 using System.Collections.Generic; 3 24 using System.Linq; 4 25 using HEAL.Attic; 5 26 using HeuristicLab.Common; 6 27 using HeuristicLab.Core; … … 8 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 9 30 using HeuristicLab.Parameters; 10 using HEAL.Attic;11 31 using HeuristicLab.Random; 12 32 using static HeuristicLab.Problems.DataAnalysis.Symbolic.SymbolicExpressionHashExtensions; 13 33 14 34 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 15 [Item("DiversityCrossover", "Simple crossover operator pr ioritizing internal nodes according to the given probability.")]35 [Item("DiversityCrossover", "Simple crossover operator preventing swap between subtrees with the same hash value.")] 16 36 [StorableType("ED35B0D9-9704-4D32-B10B-8F9870E76781")] 17 37 public sealed class SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData { … … 20 40 private const string WindowingParameterName = "Windowing"; 21 41 private const string ProportionalSamplingParameterName = "ProportionalSampling"; 42 private const string StrictHashingParameterName = "StrictHashing"; 22 43 23 44 private static readonly Func<byte[], ulong> hashFunction = HashUtil.JSHash; … … 35 56 get { return (IValueLookupParameter<BoolValue>)Parameters[ProportionalSamplingParameterName]; } 36 57 } 58 59 public IFixedValueParameter<BoolValue> StrictHashingParameter { 60 get { return (IFixedValueParameter<BoolValue>)Parameters[StrictHashingParameterName]; } 61 } 37 62 #endregion 38 63 … … 49 74 get { return ProportionalSamplingParameter.ActualValue; } 50 75 } 76 77 bool StrictHashing { 78 get { return StrictHashingParameter.Value.Value; } 79 } 51 80 #endregion 81 82 83 [StorableHook(HookType.AfterDeserialization)] 84 private void AfterDeserialization() { 85 if (!Parameters.ContainsKey(StrictHashingParameterName)) { 86 Parameters.Add(new FixedValueParameter<BoolValue>(StrictHashingParameterName, "Use strict hashing when calculating subtree hash values.")); 87 } 88 } 52 89 53 90 public SymbolicDataAnalysisExpressionDiversityPreservingCrossover() { … … 56 93 Parameters.Add(new ValueLookupParameter<BoolValue>(WindowingParameterName, "Use proportional sampling with windowing for cutpoint selection.", new BoolValue(false))); 57 94 Parameters.Add(new ValueLookupParameter<BoolValue>(ProportionalSamplingParameterName, "Select cutpoints proportionally using probabilities as weights instead of randomly.", new BoolValue(true))); 95 Parameters.Add(new FixedValueParameter<BoolValue>(StrictHashingParameterName, "Use strict hashing when calculating subtree hash values.")); 58 96 } 59 97 … … 72 110 } 73 111 74 public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, double internalCrossoverPointProbability, int maxLength, int maxDepth, bool windowing, bool proportionalSampling = false) { 75 var leafCrossoverPointProbability = 1 - internalCrossoverPointProbability; 76 77 var nodes0 = ActualRoot(parent0).MakeNodes().Sort(hashFunction); 78 var nodes1 = ActualRoot(parent1).MakeNodes().Sort(hashFunction); 112 public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, double internalCrossoverPointProbability, int maxLength, int maxDepth, bool windowing, bool proportionalSampling = false, bool strictHashing = false) { 113 var nodes0 = ActualRoot(parent0).MakeNodes(strictHashing).Sort(hashFunction); 114 var nodes1 = ActualRoot(parent1).MakeNodes(strictHashing).Sort(hashFunction); 79 115 80 116 IList<HashNode<ISymbolicExpressionTreeNode>> sampled0; … … 126 162 var proportionalSampling = ProportionalSampling.Value; 127 163 128 return Cross(random, parent0, parent1, internalCrossoverPointProbability, maxLength, maxDepth, windowing, proportionalSampling );164 return Cross(random, parent0, parent1, internalCrossoverPointProbability, maxLength, maxDepth, windowing, proportionalSampling, StrictHashing); 129 165 } 130 166 -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
r16892 r16988 140 140 FormatPower(node, strBuilder, "3"); 141 141 } else if (node.Symbol is CubeRoot) { 142 FormatPower(node, strBuilder, "1.0/3"); 142 strBuilder.Append("Cbrt("); 143 FormatRecursively(node.GetSubtree(0), strBuilder); 144 strBuilder.Append(")"); 143 145 } else if (node.Symbol is Power) { 144 146 FormatFunction(node, "Math.Pow", strBuilder); … … 252 254 strBuilder.AppendLine("public static class Model {"); 253 255 GenerateAverageSource(strBuilder); 256 GenerateCbrtSource(strBuilder); 254 257 GenerateIfThenElseSource(strBuilder); 255 258 GenerateFactorSource(strBuilder); … … 293 296 strBuilder.AppendLine("private static double Average(params double[] values) {"); 294 297 strBuilder.AppendLine(" return values.Average();"); 298 strBuilder.AppendLine("}"); 299 } 300 private void GenerateCbrtSource(StringBuilder strBuilder) { 301 strBuilder.AppendLine("private static double Cbrt(double x) {"); 302 strBuilder.AppendLine(" return x < 0 ? -Math.Pow(-x, 1.0 / 3.0) : Math.Pow(x, 1.0 / 3.0);"); 295 303 strBuilder.AppendLine("}"); 296 304 } -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs
r16892 r16988 144 144 stringBuilder.Append($"POWER({FormatRecursively(node.GetSubtree(0))}, 3)"); 145 145 } else if (symbol is CubeRoot) { 146 stringBuilder.Append($"POWER({FormatRecursively(node.GetSubtree(0))}, 1/3)"); 146 var arg_expr = FormatRecursively(node.GetSubtree(0)); 147 stringBuilder.Append($"IF({arg_expr} < 0, -POWER(-{arg_expr}, 1/3), POWER({arg_expr}, 1/3))"); 147 148 } else if (symbol is Division) { 148 149 if (node.SubtreeCount == 1) { -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r16892 r16988 138 138 strBuilder.Append(@"\left("); 139 139 } else if (node.Symbol is CubeRoot) { 140 strBuilder.Append(@"\ left(");140 strBuilder.Append(@"\operatorname{cbrt}\left("); 141 141 } else if (node.Symbol is Sine) { 142 142 strBuilder.Append(@"\sin \left( "); … … 420 420 strBuilder.Append(@"\right)^3"); 421 421 } else if (node.Symbol is CubeRoot) { 422 strBuilder.Append(@"\right) ^\frac{1}{3}");422 strBuilder.Append(@"\right)"); 423 423 } else if (node.Symbol is Sine) { 424 424 strBuilder.Append(@" \right) "); -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs
r16892 r16988 188 188 stringBuilder.Append(").^3"); 189 189 } else if (symbol is CubeRoot) { 190 stringBuilder.Append(" (");191 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 192 stringBuilder.Append(" ).^(1/3)");190 stringBuilder.Append("NTHROOT("); 191 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 192 stringBuilder.Append(", 3)"); 193 193 } else if (symbol is GreaterThan) { 194 194 stringBuilder.Append("(("); -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs
r16892 r16988 115 115 FormatPower(node, strBuilder, "3"); 116 116 } else if (node.Symbol is CubeRoot) { 117 FormatPower(node, strBuilder, "1/3"); 117 strBuilder.Append("CubeRoot["); 118 FormatRecursively(node.GetSubtree(0), strBuilder); 119 strBuilder.Append("]"); 118 120 } else if (node.Symbol is Power) { 119 121 FormatFunction(node, "Power", strBuilder); -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs
r16892 r16988 92 92 stringBuilder.Append(FormatPower(node.GetSubtree(0), "3")); 93 93 } else if (symbol is CubeRoot) { 94 stringBuilder.Append(FormatPower(node.GetSubtree(0), "(1/3)")); 94 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 95 stringBuilder.Append(" cbrt"); 95 96 } else if (symbol is Division) { 96 97 if (node.SubtreeCount == 1) { -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/HashExtensions.cs
r16662 r16988 21 21 22 22 using System; 23 using System. Collections.Generic;23 using System.Linq; 24 24 25 25 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 38 38 public SimplifyAction Simplify; 39 39 40 public IComparer<T> Comparer;40 //public IComparer<T> Comparer; 41 41 42 42 public bool IsLeaf => Arity == 0; 43 43 44 public HashNode(IComparer<T> comparer) {45 Comparer = comparer;46 }47 48 privateHashNode() { }44 //public HashNode(IComparer<T> comparer) { 45 // Comparer = comparer; 46 //} 47 48 //public HashNode() { } 49 49 50 50 public int CompareTo(HashNode<T> other) { 51 var res = Comparer.Compare(Data, other.Data); 52 return res == 0 ? CalculatedHashValue.CompareTo(other.CalculatedHashValue) : res; 51 return CalculatedHashValue.CompareTo(other.CalculatedHashValue); 53 52 } 54 53 … … 103 102 104 103 public static HashNode<T>[] Simplify<T>(this HashNode<T>[] nodes, Func<byte[], ulong> hashFunction) where T : class { 105 var reduced = nodes.UpdateNodeSizes().Reduce().Sort(hashFunction); 106 107 for (int i = 0; i < reduced.Length; ++i) { 108 var node = reduced[i]; 109 if (node.IsLeaf) { 110 continue; 111 } 112 node.Simplify?.Invoke(ref reduced, i); 113 } 114 // detect if anything was simplified 115 var count = 0; 116 foreach (var node in reduced) { 117 if (!node.Enabled) { ++count; } 118 } 119 if (count == 0) { 120 return reduced; 121 } 122 123 var simplified = new HashNode<T>[reduced.Length - count]; 124 int j = 0; 125 foreach (var node in reduced) { 126 if (node.Enabled) { 127 simplified[j++] = node; 128 } 129 } 130 return simplified.UpdateNodeSizes().Reduce().Sort(hashFunction); 104 bool simplified = false; 105 nodes = nodes.UpdateNodeSizes().Reduce().Sort(hashFunction); 106 do { 107 if (simplified) { 108 simplified = false; 109 nodes = nodes.Where(x => x.Enabled).ToArray().UpdateNodeSizes().Reduce().Sort(hashFunction); 110 } 111 112 for (int i = 0; i < nodes.Length; ++i) { 113 var node = nodes[i]; 114 if (node.IsLeaf) { 115 continue; 116 } 117 node.Simplify?.Invoke(ref nodes, i); 118 for (int j = i - node.Size; j < i; ++j) { 119 // detect if anything was simplified 120 if (!nodes[j].Enabled) { 121 simplified = true; 122 break; 123 } 124 } 125 } 126 } while (simplified); 127 return nodes.UpdateNodeSizes().Sort(hashFunction); 131 128 } 132 129 … … 207 204 } 208 205 209 p rivatestatic HashNode<T>[] Reduce<T>(this HashNode<T>[] nodes) where T : class {206 public static HashNode<T>[] Reduce<T>(this HashNode<T>[] nodes) where T : class { 210 207 int count = 0; 211 208 for (int i = 0; i < nodes.Length; ++i) { -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/SymbolicExpressionTreeHash.cs
r16662 r16988 38 38 private static readonly Constant constant = new Constant(); 39 39 40 private static readonly ISymbolicExpressionTreeNodeComparer comparer = new SymbolicExpressionTreeNodeComparer();41 40 private static ISymbolicExpressionTreeNode ActualRoot(this ISymbolicExpressionTree tree) => tree.Root.GetSubtree(0).GetSubtree(0); 42 41 … … 74 73 } 75 74 var hash = (ulong)name.GetHashCode(); 76 var hashNode = new HashNode<ISymbolicExpressionTreeNode> (comparer){75 var hashNode = new HashNode<ISymbolicExpressionTreeNode> { 77 76 Data = node, 78 77 Arity = node.SubtreeCount, -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r16892 r16988 132 132 <ItemGroup> 133 133 <Compile Include="Analyzers\SymbolicDataAnalysisBottomUpDiversityAnalyzer.cs" /> 134 <Compile Include="Analyzers\SymbolicDataAnalysisBuildingBlockAnalyzer.cs" />135 134 <Compile Include="Analyzers\SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs" /> 136 135 <Compile Include="Analyzers\SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer.cs" /> … … 172 171 <Compile Include="Interpreter\SymbolicDataAnalysisExpressionTreeNativeInterpreter.cs" /> 173 172 <Compile Include="Selectors\DiversitySelector.cs" /> 173 <Compile Include="SymbolicDataAnalysisExpressionTreeAverageSimilarityCalculator.cs" /> 174 174 <Compile Include="SymbolicDataAnalysisExpressionTreeSimplificationOperator.cs" /> 175 175 <Compile Include="SymbolicDataAnalysisModelComplexityCalculator.cs" /> -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/BatchOperations.cs
r16662 r16988 101 101 public static void CubeRoot(double[] a, double[] b) { 102 102 for (int i = 0; i < BATCHSIZE; ++i) 103 a[i] = Math.Pow(b[i], 1d / 3d);103 a[i] = b[i] < 0 ? -Math.Pow(-b[i], 1d / 3d) : Math.Pow(b[i], 1d / 3d); 104 104 } 105 105 -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs
r16892 r16988 247 247 case OpCodes.CubeRoot: { 248 248 var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 249 return Expression.Power(arg, Expression.Constant(1.0 / 3.0)); 249 return Expression.Condition(Expression.LessThan(arg, Expression.Constant(0.0)), 250 Expression.Negate(Expression.Power(Expression.Negate(arg), Expression.Constant(1.0 / 3.0))), 251 Expression.Power(arg, Expression.Constant(1.0 / 3.0))); 250 252 } 251 253 case OpCodes.Root: { … … 514 516 var x1 = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 515 517 var x2 = MakeExpr(node.GetSubtree(1), variableIndices, row, columns); 516 return Expression.Divide(x1, 517 Expression.Call(Sqrt, 518 return Expression.Divide(x1, 519 Expression.Call(Sqrt, 518 520 Expression.Add( 519 521 Expression.Constant(1.0), -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
r16892 r16988 336 336 case OpCodes.CubeRoot: { 337 337 CompileInstructions(il, state, ds); 338 var c1 = il.DefineLabel(); 339 var end = il.DefineLabel(); 340 341 il.Emit(System.Reflection.Emit.OpCodes.Dup); // x 342 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0); 343 il.Emit(System.Reflection.Emit.OpCodes.Clt); // x < 0? 344 il.Emit(System.Reflection.Emit.OpCodes.Brfalse, c1); 345 il.Emit(System.Reflection.Emit.OpCodes.Neg); // x = -x 338 346 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0 / 3.0); 339 347 il.Emit(System.Reflection.Emit.OpCodes.Call, power); 348 il.Emit(System.Reflection.Emit.OpCodes.Neg); // -Math.Pow(-x, 1/3) 349 il.Emit(System.Reflection.Emit.OpCodes.Br, end); 350 il.MarkLabel(c1); 351 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0 / 3.0); 352 il.Emit(System.Reflection.Emit.OpCodes.Call, power); 353 il.MarkLabel(end); 340 354 return; 341 355 } -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs
r16662 r16988 232 232 } 233 233 case OpCodes.CubeRoot: { 234 return Math.Pow(Evaluate(dataset, ref row, state), 1.0 / 3.0); 234 var arg = Evaluate(dataset, ref row, state); 235 return arg < 0 ? -Math.Pow(-arg, 1.0 / 3.0) : Math.Pow(arg, 1.0 / 3.0); 235 236 } 236 237 case OpCodes.Root: { -
branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs
r16662 r16988 245 245 instr.value = Math.Sqrt(code[instr.childIndex].value); 246 246 } else if (instr.opCode == OpCodes.CubeRoot) { 247 instr.value = Math.Pow(code[instr.childIndex].value, 1.0 / 3.0); 247 var arg = code[instr.childIndex].value; 248 instr.value = arg < 0 ? -Math.Pow(-arg, 1.0 / 3.0) : Math.Pow(arg, 1.0 / 3.0); 248 249 } else if (instr.opCode == OpCodes.Root) { 249 250 double x = code[instr.childIndex].value;
Note: See TracChangeset
for help on using the changeset viewer.