Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17460 was 17452, checked in by pfleck, 5 years ago

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

File size: 6.2 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
38    public override bool CanTransformType(Type type) {
39      return typeof(Vector<T>).IsAssignableFrom(type);
40    }
41
42    public override Box CreateBox(object o, Mapper mapper) {
43      var box = base.CreateBox(o, mapper);
44      box.Value = new ScalarValueBox();
45      return box;
46    }
47
48    protected override void Populate(Box box, Vector<T> vector, Mapper mapper) {
49      var storageId = mapper.GetBoxId(vector.Storage);
50      box.Value.ULong = storageId;
51    }
52
53    protected override Vector<T> Extract(Box box, Type type, Mapper mapper) {
54      var storageId = (uint)box.Value.ULong;
55      var storage = (VectorStorage<T>)mapper.GetObject(storageId);
56      return Vector<T>.Build.OfStorage(storage);
57    }
58  }
59
60  [Transformer("76A21F63-36B4-4073-ABBC-56623ECBF42E", 400)]
61  public class DoubleVectorTransformer : VectorTransformer<double> { }
62
63  [Transformer("78276F91-D075-4472-B81B-3A736184CC22", 400)]
64  public class SingleVectorTransformer : VectorTransformer<float> { }
65
66  [Transformer("9B131989-4528-4A59-B64D-0FE07693714F", 400)]
67  public class ComplexVectorTransformer : VectorTransformer<Complex> { }
68
69  [Transformer("2D978104-A174-4AE2-AEA1-20FD50CADF0D", 400)]
70  public class Complex32VectorTransformer : VectorTransformer<Complex32> { }
71  #endregion
72
73  #region Dense Vector Storage Transformer
74  public abstract class DenseVectorStorageTransformer<T> : BoxTransformer<DenseVectorStorage<T>> where T : struct, IEquatable<T>, IFormattable {
75
76    public override Box CreateBox(object o, Mapper mapper) {
77      var box = base.CreateBox(o, mapper);
78      box.Value = new ScalarValueBox();
79      return box;
80    }
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;
85    }
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);
98    }
99  }
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
157}
Note: See TracBrowser for help on using the repository browser.