- Timestamp:
- 10/23/16 09:44:29 (8 years ago)
- Location:
- branches/symbreg-factors-2650
- Files:
-
- 47 edited
- 14 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650
- Property svn:mergeinfo changed
/trunk/sources merged: 14332,14340-14350
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Algorithms.DataAnalysis merged: 14345
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis.Views
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Algorithms.DataAnalysis.Views (added) merged: 14345-14346
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/HeuristicLab.Algorithms.DataAnalysis.Views-3.4.csproj
r14242 r14351 131 131 <DependentUpon>OneFactorClassificationModelView.cs</DependentUpon> 132 132 </Compile> 133 <Compile Include="RandomForestClassificationSolutionView.cs"> 134 <SubType>UserControl</SubType> 135 </Compile> 136 <Compile Include="RandomForestClassificationSolutionView.Designer.cs"> 137 <DependentUpon>RandomForestClassificationSolutionView.cs</DependentUpon> 138 </Compile> 139 <Compile Include="RandomForestModelView.cs"> 140 <SubType>UserControl</SubType> 141 </Compile> 142 <Compile Include="RandomForestModelView.Designer.cs"> 143 <DependentUpon>RandomForestModelView.cs</DependentUpon> 144 </Compile> 145 <Compile Include="RandomForestRegressionSolutionView.cs"> 146 <SubType>UserControl</SubType> 147 </Compile> 148 <Compile Include="RandomForestRegressionSolutionView.Designer.cs"> 149 <DependentUpon>RandomForestRegressionSolutionView.cs</DependentUpon> 150 </Compile> 151 <Compile Include="GradientBoostedTreesModelView.cs"> 152 <SubType>UserControl</SubType> 153 </Compile> 154 <Compile Include="GradientBoostedTreesModelView.Designer.cs"> 155 <DependentUpon>GradientBoostedTreesModelView.cs</DependentUpon> 156 </Compile> 133 157 <Compile Include="MeanProdView.cs"> 134 158 <SubType>UserControl</SubType> … … 198 222 <Compile Include="SupportVectorMachineModelView.Designer.cs"> 199 223 <DependentUpon>SupportVectorMachineModelView.cs</DependentUpon> 224 </Compile> 225 <Compile Include="GradientBoostedTreesSolutionView.cs"> 226 <SubType>UserControl</SubType> 227 </Compile> 228 <Compile Include="GradientBoostedTreesSolutionView.Designer.cs"> 229 <DependentUpon>GradientBoostedTreesSolutionView.cs</DependentUpon> 200 230 </Compile> 201 231 </ItemGroup> … … 251 281 <Private>False</Private> 252 282 </ProjectReference> 283 <ProjectReference Include="..\..\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding\3.4\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj"> 284 <Project>{06D4A186-9319-48A0-BADE-A2058D462EEA}</Project> 285 <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4</Name> 286 </ProjectReference> 253 287 <ProjectReference Include="..\..\HeuristicLab.MainForm.WindowsForms\3.3\HeuristicLab.MainForm.WindowsForms-3.3.csproj"> 254 288 <Project>{AB687BBE-1BFE-476B-906D-44237135431D}</Project> … … 275 309 <Name>HeuristicLab.PluginInfrastructure-3.3</Name> 276 310 <Private>False</Private> 311 </ProjectReference> 312 <ProjectReference Include="..\..\HeuristicLab.Problems.DataAnalysis.Symbolic.Classification\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic.Classification-3.4.csproj"> 313 <Project>{05BAE4E1-A9FA-4644-AA77-42558720159E}</Project> 314 <Name>HeuristicLab.Problems.DataAnalysis.Symbolic.Classification-3.4</Name> 315 </ProjectReference> 316 <ProjectReference Include="..\..\HeuristicLab.Problems.DataAnalysis.Symbolic.Regression\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.csproj"> 317 <Project>{5AC82412-911B-4FA2-A013-EDC5E3F3FCC2}</Project> 318 <Name>HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4</Name> 319 </ProjectReference> 320 <ProjectReference Include="..\..\HeuristicLab.Problems.DataAnalysis.Symbolic.Views\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic.Views-3.4.csproj"> 321 <Project>{7a2531ce-3f7c-4f13-bcca-ed6dc27a7086}</Project> 322 <Name>HeuristicLab.Problems.DataAnalysis.Symbolic.Views-3.4</Name> 323 </ProjectReference> 324 <ProjectReference Include="..\..\HeuristicLab.Problems.DataAnalysis.Symbolic\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj"> 325 <Project>{3d28463f-ec96-4d82-afee-38be91a0ca00}</Project> 326 <Name>HeuristicLab.Problems.DataAnalysis.Symbolic-3.4</Name> 277 327 </ProjectReference> 278 328 <ProjectReference Include="..\..\HeuristicLab.Problems.DataAnalysis.Views\3.4\HeuristicLab.Problems.DataAnalysis.Views-3.4.csproj"> -
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/KMeansClusteringModelView.cs
r14185 r14351 19 19 */ 20 20 #endregion 21 using System;22 21 using System.Linq; 23 using System.IO;24 using System.Windows.Forms;25 22 using HeuristicLab.MainForm; 26 23 using HeuristicLab.MainForm.WindowsForms; -
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/Plugin.cs.frame
r14195 r14351 37 37 [PluginDependency("HeuristicLab.Data", "3.3")] 38 38 [PluginDependency("HeuristicLab.Data.Views", "3.3")] 39 [PluginDependency("HeuristicLab.Encodings.SymbolicExpressionTreeEncoding", "3.4")] 39 40 [PluginDependency("HeuristicLab.LibSVM", "3.12")] 40 41 [PluginDependency("HeuristicLab.MainForm", "3.3")] … … 44 45 [PluginDependency("HeuristicLab.Problems.DataAnalysis", "3.4")] 45 46 [PluginDependency("HeuristicLab.Problems.DataAnalysis.Views", "3.4")] 47 [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic", "3.4")] 48 [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic.Regression", "3.4")] 49 [PluginDependency("HeuristicLab.Problems.DataAnalysis.Symbolic.Classification", "3.4")] 46 50 public class HeuristicLabAlgorithmsDataAnalysisViewsPlugin : PluginBase { 47 51 } -
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesAlgorithm.cs
r14185 r14351 269 269 } else { 270 270 // otherwise we produce a regression solution 271 Results.Add(new Result("Solution", new RegressionSolution(model, problemData)));271 Results.Add(new Result("Solution", new GradientBoostedTreesSolution(model, problemData))); 272 272 } 273 273 } -
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesSolution.cs
r14185 r14351 20 20 #endregion 21 21 22 using System.Collections.Generic;23 using System.Linq;24 22 using HeuristicLab.Common; 25 23 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; -
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/RegressionTreeModel.cs
r14185 r14351 28 28 using HeuristicLab.Common; 29 29 using HeuristicLab.Core; 30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 32 using HeuristicLab.Problems.DataAnalysis; 33 using HeuristicLab.Problems.DataAnalysis.Symbolic; 34 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; 32 35 33 36 namespace HeuristicLab.Algorithms.DataAnalysis { … … 210 213 } 211 214 215 /// <summary> 216 /// Transforms the tree model to a symbolic regression solution 217 /// </summary> 218 /// <param name="problemData"></param> 219 /// <returns>A new symbolic regression solution which matches the tree model</returns> 220 public ISymbolicRegressionSolution CreateSymbolicRegressionSolution(IRegressionProblemData problemData) { 221 var rootSy = new ProgramRootSymbol(); 222 var startSy = new StartSymbol(); 223 var varCondSy = new VariableCondition() { IgnoreSlope = true }; 224 var constSy = new Constant(); 225 226 var startNode = startSy.CreateTreeNode(); 227 startNode.AddSubtree(CreateSymbolicRegressionTreeRecursive(tree, 0, varCondSy, constSy)); 228 var rootNode = rootSy.CreateTreeNode(); 229 rootNode.AddSubtree(startNode); 230 var model = new SymbolicRegressionModel(TargetVariable, new SymbolicExpressionTree(rootNode), new SymbolicDataAnalysisExpressionTreeLinearInterpreter()); 231 return model.CreateRegressionSolution(problemData); 232 } 233 234 private ISymbolicExpressionTreeNode CreateSymbolicRegressionTreeRecursive(TreeNode[] treeNodes, int nodeIdx, VariableCondition varCondSy, Constant constSy) { 235 var curNode = treeNodes[nodeIdx]; 236 if (curNode.VarName == TreeNode.NO_VARIABLE) { 237 var node = (ConstantTreeNode)constSy.CreateTreeNode(); 238 node.Value = curNode.Val; 239 return node; 240 } else { 241 var node = (VariableConditionTreeNode)varCondSy.CreateTreeNode(); 242 node.VariableName = curNode.VarName; 243 node.Threshold = curNode.Val; 244 245 var left = CreateSymbolicRegressionTreeRecursive(treeNodes, curNode.LeftIdx, varCondSy, constSy); 246 var right = CreateSymbolicRegressionTreeRecursive(treeNodes, curNode.RightIdx, varCondSy, constSy); 247 node.AddSubtree(left); 248 node.AddSubtree(right); 249 return node; 250 } 251 } 252 253 212 254 private string TreeToString(int idx, string part) { 213 255 var n = tree[idx]; -
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/IRandomForestClassificationSolution.cs
r14185 r14351 30 30 public interface IRandomForestClassificationSolution : IClassificationSolution { 31 31 new IRandomForestModel Model { get; } 32 int NumberOfTrees { get; } 32 33 } 33 34 } -
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/IRandomForestModel.cs
r14185 r14351 20 20 #endregion 21 21 22 using HeuristicLab. Optimization;22 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 23 23 using HeuristicLab.Problems.DataAnalysis; 24 using HeuristicLab.Core; 25 using System.Collections.Generic; 24 26 25 27 26 namespace HeuristicLab.Algorithms.DataAnalysis { … … 30 29 /// </summary> 31 30 public interface IRandomForestModel : IConfidenceRegressionModel, IClassificationModel { 31 int NumberOfTrees { get; } 32 ISymbolicExpressionTree ExtractTree(int treeIdx); // returns a specific tree from the random forest as a ISymbolicRegressionModel 32 33 } 33 34 } -
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/Interfaces/IRandomForestRegressionSolution.cs
r14185 r14351 20 20 #endregion 21 21 22 using HeuristicLab.Optimization;23 22 using HeuristicLab.Problems.DataAnalysis; 24 using HeuristicLab. Core;23 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; 25 24 26 25 namespace HeuristicLab.Algorithms.DataAnalysis { … … 30 29 public interface IRandomForestRegressionSolution : IConfidenceRegressionSolution { 31 30 new IRandomForestModel Model { get; } 31 int NumberOfTrees { get; } 32 32 } 33 33 } -
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassificationSolution.cs
r14185 r14351 38 38 } 39 39 40 public int NumberOfTrees { 41 get { return Model.NumberOfTrees; } 42 } 43 40 44 [StorableConstructor] 41 45 private RandomForestClassificationSolution(bool deserializing) : base(deserializing) { } -
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModel.cs
r14230 r14351 25 25 using HeuristicLab.Common; 26 26 using HeuristicLab.Core; 27 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 using HeuristicLab.Problems.DataAnalysis; 30 using HeuristicLab.Problems.DataAnalysis.Symbolic; 29 31 30 32 namespace HeuristicLab.Algorithms.DataAnalysis { … … 49 51 } 50 52 53 public int NumberOfTrees { 54 get { return nTrees; } 55 } 51 56 52 57 // instead of storing the data of the model itself … … 64 69 [Storable] 65 70 private double m; 66 67 71 68 72 [StorableConstructor] … … 197 201 } 198 202 203 public ISymbolicExpressionTree ExtractTree(int treeIdx) { 204 // hoping that the internal representation of alglib is stable 205 206 // TREE FORMAT 207 // W[Offs] - size of sub-array (for the tree) 208 // node info: 209 // W[K+0] - variable number (-1 for leaf mode) 210 // W[K+1] - threshold (class/value for leaf node) 211 // W[K+2] - ">=" branch index (absent for leaf node) 212 213 // skip irrelevant trees 214 int offset = 0; 215 for (int i = 0; i < treeIdx - 1; i++) { 216 offset = offset + (int)Math.Round(randomForest.innerobj.trees[offset]); 217 } 218 219 var constSy = new Constant(); 220 var varCondSy = new VariableCondition() { IgnoreSlope = true }; 221 222 var node = CreateRegressionTreeRec(randomForest.innerobj.trees, offset, offset + 1, constSy, varCondSy); 223 224 var startNode = new StartSymbol().CreateTreeNode(); 225 startNode.AddSubtree(node); 226 var root = new ProgramRootSymbol().CreateTreeNode(); 227 root.AddSubtree(startNode); 228 return new SymbolicExpressionTree(root); 229 } 230 231 private ISymbolicExpressionTreeNode CreateRegressionTreeRec(double[] trees, int offset, int k, Constant constSy, VariableCondition varCondSy) { 232 233 // alglib source for evaluation of one tree (dfprocessinternal) 234 // offs = 0 235 // 236 // Set pointer to the root 237 // 238 // k = offs + 1; 239 // 240 // // 241 // // Navigate through the tree 242 // // 243 // while (true) { 244 // if ((double)(df.trees[k]) == (double)(-1)) { 245 // if (df.nclasses == 1) { 246 // y[0] = y[0] + df.trees[k + 1]; 247 // } else { 248 // idx = (int)Math.Round(df.trees[k + 1]); 249 // y[idx] = y[idx] + 1; 250 // } 251 // break; 252 // } 253 // if ((double)(x[(int)Math.Round(df.trees[k])]) < (double)(df.trees[k + 1])) { 254 // k = k + innernodewidth; 255 // } else { 256 // k = offs + (int)Math.Round(df.trees[k + 2]); 257 // } 258 // } 259 260 if ((double)(trees[k]) == (double)(-1)) { 261 var constNode = (ConstantTreeNode)constSy.CreateTreeNode(); 262 constNode.Value = trees[k + 1]; 263 return constNode; 264 } else { 265 var condNode = (VariableConditionTreeNode)varCondSy.CreateTreeNode(); 266 condNode.VariableName = AllowedInputVariables[(int)Math.Round(trees[k])]; 267 condNode.Threshold = trees[k + 1]; 268 condNode.Slope = double.PositiveInfinity; 269 270 var left = CreateRegressionTreeRec(trees, offset, k + 3, constSy, varCondSy); 271 var right = CreateRegressionTreeRec(trees, offset, offset + (int)Math.Round(trees[k + 2]), constSy, varCondSy); 272 273 condNode.AddSubtree(left); // not 100% correct because interpreter uses: if(x <= thres) left() else right() and RF uses if(x < thres) left() else right() (see above) 274 condNode.AddSubtree(right); 275 return condNode; 276 } 277 } 278 199 279 200 280 public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) { -
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegressionSolution.cs
r14185 r14351 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; 24 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 26 using HeuristicLab.Problems.DataAnalysis; 27 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; 26 28 27 29 namespace HeuristicLab.Algorithms.DataAnalysis { … … 36 38 get { return (IRandomForestModel)base.Model; } 37 39 set { base.Model = value; } 40 } 41 42 public int NumberOfTrees { 43 get { return Model.NumberOfTrees; } 38 44 } 39 45 -
branches/symbreg-factors-2650/HeuristicLab.DataPreprocessing/3.4
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.DataPreprocessing/3.4 (added) merged: 14332
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.DataPreprocessing/3.4/PreprocessingContext.cs
r14185 r14351 67 67 if (problemData == null) throw new ArgumentNullException("problemData"); 68 68 if (source != null && ExtractProblemData(source) != problemData) 69 throw new ArgumentException("The ProblemData extracted from the Source is different than the given ProblemData.");69 source = null; // ignore the source if the source's problem data is different 70 70 Source = source ?? problemData; 71 71 var namedSource = Source as INamedItem; -
branches/symbreg-factors-2650/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding (added) merged: 14340,14342,14344
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Grammars/SymbolicExpressionGrammarBase.cs
r14185 r14351 82 82 protected SymbolicExpressionGrammarBase(bool deserializing) 83 83 : base(deserializing) { 84 cachedMinExpressionLength = new Dictionary<string, int>();85 cachedMaxExpressionLength = new Dictionary<Tuple<string, int>, int>();86 cachedMinExpressionDepth = new Dictionary<string, int>();87 cachedMaxExpressionDepth = new Dictionary<string, int>();88 89 cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>();90 cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>();91 84 92 85 symbols = new Dictionary<string, ISymbol>(); … … 100 93 protected SymbolicExpressionGrammarBase(SymbolicExpressionGrammarBase original, Cloner cloner) 101 94 : base(original, cloner) { 102 cachedMinExpressionLength = new Dictionary<string, int>();103 cachedMaxExpressionLength = new Dictionary<Tuple<string, int>, int>();104 cachedMinExpressionDepth = new Dictionary<string, int>();105 cachedMaxExpressionDepth = new Dictionary<string, int>();106 107 cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>();108 cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>();109 95 110 96 symbols = original.symbols.ToDictionary(x => x.Key, y => cloner.Clone(y.Value)); … … 124 110 protected SymbolicExpressionGrammarBase(string name, string description) 125 111 : base(name, description) { 126 cachedMinExpressionLength = new Dictionary<string, int>();127 cachedMaxExpressionLength = new Dictionary<Tuple<string, int>, int>();128 cachedMinExpressionDepth = new Dictionary<string, int>();129 cachedMaxExpressionDepth = new Dictionary<string, int>();130 131 cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>();132 cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>();133 134 112 symbols = new Dictionary<string, ISymbol>(); 135 113 symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>(); … … 322 300 } 323 301 324 private readonly Dictionary<Tuple<string, string>, bool> cachedIsAllowedChildSymbol ;302 private readonly Dictionary<Tuple<string, string>, bool> cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>(); 325 303 public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) { 326 304 if (allowedChildSymbols.Count == 0) return false; … … 352 330 } 353 331 354 private readonly Dictionary<Tuple<string, string, int>, bool> cachedIsAllowedChildSymbolIndex ;332 private readonly Dictionary<Tuple<string, string, int>, bool> cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>(); 355 333 public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 356 334 if (!child.Enabled) return false; … … 412 390 } 413 391 414 private readonly Dictionary<string, int> cachedMinExpressionLength ;392 private readonly Dictionary<string, int> cachedMinExpressionLength = new Dictionary<string, int>(); 415 393 public int GetMinimumExpressionLength(ISymbol symbol) { 416 394 int res; … … 423 401 if (cachedMinExpressionLength.TryGetValue(symbol.Name, out res)) return res; 424 402 425 res = GetMinimumExpressionLengthRec(symbol); 426 foreach (var entry in cachedMinExpressionLength.Where(e => e.Value >= int.MaxValue).ToList()) { 427 if (entry.Key != symbol.Name) cachedMinExpressionLength.Remove(entry.Key); 428 } 429 return res; 430 } 431 } 432 433 private int GetMinimumExpressionLengthRec(ISymbol symbol) { 434 int temp; 435 if (!cachedMinExpressionLength.TryGetValue(symbol.Name, out temp)) { 436 cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion 437 long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol)) 438 let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex) 439 where s.InitialFrequency > 0.0 440 select GetMinimumExpressionLengthRec(s)).DefaultIfEmpty(0).Min() 441 select minForSlot).DefaultIfEmpty(0).Sum(); 442 443 cachedMinExpressionLength[symbol.Name] = (int)Math.Min(sumOfMinExpressionLengths, int.MaxValue); 403 GrammarUtils.CalculateMinimumExpressionLengths(this, cachedMinExpressionLength); 444 404 return cachedMinExpressionLength[symbol.Name]; 445 405 } 446 return temp;447 } 448 449 private readonly Dictionary<Tuple<string, int>, int> cachedMaxExpressionLength ;406 } 407 408 409 private readonly Dictionary<Tuple<string, int>, int> cachedMaxExpressionLength = new Dictionary<Tuple<string, int>, int>(); 450 410 public int GetMaximumExpressionLength(ISymbol symbol, int maxDepth) { 451 411 int temp; … … 469 429 } 470 430 471 private readonly Dictionary<string, int> cachedMinExpressionDepth ;431 private readonly Dictionary<string, int> cachedMinExpressionDepth = new Dictionary<string, int>(); 472 432 public int GetMinimumExpressionDepth(ISymbol symbol) { 473 433 int res; … … 480 440 if (cachedMinExpressionDepth.TryGetValue(symbol.Name, out res)) return res; 481 441 482 res = GetMinimumExpressionDepthRec(symbol); 483 foreach (var entry in cachedMinExpressionDepth.Where(e => e.Value >= int.MaxValue).ToList()) { 484 if (entry.Key != symbol.Name) cachedMinExpressionDepth.Remove(entry.Key); 485 } 486 return res; 487 } 488 } 489 private int GetMinimumExpressionDepthRec(ISymbol symbol) { 490 int temp; 491 if (!cachedMinExpressionDepth.TryGetValue(symbol.Name, out temp)) { 492 cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion 493 long minDepth = 1 + (from argIndex in Enumerable.Range(0, GetMinimumSubtreeCount(symbol)) 494 let minForSlot = (long)(from s in GetAllowedChildSymbols(symbol, argIndex) 495 where s.InitialFrequency > 0.0 496 select GetMinimumExpressionDepthRec(s)).DefaultIfEmpty(0).Min() 497 select minForSlot).DefaultIfEmpty(0).Max(); 498 cachedMinExpressionDepth[symbol.Name] = (int)Math.Min(minDepth, int.MaxValue); 442 GrammarUtils.CalculateMinimumExpressionDepth(this, cachedMinExpressionDepth); 499 443 return cachedMinExpressionDepth[symbol.Name]; 500 444 } 501 return temp; 502 } 503 504 private readonly Dictionary<string, int> cachedMaxExpressionDepth; 445 } 446 447 private readonly Dictionary<string, int> cachedMaxExpressionDepth = new Dictionary<string, int>(); 505 448 public int GetMaximumExpressionDepth(ISymbol symbol) { 506 449 int temp; -
branches/symbreg-factors-2650/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj
r12897 r14351 129 129 <Compile Include="ArchitectureManipulators\SubroutineDuplicater.cs" /> 130 130 <Compile Include="ArchitectureManipulators\SymbolicExpressionTreeArchitectureManipulator.cs" /> 131 <Compile Include="Grammars\GrammarUtils.cs" /> 131 132 <Compile Include="SymbolicExpressionTreeProblem.cs" /> 132 133 <Compile Include="Compiler\Instruction.cs" /> -
branches/symbreg-factors-2650/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeEncoding.cs
r14185 r14351 164 164 public SymbolicExpressionTreeEncoding(string name, ISymbolicExpressionGrammar grammar, int maximumLength, int maximumDepth) 165 165 : base(name) { 166 treeLengthParameter = new FixedValueParameter<IntValue>( "Maximum Tree Length", "Maximal length of the symbolic expression.", new IntValue(maximumLength));167 treeDepthParameter = new FixedValueParameter<IntValue>( "Maximum Tree Depth", "Maximal depth of the symbolic expression. The minimum depth needed for the algorithm is 3 because two levels are reserved for the ProgramRoot and the Start symbol.", new IntValue(maximumDepth));168 grammarParameter = new ValueParameter<ISymbolicExpressionGrammar>( "Grammar", "The grammar that should be used for symbolic expression tree.", grammar);169 functionDefinitionsParameter = new FixedValueParameter<IntValue>( "Function Definitions", "Maximal number of automatically defined functions", new IntValue(0));170 functionArgumentsParameter = new FixedValueParameter<IntValue>( "Function Arguments", "Maximal number of arguments of automatically defined functions.", new IntValue(0));166 treeLengthParameter = new FixedValueParameter<IntValue>(Name + ".Maximum Tree Length", "Maximal length of the symbolic expression.", new IntValue(maximumLength)); 167 treeDepthParameter = new FixedValueParameter<IntValue>(Name + ".Maximum Tree Depth", "Maximal depth of the symbolic expression. The minimum depth needed for the algorithm is 3 because two levels are reserved for the ProgramRoot and the Start symbol.", new IntValue(maximumDepth)); 168 grammarParameter = new ValueParameter<ISymbolicExpressionGrammar>(Name + ".Grammar", "The grammar that should be used for symbolic expression tree.", grammar); 169 functionDefinitionsParameter = new FixedValueParameter<IntValue>(Name + ".Function Definitions", "Maximal number of automatically defined functions", new IntValue(0)); 170 functionArgumentsParameter = new FixedValueParameter<IntValue>(Name + ".Function Arguments", "Maximal number of arguments of automatically defined functions.", new IntValue(0)); 171 171 172 172 Parameters.Add(treeLengthParameter); -
branches/symbreg-factors-2650/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/SimpleSymbol.cs
r14185 r14351 61 61 } 62 62 63 public SimpleSymbol(string name, int arity) 64 : this(name, string.Empty, arity, arity) { 65 } 66 63 67 public SimpleSymbol(string name, string description, int minimumArity, int maximumArity) 64 68 : base(name, description) { -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis merged: 14348
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic merged: 14345,14347,14350
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression merged: 14349
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4 merged: 14349
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/Evaluators/SymbolicRegressionConstantOptimizationEvaluator.cs
r14266 r14351 183 183 List<string> variableNames = new List<string>(); 184 184 List<string> categoricalVariableValues = new List<string>(); 185 List<int> lags = new List<int>(); 185 186 186 187 AutoDiff.Term func; 187 if (!TryTransformToAutoDiff(tree.Root.GetSubtree(0), variables, parameters, variableNames, categoricalVariableValues, updateVariableWeights, out func))188 if (!TryTransformToAutoDiff(tree.Root.GetSubtree(0), variables, parameters, variableNames, lags, categoricalVariableValues, updateVariableWeights, out func)) 188 189 throw new NotSupportedException("Could not optimize constants of symbolic expression tree due to not supported symbols used in the tree."); 189 190 if (variableNames.Count == 0) return 0.0; // gkronber: constant expressions always have a R² of 0.0 … … 235 236 foreach (var r in rows) { 236 237 for (int col = 0; col < variableNames.Count; col++) { 238 int lag = lags[col]; 237 239 if (ds.VariableHasType<double>(variableNames[col])) { 238 x[row, col] = ds.GetDoubleValue(variableNames[col], r );240 x[row, col] = ds.GetDoubleValue(variableNames[col], r + lag); 239 241 } else if (ds.VariableHasType<string>(variableNames[col])) { 240 242 x[row, col] = ds.GetStringValue(variableNames[col], r) == categoricalVariableValues[col] ? 1 : 0; … … 257 259 alglib.lsfitfit(state, function_cx_1_func, function_cx_1_grad, null, null); 258 260 alglib.lsfitresults(state, out info, out c, out rep); 259 } 260 catch (ArithmeticException) { 261 } catch (ArithmeticException) { 261 262 return originalQuality; 262 } 263 catch (alglib.alglibexception) { 263 } catch (alglib.alglibexception) { 264 264 return originalQuality; 265 265 } … … 312 312 313 313 private static bool TryTransformToAutoDiff(ISymbolicExpressionTreeNode node, List<AutoDiff.Variable> variables, List<AutoDiff.Variable> parameters, 314 List<string> variableNames, List< string> categoricalVariableValues, bool updateVariableWeights, out AutoDiff.Term term) {314 List<string> variableNames, List<int> lags, List<string> categoricalVariableValues, bool updateVariableWeights, out AutoDiff.Term term) { 315 315 if (node.Symbol is Constant) { 316 316 var var = new AutoDiff.Variable(); … … 325 325 var varValue = factorVarNode != null ? factorVarNode.VariableValue : string.Empty; 326 326 var par = FindOrCreateParameter(varNode.VariableName, varValue, parameters, variableNames, categoricalVariableValues); 327 lags.Add(0); 327 328 328 329 if (updateVariableWeights) { … … 349 350 return true; 350 351 } 352 if (node.Symbol is LaggedVariable) { 353 var varNode = node as LaggedVariableTreeNode; 354 var par = new AutoDiff.Variable(); 355 parameters.Add(par); 356 variableNames.Add(varNode.VariableName); 357 lags.Add(varNode.Lag); 358 359 if (updateVariableWeights) { 360 var w = new AutoDiff.Variable(); 361 variables.Add(w); 362 term = AutoDiff.TermBuilder.Product(w, par); 363 } else { 364 term = par; 365 } 366 return true; 367 } 351 368 if (node.Symbol is Addition) { 352 369 List<AutoDiff.Term> terms = new List<Term>(); 353 370 foreach (var subTree in node.Subtrees) { 354 371 AutoDiff.Term t; 355 if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, categoricalVariableValues, updateVariableWeights, out t)) {372 if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, lags, categoricalVariableValues, updateVariableWeights, out t)) { 356 373 term = null; 357 374 return false; … … 366 383 for (int i = 0; i < node.SubtreeCount; i++) { 367 384 AutoDiff.Term t; 368 if (!TryTransformToAutoDiff(node.GetSubtree(i), variables, parameters, variableNames, categoricalVariableValues, updateVariableWeights, out t)) {385 if (!TryTransformToAutoDiff(node.GetSubtree(i), variables, parameters, variableNames, lags, categoricalVariableValues, updateVariableWeights, out t)) { 369 386 term = null; 370 387 return false; … … 381 398 foreach (var subTree in node.Subtrees) { 382 399 AutoDiff.Term t; 383 if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, categoricalVariableValues, updateVariableWeights, out t)) {400 if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, lags, categoricalVariableValues, updateVariableWeights, out t)) { 384 401 term = null; 385 402 return false; … … 396 413 foreach (var subTree in node.Subtrees) { 397 414 AutoDiff.Term t; 398 if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, categoricalVariableValues, updateVariableWeights, out t)) {415 if (!TryTransformToAutoDiff(subTree, variables, parameters, variableNames, lags, categoricalVariableValues, updateVariableWeights, out t)) { 399 416 term = null; 400 417 return false; … … 408 425 if (node.Symbol is Logarithm) { 409 426 AutoDiff.Term t; 410 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, categoricalVariableValues, updateVariableWeights, out t)) {427 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, categoricalVariableValues, updateVariableWeights, out t)) { 411 428 term = null; 412 429 return false; … … 418 435 if (node.Symbol is Exponential) { 419 436 AutoDiff.Term t; 420 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, categoricalVariableValues, updateVariableWeights, out t)) {437 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, categoricalVariableValues, updateVariableWeights, out t)) { 421 438 term = null; 422 439 return false; … … 428 445 if (node.Symbol is Square) { 429 446 AutoDiff.Term t; 430 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, categoricalVariableValues, updateVariableWeights, out t)) {447 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, categoricalVariableValues, updateVariableWeights, out t)) { 431 448 term = null; 432 449 return false; … … 438 455 if (node.Symbol is SquareRoot) { 439 456 AutoDiff.Term t; 440 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, categoricalVariableValues, updateVariableWeights, out t)) {457 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, categoricalVariableValues, updateVariableWeights, out t)) { 441 458 term = null; 442 459 return false; … … 448 465 if (node.Symbol is Sine) { 449 466 AutoDiff.Term t; 450 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, categoricalVariableValues, updateVariableWeights, out t)) {467 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, categoricalVariableValues, updateVariableWeights, out t)) { 451 468 term = null; 452 469 return false; … … 458 475 if (node.Symbol is Cosine) { 459 476 AutoDiff.Term t; 460 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, categoricalVariableValues, updateVariableWeights, out t)) {477 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, categoricalVariableValues, updateVariableWeights, out t)) { 461 478 term = null; 462 479 return false; … … 468 485 if (node.Symbol is Tangent) { 469 486 AutoDiff.Term t; 470 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, categoricalVariableValues, updateVariableWeights, out t)) {487 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, categoricalVariableValues, updateVariableWeights, out t)) { 471 488 term = null; 472 489 return false; … … 478 495 if (node.Symbol is Erf) { 479 496 AutoDiff.Term t; 480 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, categoricalVariableValues, updateVariableWeights, out t)) {497 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, categoricalVariableValues, updateVariableWeights, out t)) { 481 498 term = null; 482 499 return false; … … 488 505 if (node.Symbol is Norm) { 489 506 AutoDiff.Term t; 490 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, categoricalVariableValues, updateVariableWeights, out t)) {507 if (!TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, categoricalVariableValues, updateVariableWeights, out t)) { 491 508 term = null; 492 509 return false; … … 502 519 variables.Add(alpha); 503 520 AutoDiff.Term branchTerm; 504 if (TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, categoricalVariableValues, updateVariableWeights, out branchTerm)) {521 if (TryTransformToAutoDiff(node.GetSubtree(0), variables, parameters, variableNames, lags, categoricalVariableValues, updateVariableWeights, out branchTerm)) { 505 522 term = branchTerm * alpha + beta; 506 523 return true; … … 547 564 !(n.Symbol is BinaryFactorVariable) && 548 565 !(n.Symbol is FactorVariable) && 566 !(n.Symbol is LaggedVariable) && 549 567 !(n.Symbol is Constant) && 550 568 !(n.Symbol is Addition) && -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/InfixExpressionFormatter.cs
r14251 r14351 81 81 } 82 82 strBuilder.Append(")"); 83 } else { 84 // function with multiple arguments 85 strBuilder.Append(token).Append("("); 86 FormatRecursively(node.Subtrees.First(), strBuilder); 87 foreach (var subtree in node.Subtrees.Skip(1)) { 88 strBuilder.Append(", "); 89 FormatRecursively(subtree, strBuilder); 90 } 91 strBuilder.Append(")"); 83 92 } 84 93 } else if (node.SubtreeCount == 1) { … … 94 103 FormatRecursively(node.GetSubtree(0), strBuilder); 95 104 } else { 96 // function 105 // function with only one argument 97 106 strBuilder.Append(token).Append("("); 98 107 FormatRecursively(node.GetSubtree(0), strBuilder); … … 101 110 } else { 102 111 // no subtrees 103 if (node.Symbol is Variable) { 112 if (node.Symbol is LaggedVariable) { 113 var varNode = node as LaggedVariableTreeNode; 114 if (!varNode.Weight.IsAlmost(1.0)) { 115 strBuilder.Append("("); 116 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight); 117 strBuilder.Append("*"); 118 } 119 strBuilder.Append("LAG("); 120 if (varNode.VariableName.Contains("'")) { 121 strBuilder.AppendFormat("\"{0}\"", varNode.VariableName); 122 } else { 123 strBuilder.AppendFormat("'{0}'", varNode.VariableName); 124 } 125 strBuilder.Append(", ") 126 .AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Lag) 127 .Append(")"); 128 } else if (node.Symbol is Variable) { 104 129 var varNode = node as VariableTreeNode; 105 130 if (!varNode.Weight.IsAlmost(1.0)) { … … 144 169 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constNode.Value); 145 170 else 146 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1)171 strBuilder.AppendFormat(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1 147 172 } 148 173 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Importer/InfixExpressionParser.cs
r14330 r14351 26 26 using System.Text; 27 27 using HeuristicLab.Collections; 28 using HeuristicLab.Common; 28 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 30 … … 48 49 /// </summary> 49 50 public sealed class InfixExpressionParser { 50 private enum TokenType { Operator, Identifier, Number, LeftPar, RightPar, Eq, End, NA };51 private enum TokenType { Operator, Identifier, Number, LeftPar, RightPar, Comma, Eq, End, NA }; 51 52 private class Token { 52 53 internal double doubleVal; … … 114 115 { "MEAN", new Average()}, 115 116 { "IF", new IfThenElse()}, 116 { " >", new GreaterThan()},117 { " <", new LessThan()},117 { "GT", new GreaterThan()}, 118 { "LT", new LessThan()}, 118 119 { "AND", new And()}, 119 120 { "OR", new Or()}, … … 121 122 { "XOR", new Xor()}, 122 123 { "DIFF", new Derivative()}, 124 { "LAG", new LaggedVariable() }, 123 125 }; 124 126 … … 150 152 } 151 153 if (char.IsDigit(str[pos])) { 152 // read number (=> read until white space or operator )154 // read number (=> read until white space or operator or comma) 153 155 var sb = new StringBuilder(); 154 156 sb.Append(str[pos]); … … 159 161 && str[pos] != '*' 160 162 && str[pos] != '/' 161 && str[pos] != ')') { 163 && str[pos] != ')' 164 && str[pos] != ',') { 162 165 sb.Append(str[pos]); 163 166 pos++; … … 226 229 pos++; 227 230 yield return new Token { TokenType = TokenType.Eq, strVal = "=" }; 231 } else if (str[pos] == ',') { 232 pos++; 233 yield return new Token { TokenType = TokenType.Comma, strVal = "," }; 228 234 } else { 229 235 throw new ArgumentException("Invalid character: " + str[pos]); … … 232 238 } 233 239 240 // S = Expr EOF 241 // Expr = ['-' | '+'] Term { '+' Term | '-' Term } 242 // Term = Fact { '*' Fact | '/' Fact } 243 // Fact = '(' Expr ')' | funcId '(' ArgList ')' | varId | number 244 // ArgList = Expr { ',' Expr } 234 245 private ISymbolicExpressionTreeNode ParseS(Queue<Token> tokens) { 235 246 var expr = ParseExpr(tokens); … … 339 350 } 340 351 341 // Fact = '(' Expr ')' | funcId '(' Expr ')' | varId [ = valId ] | number352 // Fact = '(' Expr ')' | 'LAG' '(' varId ',' ['+' | '-' ] number ')' | funcId '(' Expr ')' | varId [ = valId ] | number 342 353 private ISymbolicExpressionTreeNode ParseFact(Queue<Token> tokens) { 343 354 var next = tokens.Peek(); … … 359 370 if (lPar.TokenType != TokenType.LeftPar) 360 371 throw new ArgumentException("expected ("); 361 var expr = ParseExpr(tokens); 372 373 // handle 'lag' specifically 374 if (funcNode.Symbol is LaggedVariable) { 375 var varId = tokens.Dequeue(); 376 if (varId.TokenType != TokenType.Identifier) throw new ArgumentException("Identifier expected. Format for lagged variables: \"lag(x, -1)\""); 377 var comma = tokens.Dequeue(); 378 if (comma.TokenType != TokenType.Comma) throw new ArgumentException("',' expected, Format for lagged variables: \"lag(x, -1)\""); 379 double sign = 1.0; 380 if (tokens.Peek().strVal == "+" || tokens.Peek().strVal == "-") { 381 // read sign 382 var signTok = tokens.Dequeue(); 383 if (signTok.strVal == "-") sign = -1.0; 384 } 385 var lagToken = tokens.Dequeue(); 386 if (lagToken.TokenType != TokenType.Number) throw new ArgumentException("Number expected, Format for lagged variables: \"lag(x, -1)\""); 387 if (!lagToken.doubleVal.IsAlmost(Math.Round(lagToken.doubleVal))) 388 throw new ArgumentException("Time lags must be integer values"); 389 var laggedVarNode = funcNode as LaggedVariableTreeNode; 390 laggedVarNode.VariableName = varId.strVal; 391 laggedVarNode.Lag = (int)Math.Round(sign * lagToken.doubleVal); 392 laggedVarNode.Weight = 1.0; 393 } else { 394 // functions 395 var args = ParseArgList(tokens); 396 // check number of arguments 397 if (funcNode.Symbol.MinimumArity > args.Length || funcNode.Symbol.MaximumArity < args.Length) { 398 throw new ArgumentException(string.Format("Symbol {0} requires between {1} and {2} arguments.", funcId, 399 funcNode.Symbol.MinimumArity, funcNode.Symbol.MaximumArity)); 400 } 401 foreach (var arg in args) funcNode.AddSubtree(arg); 402 } 403 362 404 var rPar = tokens.Dequeue(); 363 405 if (rPar.TokenType != TokenType.RightPar) 364 406 throw new ArgumentException("expected )"); 365 407 366 funcNode.AddSubtree(expr);367 408 return funcNode; 368 409 } else { … … 396 437 } 397 438 439 // ArgList = Expr { ',' Expr } 440 private ISymbolicExpressionTreeNode[] ParseArgList(Queue<Token> tokens) { 441 var exprList = new List<ISymbolicExpressionTreeNode>(); 442 exprList.Add(ParseExpr(tokens)); 443 while (tokens.Peek().TokenType != TokenType.RightPar) { 444 var comma = tokens.Dequeue(); 445 if (comma.TokenType != TokenType.Comma) throw new ArgumentException("expected ',' "); 446 exprList.Add(ParseExpr(tokens)); 447 } 448 return exprList.ToArray(); 449 } 398 450 } 399 451 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs
r14185 r14351 614 614 case OpCodes.VariableCondition: { 615 615 var variableConditionTreeNode = (VariableConditionTreeNode)node; 616 if (variableConditionTreeNode.Symbol.IgnoreSlope) throw new NotSupportedException("Strict variable conditionals are not supported"); 616 617 var variableName = variableConditionTreeNode.VariableName; 617 618 var indexExpr = Expression.Constant(variableIndices[variableName]); -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs
r14251 r14351 488 488 if (row < 0 || row >= dataset.Rows) return double.NaN; 489 489 var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode; 490 double variableValue = ((IList<double>)currentInstr.data)[row]; 491 double x = variableValue - variableConditionTreeNode.Threshold; 492 double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x)); 493 494 double trueBranch = Evaluate(dataset, ref row, state); 495 double falseBranch = Evaluate(dataset, ref row, state); 496 497 return trueBranch * p + falseBranch * (1 - p); 490 if (!variableConditionTreeNode.Symbol.IgnoreSlope) { 491 double variableValue = ((IList<double>)currentInstr.data)[row]; 492 double x = variableValue - variableConditionTreeNode.Threshold; 493 double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x)); 494 495 double trueBranch = Evaluate(dataset, ref row, state); 496 double falseBranch = Evaluate(dataset, ref row, state); 497 498 return trueBranch * p + falseBranch * (1 - p); 499 } else { 500 // strict threshold 501 double variableValue = ((IList<double>)currentInstr.data)[row]; 502 if (variableValue <= variableConditionTreeNode.Threshold) { 503 var left = Evaluate(dataset, ref row, state); 504 state.SkipInstructions(); 505 return left; 506 } else { 507 state.SkipInstructions(); 508 return Evaluate(dataset, ref row, state); 509 } 510 } 498 511 } 499 512 default: -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs
r14330 r14351 126 126 private readonly object syncRoot = new object(); 127 127 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, IDataset dataset, IEnumerable<int> rows) { 128 if (!rows.Any()) return Enumerable.Empty<double>(); 128 129 if (CheckExpressionsWithIntervalArithmetic) 129 130 throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter."); … … 171 172 if (row < 0 || row >= dataset.Rows) instr.value = double.NaN; 172 173 var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode; 173 double variableValue = ((IList<double>)instr.data)[row]; 174 double x = variableValue - variableConditionTreeNode.Threshold; 175 double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x)); 176 177 double trueBranch = code[instr.childIndex].value; 178 double falseBranch = code[instr.childIndex + 1].value; 179 180 instr.value = trueBranch * p + falseBranch * (1 - p); 174 if (!variableConditionTreeNode.Symbol.IgnoreSlope) { 175 double variableValue = ((IList<double>)instr.data)[row]; 176 double x = variableValue - variableConditionTreeNode.Threshold; 177 double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x)); 178 179 double trueBranch = code[instr.childIndex].value; 180 double falseBranch = code[instr.childIndex + 1].value; 181 182 instr.value = trueBranch * p + falseBranch * (1 - p); 183 } else { 184 double variableValue = ((IList<double>)instr.data)[row]; 185 if (variableValue <= variableConditionTreeNode.Threshold) { 186 instr.value = code[instr.childIndex].value; 187 } else { 188 instr.value = code[instr.childIndex + 1].value; 189 } 190 } 181 191 } else if (instr.opCode == OpCodes.Add) { 182 192 double s = code[instr.childIndex].value; … … 433 443 for (int j = 1; j != seq.Length; ++j) 434 444 seq[j].skip = true; 435 }436 break;445 break; 446 } 437 447 } 438 448 #endregion -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/VariableCondition.cs
r14238 r14351 149 149 } 150 150 } 151 } 152 153 /// <summary> 154 /// Flag to indicate if the interpreter should ignore the slope parameter (introduced for representation of expression trees) 155 /// </summary> 156 [Storable] 157 private bool ignoreSlope; 158 public bool IgnoreSlope { 159 get { return ignoreSlope; } 160 set { ignoreSlope = value; } 151 161 } 152 162 -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/VariableConditionTreeNode.cs
r14238 r14351 96 96 97 97 public override string ToString() { 98 if (slope.IsAlmost(0.0)) 98 if (slope.IsAlmost(0.0) || Symbol.IgnoreSlope) { 99 return variableName + " < " + threshold.ToString("E4"); 100 } else { 99 101 return variableName + " > " + threshold.ToString("E4") + Environment.NewLine + 100 "slope: " + slope.ToString("E4"); 101 else 102 return variableName + " > " + threshold.ToString("E4"); 102 "slope: " + slope.ToString("E4"); 103 } 103 104 } 104 105 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Views
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.DataAnalysis.Views merged: 14348
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Views/3.4/HeuristicLab.Problems.DataAnalysis.Views-3.4.csproj
r14248 r14351 252 252 <Compile Include="Regression\RegressionFeatureCorrelationView.Designer.cs"> 253 253 <DependentUpon>RegressionFeatureCorrelationView.cs</DependentUpon> 254 </Compile> 255 <Compile Include="Regression\RegressionSolutionVariableImpactsView.cs"> 256 <SubType>UserControl</SubType> 257 </Compile> 258 <Compile Include="Regression\RegressionSolutionVariableImpactsView.Designer.cs"> 259 <DependentUpon>RegressionSolutionVariableImpactsView.cs</DependentUpon> 254 260 </Compile> 255 261 <Compile Include="Regression\RegressionSolutionGradientView.cs"> -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Views/3.4/Solution Views/RegressionSolutionView.Designer.cs
r14185 r14351 46 46 /// </summary> 47 47 private void InitializeComponent() { 48 this.btnImpactCalculation = new System.Windows.Forms.Button();49 48 ((System.ComponentModel.ISupportInitialize)(this.splitContainer)).BeginInit(); 50 49 this.splitContainer.Panel1.SuspendLayout(); … … 54 53 this.detailsGroupBox.SuspendLayout(); 55 54 this.SuspendLayout(); 56 //57 // btnImpactCalculation58 //59 this.btnImpactCalculation.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)));60 this.btnImpactCalculation.Image = HeuristicLab.Common.Resources.VSImageLibrary.Zoom;61 this.btnImpactCalculation.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;62 this.btnImpactCalculation.Name = "btnImpactCalculation";63 this.btnImpactCalculation.TabIndex = 6;64 this.btnImpactCalculation.Size = new System.Drawing.Size(110, 24);65 this.btnImpactCalculation.Text = "Variable Impacts";66 this.btnImpactCalculation.TextAlign = System.Drawing.ContentAlignment.MiddleRight;67 this.btnImpactCalculation.UseVisualStyleBackColor = true;68 this.btnImpactCalculation.Click += new System.EventHandler(this.btnImpactCalculation_Click);69 this.toolTip.SetToolTip(this.btnImpactCalculation, "Calculate impacts");70 //71 // flowLayoutPanel72 //73 this.flowLayoutPanel.Controls.Add(this.btnImpactCalculation);74 //75 // splitContainer76 //77 //78 55 // itemsGroupBox 79 56 // … … 104 81 105 82 #endregion 106 107 protected System.Windows.Forms.Button btnImpactCalculation;108 83 } 109 84 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis.Views/3.4/Solution Views/RegressionSolutionView.cs
r14185 r14351 20 20 #endregion 21 21 22 using System;23 using System.Linq;24 using System.Threading.Tasks;25 22 using System.Windows.Forms; 26 23 using HeuristicLab.Core; 27 using HeuristicLab.Data;28 using HeuristicLab.Data.Views;29 24 using HeuristicLab.MainForm; 30 using HeuristicLab.MainForm.WindowsForms;31 25 32 26 namespace HeuristicLab.Problems.DataAnalysis.Views { … … 41 35 get { return (RegressionSolutionBase)base.Content; } 42 36 set { base.Content = value; } 43 }44 45 protected override void SetEnabledStateOfControls() {46 base.SetEnabledStateOfControls();47 btnImpactCalculation.Enabled = Content != null && !Locked;48 }49 50 protected virtual void btnImpactCalculation_Click(object sender, EventArgs e) {51 var mainForm = (MainForm.WindowsForms.MainForm)MainFormManager.MainForm;52 var view = new StringConvertibleArrayView();53 view.Caption = Content.Name + " Variable Impacts";54 view.Show();55 56 Task.Factory.StartNew(() => {57 try {58 mainForm.AddOperationProgressToView(view, "Calculating variable impacts for " + Content.Name);59 60 var impacts = RegressionSolutionVariableImpactsCalculator.CalculateImpacts(Content);61 var impactArray = new DoubleArray(impacts.Select(i => i.Item2).ToArray());62 impactArray.ElementNames = impacts.Select(i => i.Item1);63 view.Content = (DoubleArray)impactArray.AsReadOnly();64 }65 finally {66 mainForm.RemoveOperationProgressFromView(view);67 }68 });69 37 } 70 38 -
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionSolutionVariableImpactsCalculator.cs
r14249 r14351 171 171 // new var has same empirical distribution but the relation to y is broken 172 172 rand = new FastRandom(31415); 173 replacementValues = rows.Select(r => originalValues[r]).Shuffle(rand).ToList(); 173 // prepare a complete column for the dataset 174 replacementValues = Enumerable.Repeat(double.NaN, dataset.Rows).ToList(); 175 // shuffle only the selected rows 176 var shuffledValues = rows.Select(r => originalValues[r]).Shuffle(rand).ToList(); 177 int i = 0; 178 // update column values 179 foreach (var r in rows) { 180 replacementValues[r] = shuffledValues[i++]; 181 } 174 182 break; 175 183 case ReplacementMethodEnum.Noise: … … 177 185 var stdDev = rows.Select(r => originalValues[r]).StandardDeviation(); 178 186 rand = new FastRandom(31415); 179 replacementValues = rows.Select(_ => NormalDistributedRandom.NextDouble(rand, avg, stdDev)).ToList(); 187 // prepare a complete column for the dataset 188 replacementValues = Enumerable.Repeat(double.NaN, dataset.Rows).ToList(); 189 // update column values 190 foreach (var r in rows) { 191 replacementValues[r] = NormalDistributedRandom.NextDouble(rand, avg, stdDev); 192 } 180 193 break; 181 194 -
branches/symbreg-factors-2650/HeuristicLab.Problems.Instances.DataAnalysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis merged: 14343
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.Instances.DataAnalysis.Views
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Problems.Instances.DataAnalysis.Views merged: 14343
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.Instances.DataAnalysis.Views/3.3/DataAnalysisImportTypeDialog.cs
r14330 r14351 82 82 NumberFormatInfo = (NumberFormatInfo)DecimalSeparatorComboBox.SelectedValue, 83 83 DateTimeFormatInfo = (DateTimeFormatInfo)DateTimeFormatComboBox.SelectedValue, 84 VariableNamesAvailable = CheckboxColumnNames.Checked 84 VariableNamesAvailable = CheckboxColumnNames.Checked, 85 Encoding = (Encoding) EncodingComboBox.SelectedValue 85 86 }; 86 87 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.Instances.DataAnalysis/3.3/DataAnalysisCSVFormat.cs
r14185 r14351 21 21 22 22 using System.Globalization; 23 using System.Text; 23 24 24 25 namespace HeuristicLab.Problems.Instances.DataAnalysis { … … 28 29 public DateTimeFormatInfo DateTimeFormatInfo { get; set; } 29 30 public bool VariableNamesAvailable { get; set; } 31 public Encoding Encoding { get; set; } 30 32 } 31 33 } -
branches/symbreg-factors-2650/HeuristicLab.Problems.Instances.DataAnalysis/3.3/DataAnalysisInstanceProvider.cs
r14185 r14351 40 40 public TData ImportData(string path, ImportType type, DataAnalysisCSVFormat csvFormat) { 41 41 TableFileParser csvFileParser = new TableFileParser(); 42 csvFileParser.Encoding = csvFormat.Encoding; 42 43 long fileSize = new FileInfo(path).Length; 43 44 csvFileParser.ProgressChanged += (sender, e) => { -
branches/symbreg-factors-2650/HeuristicLab.Tests
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Tests merged: 14341,14347,14350
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/InfixExpressionParserTest.cs
r14109 r14351 86 86 Console.WriteLine(formatter.Format(parser.Parse("x1*x2+x3*x4"))); 87 87 88 89 Console.WriteLine(formatter.Format(parser.Parse("POW(3, 2)"))); 90 Console.WriteLine(formatter.Format(parser.Parse("POW(3.1, 2.1)"))); 91 Console.WriteLine(formatter.Format(parser.Parse("POW(3.1 , 2.1)"))); 92 Console.WriteLine(formatter.Format(parser.Parse("POW(3.1 ,2.1)"))); 93 Console.WriteLine(formatter.Format(parser.Parse("POW(-3.1 , - 2.1)"))); 94 Console.WriteLine(formatter.Format(parser.Parse("ROOT(3, 2)"))); 95 Console.WriteLine(formatter.Format(parser.Parse("ROOT(3.1, 2.1)"))); 96 Console.WriteLine(formatter.Format(parser.Parse("ROOT(3.1 , 2.1)"))); 97 Console.WriteLine(formatter.Format(parser.Parse("ROOT(3.1 ,2.1)"))); 98 Console.WriteLine(formatter.Format(parser.Parse("ROOT(-3.1 , - 2.1)"))); 99 100 Console.WriteLine(formatter.Format(parser.Parse("IF(GT( 0, 1), 1, 0)"))); 101 Console.WriteLine(formatter.Format(parser.Parse("IF(LT(0,1), 1 , 0)"))); 102 103 Console.WriteLine(formatter.Format(parser.Parse("LAG(x, 1)"))); 104 Console.WriteLine(formatter.Format(parser.Parse("LAG(x, -1)"))); 105 Console.WriteLine(formatter.Format(parser.Parse("LAG(x, +1)"))); 106 Console.WriteLine(formatter.Format(parser.Parse("x * LAG('x', +1)"))); 107 88 108 } 89 109 } -
branches/symbreg-factors-2650/HeuristicLab.Tests/HeuristicLab.Tests.csproj
r14277 r14351 565 565 <Compile Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4\SubroutineDuplicaterTest.cs" /> 566 566 <Compile Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4\SubtreeCrossoverTest.cs" /> 567 <Compile Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4\GrammarsTest.cs" /> 567 568 <Compile Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4\Util.cs" /> 568 569 <Compile Include="HeuristicLab.Persistence-3.3\StorableAttributeTests.cs" />
Note: See TracChangeset
for help on using the changeset viewer.