Changeset 2226


Ignore:
Timestamp:
08/03/09 15:18:36 (12 years ago)
Author:
gkronber
Message:
  • Added extension method IsAlmost to type double
  • changed evaluation semantics of boolean operations in HL3TreeEvaluator

(#713)

Location:
trunk/sources
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/HL3TreeEvaluator.cs

    r2222 r2226  
    2222using System;
    2323using System.Diagnostics;
     24using HeuristicLab.Modeling; // double.IsAlmost extension
    2425
    2526namespace HeuristicLab.GP.StructureIdentification {
     
    2930  /// </summary>
    3031  public class HL3TreeEvaluator : TreeEvaluatorBase {
    31 
    32 
    3332    protected override double EvaluateBakedCode() {
    3433      Instr currInstr = codeArr[PC++];
     
    3635        case EvaluatorSymbolTable.VARIABLE: {
    3736            int row = sampleIndex + currInstr.i_arg1;
    38             if (row < 0 || row >= dataset.Rows) return double.NaN;
     37            if (row < 0 || row >= dataset.Rows) throw new InvalidOperationException("Out of range access to dataset row: " + row);
    3938            else return currInstr.d_arg0 * dataset.GetValue(row, currInstr.i_arg0);
    4039          }
     
    4443        case EvaluatorSymbolTable.DIFFERENTIAL: {
    4544            int row = sampleIndex + currInstr.i_arg1;
    46             if (row < 0 || row >= dataset.Rows) return double.NaN;
     45            if (row < 0 || row >= dataset.Rows) throw new InvalidOperationException("Out of range access to dataset row: " + row);
    4746            else if (row < 1) return 0.0;
    4847            else {
     
    117116            return Math.Tan(EvaluateBakedCode());
    118117          }
    119         case EvaluatorSymbolTable.AND: { // only defined for inputs 1 and 0
     118        case EvaluatorSymbolTable.AND: {
    120119            double result = EvaluateBakedCode();
    121120            for (int i = 1; i < currInstr.arity; i++) {
    122               if (result == 0.0) SkipBakedCode();
     121              if (result < 0.0) SkipBakedCode();
    123122              else {
    124123                result = EvaluateBakedCode();
    125124              }
    126               Debug.Assert(result == 0.0 || result == 1.0);
    127125            }
    128             return result;
     126            return Math.Sign(result);
    129127          }
    130128        case EvaluatorSymbolTable.EQU: {
    131129            double x = EvaluateBakedCode();
    132130            double y = EvaluateBakedCode();
    133             if (Math.Abs(x - y) < EPSILON) return 1.0; else return 0.0;
     131            if (x.IsAlmost(y)) return 1.0; else return -1.0;
    134132          }
    135133        case EvaluatorSymbolTable.GT: {
     
    137135            double y = EvaluateBakedCode();
    138136            if (x > y) return 1.0;
    139             else return 0.0;
     137            else return -1.0;
    140138          }
    141         case EvaluatorSymbolTable.IFTE: { // only defined for condition 0 or 1
     139        case EvaluatorSymbolTable.IFTE: {
    142140            double condition = EvaluateBakedCode();
    143             Debug.Assert(condition == 0.0 || condition == 1.0);
    144141            double result;
    145             if (condition == 0.0) {
     142            if (condition < 0.0) {
    146143              result = EvaluateBakedCode(); SkipBakedCode();
    147144            } else {
     
    154151            double y = EvaluateBakedCode();
    155152            if (x < y) return 1.0;
    156             else return 0.0;
     153            else return -1.0;
    157154          }
    158         case EvaluatorSymbolTable.NOT: { // only defined for inputs 0 or 1
    159             double result = EvaluateBakedCode();
    160             Debug.Assert(result == 0.0 || result == 1.0);
    161             return Math.Abs(result - 1.0);
     155        case EvaluatorSymbolTable.NOT: {
     156            return -EvaluateBakedCode();
    162157          }
    163         case EvaluatorSymbolTable.OR: { // only defined for inputs 0 or 1
     158        case EvaluatorSymbolTable.OR: {
    164159            double result = EvaluateBakedCode();
    165160            for (int i = 1; i < currInstr.arity; i++) {
    166               if (result > 0.0) SkipBakedCode();
     161              if (result >= 0.0) SkipBakedCode();
    167162              else {
    168163                result = EvaluateBakedCode();
    169                 Debug.Assert(result == 0.0 || result == 1.0);
    170164              }
    171165            }
    172             return result;
     166            return Math.Sign(result);
    173167          }
    174         case EvaluatorSymbolTable.XOR: { // only defined for inputs 0 or 1
     168        case EvaluatorSymbolTable.XOR: {
    175169            double x = EvaluateBakedCode();
    176170            double y = EvaluateBakedCode();
    177             return Math.Abs(x - y);
     171            if (x > y) {
     172              double tmp = x;
     173              x = y;
     174              y = tmp;
     175            }
     176            // invariant y >= x
     177            if (y < 0.0 || x > 0.0) return -1.0;
     178            else return 1.0;
    178179          }
    179180        default: {
  • trunk/sources/HeuristicLab.LinearRegression/3.2/LinearRegressionOperator.cs

    r2222 r2226  
    2626using HeuristicLab.Data;
    2727using HeuristicLab.DataAnalysis;
     28using HeuristicLab.Modeling;
    2829using HeuristicLab.GP;
    2930using HeuristicLab.GP.StructureIdentification;
     
    5455      double[] coefficients = CalculateCoefficients(inputMatrix, targetVector);
    5556      IFunctionTree tree = CreateModel(coefficients, allowedColumns.Select(i => dataset.GetVariableName(i)).ToList());
    56      
     57
    5758      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("LinearRegressionModel"), new GeneticProgrammingModel(tree)));
    5859      return null;
    59     }
    60 
    61     private bool IsAlmost(double x, double y) {
    62       return Math.Abs(x - y) < 1.0E-12;
    6360    }
    6461
     
    125122      for (int i = 0; i < dataset.Columns; i++) {
    126123        if (i == targetVariable) continue;
    127         if (!IsAlmost(dataset.GetMinimum(i, start, end), 0.0) ||
    128             !IsAlmost(dataset.GetMaximum(i, start, end), 0.0))
     124        if (!dataset.GetMinimum(i, start, end).IsAlmost(0.0) ||
     125            !dataset.GetMaximum(i, start, end).IsAlmost(0.0))
    129126          allowedColumns.Add(i);
    130127      }
  • trunk/sources/HeuristicLab.Modeling/3.2/HeuristicLab.Modeling-3.2.csproj

    r2041 r2226  
    8383  <ItemGroup>
    8484    <Compile Include="ClassificationProblemInjector.cs" />
     85    <Compile Include="DoubleExtensions.cs" />
    8586    <Compile Include="VariableImpactCalculatorBase.cs" />
    8687    <Compile Include="VariableEvaluationImpactCalculator.cs" />
  • trunk/sources/HeuristicLab.Modeling/3.2/SimpleEvaluatorBase.cs

    r2136 r2226  
    3131
    3232    public abstract double Evaluate(double[,] values);
    33 
    34     protected static bool IsAlmost(double x, double y) {
    35       return Math.Abs(x - y) < 1.0E-12;
    36     }
    3733  }
    3834}
  • trunk/sources/HeuristicLab.Modeling/3.2/SimpleMeanAbsolutePercentageOfRangeErrorEvaluator.cs

    r2136 r2226  
    5353      double range = Statistics.Range(originalValues);
    5454      if (double.IsInfinity(range)) throw new ArgumentException("Range of elements in values is infinity");
    55       if (IsAlmost(range, 0.0)) throw new ArgumentException("Range of elements in values is zero");
     55      if (range.IsAlmost(0.0)) throw new ArgumentException("Range of elements in values is zero");
    5656
    5757      for (int i = 0; i < values.GetLength(0); i++) {
  • trunk/sources/HeuristicLab.Modeling/3.2/SimpleVarianceAccountedForEvaluator.cs

    r2136 r2226  
    6969      double errorsVariance = Statistics.Variance(errors);
    7070      double originalsVariance = Statistics.Variance(originalTargetVariableValues);
    71       if (IsAlmost(originalsVariance, 0.0))
    72         if (IsAlmost(errorsVariance, 0.0)) {
     71      if (originalsVariance.IsAlmost(0.0))
     72        if (errorsVariance.IsAlmost(0.0)) {
    7373          return 1.0;
    7474        } else {
  • trunk/sources/HeuristicLab.Modeling/3.2/VariableEvaluationImpactCalculator.cs

    r2165 r2226  
    6464    protected override double[] PostProcessImpacts(double[] impacts) {
    6565      double mseSum = impacts.Sum();
    66       if (IsAlmost(mseSum, 0.0)) mseSum = 1.0;
     66      if (mseSum.IsAlmost(0.0)) mseSum = 1.0;
    6767      for (int i = 0; i < impacts.Length; i++) {
    6868        impacts[i] = impacts[i] / mseSum;
Note: See TracChangeset for help on using the changeset viewer.