Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Optimization.Operators/3.3/DiscreteDoubleValueModifier.cs @ 6409

Last change on this file since 6409 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Optimization.Operators {
30  /// <summary>
31  /// Base class for modifying a double value according to a certain function in discrete intervalls.
32  /// </summary>
33  [Item("DiscreteDoubleValueModifier", "Base class for modifying a double value according to a certain function in discrete intervalls.")]
34  [StorableClass]
35  public abstract class DiscreteDoubleValueModifier : SingleSuccessorOperator, IDiscreteDoubleValueModifier {
36    /// <summary>
37    /// The parameter that should be modified.
38    /// </summary>
39    public ILookupParameter<DoubleValue> ValueParameter {
40      get { return (ILookupParameter<DoubleValue>)Parameters["Value"]; }
41    }
42    /// <summary>
43    /// The start value of the parameter, will be assigned to <see cref="ValueParameter"/> as soon as <see cref="IndexParamter"/> equals <see cref="StartIndexParameter"/>.
44    /// </summary>
45    public IValueLookupParameter<DoubleValue> StartValueParameter {
46      get { return (IValueLookupParameter<DoubleValue>)Parameters["StartValue"]; }
47    }
48    /// <summary>
49    /// The end value of the parameter, will be assigned to <see cref="ValueParameter"/> as soon as <see cref="IndexParamter"/> equals <see cref="EndIndexParameter"/>.
50    /// </summary>
51    public IValueLookupParameter<DoubleValue> EndValueParameter {
52      get { return (IValueLookupParameter<DoubleValue>)Parameters["EndValue"]; }
53    }
54    /// <summary>
55    /// The index that denotes from which point in the function (relative to <see cref="StartIndexParameter"/> and <see cref="EndIndexParameter"/> the value should be assigned.
56    /// </summary>
57    public ILookupParameter<IntValue> IndexParameter {
58      get { return (ILookupParameter<IntValue>)Parameters["Index"]; }
59    }
60    /// <summary>
61    /// As soon as <see cref="IndexParameter"/> is &gt;= this parameter the value will start to be modified.
62    /// </summary>
63    public IValueLookupParameter<IntValue> StartIndexParameter {
64      get { return (IValueLookupParameter<IntValue>)Parameters["StartIndex"]; }
65    }
66    /// <summary>
67    /// As long as <see cref="IndexParameter"/> is &lt;= this parameter the value will start to be modified.
68    /// </summary>
69    public IValueLookupParameter<IntValue> EndIndexParameter {
70      get { return (IValueLookupParameter<IntValue>)Parameters["EndIndex"]; }
71    }
72
73    [StorableConstructor]
74    protected DiscreteDoubleValueModifier(bool deserializing) : base(deserializing) { }
75    protected DiscreteDoubleValueModifier(DiscreteDoubleValueModifier original, Cloner cloner) : base(original, cloner) { }
76    /// <summary>
77    /// Initializes a new instance of <see cref="DiscreteDoubleValueModifier"/> with 6 parameters
78    /// (<c>Value</c>, <c>StartValue</c>, <c>EndValue</c>, <c>Index</c>, <c>StartIndex</c>, <c>EndIndex</c>).
79    /// </summary>
80    protected DiscreteDoubleValueModifier()
81      : base() {
82      Parameters.Add(new LookupParameter<DoubleValue>("Value", "The double value to modify."));
83      Parameters.Add(new ValueLookupParameter<DoubleValue>("StartValue", "The start value of 'Value'."));
84      Parameters.Add(new ValueLookupParameter<DoubleValue>("EndValue", "The end value of 'Value'."));
85      Parameters.Add(new LookupParameter<IntValue>("Index", "The current index."));
86      Parameters.Add(new ValueLookupParameter<IntValue>("StartIndex", "The start index at which to start modifying 'Value'."));
87      Parameters.Add(new ValueLookupParameter<IntValue>("EndIndex", "The end index by which 'Value' should have reached 'EndValue'."));
88    }
89
90    /// <summary>
91    /// Checks whether index is between start and end and forwards the call to <see cref="Modify"/> if startIndex &lt; index &lt; endIndex.
92    /// </summary>
93    /// <remarks>
94    /// If index = startIndex the call will not be forwarded and startValue will be used. The same with endIndex and endValue.
95    /// </remarks>
96    /// <returns>What the base class returns.</returns>
97    public override IOperation Apply() {
98      int index = IndexParameter.ActualValue.Value, startIndex = StartIndexParameter.ActualValue.Value;
99      if (index >= startIndex) {
100        int endIndex = EndIndexParameter.ActualValue.Value;
101        DoubleValue value = ValueParameter.ActualValue;
102        if (value == null) {
103          value = new DoubleValue();
104          ValueParameter.ActualValue = value;
105        }
106        double newValue = value.Value;
107        if (index == startIndex) {
108          newValue = StartValueParameter.ActualValue.Value;
109        } else if (index == endIndex) {
110          newValue = EndValueParameter.ActualValue.Value;
111        } else if (index < endIndex) {
112          double start = StartValueParameter.ActualValue.Value, end = EndValueParameter.ActualValue.Value;
113          newValue = Modify(value.Value, start, end, index, startIndex, endIndex);
114        }
115        value.Value = newValue;
116      }
117      return base.Apply();
118    }
119
120    /// <summary>
121    /// Modifies a given value according to two support points denoted by (startIndex; startValue) and (endIndex; endValue).
122    /// The current 'index' and the last value of 'value' is also given.
123    /// </summary>
124    /// <param name="value">The last value.</param>
125    /// <param name="startValue">The start value.</param>
126    /// <param name="endValue">The end value.</param>
127    /// <param name="index">The current index.</param>
128    /// <param name="startIndex">The start index.</param>
129    /// <param name="endIndex">The end index.</param>
130    /// <returns>The new value.</returns>
131    protected abstract double Modify(double value, double startValue, double endValue, int index, int startIndex, int endIndex);
132  }
133}
Note: See TracBrowser for help on using the repository browser.