[17449] | 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.Collections.Generic;
|
---|
| 24 | using System.IO;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Numerics;
|
---|
| 27 | using System.Runtime.Serialization;
|
---|
| 28 | using Google.Protobuf;
|
---|
| 29 | using HEAL.Attic;
|
---|
| 30 | using MathNet.Numerics;
|
---|
| 31 | using MathNet.Numerics.LinearAlgebra;
|
---|
| 32 | using MathNet.Numerics.LinearAlgebra.Storage;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.MathNet.Numerics {
|
---|
| 35 | #region Vector Transformer
|
---|
| 36 | public abstract class VectorTransformer<T> : BoxTransformer<Vector<T>> where T : struct, IEquatable<T>, IFormattable {
|
---|
[17452] | 37 |
|
---|
[17449] | 38 | public override bool CanTransformType(Type type) {
|
---|
| 39 | return typeof(Vector<T>).IsAssignableFrom(type);
|
---|
| 40 | }
|
---|
[17452] | 41 |
|
---|
[17449] | 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 | }
|
---|
[17452] | 47 |
|
---|
[17449] | 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 | }
|
---|
[17452] | 52 |
|
---|
[17449] | 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 |
|
---|
[17452] | 60 | [Transformer("76A21F63-36B4-4073-ABBC-56623ECBF42E", 400)]
|
---|
| 61 | public class DoubleVectorTransformer : VectorTransformer<double> { }
|
---|
| 62 |
|
---|
[17449] | 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
|
---|
[17452] | 74 | public abstract class DenseVectorStorageTransformer<T> : BoxTransformer<DenseVectorStorage<T>> where T : struct, IEquatable<T>, IFormattable {
|
---|
| 75 |
|
---|
[17449] | 76 | public override Box CreateBox(object o, Mapper mapper) {
|
---|
| 77 | var box = base.CreateBox(o, mapper);
|
---|
[17452] | 78 | box.Value = new ScalarValueBox();
|
---|
[17449] | 79 | return box;
|
---|
| 80 | }
|
---|
[17452] | 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;
|
---|
[17449] | 85 | }
|
---|
[17452] | 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);
|
---|
[17449] | 91 | }
|
---|
[17452] | 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 | }
|
---|
[17449] | 99 | }
|
---|
[17452] | 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> { }
|
---|
[17449] | 112 | #endregion
|
---|
| 113 |
|
---|
| 114 | #region Sparse Vector Storage Transformer
|
---|
[17452] | 115 | public abstract class SparseVectorStorageTransformer<T> : BoxTransformer<SparseVectorStorage<T>> where T : struct, IEquatable<T>, IFormattable {
|
---|
| 116 |
|
---|
[17449] | 117 | public override Box CreateBox(object o, Mapper mapper) {
|
---|
| 118 | var box = base.CreateBox(o, mapper);
|
---|
| 119 | box.Values = new RepeatedValueBox() {
|
---|
[17452] | 120 | UInts = new RepeatedUIntBox()
|
---|
[17449] | 121 | };
|
---|
| 122 | return box;
|
---|
| 123 | }
|
---|
[17452] | 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 });
|
---|
[17449] | 130 | }
|
---|
[17452] | 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 |
|
---|
[17449] | 140 | var data = indices.Zip(values, Tuple.Create);
|
---|
[17452] | 141 | return SparseVectorStorage<T>.OfIndexedEnumerable(valueCount, data);
|
---|
[17449] | 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[17452] | 145 | [Transformer("AC9ACF75-7E50-460F-B81A-57D7269548AE", 200)]
|
---|
| 146 | public class DoubleSparseVectorStorageTransform : SparseVectorStorageTransformer<double> { }
|
---|
[17449] | 147 |
|
---|
[17452] | 148 | [Transformer("28BD551A-ACF1-48D0-B83F-C328902C8AB1", 200)]
|
---|
| 149 | public class SingleSparseVectorStorageTransform : SparseVectorStorageTransformer<float> { }
|
---|
[17449] | 150 |
|
---|
[17452] | 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
|
---|
[17449] | 157 | } |
---|