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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HEAL.Attic;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Optimization.Operators {
|
---|
30 |
|
---|
31 | /// <summary>
|
---|
32 | /// Modifies a value by exponentially.
|
---|
33 | /// </summary>
|
---|
34 | [Item("GeneralizedExponentialDiscreteDoubleValueModifier", @"Modifies the value exponentially.
|
---|
35 | The base is a standardized exponential function with exponent `Base`
|
---|
36 | that is then transformed to pass through the startValue at the startIndex
|
---|
37 | and through the endValue at the endIndex.
|
---|
38 | For a slow change initially use a value > 1 for `Base`.
|
---|
39 | For a steep change initially use a value < 1 for `Base`.
|
---|
40 | Negative slopes are automatically generated if the start value is greater than the end value.
|
---|
41 | If 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 | /// <summary>
|
---|
61 | /// Calculates a new value based on exponential decay or growth.
|
---|
62 | /// </summary>
|
---|
63 | /// <exception cref="ArgumentException">Thrown if Base <= 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 | }
|
---|