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 |
|
---|
22 | using HeuristicLab.Core;
|
---|
23 | using HeuristicLab.Data;
|
---|
24 | using HeuristicLab.Operators;
|
---|
25 | using HeuristicLab.Parameters;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace 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 >= 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 <= 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 < index < 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 | double newValue = value.Value;
|
---|
99 | if (index == startIndex) {
|
---|
100 | newValue = StartValueParameter.ActualValue.Value;
|
---|
101 | } else if (index == endIndex) {
|
---|
102 | newValue = EndValueParameter.ActualValue.Value;
|
---|
103 | } else if (index < endIndex) {
|
---|
104 | double start = StartValueParameter.ActualValue.Value, end = EndValueParameter.ActualValue.Value;
|
---|
105 | newValue = Modify(value.Value, start, end, index, startIndex, endIndex);
|
---|
106 | }
|
---|
107 | if (value == null) value = new DoubleValue(newValue);
|
---|
108 | else value.Value = newValue;
|
---|
109 | }
|
---|
110 | return base.Apply();
|
---|
111 | }
|
---|
112 |
|
---|
113 | /// <summary>
|
---|
114 | /// Modifies a given value according to two support points denoted by (startIndex; startValue) and (endIndex; endValue).
|
---|
115 | /// The current 'index' and the last value of 'value' is also given.
|
---|
116 | /// </summary>
|
---|
117 | /// <param name="value">The last value.</param>
|
---|
118 | /// <param name="startValue">The start value.</param>
|
---|
119 | /// <param name="endValue">The end value.</param>
|
---|
120 | /// <param name="index">The current index.</param>
|
---|
121 | /// <param name="startIndex">The start index.</param>
|
---|
122 | /// <param name="endIndex">The end index.</param>
|
---|
123 | /// <returns>The new value.</returns>
|
---|
124 | protected abstract double Modify(double value, double startValue, double endValue, int index, int startIndex, int endIndex);
|
---|
125 | }
|
---|
126 | }
|
---|