Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17452


Ignore:
Timestamp:
02/26/20 09:32:36 (5 years ago)
Author:
pfleck
Message:

#3040 Improved Persistence for Vectors (removed the generic transformer and used the existing array transformer instead).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.MathNet.Numerics/VectorTransformer.cs

    r17449 r17452  
    3535  #region Vector Transformer
    3636  public abstract class VectorTransformer<T> : BoxTransformer<Vector<T>> where T : struct, IEquatable<T>, IFormattable {
     37
    3738    public override bool CanTransformType(Type type) {
    3839      return typeof(Vector<T>).IsAssignableFrom(type);
    3940    }
     41
    4042    public override Box CreateBox(object o, Mapper mapper) {
    4143      var box = base.CreateBox(o, mapper);
     
    4345      return box;
    4446    }
     47
    4548    protected override void Populate(Box box, Vector<T> vector, Mapper mapper) {
    4649      var storageId = mapper.GetBoxId(vector.Storage);
    4750      box.Value.ULong = storageId;
    4851    }
     52
    4953    protected override Vector<T> Extract(Box box, Type type, Mapper mapper) {
    5054      var storageId = (uint)box.Value.ULong;
     
    5357    }
    5458  }
     59
     60  [Transformer("76A21F63-36B4-4073-ABBC-56623ECBF42E", 400)]
     61  public class DoubleVectorTransformer : VectorTransformer<double> { }
    5562
    5663  [Transformer("78276F91-D075-4472-B81B-3A736184CC22", 400)]
     
    6269  [Transformer("2D978104-A174-4AE2-AEA1-20FD50CADF0D", 400)]
    6370  public class Complex32VectorTransformer : VectorTransformer<Complex32> { }
    64 
    65   [Transformer("76A21F63-36B4-4073-ABBC-56623ECBF42E", 400)]
    66   public class DoubleVectorTransformer : VectorTransformer<double> { }
    6771  #endregion
    6872
    6973  #region Dense Vector Storage Transformer
    70   [Transformer("CA28BA16-2C95-4270-9A04-3502D492F070", 200)]
    71   public class DenseDoubleVectorStorageTransformer : BoxTransformer<DenseVectorStorage<double>> {
    72     public override Box CreateBox(object o, Mapper mapper) {
    73       var box = base.CreateBox(o, mapper);
    74       box.Values = new RepeatedValueBox() {
    75         Doubles = new RepeatedDoubleBox()
    76       };
    77       return box;
    78     }
    79     protected override void Populate(Box box, DenseVectorStorage<double> storage, Mapper mapper) {
    80       box.Values.Doubles.Values.AddRange(storage.Data);
    81     }
    82     protected override DenseVectorStorage<double> Extract(Box box, Type type, Mapper mapper) {
    83       var data = box.Values.Doubles.Values;
    84       return DenseVectorStorage<double>.OfEnumerable(data);
    85     }
    86   }
    87   #endregion
     74  public abstract class DenseVectorStorageTransformer<T> : BoxTransformer<DenseVectorStorage<T>> where T : struct, IEquatable<T>, IFormattable {
    8875
    89   #region Sparse Vector Storage Transformer
    90   [Transformer("25FDFB3B-5849-44F6-81AC-3D16CB093ADE", 200)]
    91   public class SparseDoubleVectorStorageTransformer : BoxTransformer<SparseVectorStorage<double>> {
    92     public override Box CreateBox(object o, Mapper mapper) {
    93       var box = base.CreateBox(o, mapper);
    94       box.Value = new ScalarValueBox();
    95       box.Values = new RepeatedValueBox() {
    96         Doubles = new RepeatedDoubleBox(),
    97         Ints = new RepeatedIntBox()
    98       };
    99       return box;
    100     }
    101     protected override void Populate(Box box, SparseVectorStorage<double> storage, Mapper mapper) {
    102       box.Value.Long = storage.ValueCount;
    103       box.Values.Doubles.Values.AddRange(storage.Values);
    104       box.Values.Ints.Values.AddRange(storage.Indices);
    105     }
    106     protected override SparseVectorStorage<double> Extract(Box box, Type type, Mapper mapper) {
    107       int valueCount = (int)box.Value.Long;
    108       var values = box.Values.Doubles.Values;
    109       var indices = box.Values.Ints.Values;
    110       var data = indices.Zip(values, Tuple.Create);
    111       return SparseVectorStorage<double>.OfIndexedEnumerable(valueCount, data);
    112     }
    113   }
    114   #endregion
    115 
    116 
    117 
    118   [Transformer("45EFC879-241D-404F-BCC1-341A41F6AB90", 1000)]
    119   public class DataContractTransformer : BoxTransformer<object> {
    120     private readonly DataContractSerializer serializer = new DataContractSerializer(typeof(DenseVectorStorage<>));
    121     public override bool CanTransformType(Type type) {
    122       return Attribute.GetCustomAttribute(type, typeof(SerializableAttribute)) != null &&
    123              Attribute.GetCustomAttribute(type, typeof(DataContractAttribute)) != null;
    124     }
    12576    public override Box CreateBox(object o, Mapper mapper) {
    12677      var box = base.CreateBox(o, mapper);
     
    12879      return box;
    12980    }
    130     protected override void Populate(Box box, object o, Mapper mapper) {
    131       using (var stream = new MemoryStream()) {
    132         serializer.WriteObject(stream, o);
    133         var data = stream.ToArray();
    134         box.Value.Bytes = ByteString.CopyFrom(data);
    135       }
     81
     82    protected override void Populate(Box box, DenseVectorStorage<T> storage, Mapper mapper) {
     83      var dataId = mapper.GetBoxId(storage.Data);
     84      box.Value.ULong = dataId;
    13685    }
    137     protected override object Extract(Box box, Type type, Mapper mapper) {
    138       var data = box.Value.Bytes.ToByteArray();
    139       using (var stream = new MemoryStream()) {
    140         stream.Write(data, 0, data.Length);
    141         stream.Seek(0, SeekOrigin.Begin);
    142         return serializer.ReadObject(stream);
    143       }
     86
     87    protected override DenseVectorStorage<T> Extract(Box box, Type type, Mapper mapper) {
     88      var dataId = (uint)box.Value.ULong;
     89      var data = (T[])mapper.GetObject(dataId);
     90      return DenseVectorStorage<T>.OfEnumerable(data);
     91    }
     92
     93    public override void FillFromBox(object obj, Box box, Mapper mapper) {
     94      var storage = (DenseVectorStorage<T>)obj;
     95      var dataId = (uint)box.Value.ULong;
     96      var data = (T[])mapper.GetObject(dataId);
     97      Array.Copy(data, storage.Data, data.Length);
    14498    }
    14599  }
     100
     101  [Transformer("24705F16-FDB8-487A-ABEA-F69D58B50D60", 200)]
     102  public class DoubleDenseVectorStorageTransform : DenseVectorStorageTransformer<double> { }
     103
     104  [Transformer("87FBC62F-E8A0-4C50-9289-CAD2B7F448E4", 200)]
     105  public class SingleDenseVectorStorageTransform : DenseVectorStorageTransformer<float> { }
     106
     107  [Transformer("4391F230-2C0C-4876-B2EF-DE43197F4557", 200)]
     108  public class ComplexDenseVectorStorageTransform : DenseVectorStorageTransformer<Complex> { }
     109
     110  [Transformer("6E07F8A9-DF19-4A2F-B7A3-209917B58355", 200)]
     111  public class Complex32DenseVectorStorageTransform : DenseVectorStorageTransformer<Complex32> { }
     112  #endregion
     113
     114  #region Sparse Vector Storage Transformer
     115  public abstract class SparseVectorStorageTransformer<T> : BoxTransformer<SparseVectorStorage<T>> where T : struct, IEquatable<T>, IFormattable {
     116
     117    public override Box CreateBox(object o, Mapper mapper) {
     118      var box = base.CreateBox(o, mapper);
     119      box.Values = new RepeatedValueBox() {
     120        UInts = new RepeatedUIntBox()
     121      };
     122      return box;
     123    }
     124
     125    protected override void Populate(Box box, SparseVectorStorage<T> storage, Mapper mapper) {
     126      var valueCount = storage.ValueCount;
     127      var valuesId = mapper.GetBoxId(storage.Values);
     128      var indicesId = mapper.GetBoxId(storage.Indices);
     129      box.Values.UInts.Values.AddRange(new[] { (uint)valueCount, valuesId, indicesId });
     130    }
     131
     132    protected override SparseVectorStorage<T> Extract(Box box, Type type, Mapper mapper) {
     133      var boxValues = box.Values.UInts.Values;
     134      int valueCount = (int)boxValues[0];
     135      uint valuesId = boxValues[1];
     136      uint indicesId = boxValues[2];
     137      var values = (T[])mapper.GetObject(valuesId);
     138      var indices = (int[])mapper.GetObject(indicesId);
     139
     140      var data = indices.Zip(values, Tuple.Create);
     141      return SparseVectorStorage<T>.OfIndexedEnumerable(valueCount, data);
     142    }
     143  }
     144
     145  [Transformer("AC9ACF75-7E50-460F-B81A-57D7269548AE", 200)]
     146  public class DoubleSparseVectorStorageTransform : SparseVectorStorageTransformer<double> { }
     147
     148  [Transformer("28BD551A-ACF1-48D0-B83F-C328902C8AB1", 200)]
     149  public class SingleSparseVectorStorageTransform : SparseVectorStorageTransformer<float> { }
     150
     151  [Transformer("B3CF9306-8359-4DEF-B68C-83F1D936A86F", 200)]
     152  public class ComplexSparseVectorStorageTransform : SparseVectorStorageTransformer<Complex> { }
     153
     154  [Transformer("57674C51-02BC-44D2-81C5-D8F5F6322C87", 200)]
     155  public class Complex32SparseVectorStorageTransform : SparseVectorStorageTransformer<Complex32> { }
     156  #endregion
    146157}
Note: See TracChangeset for help on using the changeset viewer.