#1553: added remaining unit tests to create and run optimizer samples.
[hl.git] / sources / HeuristicLab.Optimization.Operators / 3.3 / DiscreteDoubleValueModifier.cs
blobffa9cbaca35ce7cee5b49d7191ac47f4fab059ed
1 #region License Information
2 /* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 * This file is part of HeuristicLab.
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.
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.
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/>.
20 #endregion
22 using HeuristicLab.Common;
23 using HeuristicLab.Core;
24 using HeuristicLab.Data;
25 using HeuristicLab.Operators;
26 using HeuristicLab.Parameters;
27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29 namespace 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 #region parameter properties
37 /// <summary>
38 /// The parameter that should be modified.
39 /// </summary>
40 public ILookupParameter<DoubleValue> ValueParameter {
41 get { return (ILookupParameter<DoubleValue>)Parameters["Value"]; }
43 /// <summary>
44 /// The start value of the parameter, will be assigned to <see cref="ValueParameter"/> as soon as <see cref="IndexParamter"/> equals <see cref="StartIndexParameter"/>.
45 /// </summary>
46 public IValueLookupParameter<DoubleValue> StartValueParameter {
47 get { return (IValueLookupParameter<DoubleValue>)Parameters["StartValue"]; }
49 /// <summary>
50 /// The end value of the parameter, will be assigned to <see cref="ValueParameter"/> as soon as <see cref="IndexParamter"/> equals <see cref="EndIndexParameter"/>.
51 /// </summary>
52 public IValueLookupParameter<DoubleValue> EndValueParameter {
53 get { return (IValueLookupParameter<DoubleValue>)Parameters["EndValue"]; }
55 /// <summary>
56 /// The index that denotes from which point in the function (relative to <see cref="StartIndexParameter"/> and <see cref="EndIndexParameter"/> the value should be assigned.
57 /// </summary>
58 public ILookupParameter<IntValue> IndexParameter {
59 get { return (ILookupParameter<IntValue>)Parameters["Index"]; }
61 /// <summary>
62 /// As soon as <see cref="IndexParameter"/> is &gt;= this parameter the value will start to be modified.
63 /// </summary>
64 public IValueLookupParameter<IntValue> StartIndexParameter {
65 get { return (IValueLookupParameter<IntValue>)Parameters["StartIndex"]; }
67 /// <summary>
68 /// As long as <see cref="IndexParameter"/> is &lt;= this parameter the value will start to be modified.
69 /// </summary>
70 public IValueLookupParameter<IntValue> EndIndexParameter {
71 get { return (IValueLookupParameter<IntValue>)Parameters["EndIndex"]; }
73 #endregion
74 [StorableConstructor]
75 protected DiscreteDoubleValueModifier(bool deserializing) : base(deserializing) { }
76 protected DiscreteDoubleValueModifier(DiscreteDoubleValueModifier original, Cloner cloner) : base(original, cloner) { }
77 /// <summary>
78 /// Initializes a new instance of <see cref="DiscreteDoubleValueModifier"/> with 6 parameters
79 /// (<c>Value</c>, <c>StartValue</c>, <c>EndValue</c>, <c>Index</c>, <c>StartIndex</c>, <c>EndIndex</c>).
80 /// </summary>
81 protected DiscreteDoubleValueModifier()
82 : base() {
83 Parameters.Add(new LookupParameter<DoubleValue>("Value", "The double value to modify."));
84 Parameters.Add(new ValueLookupParameter<DoubleValue>("StartValue", "The start value of 'Value'."));
85 Parameters.Add(new ValueLookupParameter<DoubleValue>("EndValue", "The end value of 'Value'."));
86 Parameters.Add(new LookupParameter<IntValue>("Index", "The current index."));
87 Parameters.Add(new ValueLookupParameter<IntValue>("StartIndex", "The start index at which to start modifying 'Value'."));
88 Parameters.Add(new ValueLookupParameter<IntValue>("EndIndex", "The end index by which 'Value' should have reached 'EndValue'."));
91 /// <summary>
92 /// Checks whether index is between start and end and forwards the call to <see cref="Modify"/> if startIndex &lt; index &lt; endIndex.
93 /// </summary>
94 /// <remarks>
95 /// If index = startIndex the call will not be forwarded and startValue will be used. The same with endIndex and endValue.
96 /// </remarks>
97 /// <returns>What the base class returns.</returns>
98 public override IOperation Apply() {
99 int index = IndexParameter.ActualValue.Value, startIndex = StartIndexParameter.ActualValue.Value;
100 if (index >= startIndex) {
101 int endIndex = EndIndexParameter.ActualValue.Value;
102 DoubleValue value = ValueParameter.ActualValue;
103 if (value == null) {
104 value = new DoubleValue();
105 ValueParameter.ActualValue = value;
107 double newValue = value.Value;
108 if (index == startIndex) {
109 newValue = StartValueParameter.ActualValue.Value;
110 } else if (index == endIndex) {
111 newValue = EndValueParameter.ActualValue.Value;
112 } else if (index < endIndex) {
113 double start = StartValueParameter.ActualValue.Value, end = EndValueParameter.ActualValue.Value;
114 newValue = Modify(value.Value, start, end, index, startIndex, endIndex);
116 value.Value = newValue;
118 return base.Apply();
121 /// <summary>
122 /// Modifies a given value according to two support points denoted by (startIndex; startValue) and (endIndex; endValue).
123 /// The current 'index' and the last value of 'value' is also given.
124 /// </summary>
125 /// <param name="value">The last value.</param>
126 /// <param name="startValue">The start value.</param>
127 /// <param name="endValue">The end value.</param>
128 /// <param name="index">The current index.</param>
129 /// <param name="startIndex">The start index.</param>
130 /// <param name="endIndex">The end index.</param>
131 /// <returns>The new value.</returns>
132 protected abstract double Modify(double value, double startValue, double endValue, int index, int startIndex, int endIndex);