Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization.Operators/3.3/GeneralizedExponentialDiscreteDoubleValueModifier.cs @ 17886

Last change on this file since 17886 was 17732, checked in by dpiringe, 4 years ago

#3081: merged r17729:r17730 into stable

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HEAL.Attic;
28
29namespace HeuristicLab.Optimization.Operators {
30
31  /// <summary>
32  /// Modifies a value by exponentially.
33  /// </summary>
34  [Item("GeneralizedExponentialDiscreteDoubleValueModifier", @"Modifies the value exponentially.
35The base is a standardized exponential function with exponent `Base`
36that is then transformed to pass through the startValue at the startIndex
37and through the endValue at the endIndex.
38For a slow change initially use a value > 1 for `Base`.
39For a steep change initially use a value < 1 for `Base`.
40Negative slopes are automatically generated if the start value is greater than the end value.
41If you use `base`=1 you will get a linear interpolation.")]
42  [StorableType("349D17F2-44D8-46EB-813F-E6D6E73B007F")]
43  public class GeneralizedExponentialDiscreteDoubleValueModifier : DiscreteDoubleValueModifier {
44
45    protected ValueLookupParameter<DoubleValue> BaseParameter {
46      get { return (ValueLookupParameter<DoubleValue>) Parameters["Base"]; }
47    }
48    private double Base { get { return BaseParameter.Value.Value; } }
49
50    [StorableConstructor]
51    protected GeneralizedExponentialDiscreteDoubleValueModifier(StorableConstructorFlag _) : base(_) { }
52    protected GeneralizedExponentialDiscreteDoubleValueModifier(GeneralizedExponentialDiscreteDoubleValueModifier original, Cloner cloner) : base(original, cloner) { }
53    public GeneralizedExponentialDiscreteDoubleValueModifier() {
54      Parameters.Add(new ValueLookupParameter<DoubleValue>("Base", "Base of the exponential function. Must be > 0. If > 1 steep in the end, if < 1 steep at the start, if == 1 linear interpolation.", new DoubleValue(0.00001)));
55    }
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new GeneralizedExponentialDiscreteDoubleValueModifier(this, cloner);
58    }
59
60    protected override double Modify(double value, double startValue, double endValue, int index, int startIndex, int endIndex) {
61      return Apply(value, startValue, endValue, index, startIndex, endIndex, Base);
62    }
63
64    /// <summary>
65    /// Calculates a new value based on exponential decay or growth.
66    /// </summary>
67    /// <exception cref="ArgumentException">Thrown if Base &lt;= 0.</exception>
68    /// <param name="value">The previous value.</param>
69    /// <param name="startValue">The initial value.</param>
70    /// <param name="endValue">The final value.</param>
71    /// <param name="index">The current index.</param>
72    /// <param name="startIndex">The initial index.</param>
73    /// <param name="endIndex">The final index.</param>
74    /// <returns>The new value.</returns>
75    public static double Apply(double value, double startValue, double endValue, int index, int startIndex, int endIndex, double @base) {
76      if (@base <= 0)
77        throw new ArgumentException("Base must be > 0.");
78      if (@base == 1.0)
79        return startValue + (endValue - startValue) * (index - startIndex) / (endIndex - startIndex);
80      return startValue + (endValue - startValue) * (Math.Pow(@base, 1.0 * (index - startIndex) / (endIndex - startIndex)) - 1) / (@base - 1);
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.