Changeset 7695
- Timestamp:
- 04/03/12 00:32:03 (13 years ago)
- Location:
- trunk/sources
- Files:
-
- 2 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r7462 r7695 112 112 } else if (node.Symbol is Exponential) { 113 113 strBuilder.Append(@"\exp \left( "); 114 } else if (node.Symbol is Square) { 115 strBuilder.Append(@"\left("); 116 } else if (node.Symbol is SquareRoot) { 117 strBuilder.Append(@"\sqrt{"); 114 118 } else if (node.Symbol is Sine) { 115 119 strBuilder.Append(@"\sin \left( "); … … 209 213 } else if (node.Symbol is Exponential) { 210 214 throw new InvalidOperationException(); 215 } else if (node.Symbol is Square) { 216 throw new InvalidOperationException(); 217 } else if (node.Symbol is SquareRoot) { 218 throw new InvalidOperationException(); 211 219 } else if (node.Symbol is Sine) { 212 220 throw new InvalidOperationException(); … … 266 274 } else if (node.Symbol is Exponential) { 267 275 strBuilder.Append(@" \right) "); 276 } else if (node.Symbol is Square) { 277 strBuilder.Append(@"\right)^2"); 278 } else if (node.Symbol is SquareRoot) { 279 strBuilder.Append(@"}"); 268 280 } else if (node.Symbol is Sine) { 269 281 strBuilder.Append(@" \right) "); -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs
r7653 r7695 20 20 #endregion 21 21 22 using System.Globalization; 22 23 using System.Text; 23 using System.Linq;24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 using HeuristicLab. Common;26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using HeuristicLab.Problems.DataAnalysis;28 using System.Collections.Generic;29 using System;30 using System.Globalization;31 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;32 28 33 29 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 84 80 } 85 81 86 public string FormatOnlyExpression(ISymbolicExpressionTreeNode expressionNode) 87 { 82 public string FormatOnlyExpression(ISymbolicExpressionTreeNode expressionNode) { 88 83 var stringBuilder = new StringBuilder(); 89 84 stringBuilder.AppendLine(" for " + CurrentIndexVariable + " = 1:1:rows"); … … 151 146 } else if (symbol is Exponential) { 152 147 stringBuilder.Append("exp("); 148 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 149 stringBuilder.Append(")"); 150 } else if (symbol is Square) { 151 stringBuilder.Append("("); 152 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 153 stringBuilder.Append(").^2"); 154 } else if (symbol is SquareRoot) { 155 stringBuilder.Append("sqrt("); 153 156 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 154 157 stringBuilder.Append(")"); … … 283 286 284 287 285 private string LagToString(int lag) 286 { 288 private string LagToString(int lag) { 287 289 if (lag < 0) { 288 290 return "(" + CurrentIndexVariable + "" + lag + ")"; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/FullFunctionalExpressionGrammar.cs
r7259 r7695 55 55 var pow = new Power(); 56 56 pow.InitialFrequency = 0.0; 57 var square = new Square(); 58 square.InitialFrequency = 0.0; 57 59 var root = new Root(); 58 60 root.InitialFrequency = 0.0; 61 var sqrt = new SquareRoot(); 62 sqrt.InitialFrequency = 0.0; 59 63 var exp = new Exponential(); 60 64 var @if = new IfThenElse(); … … 82 86 laggedVariable.InitialFrequency = 0.0; 83 87 84 var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, pow, root, exp, @if, gt, lt, and, or, not, timeLag, integral, derivative, constant, variableSymbol, laggedVariable, variableCondition };85 var unaryFunctionSymbols = new List<Symbol>() { s in, cos, tan, log, exp, not, timeLag, integral, derivative };88 var allSymbols = new List<Symbol>() { add, sub, mul, div, mean, sin, cos, tan, log, square, pow, sqrt, root, exp, @if, gt, lt, and, or, not, timeLag, integral, derivative, constant, variableSymbol, laggedVariable, variableCondition }; 89 var unaryFunctionSymbols = new List<Symbol>() { square, sqrt, sin, cos, tan, log, exp, not, timeLag, integral, derivative }; 86 90 87 91 var binaryFunctionSymbols = new List<Symbol>() { pow, root, gt, lt, variableCondition }; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/TypeCoherentExpressionGrammar.cs
r7259 r7695 65 65 var log = new Logarithm(); 66 66 var pow = new Power(); 67 var square = new Square(); 67 68 var root = new Root(); 69 var sqrt = new SquareRoot(); 68 70 var exp = new Exponential(); 69 71 var @if = new IfThenElse(); … … 93 95 var realValuedSymbols = new GroupSymbol(RealValuedSymbolsName, new List<ISymbol>() { arithmeticSymbols, trigonometricSymbols, exponentialAndLogarithmicSymbols, terminalSymbols }); 94 96 95 var powerSymbols = new GroupSymbol(PowerFunctionsName, new List<ISymbol> { pow, root });97 var powerSymbols = new GroupSymbol(PowerFunctionsName, new List<ISymbol> { square, pow, sqrt, root }); 96 98 97 99 var conditionSymbols = new GroupSymbol(ConditionsName, new List<ISymbol> { @if, variableCondition }); … … 111 113 SetSubtreeCount(arithmeticSymbols, 2, 2); 112 114 SetSubtreeCount(trigonometricSymbols, 1, 1); 113 SetSubtreeCount(powerSymbols, 2, 2); 115 SetSubtreeCount(pow, 2, 2); 116 SetSubtreeCount(root, 2, 2); 117 SetSubtreeCount(square, 1, 1); 118 SetSubtreeCount(sqrt, 1, 1); 114 119 SetSubtreeCount(exponentialAndLogarithmicSymbols, 1, 1); 115 120 SetSubtreeCount(terminalSymbols, 0, 0); … … 140 145 AddAllowedChildSymbol(powerSymbols, laggedVariable, 0); 141 146 AddAllowedChildSymbol(powerSymbols, constant, 1); 147 AddAllowedChildSymbol(square, realValuedSymbols, 0); 148 AddAllowedChildSymbol(sqrt, realValuedSymbols, 0); 142 149 143 150 AddAllowedChildSymbol(@if, comparisonSymbols, 0); -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r7506 r7695 161 161 <Compile Include="Symbols\Constant.cs" /> 162 162 <Compile Include="Symbols\ConstantTreeNode.cs" /> 163 <Compile Include="Symbols\SquareRoot.cs" /> 164 <Compile Include="Symbols\Square.cs" /> 163 165 <Compile Include="Symbols\Cosine.cs" /> 164 166 <Compile Include="Symbols\Derivative.cs" /> -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeInterpreter.cs
r7506 r7695 133 133 134 134 public const byte VariableCondition = 27; 135 136 public const byte Square = 28; 137 public const byte SquareRoot = 29; 135 138 } 136 139 #endregion … … 154 157 { typeof(Average), OpCodes.Average}, 155 158 { typeof(InvokeFunction), OpCodes.Call }, 156 { typeof( HeuristicLab.Problems.DataAnalysis.Symbolic.Variable), OpCodes.Variable },159 { typeof(Variable), OpCodes.Variable }, 157 160 { typeof(LaggedVariable), OpCodes.LagVariable }, 158 161 { typeof(Constant), OpCodes.Constant }, … … 163 166 { typeof(Integral), OpCodes.Integral}, 164 167 { typeof(Derivative), OpCodes.Derivative}, 165 { typeof(VariableCondition),OpCodes.VariableCondition} 168 { typeof(VariableCondition),OpCodes.VariableCondition}, 169 { typeof(Square),OpCodes.Square}, 170 {typeof(SquareRoot),OpCodes.SquareRoot} 166 171 }; 167 172 … … 305 310 return Math.Tan(Evaluate(dataset, ref row, state)); 306 311 } 312 case OpCodes.Square: { 313 return Math.Pow(Evaluate(dataset, ref row, state), 2); 314 } 307 315 case OpCodes.Power: { 308 316 double x = Evaluate(dataset, ref row, state); 309 317 double y = Math.Round(Evaluate(dataset, ref row, state)); 310 318 return Math.Pow(x, y); 319 } 320 case OpCodes.SquareRoot: { 321 return Math.Sqrt(Evaluate(dataset, ref row, state)); 311 322 } 312 323 case OpCodes.Root: { -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs
r7259 r7695 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Diagnostics;25 24 using System.Linq; 26 25 using HeuristicLab.Common; … … 41 40 private Exponential expSymbol = new Exponential(); 42 41 private Root rootSymbol = new Root(); 42 private Square sqrSymbol = new Square(); 43 private SquareRoot sqrtSymbol = new SquareRoot(); 43 44 private Power powSymbol = new Power(); 44 45 private Sine sineSymbol = new Sine(); … … 125 126 private bool IsRoot(ISymbolicExpressionTreeNode node) { 126 127 return node.Symbol is Root; 128 } 129 private bool IsSquare(ISymbolicExpressionTreeNode node) { 130 return node.Symbol is Square; 131 } 132 private bool IsSquareRoot(ISymbolicExpressionTreeNode node) { 133 return node.Symbol is SquareRoot; 127 134 } 128 135 private bool IsPower(ISymbolicExpressionTreeNode node) { … … 201 208 } else if (IsExp(original)) { 202 209 return SimplifyExp(original); 210 } else if (IsSquare(original)) { 211 return SimplifySquare(original); 212 } else if (IsSquareRoot(original)) { 213 return SimplifySquareRoot(original); 214 } else if (IsPower(original)) { 215 return SimplifyPower(original); 203 216 } else if (IsRoot(original)) { 204 217 return SimplifyRoot(original); 205 } else if (IsPower(original)) {206 return SimplifyPower(original);207 218 } else if (IsSine(original)) { 208 219 return SimplifySine(original); … … 348 359 return MakeExp(GetSimplifiedTree(original.GetSubtree(0))); 349 360 } 361 private ISymbolicExpressionTreeNode SimplifySquare(ISymbolicExpressionTreeNode original) { 362 return MakeSquare(GetSimplifiedTree(original.GetSubtree(0))); 363 } 364 private ISymbolicExpressionTreeNode SimplifySquareRoot(ISymbolicExpressionTreeNode original) { 365 return MakeSquareRoot(GetSimplifiedTree(original.GetSubtree(0))); 366 } 350 367 351 368 private ISymbolicExpressionTreeNode SimplifyLog(ISymbolicExpressionTreeNode original) { … … 532 549 } 533 550 } 551 552 private ISymbolicExpressionTreeNode MakeSquare(ISymbolicExpressionTreeNode node) { 553 if (IsConstant(node)) { 554 var constT = node as ConstantTreeNode; 555 return MakeConstant(constT.Value * constT.Value); 556 } else if (IsSquareRoot(node)) { 557 return node.GetSubtree(0); 558 } else { 559 var sqrNode = sqrSymbol.CreateTreeNode(); 560 sqrNode.AddSubtree(node); 561 return sqrNode; 562 } 563 } 564 private ISymbolicExpressionTreeNode MakeSquareRoot(ISymbolicExpressionTreeNode node) { 565 if (IsConstant(node)) { 566 var constT = node as ConstantTreeNode; 567 return MakeConstant(Math.Sqrt(constT.Value)); 568 } else if (IsSquare(node)) { 569 return node.GetSubtree(0); 570 } else { 571 var sqrtNode = sqrtSymbol.CreateTreeNode(); 572 sqrtNode.AddSubtree(node); 573 return sqrtNode; 574 } 575 } 576 534 577 private ISymbolicExpressionTreeNode MakeLog(ISymbolicExpressionTreeNode node) { 535 578 if (IsConstant(node)) { … … 636 679 } else if (IsDivision(a) && IsConstant(b)) { 637 680 // (a1 / a2) / c => (a1 / (a2 * c)) 638 Trace.Assert(a.Subtrees.Count() == 2);639 681 return MakeFraction(a.GetSubtree(0), MakeProduct(a.GetSubtree(1), b)); 640 682 } else if (IsDivision(a) && IsDivision(b)) { 641 683 // (a1 / a2) / (b1 / b2) => 642 Trace.Assert(a.Subtrees.Count() == 2);643 Trace.Assert(b.Subtrees.Count() == 2);644 684 return MakeFraction(MakeProduct(a.GetSubtree(0), b.GetSubtree(1)), MakeProduct(a.GetSubtree(1), b.GetSubtree(0))); 645 685 } else if (IsDivision(a)) { 646 686 // (a1 / a2) / b => (a1 / (a2 * b)) 647 Trace.Assert(a.Subtrees.Count() == 2);648 687 return MakeFraction(a.GetSubtree(0), MakeProduct(a.GetSubtree(1), b)); 649 688 } else if (IsDivision(b)) { 650 689 // a / (b1 / b2) => (a * b2) / b1 651 Trace.Assert(b.Subtrees.Count() == 2);652 690 return MakeFraction(MakeProduct(a, b.GetSubtree(1)), b.GetSubtree(0)); 653 691 } else { … … 774 812 } else if (IsDivision(a) && IsDivision(b)) { 775 813 // (a1 / a2) * (b1 / b2) => (a1 * b1) / (a2 * b2) 776 Trace.Assert(a.Subtrees.Count() == 2);777 Trace.Assert(b.Subtrees.Count() == 2);778 814 return MakeFraction(MakeProduct(a.GetSubtree(0), b.GetSubtree(0)), MakeProduct(a.GetSubtree(1), b.GetSubtree(1))); 779 815 } else if (IsDivision(a)) { 780 816 // (a1 / a2) * b => (a1 * b) / a2 781 Trace.Assert(a.Subtrees.Count() == 2);782 817 return MakeFraction(MakeProduct(a.GetSubtree(0), b), a.GetSubtree(1)); 783 818 } else if (IsDivision(b)) { 784 819 // a * (b1 / b2) => (b1 * a) / b2 785 Trace.Assert(b.Subtrees.Count() == 2);786 820 return MakeFraction(MakeProduct(b.GetSubtree(0), a), b.GetSubtree(1)); 787 821 } else if (IsMultiplication(a) && IsMultiplication(b)) { … … 912 946 return MakeConstant(1.0 / ((ConstantTreeNode)x).Value); 913 947 } else if (IsDivision(x)) { 914 Trace.Assert(x.Subtrees.Count() == 2);915 948 return MakeFraction(x.GetSubtree(1), x.GetSubtree(0)); 916 949 } else { -
trunk/sources/HeuristicLab.Tests/HeuristicLab.Tests.csproj
r7650 r7695 102 102 </PropertyGroup> 103 103 <ItemGroup> 104 <Reference Include="ALGLIB-3. 4.0">105 <HintPath>..\bin\ALGLIB-3. 4.0.dll</HintPath>104 <Reference Include="ALGLIB-3.5.0, Version=3.5.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 105 <HintPath>..\bin\ALGLIB-3.5.0.dll</HintPath> 106 106 </Reference> 107 107 <Reference Include="Calendar.DayView"> … … 112 112 <HintPath>..\bin\Google.ProtocolBuffers-0.9.1.dll</HintPath> 113 113 <Private>True</Private> 114 </Reference>115 <Reference Include="HeuristicLab.ALGLIB-3.4.0">116 <HintPath>..\bin\HeuristicLab.ALGLIB-3.4.0.dll</HintPath>117 114 </Reference> 118 115 <Reference Include="HeuristicLab.LibSVM-1.6.3">
Note: See TracChangeset
for help on using the changeset viewer.