Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17365


Ignore:
Timestamp:
11/22/19 13:23:49 (5 years ago)
Author:
pfleck
Message:

#3040 Added explicit vector types to avoid type-missmatches when representing vectors as IList<T>, List<T> or IReadOnlyList<T>.

Location:
branches/3040_VectorBasedGP
Files:
6 added
5 edited

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs

    r17364 r17365  
    142142        else if (iter.Current[i] is string s)
    143143          transposed.Add(new List<string>() { s });
    144         else if (iter.Current[i] is IReadOnlyList<double> dv)
    145           transposed.Add(new List<IReadOnlyList<double>>() { dv });
     144        else if (iter.Current[i] is DoubleVector dv)
     145          transposed.Add(new List<DoubleVector>() { dv });
     146        else if (iter.Current[i] is StringVector sv)
     147          transposed.Add(new List<StringVector>() { sv });
     148        else if (iter.Current[i] is DateTimeVector dtv)
     149          transposed.Add(new List<DateTimeVector>() { dtv });
    146150        else throw new NotSupportedException(string.Format("Variable {0} has type {1}. This is not supported when converting from row-wise data.", vnames[i], iter.Current[i].GetType()));
    147151      }
     
    221225    }
    222226    public IEnumerable<string> DoubleVectorVariables {
    223       get { return variableValues.Where(p => p.Value is IList<IReadOnlyList<double>>).Select(p => p.Key); }
     227      get { return variableValues.Where(p => p.Value is IList<DoubleVector>).Select(p => p.Key); }
     228    }
     229    public IEnumerable<string> StringVectorVariables {
     230      get { return variableValues.Where(p => p.Value is IList<StringVector>).Select(p => p.Key); }
     231    }
     232    public IEnumerable<string> DateTimeVectorVariables {
     233      get { return variableValues.Where(p => p.Value is IList<DateTimeVector>).Select(p => p.Key); }
    224234    }
    225235
     
    233243      return GetValues<DateTime>(variableName);
    234244    }
    235     public IEnumerable<IReadOnlyList<double>> GetDoubleVectorValues(string variableName) {
    236       return GetValues<IReadOnlyList<double>>(variableName);
     245    public IEnumerable<DoubleVector> GetDoubleVectorValues(string variableName) {
     246      return GetValues<DoubleVector>(variableName);
     247    }
     248    public IEnumerable<StringVector> GetStringVectorValues(string variableName) {
     249      return GetValues<StringVector>(variableName);
     250    }
     251    public IEnumerable<DateTimeVector> GetDateTimeVectorValues(string variableName) {
     252      return GetValues<DateTimeVector>(variableName);
    237253    }
    238254
     
    273289    }
    274290
    275     public IReadOnlyList<double> GetDoubleVectorValue(string variableName, int row) {
    276       var values = GetValues<IReadOnlyList<double>>(variableName);
    277       return values[row];
    278     }
    279     public IEnumerable<IReadOnlyList<double>> GetDoubleVectorValues(string variableName, IEnumerable<int> rows) {
    280       return GetValues<IReadOnlyList<double>>(variableName, rows);
    281     }
    282     public ReadOnlyCollection<IReadOnlyList<double>> GetReadOnlyDoubleVectorValues(string variableName) {
    283       var values = GetValues<IReadOnlyList<double>>(variableName);
    284       return new ReadOnlyCollection<IReadOnlyList<double>>(values);
     291    public DoubleVector GetDoubleVectorValue(string variableName, int row) {
     292      var values = GetValues<DoubleVector>(variableName);
     293      return values[row];
     294    }
     295    public IEnumerable<DoubleVector> GetDoubleVectorValues(string variableName, IEnumerable<int> rows) {
     296      return GetValues<DoubleVector>(variableName, rows);
     297    }
     298    public ReadOnlyCollection<DoubleVector> GetReadOnlyDoubleVectorValues(string variableName) {
     299      var values = GetValues<DoubleVector>(variableName);
     300      return new ReadOnlyCollection<DoubleVector>(values);
     301    }
     302
     303    public StringVector GetStringVectorValue(string variableName, int row) {
     304      var values = GetValues<StringVector>(variableName);
     305      return values[row];
     306    }
     307    public IEnumerable<StringVector> GetStringVectorValues(string variableName, IEnumerable<int> rows) {
     308      return GetValues<StringVector>(variableName, rows);
     309    }
     310    public ReadOnlyCollection<StringVector> GetReadOnlyStringVectorValues(string variableName) {
     311      var values = GetValues<StringVector>(variableName);
     312      return new ReadOnlyCollection<StringVector>(values);
     313    }
     314
     315    public DateTimeVector GetDateTimeVectorValue(string variableName, int row) {
     316      var values = GetValues<DateTimeVector>(variableName);
     317      return values[row];
     318    }
     319    public IEnumerable<DateTimeVector> GetDateTimeVectorValues(string variableName, IEnumerable<int> rows) {
     320      return GetValues<DateTimeVector>(variableName, rows);
     321    }
     322    public ReadOnlyCollection<DateTimeVector> GetReadOnlyDateTimeVectorValues(string variableName) {
     323      var values = GetValues<DateTimeVector>(variableName);
     324      return new ReadOnlyCollection<DateTimeVector>(values);
    285325    }
    286326
     
    316356    }
    317357    protected static bool IsAllowedType(Type type) {
    318       return type == typeof(double) || type == typeof(string) || type == typeof(DateTime) || type == typeof(IReadOnlyList<double>);
     358      return type == typeof(double) || type == typeof(string) || type == typeof(DateTime)
     359             || type == typeof(DoubleVector) || type == typeof(StringVector) || type == typeof(DateTimeVector);
    319360    }
    320361
     
    361402      if (dateTimeValues != null) return new List<DateTime>(dateTimeValues);
    362403
    363       var doubleVectorValues = values as IList<IReadOnlyList<double>>;
    364       if (doubleVectorValues != null) return doubleVectorValues.Select(x => new List<double>(x)).Cast<IReadOnlyList<double>>().ToList();
     404      var doubleVectorValues = values as IList<DoubleVector>;
     405      if (doubleVectorValues != null) return doubleVectorValues.Select(x => new DoubleVector(x)).ToList();
     406
     407      var stringVectorValues = values as IList<StringVector>;
     408      if (stringVectorValues != null) return stringVectorValues.Select(x => new StringVector(x)).ToList();
     409
     410      var dateTimeVectorValues = values as IList<DateTimeVector>;
     411      if (dateTimeVectorValues != null) return dateTimeVectorValues.Select(x => new DateTimeVector(x)).ToList();
    365412
    366413      throw new ArgumentException(string.Format("Unsupported variable type {0}.", GetElementType(values)));
     
    403450    string IStringConvertibleMatrix.GetValue(int rowIndex, int columnIndex) {
    404451      var value = variableValues[variableNames[columnIndex]][rowIndex];
    405       if (value is IReadOnlyList<double> doubleVector)
    406         return $"[{string.Join(", ", doubleVector)}]";
    407       return variableValues[variableNames[columnIndex]][rowIndex].ToString();
     452      if (value is IVector vector)
     453        return $"[{string.Join(", ", vector.Cast<object>())}]";
     454      return value.ToString();
    408455    }
    409456    bool IStringConvertibleMatrix.SetValue(string value, int rowIndex, int columnIndex) {
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis/3.4/HeuristicLab.Problems.DataAnalysis-3.4.csproj

    r16788 r17365  
    167167    <Compile Include="Implementation\Transformations\ShiftToRangeTransformation.cs" />
    168168    <Compile Include="Implementation\Transformations\Transformation.cs" />
     169    <Compile Include="Implementation\Vectors\DateTimeVector.cs" />
     170    <Compile Include="Implementation\Vectors\StringVector.cs" />
     171    <Compile Include="Implementation\Vectors\DoubleVector.cs" />
     172    <Compile Include="Implementation\Vectors\Vector.cs" />
    169173    <Compile Include="Interfaces\Classification\IClassificationEnsembleModel.cs">
    170174      <SubType>Code</SubType>
     
    179183    <Compile Include="Interfaces\ITransformation.cs" />
    180184    <Compile Include="Interfaces\ITransformationMapper.cs" />
     185    <Compile Include="Interfaces\IVector.cs" />
    181186    <Compile Include="Interfaces\Regression\IConfidenceRegressionModel.cs" />
    182187    <Compile Include="Interfaces\Regression\IConfidenceRegressionSolution.cs" />
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs

    r17364 r17365  
    162162
    163163      var variables = dataset.VariableNames.Where(variable =>
    164         dataset.VariableHasType<double>(variable) || dataset.VariableHasType<string>(variable) || dataset.VariableHasType<IReadOnlyList<double>>(variable));
     164        dataset.VariableHasType<double>(variable) || dataset.VariableHasType<string>(variable) || dataset.VariableHasType<DoubleVector> (variable));
    165165      var inputVariables = new CheckedItemList<StringValue>(variables.Select(x => new StringValue(x).AsReadOnly()));
    166166      foreach (StringValue x in inputVariables)
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/IDataset.cs

    r17364 r17365  
    3535    IEnumerable<string> DateTimeVariables { get; }
    3636    IEnumerable<string> DoubleVectorVariables { get; }
     37    IEnumerable<string> StringVectorVariables { get; }
     38    IEnumerable<string> DateTimeVectorVariables { get; }
    3739
    3840    bool ContainsVariable(string variablename);
     
    5456    ReadOnlyCollection<DateTime> GetReadOnlyDateTimeValues(string variableName);
    5557
    56     IReadOnlyList<double> GetDoubleVectorValue(string variableName, int row);
    57     IEnumerable<IReadOnlyList<double>> GetDoubleVectorValues(string variableName);
    58     IEnumerable<IReadOnlyList<double>> GetDoubleVectorValues(string variableName, IEnumerable<int> rows);
    59     ReadOnlyCollection<IReadOnlyList<double>> GetReadOnlyDoubleVectorValues(string variableName);
     58    DoubleVector GetDoubleVectorValue(string variableName, int row);
     59    IEnumerable<DoubleVector> GetDoubleVectorValues(string variableName);
     60    IEnumerable<DoubleVector> GetDoubleVectorValues(string variableName, IEnumerable<int> rows);
     61    ReadOnlyCollection<DoubleVector> GetReadOnlyDoubleVectorValues(string variableName);
     62
     63    StringVector GetStringVectorValue(string variableName, int row);
     64    IEnumerable<StringVector> GetStringVectorValues(string variableName);
     65    IEnumerable<StringVector> GetStringVectorValues(string variableName, IEnumerable<int> rows);
     66    ReadOnlyCollection<StringVector> GetReadOnlyStringVectorValues(string variableName);
     67
     68    DateTimeVector GetDateTimeVectorValue(string variableName, int row);
     69    IEnumerable<DateTimeVector> GetDateTimeVectorValues(string variableName);
     70    IEnumerable<DateTimeVector> GetDateTimeVectorValues(string variableName, IEnumerable<int> rows);
     71    ReadOnlyCollection<DateTimeVector> GetReadOnlyDateTimeVectorValues(string variableName);
    6072  }
    6173}
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.Instances.DataAnalysis/3.3/Regression/VectorData/VectorDataTestOne.cs

    r17364 r17365  
    2424using System.Collections.Generic;
    2525using System.Linq;
    26 using System.Runtime.Remoting.Messaging;
    2726using HeuristicLab.Core;
     27using HeuristicLab.Problems.DataAnalysis;
    2828using HeuristicLab.Random;
    2929
     
    5757
    5858      double x1, x2, x3;
    59       IReadOnlyList<double> v1, v2;
     59      DoubleVector v1, v2;
    6060      double y;
    6161
     
    6363      var x2Column = new List<double>(100);
    6464      var x3Column = new List<double>(100);
    65       var v1Column = new List<IReadOnlyList<double>>(100);
    66       var v2Column = new List<IReadOnlyList<double>>(100);
     65      var v1Column = new List<DoubleVector>(100);
     66      var v2Column = new List<DoubleVector>(100);
    6767      var yColumn = new List<double>(100);
    6868
     
    9191      return (max - min) * rand.NextDouble() + min;
    9292    }
    93     private static double[] GetRandomDoubleVector(double min, double max, int length, IRandom rand) {
     93    private static DoubleVector GetRandomDoubleVector(double min, double max, int length, IRandom rand) {
    9494      var values = new double[length];
    9595      for (int i = 0; i < values.Length; i++) {
    9696        values[i] = GetRandomDouble(min, max, rand);
    9797      }
    98       return values;
     98      return new DoubleVector(values);
    9999    }
    100100  }
Note: See TracChangeset for help on using the changeset viewer.