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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HEAL.Attic;
|
---|
25 | using MathNet.Numerics;
|
---|
26 | using MathNet.Numerics.LinearAlgebra;
|
---|
27 | using MathNet.Numerics.LinearAlgebra.Storage;
|
---|
28 | using Complex = System.Numerics.Complex;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.MathNet.Numerics {
|
---|
31 | #region Vector Transformer
|
---|
32 | public abstract class VectorTransformer<T> : BoxTransformer<Vector<T>> where T : struct, IEquatable<T>, IFormattable {
|
---|
33 |
|
---|
34 | public override bool CanTransformType(Type type) {
|
---|
35 | return typeof(Vector<T>).IsAssignableFrom(type);
|
---|
36 | }
|
---|
37 |
|
---|
38 | public override Box CreateBox(object o, Mapper mapper) {
|
---|
39 | var box = base.CreateBox(o, mapper);
|
---|
40 | box.Value = new ScalarValueBox();
|
---|
41 | return box;
|
---|
42 | }
|
---|
43 |
|
---|
44 | protected override void Populate(Box box, Vector<T> vector, Mapper mapper) {
|
---|
45 | var storageId = mapper.GetBoxId(vector.Storage);
|
---|
46 | box.Value.ULong = storageId;
|
---|
47 | }
|
---|
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("76A21F63-36B4-4073-ABBC-56623ECBF42E", 400)]
|
---|
57 | public class DoubleVectorTransformer : VectorTransformer<double> { }
|
---|
58 |
|
---|
59 | [Transformer("78276F91-D075-4472-B81B-3A736184CC22", 400)]
|
---|
60 | public class SingleVectorTransformer : VectorTransformer<float> { }
|
---|
61 |
|
---|
62 | [Transformer("9B131989-4528-4A59-B64D-0FE07693714F", 400)]
|
---|
63 | public class ComplexVectorTransformer : VectorTransformer<Complex> { }
|
---|
64 |
|
---|
65 | [Transformer("2D978104-A174-4AE2-AEA1-20FD50CADF0D", 400)]
|
---|
66 | public class Complex32VectorTransformer : VectorTransformer<Complex32> { }
|
---|
67 | #endregion
|
---|
68 |
|
---|
69 | #region Dense Vector Storage Transformer
|
---|
70 | public abstract class DenseVectorStorageTransformer<T> : BoxTransformer<DenseVectorStorage<T>> where T : struct, IEquatable<T>, IFormattable {
|
---|
71 |
|
---|
72 | public override Box CreateBox(object o, Mapper mapper) {
|
---|
73 | var box = base.CreateBox(o, mapper);
|
---|
74 | box.Value = new ScalarValueBox();
|
---|
75 | return box;
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected override void Populate(Box box, DenseVectorStorage<T> storage, Mapper mapper) {
|
---|
79 | var dataId = mapper.GetBoxId(storage.Data);
|
---|
80 | box.Value.ULong = dataId;
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected override DenseVectorStorage<T> Extract(Box box, Type type, Mapper mapper) {
|
---|
84 | var dataId = (uint)box.Value.ULong;
|
---|
85 | var data = (T[])mapper.GetObject(dataId);
|
---|
86 | return DenseVectorStorage<T>.OfEnumerable(data);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public override void FillFromBox(object obj, Box box, Mapper mapper) {
|
---|
90 | var storage = (DenseVectorStorage<T>)obj;
|
---|
91 | var dataId = (uint)box.Value.ULong;
|
---|
92 | var data = (T[])mapper.GetObject(dataId);
|
---|
93 | Array.Copy(data, storage.Data, data.Length);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | [Transformer("24705F16-FDB8-487A-ABEA-F69D58B50D60", 200)]
|
---|
98 | public class DoubleDenseVectorStorageTransform : DenseVectorStorageTransformer<double> { }
|
---|
99 |
|
---|
100 | [Transformer("87FBC62F-E8A0-4C50-9289-CAD2B7F448E4", 200)]
|
---|
101 | public class SingleDenseVectorStorageTransform : DenseVectorStorageTransformer<float> { }
|
---|
102 |
|
---|
103 | [Transformer("4391F230-2C0C-4876-B2EF-DE43197F4557", 200)]
|
---|
104 | public class ComplexDenseVectorStorageTransform : DenseVectorStorageTransformer<Complex> { }
|
---|
105 |
|
---|
106 | [Transformer("6E07F8A9-DF19-4A2F-B7A3-209917B58355", 200)]
|
---|
107 | public class Complex32DenseVectorStorageTransform : DenseVectorStorageTransformer<Complex32> { }
|
---|
108 | #endregion
|
---|
109 |
|
---|
110 | #region Sparse Vector Storage Transformer
|
---|
111 | public abstract class SparseVectorStorageTransformer<T> : BoxTransformer<SparseVectorStorage<T>> where T : struct, IEquatable<T>, IFormattable {
|
---|
112 |
|
---|
113 | public override Box CreateBox(object o, Mapper mapper) {
|
---|
114 | var box = base.CreateBox(o, mapper);
|
---|
115 | box.Values = new RepeatedValueBox() {
|
---|
116 | UInts = new RepeatedUIntBox()
|
---|
117 | };
|
---|
118 | return box;
|
---|
119 | }
|
---|
120 |
|
---|
121 | protected override void Populate(Box box, SparseVectorStorage<T> storage, Mapper mapper) {
|
---|
122 | var valueCount = storage.ValueCount;
|
---|
123 | var valuesId = mapper.GetBoxId(storage.Values);
|
---|
124 | var indicesId = mapper.GetBoxId(storage.Indices);
|
---|
125 | box.Values.UInts.Values.AddRange(new[] { (uint)valueCount, valuesId, indicesId });
|
---|
126 | }
|
---|
127 |
|
---|
128 | protected override SparseVectorStorage<T> Extract(Box box, Type type, Mapper mapper) {
|
---|
129 | var boxValues = box.Values.UInts.Values;
|
---|
130 | int valueCount = (int)boxValues[0];
|
---|
131 | uint valuesId = boxValues[1];
|
---|
132 | uint indicesId = boxValues[2];
|
---|
133 | var values = (T[])mapper.GetObject(valuesId);
|
---|
134 | var indices = (int[])mapper.GetObject(indicesId);
|
---|
135 |
|
---|
136 | var data = indices.Zip(values, Tuple.Create);
|
---|
137 | return SparseVectorStorage<T>.OfIndexedEnumerable(valueCount, data);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | [Transformer("AC9ACF75-7E50-460F-B81A-57D7269548AE", 200)]
|
---|
142 | public class DoubleSparseVectorStorageTransform : SparseVectorStorageTransformer<double> { }
|
---|
143 |
|
---|
144 | [Transformer("28BD551A-ACF1-48D0-B83F-C328902C8AB1", 200)]
|
---|
145 | public class SingleSparseVectorStorageTransform : SparseVectorStorageTransformer<float> { }
|
---|
146 |
|
---|
147 | [Transformer("B3CF9306-8359-4DEF-B68C-83F1D936A86F", 200)]
|
---|
148 | public class ComplexSparseVectorStorageTransform : SparseVectorStorageTransformer<Complex> { }
|
---|
149 |
|
---|
150 | [Transformer("57674C51-02BC-44D2-81C5-D8F5F6322C87", 200)]
|
---|
151 | public class Complex32SparseVectorStorageTransform : SparseVectorStorageTransformer<Complex32> { }
|
---|
152 | #endregion
|
---|
153 | } |
---|