source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeMatrixConverter.cs @ 17843

Last change on this file since 17843 was 17843, checked in by dpiringe, 7 weeks ago

#3026

  • removed property ConvertableType from all converters
  • removed the option to fixate or loosen the path of JsonItems (obsolete)
  • added a abstract formatter SymbolicRegressionSolutionFormatterBase as base formatter for ISymbolicRegressionSolution
  • unified the construction of exporter controls
  • code cleanup
File size: 4.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using Newtonsoft.Json.Linq;
7using HeuristicLab.Data;
8using HeuristicLab.Core;
9
10namespace HeuristicLab.JsonInterface {
11  public class IntMatrixConverter : ValueTypeMatrixConverter<IntMatrix, int> {
12    public override int Priority => 1;
13
14    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
15      IntMatrix mat = item as IntMatrix;
16      IntMatrixJsonItem d = data as IntMatrixJsonItem;
17      CopyMatrixData(mat, d.Value);
18    }
19
20    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
21      new IntMatrixJsonItem() {
22        Name = value.ItemName,
23        Description = value.ItemDescription,
24        Value = Transform((IntMatrix)value),
25        Minimum = int.MinValue,
26        Maximum = int.MaxValue
27      };
28  }
29
30  public class DoubleMatrixConverter : ValueTypeMatrixConverter<DoubleMatrix, double> {
31    public override int Priority => 1;
32
33    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
34      DoubleMatrix mat = item as DoubleMatrix;
35      DoubleMatrixJsonItem d = data as DoubleMatrixJsonItem;
36      CopyMatrixData(mat, d.Value);
37    }
38
39    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
40      new DoubleMatrixJsonItem() {
41        Name = value.ItemName,
42        Description = value.ItemDescription,
43        Value = Transform((DoubleMatrix)value),
44        Minimum = double.MinValue,
45        Maximum = double.MaxValue,
46        RowNames = ((DoubleMatrix)value).RowNames,
47        ColumnNames = ((DoubleMatrix)value).ColumnNames
48      };
49  }
50
51  public class PercentMatrixConverter : ValueTypeMatrixConverter<PercentMatrix, double> {
52    public override int Priority => 2;
53
54    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
55      PercentMatrix mat = item as PercentMatrix;
56      DoubleMatrixJsonItem d = data as DoubleMatrixJsonItem;
57      CopyMatrixData(mat, d.Value);
58    }
59
60    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
61      new DoubleMatrixJsonItem() {
62        Name = value.ItemName,
63        Description = value.ItemDescription,
64        Value = Transform((PercentMatrix)value),
65        Minimum = 0.0d,
66        Maximum = 1.0d
67      };
68  }
69
70  public class BoolMatrixConverter : ValueTypeMatrixConverter<BoolMatrix, bool> {
71    public override int Priority => 1;
72
73    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
74      BoolMatrix mat = item as BoolMatrix;
75      BoolMatrixJsonItem d = data as BoolMatrixJsonItem;
76      CopyMatrixData(mat, d.Value);
77    }
78
79    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
80      new BoolMatrixJsonItem() {
81        Name = value.ItemName,
82        Description = value.ItemDescription,
83        Value = Transform((BoolMatrix)value)
84      };
85  }
86
87  public abstract class ValueTypeMatrixConverter<MatrixType, T> : BaseConverter
88    where MatrixType : ValueTypeMatrix<T>
89    where T : struct
90  {
91    public override bool CanConvertType(Type t) =>
92      typeof(MatrixType).IsAssignableFrom(t);
93
94    #region Helper
95    /// <summary>
96    /// Copies the data into the matrix object. Uses reflection to set the
97    /// row and column size of the matrix, because it is not possible to
98    /// create a new matrix at this point and there exists no public resize method.
99    /// </summary>
100    /// <param name="matrix"></param>
101    /// <param name="data"></param>
102    protected void CopyMatrixData(MatrixType matrix, T[][] data) {
103      var cols = data.Length;
104      var rows = data.Length > 0 ? data[0].Length : 0;
105
106      var rowInfo = matrix.GetType().GetProperty("Rows");
107      rowInfo.SetValue(matrix, rows);
108      var colInfo = matrix.GetType().GetProperty("Columns");
109      colInfo.SetValue(matrix, cols);
110
111      for (int x = 0; x < rows; ++x) {
112        for (int y = 0; y < cols; ++y) {
113          matrix[x, y] = data[y][x];
114        }
115      }
116    }
117
118    protected T[][] Transform(MatrixType matrix) {
119      T[][] m = new T[matrix.Columns][];
120      for (int column = 0; column < matrix.Columns; ++column) {
121        m[column] = new T[matrix.Rows];
122        for (int row = 0; row < matrix.Rows; ++row) {
123          m[column][row] = matrix[row, column];
124        }
125      }
126      return m;
127    }
128    #endregion
129  }
130}
Note: See TracBrowser for help on using the repository browser.