Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3040_VectorBasedGP/HeuristicLab.MathNet.Numerics/VectorTransformer.cs @ 17449

Last change on this file since 17449 was 17449, checked in by pfleck, 4 years ago

#3040 Added Transformers for Vectors.
Added specialiced Transformers for double Dense/SparseVectorStorage and a generic mapper for the remaining (serializable) types.

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Numerics;
27using System.Runtime.Serialization;
28using Google.Protobuf;
29using HEAL.Attic;
30using MathNet.Numerics;
31using MathNet.Numerics.LinearAlgebra;
32using MathNet.Numerics.LinearAlgebra.Storage;
33
34namespace HeuristicLab.MathNet.Numerics {
35  #region Vector Transformer
36  public abstract class VectorTransformer<T> : BoxTransformer<Vector<T>> where T : struct, IEquatable<T>, IFormattable {
37    public override bool CanTransformType(Type type) {
38      return typeof(Vector<T>).IsAssignableFrom(type);
39    }
40    public override Box CreateBox(object o, Mapper mapper) {
41      var box = base.CreateBox(o, mapper);
42      box.Value = new ScalarValueBox();
43      return box;
44    }
45    protected override void Populate(Box box, Vector<T> vector, Mapper mapper) {
46      var storageId = mapper.GetBoxId(vector.Storage);
47      box.Value.ULong = storageId;
48    }
49    protected override Vector<T> Extract(Box box, Type type, Mapper mapper) {
50      var storageId = (uint)box.Value.ULong;
51      var storage = (VectorStorage<T>)mapper.GetObject(storageId);
52      return Vector<T>.Build.OfStorage(storage);
53    }
54  }
55
56  [Transformer("78276F91-D075-4472-B81B-3A736184CC22", 400)]
57  public class SingleVectorTransformer : VectorTransformer<float> { }
58
59  [Transformer("9B131989-4528-4A59-B64D-0FE07693714F", 400)]
60  public class ComplexVectorTransformer : VectorTransformer<Complex> { }
61
62  [Transformer("2D978104-A174-4AE2-AEA1-20FD50CADF0D", 400)]
63  public class Complex32VectorTransformer : VectorTransformer<Complex32> { }
64
65  [Transformer("76A21F63-36B4-4073-ABBC-56623ECBF42E", 400)]
66  public class DoubleVectorTransformer : VectorTransformer<double> { }
67  #endregion
68
69  #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
88
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    }
125    public override Box CreateBox(object o, Mapper mapper) {
126      var box = base.CreateBox(o, mapper);
127      box.Value = new ScalarValueBox();
128      return box;
129    }
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      }
136    }
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      }
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.