Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.Problems.BioBoost/HeuristicLab.Problems.BioBoost/3.3/Utils/StepFunction.cs @ 15711

Last change on this file since 15711 was 13071, checked in by gkronber, 8 years ago

#2499: added license headers and removed unused usings

File size: 4.1 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using System;
28using System.Collections.Generic;
29using System.Globalization;
30using System.Linq;
31using System.Text.RegularExpressions;
32
33namespace HeuristicLab.BioBoost.Utils {
34
35  [StorableClass]
36  [Item("StepFunction", "A simple step function specifying upper bounds and corresponding values.")]
37  public class StepFunction : ParameterizedNamedItem {
38
39    #region parameters
40    public IValueLookupParameter<DoubleMatrix> ValuesParameter {
41      get { return (IValueLookupParameter<DoubleMatrix>) Parameters["Values"]; }
42    }
43    public DoubleMatrix Values {
44      get { return ValuesParameter.Value; }
45      set { ValuesParameter.Value = value; }
46    }
47    #endregion
48
49    #region Construction & Cloning
50    public StepFunction() {
51      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Values", "The upper bounds and corresponding values.", new DoubleMatrix(0, 2, new[] {"Upper Bound", "Value"})));
52    }
53    [StorableConstructor]
54    protected StepFunction(bool isDeserializing) : base(isDeserializing) {}
55    protected StepFunction(StepFunction orig, Cloner cloner) : base(orig, cloner) {}
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new StepFunction(this, cloner);
58    }
59    #endregion
60
61    public void Sort() {
62      if (Values.Columns != 2)
63        throw new InvalidOperationException("StepFunction values need two columns (upper bound and value)");
64      var steps = Enumerable.Range(0, Values.Rows).Select(i => new {s = Values[i, 0], v = Values[i, 1]}).OrderBy(p => p.s).ToList();
65      for (int i = 0; i<Values.Rows; i++) {
66        Values[i, 0] = steps[i].s;
67        Values[i, 1] = steps[i].v;
68      }
69    }
70
71    public double GetValue(double x) {
72      int i = 0;
73      while (i < Values.Rows-1 && x > Values[i, 0]) i++;
74      return Values[i, 1];
75    }
76
77    public static StepFunction Parse(String s, String rowSepRegex="/", String colSepRegex="->") {
78      var values = new List<Tuple<double, double>>();
79      foreach (var row in Regex.Split(s, rowSepRegex)) {
80        var cols = Regex.Split(row, colSepRegex);
81        if (cols.Length != 2) {
82          throw new ArgumentException("step function must contain two rows (separated by /" + colSepRegex + "/");
83        }
84        try {
85          values.Add(new Tuple<double, double>(ParseDouble(cols[0]), ParseDouble(cols[1])));
86        }
87        catch (Exception x) {
88          if (x is FormatException || x is ArgumentNullException || x is OverflowException) {
89            throw new ArgumentException(String.Format("could not parse row \"{0}\"", row), x);
90          }
91        }
92      }
93      var sf = new StepFunction();
94      var matrix = new DoubleMatrix(values.Count, 2, new[] {"Upper Bound", "Value"});
95      for (int i = 0; i < values.Count; i++) {
96        matrix[i, 0] = values[i].Item1;
97        matrix[i, 1] = values[i].Item2;
98      }
99      sf.Values = matrix;
100      sf.Sort();
101      return sf;
102    }
103
104
105    public static double ParseDouble(String s) {
106      return Double.Parse(s, NumberStyles.Float, NumberFormatInfo.InvariantInfo);
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.