Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/02/20 15:04:46 (4 years ago)
Author:
pfleck
Message:

#3040 Added type coherent vector grammar to enforce that the root symbol is a scalar.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeVectorInterpreter.cs

    r17460 r17463  
    105105        int row = rowEnum;
    106106        var result = Evaluate(dataset, ref row, state);
    107         if (!result.IsScalar)
    108           throw new InvalidOperationException("Result of the tree is not a scalar.");
    109         yield return result.Scalar;
     107        if (result.IsScalar)
     108          yield return result.Scalar;
     109        else
     110          yield return double.NaN;
     111        //if (!result.IsScalar)
     112        //  throw new InvalidOperationException("Result of the tree is not a scalar.");
     113        //yield return result.Scalar;
    110114        state.Reset();
    111115      }
     
    148152
    149153      public DoubleVector Vector { get; }
    150       public bool IsVector => !(Vector.Count == 1 && double.IsNaN(Vector[0]));
     154      public bool IsVector => Vector != null;
     155      //public bool IsVector => !(Vector.Count == 1 && double.IsNaN(Vector[0]));
    151156
    152157      public bool IsNaN => !IsScalar && !IsVector;
     
    154159      public EvaluationResult(double scalar) {
    155160        Scalar = scalar;
    156         Vector = NaNVector;
     161        //Vector = NaNVector;
     162        Vector = null;
    157163      }
    158164      public EvaluationResult(DoubleVector vector) {
     165        if (vector == null) throw new ArgumentNullException(nameof(vector));
    159166        Vector = vector;
    160167        Scalar = double.NaN;
     
    168175
    169176      public static readonly EvaluationResult NaN = new EvaluationResult(double.NaN);
    170       private static readonly DoubleVector NaNVector = DoubleVector.Build.Dense(1, double.NaN);
     177      //private static readonly DoubleVector NaNVector = DoubleVector.Build.Dense(1, double.NaN);
    171178    }
    172179
     
    180187      if (lhs.IsVector && rhs.IsScalar && vsFunc != null) return new EvaluationResult(vsFunc(lhs.Vector, rhs.Scalar));
    181188      if (lhs.IsVector && rhs.IsVector && vvFunc != null) return new EvaluationResult(vvFunc(lhs.Vector, rhs.Vector));
    182       throw new NotSupportedException($"Unsupported combination of argument types: ({lhs}) / ({rhs})");
     189      return EvaluationResult.NaN;
     190      //throw new NotSupportedException($"Unsupported combination of argument types: ({lhs}) / ({rhs})");
    183191    }
    184192
     
    188196      if (val.IsScalar && sFunc != null) return new EvaluationResult(sFunc(val.Scalar));
    189197      if (val.IsVector && vFunc != null) return new EvaluationResult(vFunc(val.Vector));
    190       throw new NotSupportedException($"Unsupported argument type ({val})");
     198      return EvaluationResult.NaN;
     199      //throw new NotSupportedException($"Unsupported argument type ({val})");
    191200    }
    192201    private static EvaluationResult AggregateApply(EvaluationResult val,
     
    195204      if (val.IsScalar && sFunc != null) return new EvaluationResult(sFunc(val.Scalar));
    196205      if (val.IsVector && vFunc != null) return new EvaluationResult(vFunc(val.Vector));
    197       throw new NotSupportedException($"Unsupported argument type ({val})");
     206      return EvaluationResult.NaN;
     207      //throw new NotSupportedException($"Unsupported argument type ({val})");
    198208    }
    199209
     
    335345              v => v.Mean());
    336346          }
     347        case OpCodes.StandardDeviation: {
     348            var cur = Evaluate(dataset, ref row, state);
     349            return AggregateApply(cur,
     350              s => 0,
     351              v => v.Count > 1 ? Statistics.StandardDeviation(v) : 0);
     352          }
    337353        case OpCodes.Variable: {
    338354            if (row < 0 || row >= dataset.Rows) return EvaluationResult.NaN;
Note: See TracChangeset for help on using the changeset viewer.