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