1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Encodings.VariableVector {
|
---|
30 | [StorableClass]
|
---|
31 | [Item(Name = "DoubleVariable", Description = "")]
|
---|
32 | public class DoubleVariable : Variable<double> {
|
---|
33 | public override int VirtualLength {
|
---|
34 | get { return 2; }
|
---|
35 | }
|
---|
36 |
|
---|
37 | [Storable]
|
---|
38 | protected double min;
|
---|
39 | public double Min {
|
---|
40 | get { return min; }
|
---|
41 | }
|
---|
42 | [Storable]
|
---|
43 | protected double max;
|
---|
44 | public double Max {
|
---|
45 | get { return max; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | [Storable]
|
---|
49 | protected double currentCenter;
|
---|
50 | public double CurrentCenter {
|
---|
51 | get { return currentCenter; }
|
---|
52 | set { currentCenter = value; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | [Storable]
|
---|
56 | protected double currentSpread;
|
---|
57 | public double CurrentSpread {
|
---|
58 | get { return currentSpread; }
|
---|
59 | set { currentSpread = value; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | [StorableConstructor]
|
---|
63 | protected DoubleVariable(bool deserializing) : base(deserializing) { }
|
---|
64 | protected DoubleVariable(DoubleVariable original, Cloner cloner)
|
---|
65 | : base(original, cloner) {
|
---|
66 | min = original.min;
|
---|
67 | max = original.max;
|
---|
68 | }
|
---|
69 | public DoubleVariable() : base() { }
|
---|
70 | public DoubleVariable(string variableName, IEnumerable<double> variableValues)
|
---|
71 | : base(variableName) {
|
---|
72 | min = variableValues.Min();
|
---|
73 | max = variableValues.Max();
|
---|
74 | }
|
---|
75 | protected DoubleVariable(string variableName, double min, double max)
|
---|
76 | : base(variableName) {
|
---|
77 | this.min = min;
|
---|
78 | this.max = max;
|
---|
79 | }
|
---|
80 | public DoubleVariable(string variableName, IEnumerable<double> variableValues, double center, double spread)
|
---|
81 | : this(variableName, variableValues) {
|
---|
82 | CurrentCenter = center;
|
---|
83 | CurrentSpread = spread;
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
87 | return new DoubleVariable(this, cloner);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override string ToString() {
|
---|
91 | return (currentCenter - currentCenter) + " - " + (currentCenter + currentSpread);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public override bool MatchInput(string target) {
|
---|
95 | return Match(Double.Parse(target));
|
---|
96 | }
|
---|
97 |
|
---|
98 | public override bool Match(double target) {
|
---|
99 | return currentCenter - currentSpread < target && target < currentCenter + currentSpread;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public override IVariable GetEmptyCopy() {
|
---|
103 | return new DoubleVariable(variableName, min, max);
|
---|
104 | }
|
---|
105 | public override IVariable GetSetCopy() {
|
---|
106 | DoubleVariable copy = (DoubleVariable)GetEmptyCopy();
|
---|
107 | copy.CurrentCenter = this.CurrentCenter;
|
---|
108 | copy.CurrentSpread = this.CurrentSpread;
|
---|
109 | return copy;
|
---|
110 | }
|
---|
111 |
|
---|
112 | // Important! If this mehtod is called instead of the more concrete "Randomize(IRandom random, double spreadPercentage)"
|
---|
113 | // spread percentage is 50%
|
---|
114 | public override void Randomize(IRandom random) {
|
---|
115 | Randomize(random, 50);
|
---|
116 | }
|
---|
117 |
|
---|
118 | public void Randomize(IRandom random, double spreadPercentage) {
|
---|
119 | if (spreadPercentage < 0 || spreadPercentage > 100) {
|
---|
120 | throw new ArgumentException("Spread percentage has to be between 0 and 100.");
|
---|
121 | }
|
---|
122 | double delta = Max - Min;
|
---|
123 | CurrentCenter = random.NextDouble() * delta + Min;
|
---|
124 | CurrentSpread = random.NextDouble() * (delta * (spreadPercentage / 100));
|
---|
125 | }
|
---|
126 |
|
---|
127 | public override bool Identical(IVariable target) {
|
---|
128 | var targetCast = target as DoubleVariable;
|
---|
129 | if (targetCast == null) { return false; }
|
---|
130 | if (variableName != targetCast.variableName || currentCenter != targetCast.currentCenter
|
---|
131 | || currentSpread != targetCast.currentSpread || max != targetCast.max
|
---|
132 | || min != targetCast.min) { return false; }
|
---|
133 |
|
---|
134 | return true;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public override double GetGenerality() {
|
---|
138 | double delta = max - min;
|
---|
139 | double intervalInBoundsWidth = Math.Min(max, currentCenter + currentSpread) - Math.Max(min, currentCenter - currentSpread);
|
---|
140 | double generality = intervalInBoundsWidth / delta;
|
---|
141 | return generality > 1 ? 1 : generality;
|
---|
142 | }
|
---|
143 |
|
---|
144 | public override bool IsGreaterThanOrEquallyGeneral(IVariable target) {
|
---|
145 | var targetCast = target as DoubleVariable;
|
---|
146 | if (targetCast == null) { return false; }
|
---|
147 | return currentCenter - currentSpread < targetCast.currentCenter - targetCast.currentSpread
|
---|
148 | && currentCenter + currentSpread > targetCast.currentCenter + targetCast.currentSpread;
|
---|
149 | }
|
---|
150 |
|
---|
151 | public new DoubleVariable CrossParentsAtPosition(IVariable parent2, int pos) {
|
---|
152 | var parent2Cast = parent2 as DoubleVariable;
|
---|
153 | if (parent2Cast == null) { throw new ArgumentException("Argument is not of the correct type."); }
|
---|
154 | if (pos > 1 || pos < 0) { throw new ArgumentOutOfRangeException(); }
|
---|
155 | DoubleVariable crossed = (DoubleVariable)this.GetSetCopy();
|
---|
156 | if (pos == 0) {
|
---|
157 | crossed.CurrentSpread = parent2Cast.CurrentSpread;
|
---|
158 | }
|
---|
159 | return crossed;
|
---|
160 | }
|
---|
161 |
|
---|
162 | public override void Manipulate(IRandom random, string stringValue, int pos) {
|
---|
163 | if (pos > 1 || pos < 0) { throw new ArgumentOutOfRangeException(); }
|
---|
164 | Manipulate(random, pos, 10);
|
---|
165 | }
|
---|
166 |
|
---|
167 | public void Manipulate(IRandom random, int pos, double percentage) {
|
---|
168 | if (pos > 1 || pos < 0) { throw new ArgumentOutOfRangeException(); }
|
---|
169 | double delta = max - min;
|
---|
170 | double maxChange = delta * (percentage / 100);
|
---|
171 | double actualChange = (random.NextDouble() * maxChange * 2) - maxChange;
|
---|
172 | if (pos == 0) {
|
---|
173 | currentCenter += actualChange;
|
---|
174 | } else if (pos == 1) {
|
---|
175 | currentSpread += actualChange;
|
---|
176 | //otherwise the interval could be corrupt and no input could match the rule.
|
---|
177 | currentSpread = currentSpread > 0 ? currentSpread : 0;
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | public override void Cover(IRandom random, string stringValue, double changeSymbolProbability) {
|
---|
182 | Cover(random, stringValue, 50);
|
---|
183 | }
|
---|
184 |
|
---|
185 | public void CoverWithSpreadPercentage(IRandom random, string stringValue, double spreadPercentage) {
|
---|
186 | currentCenter = double.Parse(stringValue);
|
---|
187 | double delta = max - min;
|
---|
188 | currentSpread = random.NextDouble() * (delta * (spreadPercentage / 100));
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|