Free cookie consent management tool by TermsFeed Policy Generator

Changeset 9290


Ignore:
Timestamp:
03/06/13 10:32:41 (11 years ago)
Author:
bburlacu
Message:

#2021: Removed double field from Instruction.cs, added separated double array for computing the values.

Location:
branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/Instruction.cs

    r9271 r9290  
    3232    // an optional object value (addresses for calls, argument index for arguments)
    3333    public object iArg0;
    34     // hold the value of the instruction
    35     public double value;
    3634  }
    3735}
  • branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.csproj

    r9271 r9290  
    126126      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Optimization-3.3.dll</HintPath>
    127127    </Reference>
    128     <Reference Include="HeuristicLab.Optimization.Views-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
    129       <SpecificVersion>False</SpecificVersion>
    130       <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Optimization.Views-3.3.dll</HintPath>
    131     </Reference>
    132128    <Reference Include="HeuristicLab.Parameters-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
    133129      <SpecificVersion>False</SpecificVersion>
     
    141137      <SpecificVersion>False</SpecificVersion>
    142138      <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath>
    143     </Reference>
    144     <Reference Include="HeuristicLab.Random-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
    145       <SpecificVersion>False</SpecificVersion>
    146       <HintPath>..\..\..\..\Trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath>
    147139    </Reference>
    148140    <Reference Include="System" />
     
    151143    </Reference>
    152144    <Reference Include="System.Drawing" />
    153     <Reference Include="System.Xml.Linq">
    154       <RequiredTargetFramework>3.5</RequiredTargetFramework>
    155     </Reference>
    156     <Reference Include="System.Data.DataSetExtensions">
    157       <RequiredTargetFramework>3.5</RequiredTargetFramework>
    158     </Reference>
    159     <Reference Include="System.Data" />
    160     <Reference Include="System.Xml" />
    161145  </ItemGroup>
    162146  <ItemGroup>
  • branches/HeuristicLab.DataAnalysis.Symbolic.LinearInterpreter/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs

    r9271 r9290  
    124124      }
    125125
     126      double[] values = new double[code.Length];
     127
    126128      foreach (var rowEnum in rows) {
    127129        int row = rowEnum;
    128         yield return Evaluate(dataset, ref row, code);
     130        yield return Evaluate(dataset, ref row, code, values);
    129131      }
    130132    }
    131133
    132     protected virtual double Evaluate(Dataset dataset, ref int row, Instruction[] code) {
     134    protected virtual double Evaluate(Dataset dataset, ref int row, Instruction[] code, double[] values) {
    133135      int count = 0;
    134136      for (int j = 0; j != code.Length; ++j) {
    135137        Instruction currentInstr = code[j];
    136138
     139        #region switch
    137140        switch (currentInstr.opCode) {
    138141          case OpCodes.Add: {
    139               double s = code[j - currentInstr.nArguments].value;
    140               for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
    141                 s += code[i].value;
    142               }
    143               currentInstr.value = s;
     142              double s = values[j - currentInstr.nArguments];
     143              for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
     144                s += values[i];
     145              }
     146              values[j] = s;
    144147            }
    145148            break;
    146149          case OpCodes.Sub: {
    147               double s = code[j - currentInstr.nArguments].value;
    148               for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
    149                 s -= code[i].value;
     150              double s = values[j - currentInstr.nArguments];
     151              for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
     152                s -= values[i];
    150153              }
    151154              if (currentInstr.nArguments == 1) s = -s;
    152               currentInstr.value = s;
     155              values[j] = s;
    153156            }
    154157            break;
    155158          case OpCodes.Mul: {
    156               double p = code[j - currentInstr.nArguments].value;
    157               for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
    158                 p *= code[i].value;
    159               }
    160               currentInstr.value = p;
     159              double p = values[j - currentInstr.nArguments];
     160              for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
     161                p *= values[i];
     162              }
     163              values[j] = p;
    161164            }
    162165            break;
    163166          case OpCodes.Div: {
    164               double p = code[j - currentInstr.nArguments].value;
    165               for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
    166                 p /= code[i].value;
     167              double p = values[j - currentInstr.nArguments];
     168              for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
     169                p /= values[i];
    167170              }
    168171              if (currentInstr.nArguments == 1) p = 1.0 / p;
    169               currentInstr.value = p;
     172              values[j] = p;
    170173            }
    171174            break;
    172175          case OpCodes.Average: {
    173               double sum = code[j - currentInstr.nArguments].value;
    174               for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
    175                 sum += code[i].value;
    176               }
    177               currentInstr.value = sum / currentInstr.nArguments;
     176              double sum = values[j - currentInstr.nArguments];
     177              for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
     178                sum += values[i];
     179              }
     180              values[j] = sum / currentInstr.nArguments;
    178181            }
    179182            break;
    180183          case OpCodes.Cos: {
    181               currentInstr.value = Math.Cos(code[j - 1].value);
     184              values[j] = Math.Cos(values[j - currentInstr.nArguments]);
    182185            }
    183186            break;
    184187          case OpCodes.Sin: {
    185               currentInstr.value = Math.Sin(code[j - 1].value);
     188              values[j] = Math.Sin(values[j - currentInstr.nArguments]);
    186189              break;
    187190            }
    188191          case OpCodes.Tan: {
    189               currentInstr.value = Math.Tan(code[j - 1].value);
     192              values[j] = Math.Tan(values[j - currentInstr.nArguments]);
    190193            }
    191194            break;
    192195          case OpCodes.Square: {
    193               currentInstr.value = Math.Pow(code[j - 1].value, 2);
     196              values[j] = Math.Pow(values[j - currentInstr.nArguments], 2);
    194197            }
    195198            break;
    196199          case OpCodes.Power: {
    197               double x = code[j - 2].value;
    198               double y = Math.Round(code[j - 1].value);
    199               currentInstr.value = Math.Pow(x, y);
     200              double x = values[j - currentInstr.nArguments];
     201              double y = Math.Round(values[j - currentInstr.nArguments + 1]);
     202              values[j] = Math.Pow(x, y);
    200203            }
    201204            break;
    202205          case OpCodes.SquareRoot: {
    203               currentInstr.value = Math.Sqrt(code[j - 1].value);
     206              values[j] = Math.Sqrt(values[j - currentInstr.nArguments]);
    204207            }
    205208            break;
    206209          case OpCodes.Root: {
    207               double x = code[j - 2].value;
    208               double y = Math.Round(code[j - 1].value);
    209               currentInstr.value = Math.Pow(x, 1 / y);
     210              double x = values[j - currentInstr.nArguments];
     211              double y = Math.Round(values[j - currentInstr.nArguments + 1]);
     212              values[j] = Math.Pow(x, 1 / y);
    210213            }
    211214            break;
    212215          case OpCodes.Exp: {
    213               currentInstr.value = Math.Exp(code[j - 1].value);
     216              values[j] = Math.Exp(values[j - currentInstr.nArguments]);
    214217            }
    215218            break;
    216219          case OpCodes.Log: {
    217               currentInstr.value = Math.Log(code[j - 1].value);
     220              values[j] = Math.Log(values[j - currentInstr.nArguments]);
    218221            }
    219222            break;
    220223          case OpCodes.Gamma: {
    221               currentInstr.value = double.IsNaN(code[j - 1].value) ? double.NaN : alglib.gammafunction(code[j - 1].value);
     224              values[j] = double.IsNaN(values[j - currentInstr.nArguments]) ? double.NaN : alglib.gammafunction(values[j - currentInstr.nArguments]);
    222225            }
    223226            break;
    224227          case OpCodes.Psi: {
    225               var x = code[j - 1].value;
    226               if (double.IsNaN(x)) currentInstr.value = double.NaN;
    227               else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) currentInstr.value = double.NaN;
    228               else currentInstr.value = alglib.psi(x);
     228              var x = values[j - currentInstr.nArguments];
     229              if (double.IsNaN(x)) values[j] = double.NaN;
     230              else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) values[j] = double.NaN;
     231              else values[j] = alglib.psi(x);
    229232            }
    230233            break;
    231234          case OpCodes.Dawson: {
    232               var x = code[j - 1].value;
    233               currentInstr.value = double.IsNaN(x) ? double.NaN : alglib.dawsonintegral(x);
     235              var x = values[j - currentInstr.nArguments];
     236              values[j] = double.IsNaN(x) ? double.NaN : alglib.dawsonintegral(x);
    234237            }
    235238            break;
    236239          case OpCodes.ExponentialIntegralEi: {
    237               var x = code[j - 1].value;
    238               currentInstr.value = double.IsNaN(x) ? double.NaN : alglib.exponentialintegralei(x);
     240              var x = values[j - currentInstr.nArguments];
     241              values[j] = double.IsNaN(x) ? double.NaN : alglib.exponentialintegralei(x);
    239242            }
    240243            break;
    241244          case OpCodes.SineIntegral: {
    242245              double si, ci;
    243               var x = code[j - 1].value;
    244               if (double.IsNaN(x)) currentInstr.value = double.NaN;
     246              var x = values[j - currentInstr.nArguments];
     247              if (double.IsNaN(x)) values[j] = double.NaN;
    245248              else {
    246249                alglib.sinecosineintegrals(x, out si, out ci);
    247                 currentInstr.value = si;
     250                values[j] = si;
    248251              }
    249252            }
     
    251254          case OpCodes.CosineIntegral: {
    252255              double si, ci;
    253               var x = code[j - 1].value;
    254               if (double.IsNaN(x)) currentInstr.value = double.NaN;
     256              var x = values[j - currentInstr.nArguments];
     257              if (double.IsNaN(x)) values[j] = double.NaN;
    255258              else {
    256259                alglib.sinecosineintegrals(x, out si, out ci);
    257                 currentInstr.value = ci;
     260                values[j] = ci;
    258261              }
    259262            }
     
    261264          case OpCodes.HyperbolicSineIntegral: {
    262265              double shi, chi;
    263               var x = code[j - 1].value;
    264               if (double.IsNaN(x)) currentInstr.value = double.NaN;
     266              var x = values[j - currentInstr.nArguments];
     267              if (double.IsNaN(x)) values[j] = double.NaN;
    265268              else {
    266269                alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
    267                 currentInstr.value = shi;
     270                values[j] = shi;
    268271              }
    269272            }
     
    271274          case OpCodes.HyperbolicCosineIntegral: {
    272275              double shi, chi;
    273               var x = code[j - 1].value;
    274               if (double.IsNaN(x)) currentInstr.value = double.NaN;
     276              var x = values[j - currentInstr.nArguments];
     277              if (double.IsNaN(x)) values[j] = double.NaN;
    275278              else {
    276279                alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
    277                 currentInstr.value = chi;
     280                values[j] = chi;
    278281              }
    279282            }
     
    281284          case OpCodes.FresnelCosineIntegral: {
    282285              double c = 0, s = 0;
    283               var x = code[j - 1].value;
    284               if (double.IsNaN(x)) currentInstr.value = double.NaN;
     286              var x = values[j - currentInstr.nArguments];
     287              if (double.IsNaN(x)) values[j] = double.NaN;
    285288              else {
    286289                alglib.fresnelintegral(x, ref c, ref s);
    287                 currentInstr.value = c;
     290                values[j] = c;
    288291              }
    289292            }
     
    291294          case OpCodes.FresnelSineIntegral: {
    292295              double c = 0, s = 0;
    293               var x = code[j - 1].value;
    294               if (double.IsNaN(x)) currentInstr.value = double.NaN;
     296              var x = values[j - currentInstr.nArguments];
     297              if (double.IsNaN(x)) values[j] = double.NaN;
    295298              else {
    296299                alglib.fresnelintegral(x, ref c, ref s);
    297                 currentInstr.value = s;
     300                values[j] = s;
    298301              }
    299302            }
     
    301304          case OpCodes.AiryA: {
    302305              double ai, aip, bi, bip;
    303               var x = code[j - 1].value;
    304               if (double.IsNaN(x)) currentInstr.value = double.NaN;
     306              var x = values[j - currentInstr.nArguments];
     307              if (double.IsNaN(x)) values[j] = double.NaN;
    305308              else {
    306309                alglib.airy(x, out ai, out aip, out bi, out bip);
    307                 currentInstr.value = ai;
     310                values[j] = ai;
    308311              }
    309312            }
     
    311314          case OpCodes.AiryB: {
    312315              double ai, aip, bi, bip;
    313               var x = code[j - 1].value;
    314               if (double.IsNaN(x)) currentInstr.value = double.NaN;
     316              var x = values[j - currentInstr.nArguments];
     317              if (double.IsNaN(x)) values[j] = double.NaN;
    315318              else {
    316319                alglib.airy(x, out ai, out aip, out bi, out bip);
    317                 currentInstr.value = bi;
     320                values[j] = bi;
    318321              }
    319322            }
    320323            break;
    321324          case OpCodes.Norm: {
    322               var x = code[j - 1].value;
    323               currentInstr.value = double.IsNaN(x) ? double.NaN : alglib.normaldistribution(x);
     325              var x = values[j - currentInstr.nArguments];
     326              values[j] = double.IsNaN(x) ? double.NaN : alglib.normaldistribution(x);
    324327            }
    325328            break;
    326329          case OpCodes.Erf: {
    327               var x = code[j - 1].value;
    328               currentInstr.value = double.IsNaN(x) ? double.NaN : alglib.errorfunction(x);
     330              var x = values[j - currentInstr.nArguments];
     331              values[j] = double.IsNaN(x) ? double.NaN : alglib.errorfunction(x);
    329332            }
    330333            break;
    331334          case OpCodes.Bessel: {
    332               var x = code[j - 1].value;
    333               currentInstr.value = double.IsNaN(x) ? double.NaN : alglib.besseli0(x);
     335              var x = values[j - currentInstr.nArguments];
     336              values[j] = double.IsNaN(x) ? double.NaN : alglib.besseli0(x);
    334337            }
    335338            break;
    336339          case OpCodes.IfThenElse: {
    337               double condition = code[j - currentInstr.nArguments].value;
    338               double result = condition > 0.0 ? code[j - 2].value : code[j - 1].value;
    339               currentInstr.value = result;
     340              double condition = values[j - currentInstr.nArguments];
     341              double result = condition > 0.0 ? values[j - currentInstr.nArguments] : values[j - currentInstr.nArguments + 1];
     342              values[j] = result;
    340343            }
    341344            break;
    342345          case OpCodes.AND: {
    343               double result = code[j - currentInstr.nArguments].value;
    344               for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
    345                 if (result > 0.0) result = code[i].value;
    346               }
    347               currentInstr.value = result > 0.0 ? 1.0 : -1.0;
     346              double result = values[j - currentInstr.nArguments];
     347              for (int i = j - currentInstr.nArguments + 1; i < j; i++) {
     348                if (result > 0.0) result = values[i];
     349              }
     350              values[j] = result > 0.0 ? 1.0 : -1.0;
    348351            }
    349352            break;
    350353          case OpCodes.OR: {
    351               double result = code[j - currentInstr.nArguments].value;
     354              double result = values[j - currentInstr.nArguments];
    352355              for (int i = 1; i < currentInstr.nArguments; i++) {
    353                 if (result <= 0.0) result = code[i].value; ;
    354               }
    355               currentInstr.value = result > 0.0 ? 1.0 : -1.0;
     356                if (result <= 0.0) result = values[i]; ;
     357              }
     358              values[j] = result > 0.0 ? 1.0 : -1.0;
    356359            }
    357360            break;
    358361          case OpCodes.NOT: {
    359               currentInstr.value = code[j - 1].value > 0.0 ? -1.0 : 1.0;
     362              values[j] = values[j - currentInstr.nArguments] > 0.0 ? -1.0 : 1.0;
    360363            }
    361364            break;
    362365          case OpCodes.GT: {
    363               double x = code[j - 2].value;
    364               double y = code[j - 1].value;
    365               currentInstr.value = x > y ? 1.0 : -1.0;
     366              double x = values[j - currentInstr.nArguments];
     367              double y = values[j - currentInstr.nArguments + 1];
     368              values[j] = x > y ? 1.0 : -1.0;
    366369            }
    367370            break;
    368371          case OpCodes.LT: {
    369               double x = code[j - 2].value;
    370               double y = code[j - 1].value;
    371               currentInstr.value = x < y ? 1.0 : -1.0;
     372              double x = values[j - currentInstr.nArguments];
     373              double y = values[j - currentInstr.nArguments + 1];
     374              values[j] = x < y ? 1.0 : -1.0;
    372375            }
    373376            break;
     
    393396            }
    394397          case OpCodes.Variable: {
    395               if (row < 0 || row >= dataset.Rows) currentInstr.value = double.NaN;
     398              if (row < 0 || row >= dataset.Rows) values[j] = double.NaN;
    396399              else {
    397400                var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
    398                 currentInstr.value = ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight;
     401                values[j] = ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight;
    399402              }
    400403            }
     
    403406              var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode;
    404407              int actualRow = row + laggedVariableTreeNode.Lag;
    405               if (actualRow < 0 || actualRow >= dataset.Rows) currentInstr.value = double.NaN;
    406               else {
    407                 currentInstr.value = ((IList<double>)currentInstr.iArg0)[actualRow] * laggedVariableTreeNode.Weight;
     408              if (actualRow < 0 || actualRow >= dataset.Rows) values[j] = double.NaN;
     409              else {
     410                values[j] = ((IList<double>)currentInstr.iArg0)[actualRow] * laggedVariableTreeNode.Weight;
    408411              }
    409412            }
     
    411414          case OpCodes.Constant: {
    412415              var constTreeNode = (ConstantTreeNode)currentInstr.dynamicNode;
    413               currentInstr.value = constTreeNode.Value;
     416              values[j] = constTreeNode.Value;
    414417            }
    415418            break;
     
    418421          //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function
    419422          case OpCodes.VariableCondition: {
    420               if (row < 0 || row >= dataset.Rows) currentInstr.value = double.NaN;
     423              if (row < 0 || row >= dataset.Rows) values[j] = double.NaN;
    421424              else {
    422425                var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode;
     
    425428                double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x));
    426429
    427                 double trueBranch = code[j - currentInstr.nArguments].value;
    428                 double falseBranch = code[j - currentInstr.nArguments + 1].value;
    429 
    430                 currentInstr.value = trueBranch * p + falseBranch * (1 - p);
     430                double trueBranch = values[j - currentInstr.nArguments];
     431                double falseBranch = values[j - currentInstr.nArguments + 1];
     432
     433                values[j] = trueBranch * p + falseBranch * (1 - p);
    431434              }
    432435            }
     
    435438            throw new NotSupportedException();
    436439        }
     440        #endregion
    437441
    438442        // book-keeping in order to keep all the values correct
     
    444448        if (count > narg) {
    445449          for (int i = 1; i <= count - narg; ++i)
    446             code[j - i].value = code[j - i - narg].value;
     450            values[j - i] = values[j - i - narg];
    447451        }
    448452
     
    450454      }
    451455
    452       return code[code.Length - 1].value;
     456      return values[values.Length - 1];
    453457    }
    454458  }
Note: See TracChangeset for help on using the changeset viewer.