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