Free cookie consent management tool by TermsFeed Policy Generator

Changeset 7695


Ignore:
Timestamp:
04/03/12 00:32:03 (13 years ago)
Author:
gkronber
Message:

#1810 merged patch to add square and square root function symbols by mkommend

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  
    112112      } else if (node.Symbol is Exponential) {
    113113        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{");
    114118      } else if (node.Symbol is Sine) {
    115119        strBuilder.Append(@"\sin \left( ");
     
    209213      } else if (node.Symbol is Exponential) {
    210214        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();
    211219      } else if (node.Symbol is Sine) {
    212220        throw new InvalidOperationException();
     
    266274      } else if (node.Symbol is Exponential) {
    267275        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(@"}");
    268280      } else if (node.Symbol is Sine) {
    269281        strBuilder.Append(@" \right) ");
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs

    r7653 r7695  
    2020#endregion
    2121
     22using System.Globalization;
    2223using System.Text;
    23 using System.Linq;
     24using HeuristicLab.Common;
    2425using HeuristicLab.Core;
    25 using HeuristicLab.Common;
     26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2627using 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;
    3228
    3329namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
     
    8480    }
    8581
    86     public string FormatOnlyExpression(ISymbolicExpressionTreeNode expressionNode)
    87     {
     82    public string FormatOnlyExpression(ISymbolicExpressionTreeNode expressionNode) {
    8883      var stringBuilder = new StringBuilder();
    8984      stringBuilder.AppendLine("  for " + CurrentIndexVariable + " = 1:1:rows");
     
    151146      } else if (symbol is Exponential) {
    152147        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(");
    153156        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    154157        stringBuilder.Append(")");
     
    283286
    284287
    285     private string LagToString(int lag)
    286     {
     288    private string LagToString(int lag) {
    287289      if (lag < 0) {
    288290        return "(" + CurrentIndexVariable + "" + lag + ")";
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/FullFunctionalExpressionGrammar.cs

    r7259 r7695  
    5555      var pow = new Power();
    5656      pow.InitialFrequency = 0.0;
     57      var square = new Square();
     58      square.InitialFrequency = 0.0;
    5759      var root = new Root();
    5860      root.InitialFrequency = 0.0;
     61      var sqrt = new SquareRoot();
     62      sqrt.InitialFrequency = 0.0;
    5963      var exp = new Exponential();
    6064      var @if = new IfThenElse();
     
    8286      laggedVariable.InitialFrequency = 0.0;
    8387
    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>() { sin, 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 };
    8690
    8791      var binaryFunctionSymbols = new List<Symbol>() { pow, root, gt, lt, variableCondition };
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Grammars/TypeCoherentExpressionGrammar.cs

    r7259 r7695  
    6565      var log = new Logarithm();
    6666      var pow = new Power();
     67      var square = new Square();
    6768      var root = new Root();
     69      var sqrt = new SquareRoot();
    6870      var exp = new Exponential();
    6971      var @if = new IfThenElse();
     
    9395      var realValuedSymbols = new GroupSymbol(RealValuedSymbolsName, new List<ISymbol>() { arithmeticSymbols, trigonometricSymbols, exponentialAndLogarithmicSymbols, terminalSymbols });
    9496
    95       var powerSymbols = new GroupSymbol(PowerFunctionsName, new List<ISymbol> { pow, root });
     97      var powerSymbols = new GroupSymbol(PowerFunctionsName, new List<ISymbol> { square, pow, sqrt, root });
    9698
    9799      var conditionSymbols = new GroupSymbol(ConditionsName, new List<ISymbol> { @if, variableCondition });
     
    111113      SetSubtreeCount(arithmeticSymbols, 2, 2);
    112114      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);
    114119      SetSubtreeCount(exponentialAndLogarithmicSymbols, 1, 1);
    115120      SetSubtreeCount(terminalSymbols, 0, 0);
     
    140145      AddAllowedChildSymbol(powerSymbols, laggedVariable, 0);
    141146      AddAllowedChildSymbol(powerSymbols, constant, 1);
     147      AddAllowedChildSymbol(square, realValuedSymbols, 0);
     148      AddAllowedChildSymbol(sqrt, realValuedSymbols, 0);
    142149
    143150      AddAllowedChildSymbol(@if, comparisonSymbols, 0);
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r7506 r7695  
    161161    <Compile Include="Symbols\Constant.cs" />
    162162    <Compile Include="Symbols\ConstantTreeNode.cs" />
     163    <Compile Include="Symbols\SquareRoot.cs" />
     164    <Compile Include="Symbols\Square.cs" />
    163165    <Compile Include="Symbols\Cosine.cs" />
    164166    <Compile Include="Symbols\Derivative.cs" />
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeInterpreter.cs

    r7506 r7695  
    133133
    134134      public const byte VariableCondition = 27;
     135
     136      public const byte Square = 28;
     137      public const byte SquareRoot = 29;
    135138    }
    136139    #endregion
     
    154157      { typeof(Average), OpCodes.Average},
    155158      { typeof(InvokeFunction), OpCodes.Call },
    156       { typeof(HeuristicLab.Problems.DataAnalysis.Symbolic.Variable), OpCodes.Variable },
     159      { typeof(Variable), OpCodes.Variable },
    157160      { typeof(LaggedVariable), OpCodes.LagVariable },
    158161      { typeof(Constant), OpCodes.Constant },
     
    163166      { typeof(Integral), OpCodes.Integral},
    164167      { typeof(Derivative), OpCodes.Derivative},
    165       { typeof(VariableCondition),OpCodes.VariableCondition}
     168      { typeof(VariableCondition),OpCodes.VariableCondition},
     169      { typeof(Square),OpCodes.Square},
     170      {typeof(SquareRoot),OpCodes.SquareRoot}
    166171    };
    167172
     
    305310            return Math.Tan(Evaluate(dataset, ref row, state));
    306311          }
     312        case OpCodes.Square: {
     313            return Math.Pow(Evaluate(dataset, ref row, state), 2);
     314          }
    307315        case OpCodes.Power: {
    308316            double x = Evaluate(dataset, ref row, state);
    309317            double y = Math.Round(Evaluate(dataset, ref row, state));
    310318            return Math.Pow(x, y);
     319          }
     320        case OpCodes.SquareRoot: {
     321            return Math.Sqrt(Evaluate(dataset, ref row, state));
    311322          }
    312323        case OpCodes.Root: {
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs

    r7259 r7695  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Diagnostics;
    2524using System.Linq;
    2625using HeuristicLab.Common;
     
    4140    private Exponential expSymbol = new Exponential();
    4241    private Root rootSymbol = new Root();
     42    private Square sqrSymbol = new Square();
     43    private SquareRoot sqrtSymbol = new SquareRoot();
    4344    private Power powSymbol = new Power();
    4445    private Sine sineSymbol = new Sine();
     
    125126    private bool IsRoot(ISymbolicExpressionTreeNode node) {
    126127      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;
    127134    }
    128135    private bool IsPower(ISymbolicExpressionTreeNode node) {
     
    201208      } else if (IsExp(original)) {
    202209        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);
    203216      } else if (IsRoot(original)) {
    204217        return SimplifyRoot(original);
    205       } else if (IsPower(original)) {
    206         return SimplifyPower(original);
    207218      } else if (IsSine(original)) {
    208219        return SimplifySine(original);
     
    348359      return MakeExp(GetSimplifiedTree(original.GetSubtree(0)));
    349360    }
     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    }
    350367
    351368    private ISymbolicExpressionTreeNode SimplifyLog(ISymbolicExpressionTreeNode original) {
     
    532549      }
    533550    }
     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
    534577    private ISymbolicExpressionTreeNode MakeLog(ISymbolicExpressionTreeNode node) {
    535578      if (IsConstant(node)) {
     
    636679      } else if (IsDivision(a) && IsConstant(b)) {
    637680        // (a1 / a2) / c => (a1 / (a2 * c))
    638         Trace.Assert(a.Subtrees.Count() == 2);
    639681        return MakeFraction(a.GetSubtree(0), MakeProduct(a.GetSubtree(1), b));
    640682      } else if (IsDivision(a) && IsDivision(b)) {
    641683        // (a1 / a2) / (b1 / b2) =>
    642         Trace.Assert(a.Subtrees.Count() == 2);
    643         Trace.Assert(b.Subtrees.Count() == 2);
    644684        return MakeFraction(MakeProduct(a.GetSubtree(0), b.GetSubtree(1)), MakeProduct(a.GetSubtree(1), b.GetSubtree(0)));
    645685      } else if (IsDivision(a)) {
    646686        // (a1 / a2) / b => (a1 / (a2 * b))
    647         Trace.Assert(a.Subtrees.Count() == 2);
    648687        return MakeFraction(a.GetSubtree(0), MakeProduct(a.GetSubtree(1), b));
    649688      } else if (IsDivision(b)) {
    650689        // a / (b1 / b2) => (a * b2) / b1
    651         Trace.Assert(b.Subtrees.Count() == 2);
    652690        return MakeFraction(MakeProduct(a, b.GetSubtree(1)), b.GetSubtree(0));
    653691      } else {
     
    774812      } else if (IsDivision(a) && IsDivision(b)) {
    775813        // (a1 / a2) * (b1 / b2) => (a1 * b1) / (a2 * b2)
    776         Trace.Assert(a.Subtrees.Count() == 2);
    777         Trace.Assert(b.Subtrees.Count() == 2);
    778814        return MakeFraction(MakeProduct(a.GetSubtree(0), b.GetSubtree(0)), MakeProduct(a.GetSubtree(1), b.GetSubtree(1)));
    779815      } else if (IsDivision(a)) {
    780816        // (a1 / a2) * b => (a1 * b) / a2
    781         Trace.Assert(a.Subtrees.Count() == 2);
    782817        return MakeFraction(MakeProduct(a.GetSubtree(0), b), a.GetSubtree(1));
    783818      } else if (IsDivision(b)) {
    784819        // a * (b1 / b2) => (b1 * a) / b2
    785         Trace.Assert(b.Subtrees.Count() == 2);
    786820        return MakeFraction(MakeProduct(b.GetSubtree(0), a), b.GetSubtree(1));
    787821      } else if (IsMultiplication(a) && IsMultiplication(b)) {
     
    912946        return MakeConstant(1.0 / ((ConstantTreeNode)x).Value);
    913947      } else if (IsDivision(x)) {
    914         Trace.Assert(x.Subtrees.Count() == 2);
    915948        return MakeFraction(x.GetSubtree(1), x.GetSubtree(0));
    916949      } else {
  • trunk/sources/HeuristicLab.Tests/HeuristicLab.Tests.csproj

    r7650 r7695  
    102102  </PropertyGroup>
    103103  <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>
    106106    </Reference>
    107107    <Reference Include="Calendar.DayView">
     
    112112      <HintPath>..\bin\Google.ProtocolBuffers-0.9.1.dll</HintPath>
    113113      <Private>True</Private>
    114     </Reference>
    115     <Reference Include="HeuristicLab.ALGLIB-3.4.0">
    116       <HintPath>..\bin\HeuristicLab.ALGLIB-3.4.0.dll</HintPath>
    117114    </Reference>
    118115    <Reference Include="HeuristicLab.LibSVM-1.6.3">
Note: See TracChangeset for help on using the changeset viewer.