Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Data/3.3/PercentMatrix.cs @ 15430

Last change on this file since 15430 was 13841, checked in by jlodewyc, 8 years ago

#2582 More parameter datatypes, splitting fileopening service, approving users, reopen last file, change name tasks and repetitions

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Collections.Generic;
23using System.Text;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using System.Linq;
28
29namespace HeuristicLab.Data {
30  [Item("PercentMatrix", "Represents a matrix of double values in percent.")]
31  [StorableClass]
32  public class PercentMatrix : DoubleMatrix {
33    [StorableConstructor]
34    protected PercentMatrix(bool deserializing) : base(deserializing) { }
35    protected PercentMatrix(PercentMatrix original, Cloner cloner)
36      : base(original, cloner) {
37    }
38    public PercentMatrix() : base() { }
39    public PercentMatrix(int rows, int columns) : base(rows, columns) { }
40    public PercentMatrix(int rows, int columns, IEnumerable<string> columnNames) : base(rows, columns, columnNames) { }
41    public PercentMatrix(int rows, int columns, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(rows, columns, columnNames, rowNames) { }
42    public PercentMatrix(double[,] elements) : base(elements) { }
43    public PercentMatrix(double[,] elements, IEnumerable<string> columnNames) : base(elements, columnNames) { }
44    public PercentMatrix(double[,] elements, IEnumerable<string> columnNames, IEnumerable<string> rowNames) : base(elements, columnNames, rowNames) { }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new PercentMatrix(this, cloner);
48    }
49        public static PercentMatrix Parse(string val)
50        {
51            try
52            {
53                val = val.Replace("]", string.Empty);
54
55                string[] parts = val.Split('[');
56                var r = parts.Length;
57                var c = parts[1].Split(';').Length;
58                val = val.Replace("\n", ";");
59                val = val.Replace("[", string.Empty);
60                string[] arr = System.Array.ConvertAll(val.Trim().Split(';'), p => p.Trim());
61                double[] a = arr.Where(s => s != "").ToList().ConvertAll(s => (double.Parse(s)/100)).ToArray();
62                double[,] b = new double[r, c];
63                var rowcount = 0;
64                for (int i = 0; i < a.Length;)
65                {
66                    for (int j = 0; j < c; j++)
67                    {
68                        b[rowcount, j] = a[i];
69                        i++;
70                    }
71                    rowcount++;
72                }
73                return new PercentMatrix(b);
74            }
75            catch (System.FormatException e)
76            {
77                return null;
78            }
79        }
80        public override string ToString() {
81      StringBuilder sb = new StringBuilder();
82      sb.Append("[");
83      if (matrix.Length > 0) {
84        for (int i = 0; i < Rows; i++) {
85          sb.Append("[").Append(matrix[i, 0].ToString("#0.#################### %"));  // percent format
86          for (int j = 1; j < Columns; j++)
87            sb.Append(";").Append(matrix[i, j].ToString("#0.#################### %"));  // percent format
88          sb.Append("]");
89        }
90      }
91      sb.Append("]");
92      return sb.ToString();
93    }
94
95    protected override bool Validate(string value, out string errorMessage) {
96      value = value.Replace("%", " ");
97      return base.Validate(value, out errorMessage);
98    }
99    protected override string GetValue(int rowIndex, int columIndex) {
100      return this[rowIndex, columIndex].ToString("#0.#################### %");  // percent format
101    }
102    protected override bool SetValue(string value, int rowIndex, int columnIndex) {
103      bool percent = value.Contains("%");
104      value = value.Replace("%", " ");
105      double val;
106      if (double.TryParse(value, out val)) {
107        if (percent) {
108          if (!(val).IsAlmost(this[rowIndex, columnIndex] * 100.0))
109            this[rowIndex, columnIndex] = val == 0 ? 0 : val / 100.0;
110        } else {
111          this[rowIndex, columnIndex] = val;
112        }
113        return true;
114      } else {
115        return false;
116      }
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.