Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Optimization.Operators/3.3/DiscreteDoubleValueModifier.cs @ 4678

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

Sorted usings and removed unused usings in entire solution (#1094)

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