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.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Optimization.Operators;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
35 | [Item("RealVectorSwarmUpdater", "Updates personal best point and quality as well as global best point and quality.")]
|
---|
36 | [StorableClass]
|
---|
37 | public sealed class RealVectorSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater {
|
---|
38 |
|
---|
39 | [Storable]
|
---|
40 | private ResultsCollector ResultsCollector;
|
---|
41 |
|
---|
42 | public override bool CanChangeName {
|
---|
43 | get { return false; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | #region Parameter properties
|
---|
47 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
48 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
49 | }
|
---|
50 | public IScopeTreeLookupParameter<DoubleValue> PersonalBestQualityParameter {
|
---|
51 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["PersonalBestQuality"]; }
|
---|
52 | }
|
---|
53 | public IScopeTreeLookupParameter<DoubleValue> NeighborBestQualityParameter {
|
---|
54 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["NeighborBestQuality"]; }
|
---|
55 | }
|
---|
56 | public IScopeTreeLookupParameter<RealVector> RealVectorParameter {
|
---|
57 | get { return (IScopeTreeLookupParameter<RealVector>)Parameters["RealVector"]; }
|
---|
58 | }
|
---|
59 | public IScopeTreeLookupParameter<RealVector> PersonalBestParameter {
|
---|
60 | get { return (IScopeTreeLookupParameter<RealVector>)Parameters["PersonalBest"]; }
|
---|
61 | }
|
---|
62 | public IScopeTreeLookupParameter<RealVector> NeighborBestParameter {
|
---|
63 | get { return (IScopeTreeLookupParameter<RealVector>)Parameters["NeighborBest"]; }
|
---|
64 | }
|
---|
65 | public ILookupParameter<BoolValue> MaximizationParameter {
|
---|
66 | get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
67 | }
|
---|
68 | public ILookupParameter<DoubleValue> SwarmBestQualityParameter {
|
---|
69 | get { return (ILookupParameter<DoubleValue>)Parameters["SwarmBestQuality"]; }
|
---|
70 | }
|
---|
71 | public ILookupParameter<RealVector> BestRealVectorParameter {
|
---|
72 | get { return (ILookupParameter<RealVector>)Parameters["BestRealVector"]; }
|
---|
73 | }
|
---|
74 | public IScopeTreeLookupParameter<IntArray> NeighborsParameter {
|
---|
75 | get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; }
|
---|
76 | }
|
---|
77 | public IValueLookupParameter<DoubleMatrix> VelocityBoundsParameter {
|
---|
78 | get { return (ValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
|
---|
79 | }
|
---|
80 | public ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter {
|
---|
81 | get { return (ILookupParameter<DoubleMatrix>)Parameters["CurrentVelocityBounds"]; }
|
---|
82 | }
|
---|
83 | public LookupParameter<ResultCollection> ResultsParameter {
|
---|
84 | get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
85 | }
|
---|
86 |
|
---|
87 | #region Velocity Bounds Updating
|
---|
88 | public ILookupParameter<DoubleValue> VelocityBoundsScaleParameter {
|
---|
89 | get { return (ILookupParameter<DoubleValue>)Parameters["VelocityBoundsScale"]; }
|
---|
90 | }
|
---|
91 | public OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier> VelocityBoundsScalingOperatorParameter {
|
---|
92 | get { return (OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["VelocityBoundsScalingOperator"]; }
|
---|
93 | }
|
---|
94 | public IValueLookupParameter<DoubleValue> VelocityBoundsStartValueParameter {
|
---|
95 | get { return (IValueLookupParameter<DoubleValue>)Parameters["VelocityBoundsStartValue"]; }
|
---|
96 | }
|
---|
97 | public IValueLookupParameter<DoubleValue> VelocityBoundsEndValueParameter {
|
---|
98 | get { return (IValueLookupParameter<DoubleValue>)Parameters["VelocityBoundsEndValue"]; }
|
---|
99 | }
|
---|
100 | public ILookupParameter<IntValue> VelocityBoundsIndexParameter {
|
---|
101 | get { return (ILookupParameter<IntValue>)Parameters["VelocityBoundsIndex"]; }
|
---|
102 | }
|
---|
103 | public IValueLookupParameter<IntValue> VelocityBoundsStartIndexParameter {
|
---|
104 | get { return (IValueLookupParameter<IntValue>)Parameters["VelocityBoundsStartIndex"]; }
|
---|
105 | }
|
---|
106 | public IValueLookupParameter<IntValue> VelocityBoundsEndIndexParameter {
|
---|
107 | get { return (IValueLookupParameter<IntValue>)Parameters["VelocityBoundsEndIndex"]; }
|
---|
108 | }
|
---|
109 | #endregion
|
---|
110 |
|
---|
111 | #endregion
|
---|
112 |
|
---|
113 | #region Parameter values
|
---|
114 | private DoubleValue SwarmBestQuality {
|
---|
115 | get { return SwarmBestQualityParameter.ActualValue; }
|
---|
116 | set { SwarmBestQualityParameter.ActualValue = value; }
|
---|
117 | }
|
---|
118 | private RealVector BestRealVector {
|
---|
119 | get { return BestRealVectorParameter.ActualValue; }
|
---|
120 | set { BestRealVectorParameter.ActualValue = value; }
|
---|
121 | }
|
---|
122 | private ItemArray<DoubleValue> Quality {
|
---|
123 | get { return QualityParameter.ActualValue; }
|
---|
124 | }
|
---|
125 | private ItemArray<DoubleValue> PersonalBestQuality {
|
---|
126 | get { return PersonalBestQualityParameter.ActualValue; }
|
---|
127 | set { PersonalBestQualityParameter.ActualValue = value; }
|
---|
128 | }
|
---|
129 | private ItemArray<DoubleValue> NeighborBestQuality {
|
---|
130 | get { return NeighborBestQualityParameter.ActualValue; }
|
---|
131 | set { NeighborBestQualityParameter.ActualValue = value; }
|
---|
132 | }
|
---|
133 | private ItemArray<RealVector> RealVector {
|
---|
134 | get { return RealVectorParameter.ActualValue; }
|
---|
135 | }
|
---|
136 | private ItemArray<RealVector> PersonalBest {
|
---|
137 | get { return PersonalBestParameter.ActualValue; }
|
---|
138 | set { PersonalBestParameter.ActualValue = value; }
|
---|
139 | }
|
---|
140 | private ItemArray<RealVector> NeighborBest {
|
---|
141 | get { return NeighborBestParameter.ActualValue; }
|
---|
142 | set { NeighborBestParameter.ActualValue = value; }
|
---|
143 | }
|
---|
144 | private bool Maximization {
|
---|
145 | get { return MaximizationParameter.ActualValue.Value; }
|
---|
146 | }
|
---|
147 | private ItemArray<IntArray> Neighbors {
|
---|
148 | get { return NeighborsParameter.ActualValue; }
|
---|
149 | }
|
---|
150 | private DoubleMatrix VelocityBounds {
|
---|
151 | get { return VelocityBoundsParameter.ActualValue; }
|
---|
152 | }
|
---|
153 | private DoubleMatrix CurrentVelocityBounds {
|
---|
154 | get { return CurrentVelocityBoundsParameter.ActualValue; }
|
---|
155 | set { CurrentVelocityBoundsParameter.ActualValue = value; }
|
---|
156 | }
|
---|
157 | private DoubleValue VelocityBoundsScale {
|
---|
158 | get { return VelocityBoundsScaleParameter.ActualValue; }
|
---|
159 | set { VelocityBoundsScaleParameter.ActualValue = value; }
|
---|
160 | }
|
---|
161 | private DoubleValue VelocityBoundsStartValue {
|
---|
162 | get { return VelocityBoundsStartValueParameter.ActualValue; }
|
---|
163 | }
|
---|
164 | private IDiscreteDoubleValueModifier VelocityBoundsScalingOperator {
|
---|
165 | get { return VelocityBoundsScalingOperatorParameter.Value; }
|
---|
166 | }
|
---|
167 | private ResultCollection Results {
|
---|
168 | get { return ResultsParameter.ActualValue; }
|
---|
169 | }
|
---|
170 | #endregion
|
---|
171 |
|
---|
172 | #region Construction & Cloning
|
---|
173 |
|
---|
174 | [StorableConstructor]
|
---|
175 | private RealVectorSwarmUpdater(bool deserializing) : base(deserializing) { }
|
---|
176 | private RealVectorSwarmUpdater(RealVectorSwarmUpdater original, Cloner cloner)
|
---|
177 | : base(original, cloner) {
|
---|
178 | ResultsCollector = cloner.Clone(original.ResultsCollector);
|
---|
179 | }
|
---|
180 | public RealVectorSwarmUpdater()
|
---|
181 | : base() {
|
---|
182 | Parameters.Add(new LookupParameter<DoubleValue>("SwarmBestQuality", "Swarm's best quality."));
|
---|
183 | Parameters.Add(new LookupParameter<RealVector>("BestRealVector", "Global best particle position."));
|
---|
184 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "Particles' qualities."));
|
---|
185 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("PersonalBestQuality", "Particles' personal best qualities."));
|
---|
186 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("NeighborBestQuality", "Best neighbor particles' qualities."));
|
---|
187 | Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "Particles' positions."));
|
---|
188 | Parameters.Add(new ScopeTreeLookupParameter<RealVector>("PersonalBest", "Particles' personal best positions."));
|
---|
189 | Parameters.Add(new ScopeTreeLookupParameter<RealVector>("NeighborBest", "Neighborhood (or global in case of totally connected neighborhood) best particle positions."));
|
---|
190 | Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle."));
|
---|
191 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
192 | Parameters.Add(new ValueLookupParameter<DoubleMatrix>("VelocityBounds", "Maximum velocity for each dimension.", new DoubleMatrix(new double[,] { { -1, 1 } })));
|
---|
193 | Parameters.Add(new LookupParameter<DoubleMatrix>("CurrentVelocityBounds", "Current value of velocity bounds."));
|
---|
194 | Parameters.Add(new LookupParameter<ResultCollection>("Results", "Results"));
|
---|
195 |
|
---|
196 | #region Velocity Bounds Updating
|
---|
197 | Parameters.Add(new LookupParameter<DoubleValue>("VelocityBoundsScale", "Scale parameter."));
|
---|
198 | Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("VelocityBoundsScalingOperator", "Modifies the value"));
|
---|
199 | Parameters.Add(new ValueLookupParameter<DoubleValue>("VelocityBoundsStartValue", "The start value of 'Value'.", new DoubleValue(1)));
|
---|
200 | Parameters.Add(new ValueLookupParameter<DoubleValue>("VelocityBoundsEndValue", "The end value of 'Value'.", new DoubleValue(1E-10)));
|
---|
201 | Parameters.Add(new LookupParameter<IntValue>("VelocityBoundsIndex", "The current index.", "Iterations"));
|
---|
202 | Parameters.Add(new ValueLookupParameter<IntValue>("VelocityBoundsStartIndex", "The start index at which to start modifying 'Value'.", new IntValue(0)));
|
---|
203 | Parameters.Add(new ValueLookupParameter<IntValue>("VelocityBoundsEndIndex", "The end index by which 'Value' should have reached 'EndValue'.", "MaxIterations"));
|
---|
204 | VelocityBoundsStartIndexParameter.Hidden = true;
|
---|
205 | VelocityBoundsEndIndexParameter.Hidden = true;
|
---|
206 | #endregion
|
---|
207 |
|
---|
208 | Initialize();
|
---|
209 | RegisterEvents();
|
---|
210 | }
|
---|
211 |
|
---|
212 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
213 | return new RealVectorSwarmUpdater(this, cloner);
|
---|
214 | }
|
---|
215 |
|
---|
216 | #endregion
|
---|
217 |
|
---|
218 | [StorableHook(HookType.AfterDeserialization)]
|
---|
219 | private void AfterDeserialization() {
|
---|
220 | if (!Parameters.ContainsKey("SwarmBestQuality")) {
|
---|
221 | ILookupParameter<DoubleValue> oldBestQualityParameter = Parameters["BestQuality"] as ILookupParameter<DoubleValue>;
|
---|
222 | Parameters.Add(new LookupParameter<DoubleValue>("SwarmBestQuality", "Swarm's best quality."));
|
---|
223 | if (oldBestQualityParameter.ActualName != oldBestQualityParameter.Name)
|
---|
224 | SwarmBestQualityParameter.ActualName = oldBestQualityParameter.ActualName;
|
---|
225 | Parameters.Remove("BestQuality");
|
---|
226 | }
|
---|
227 | RegisterEvents();
|
---|
228 | }
|
---|
229 |
|
---|
230 | private void RegisterEvents() {
|
---|
231 | VelocityBoundsStartValueParameter.ValueChanged += new EventHandler(VelocityBoundsStartValueParameter_ValueChanged);
|
---|
232 | VelocityBoundsStartValueParameter.Value.ValueChanged += new EventHandler(VelocityBoundsStartValueParameter_Value_ValueChanged);
|
---|
233 | }
|
---|
234 |
|
---|
235 | void VelocityBoundsStartValueParameter_Value_ValueChanged(object sender, EventArgs e) {
|
---|
236 | UpdateVelocityBoundsParamater();
|
---|
237 | }
|
---|
238 |
|
---|
239 | void UpdateVelocityBoundsParamater() {
|
---|
240 | if (VelocityBoundsParameter.Value == null) {
|
---|
241 | VelocityBoundsParameter.Value = new DoubleMatrix(1, 2);
|
---|
242 | } else if (VelocityBoundsParameter.Value.Columns != 2) {
|
---|
243 | VelocityBoundsParameter.Value = new DoubleMatrix(VelocityBoundsParameter.Value.Rows, 2);
|
---|
244 | }
|
---|
245 | if (VelocityBoundsStartValueParameter.Value != null) {
|
---|
246 | DoubleMatrix matrix = VelocityBoundsParameter.Value;
|
---|
247 | for (int i = 0; i < matrix.Rows; i++) {
|
---|
248 | matrix[i, 0] = (-1) * VelocityBoundsStartValueParameter.Value.Value;
|
---|
249 | matrix[i, 1] = VelocityBoundsStartValueParameter.Value.Value;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | void VelocityBoundsStartValueParameter_ValueChanged(object sender, EventArgs e) {
|
---|
255 | if (VelocityBoundsStartValueParameter.Value != null) {
|
---|
256 | VelocityBoundsStartValueParameter.Value.ValueChanged += new EventHandler(VelocityBoundsStartValueParameter_Value_ValueChanged);
|
---|
257 | }
|
---|
258 | UpdateVelocityBoundsParamater();
|
---|
259 | }
|
---|
260 |
|
---|
261 | private void Initialize() {
|
---|
262 | ResultsCollector = new ResultsCollector();
|
---|
263 | ResultsCollector.CollectedValues.Add(CurrentVelocityBoundsParameter);
|
---|
264 | ResultsCollector.CollectedValues.Add(VelocityBoundsParameter);
|
---|
265 |
|
---|
266 | foreach (IDiscreteDoubleValueModifier op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>()) {
|
---|
267 | VelocityBoundsScalingOperatorParameter.ValidValues.Add(op);
|
---|
268 | op.ValueParameter.ActualName = VelocityBoundsScaleParameter.Name;
|
---|
269 | op.StartValueParameter.ActualName = VelocityBoundsStartValueParameter.Name;
|
---|
270 | op.EndValueParameter.ActualName = VelocityBoundsEndValueParameter.Name;
|
---|
271 | op.IndexParameter.ActualName = VelocityBoundsIndexParameter.Name;
|
---|
272 | op.StartIndexParameter.ActualName = VelocityBoundsStartIndexParameter.Name;
|
---|
273 | op.EndIndexParameter.ActualName = VelocityBoundsEndIndexParameter.Name;
|
---|
274 | }
|
---|
275 | VelocityBoundsScalingOperatorParameter.Value = null;
|
---|
276 | }
|
---|
277 |
|
---|
278 | public override IOperation Apply() {
|
---|
279 | UpdateGlobalBest();
|
---|
280 | UpdateNeighborBest();
|
---|
281 | UpdatePersonalBest();
|
---|
282 | return UpdateVelocityBounds();
|
---|
283 | }
|
---|
284 |
|
---|
285 | private void UpdateGlobalBest() {
|
---|
286 | if (SwarmBestQuality == null)
|
---|
287 | SwarmBestQuality = new DoubleValue();
|
---|
288 | SwarmBestQuality.Value = Maximization ? Quality.Max(v => v.Value) : Quality.Min(v => v.Value);
|
---|
289 | BestRealVector = (RealVector)RealVector[Quality.FindIndex(v => v.Value == SwarmBestQuality.Value)].Clone();
|
---|
290 | }
|
---|
291 |
|
---|
292 | private void UpdateNeighborBest() {
|
---|
293 | if (Neighbors.Length > 0) {
|
---|
294 | var neighborBest = new ItemArray<RealVector>(Neighbors.Length);
|
---|
295 | var neighborBestQuality = new ItemArray<DoubleValue>(Neighbors.Length);
|
---|
296 | for (int n = 0; n < Neighbors.Length; n++) {
|
---|
297 | var pairs = Quality.Zip(RealVector, (q, p) => new { Quality = q, Point = p })
|
---|
298 | .Where((p, i) => i == n || Neighbors[n].Contains(i));
|
---|
299 | var bestNeighbor = Maximization ?
|
---|
300 | pairs.OrderByDescending(p => p.Quality.Value).First() :
|
---|
301 | pairs.OrderBy(p => p.Quality.Value).First();
|
---|
302 | neighborBest[n] = bestNeighbor.Point;
|
---|
303 | neighborBestQuality[n] = bestNeighbor.Quality;
|
---|
304 | }
|
---|
305 | NeighborBest = neighborBest;
|
---|
306 | NeighborBestQuality = neighborBestQuality;
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | private void UpdatePersonalBest() {
|
---|
311 | if (PersonalBestQuality.Length == 0)
|
---|
312 | PersonalBestQuality = (ItemArray<DoubleValue>)Quality.Clone();
|
---|
313 | for (int i = 0; i < RealVector.Length; i++) {
|
---|
314 | if (Maximization && Quality[i].Value > PersonalBestQuality[i].Value ||
|
---|
315 | !Maximization && Quality[i].Value < PersonalBestQuality[i].Value) {
|
---|
316 | PersonalBestQuality[i].Value = Quality[i].Value;
|
---|
317 | PersonalBest[i] = RealVector[i];
|
---|
318 | }
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | private IOperation UpdateVelocityBounds() {
|
---|
323 | if (CurrentVelocityBounds == null)
|
---|
324 | CurrentVelocityBounds = (DoubleMatrix)VelocityBounds.Clone();
|
---|
325 |
|
---|
326 | if (VelocityBoundsScalingOperator == null)
|
---|
327 | return new OperationCollection() {
|
---|
328 | ExecutionContext.CreateChildOperation(ResultsCollector),
|
---|
329 | base.Apply()
|
---|
330 | };
|
---|
331 |
|
---|
332 | DoubleMatrix matrix = CurrentVelocityBounds;
|
---|
333 | if (VelocityBoundsScale == null && VelocityBoundsStartValue != null) {
|
---|
334 | VelocityBoundsScale = new DoubleValue(VelocityBoundsStartValue.Value);
|
---|
335 | }
|
---|
336 | for (int i = 0; i < matrix.Rows; i++) {
|
---|
337 | for (int j = 0; j < matrix.Columns; j++) {
|
---|
338 | if (matrix[i, j] >= 0) {
|
---|
339 | matrix[i, j] = VelocityBoundsScale.Value;
|
---|
340 | } else {
|
---|
341 | matrix[i, j] = (-1) * VelocityBoundsScale.Value;
|
---|
342 | }
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | return new OperationCollection() {
|
---|
347 | ExecutionContext.CreateChildOperation(ResultsCollector),
|
---|
348 | ExecutionContext.CreateChildOperation(VelocityBoundsScalingOperator),
|
---|
349 | base.Apply()
|
---|
350 | };
|
---|
351 | }
|
---|
352 | }
|
---|
353 | }
|
---|