Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Operators/3.3/GeneralizedExponentialDiscreteDoubleValueModifier.cs @ 12769

Last change on this file since 12769 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.8 KB
RevLine 
[7704]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7704]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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  [StorableClass]
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(bool deserializing) : base(deserializing) { }
52    protected GeneralizedExponentialDiscreteDoubleValueModifier(GeneralizedExponentialDiscreteDoubleValueModifier original, Cloner cloner) : base(original, cloner) { }
53    public GeneralizedExponentialDiscreteDoubleValueModifier() {
[7705]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)));
[7704]55    }
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new GeneralizedExponentialDiscreteDoubleValueModifier(this, cloner);
58    }
59
60    /// <summary>
61    /// Calculates a new value based on exponential decay or growth.
62    /// </summary>
63    /// <exception cref="ArgumentException">Thrown if Base &lt;= 0.</exception>
64    /// <param name="value">The previous value.</param>
65    /// <param name="startValue">The initial value.</param>
66    /// <param name="endValue">The final value.</param>
67    /// <param name="index">The current index.</param>
68    /// <param name="startIndex">The initial index.</param>
69    /// <param name="endIndex">The final index.</param>
70    /// <returns>The new value.</returns>
71    protected override double Modify(double value, double startValue, double endValue, int index, int startIndex, int endIndex) {
72      if (Base <= 0)
73        throw new ArgumentException("Base must be > 0.");
74      if (Base == 1.0)
75        return startValue + (endValue - startValue)*(index - startIndex)/(endIndex - startIndex);
76      return startValue + (endValue - startValue)*(Math.Pow(Base, 1.0*(index-startIndex)/(endIndex-startIndex)) - 1)/(Base - 1);
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.