Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization.Operators/3.3/DiscreteDoubleValueModifier.cs @ 3376

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

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 6.4 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    /// <summary>
74    /// Initializes a new instance of <see cref="DiscreteDoubleValueModifier"/> with 6 parameters
75    /// (<c>Value</c>, <c>StartValue</c>, <c>EndValue</c>, <c>Index</c>, <c>StartIndex</c>, <c>EndIndex</c>).
76    /// </summary>
77    protected DiscreteDoubleValueModifier()
78      : base() {
79      Parameters.Add(new LookupParameter<DoubleValue>("Value", "The double value to modify."));
80      Parameters.Add(new ValueLookupParameter<DoubleValue>("StartValue", "The start value of 'Value'."));
81      Parameters.Add(new ValueLookupParameter<DoubleValue>("EndValue", "The end value of 'Value'."));
82      Parameters.Add(new LookupParameter<IntValue>("Index", "The current index."));
83      Parameters.Add(new ValueLookupParameter<IntValue>("StartIndex", "The start index at which to start modifying 'Value'."));
84      Parameters.Add(new ValueLookupParameter<IntValue>("EndIndex", "The end index by which 'Value' should have reached 'EndValue'."));
85    }
86
87    /// <summary>
88    /// Checks whether index is between start and end and forwards the call to <see cref="Modify"/> if startIndex &lt; index &lt; endIndex.
89    /// </summary>
90    /// <remarks>
91    /// If index = startIndex the call will not be forwarded and startValue will be used. The same with endIndex and endValue.
92    /// </remarks>
93    /// <returns>What the base class returns.</returns>
94    public override IOperation Apply() {
95      int index = IndexParameter.ActualValue.Value, startIndex = StartIndexParameter.ActualValue.Value;
96      if (index >= startIndex) {
97        int endIndex = EndIndexParameter.ActualValue.Value;
98        DoubleValue value = ValueParameter.ActualValue;
99        double newValue = value.Value;
100        if (index == startIndex) {
101          newValue = StartValueParameter.ActualValue.Value;
102        } else if (index == endIndex) {
103          newValue = EndValueParameter.ActualValue.Value;
104        } else if (index < endIndex) {
105          double start = StartValueParameter.ActualValue.Value, end = EndValueParameter.ActualValue.Value;
106          newValue = Modify(value.Value, start, end, index, startIndex, endIndex);
107        }
108        if (value == null) value = new DoubleValue(newValue);
109        else value.Value = newValue;
110      }
111      return base.Apply();
112    }
113
114    /// <summary>
115    /// Modifies a given value according to two support points denoted by (startIndex; startValue) and (endIndex; endValue).
116    /// The current 'index' and the last value of 'value' is also given.
117    /// </summary>
118    /// <param name="value">The last value.</param>
119    /// <param name="startValue">The start value.</param>
120    /// <param name="endValue">The end value.</param>
121    /// <param name="index">The current index.</param>
122    /// <param name="startIndex">The start index.</param>
123    /// <param name="endIndex">The end index.</param>
124    /// <returns>The new value.</returns>
125    protected abstract double Modify(double value, double startValue, double endValue, int index, int startIndex, int endIndex);
126  }
127}
Note: See TracBrowser for help on using the repository browser.