Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18031 was 17843, checked in by dpiringe, 3 years 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
RevLine 
[17263]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
[17284]10namespace HeuristicLab.JsonInterface {
[17394]11  public class IntMatrixConverter : ValueTypeMatrixConverter<IntMatrix, int> {
12    public override int Priority => 1;
[17828]13
[17417]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() {
[17483]22        Name = value.ItemName,
[17433]23        Description = value.ItemDescription,
[17420]24        Value = Transform((IntMatrix)value),
[17473]25        Minimum = int.MinValue,
26        Maximum = int.MaxValue
[17417]27      };
[17394]28  }
29
30  public class DoubleMatrixConverter : ValueTypeMatrixConverter<DoubleMatrix, double> {
31    public override int Priority => 1;
[17417]32
33    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
34      DoubleMatrix mat = item as DoubleMatrix;
[17471]35      DoubleMatrixJsonItem d = data as DoubleMatrixJsonItem;
[17417]36      CopyMatrixData(mat, d.Value);
37    }
38
39    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
[17471]40      new DoubleMatrixJsonItem() {
[17483]41        Name = value.ItemName,
[17433]42        Description = value.ItemDescription,
[17420]43        Value = Transform((DoubleMatrix)value),
[17473]44        Minimum = double.MinValue,
45        Maximum = double.MaxValue,
[17451]46        RowNames = ((DoubleMatrix)value).RowNames,
47        ColumnNames = ((DoubleMatrix)value).ColumnNames
[17417]48      };
[17394]49  }
50
51  public class PercentMatrixConverter : ValueTypeMatrixConverter<PercentMatrix, double> {
52    public override int Priority => 2;
[17417]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() {
[17483]62        Name = value.ItemName,
[17433]63        Description = value.ItemDescription,
[17420]64        Value = Transform((PercentMatrix)value),
[17473]65        Minimum = 0.0d,
66        Maximum = 1.0d
[17417]67      };
[17394]68  }
69
70  public class BoolMatrixConverter : ValueTypeMatrixConverter<BoolMatrix, bool> {
71    public override int Priority => 1;
[17417]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() {
[17473]81        Name = value.ItemName,
[17433]82        Description = value.ItemDescription,
[17473]83        Value = Transform((BoolMatrix)value)
[17417]84      };
[17394]85  }
86
87  public abstract class ValueTypeMatrixConverter<MatrixType, T> : BaseConverter
[17263]88    where MatrixType : ValueTypeMatrix<T>
89    where T : struct
90  {
[17828]91    public override bool CanConvertType(Type t) =>
[17843]92      typeof(MatrixType).IsAssignableFrom(t);
[17828]93
[17266]94    #region Helper
[17828]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>
[17417]102    protected void CopyMatrixData(MatrixType matrix, T[][] data) {
[17451]103      var cols = data.Length;
104      var rows = data.Length > 0 ? data[0].Length : 0;
[17374]105
[17417]106      var rowInfo = matrix.GetType().GetProperty("Rows");
107      rowInfo.SetValue(matrix, rows);
108      var colInfo = matrix.GetType().GetProperty("Columns");
109      colInfo.SetValue(matrix, cols);
[17379]110
[17417]111      for (int x = 0; x < rows; ++x) {
112        for (int y = 0; y < cols; ++y) {
[17451]113          matrix[x, y] = data[y][x];
[17263]114        }
115      }
116    }
[17417]117
118    protected T[][] Transform(MatrixType matrix) {
[17451]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];
[17417]124        }
125      }
126      return m;
127    }
[17266]128    #endregion
[17263]129  }
130}
Note: See TracBrowser for help on using the repository browser.