[7704] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
[16565] | 27 | using HEAL.Attic;
|
---|
[7704] | 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.")]
|
---|
[16565] | 42 | [StorableType("349D17F2-44D8-46EB-813F-E6D6E73B007F")]
|
---|
[7704] | 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]
|
---|
[16565] | 51 | protected GeneralizedExponentialDiscreteDoubleValueModifier(StorableConstructorFlag _) : base(_) { }
|
---|
[7704] | 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 |
|
---|
[17825] | 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 |
|
---|
[7704] | 64 | /// <summary>
|
---|
| 65 | /// Calculates a new value based on exponential decay or growth.
|
---|
| 66 | /// </summary>
|
---|
| 67 | /// <exception cref="ArgumentException">Thrown if Base <= 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>
|
---|
[17825] | 75 | public static double Apply(double value, double startValue, double endValue, int index, int startIndex, int endIndex, double @base) {
|
---|
| 76 | if (@base <= 0)
|
---|
[7704] | 77 | throw new ArgumentException("Base must be > 0.");
|
---|
[17825] | 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);
|
---|
[7704] | 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|