Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSOSwarmUpdater.cs @ 17388

Last change on this file since 17388 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 12.9 KB
RevLine 
[15102]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[15102]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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Parameters;
[16565]32using HEAL.Attic;
[15102]33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Encodings.RealVectorEncoding {
[15181]36  [Item("Swarm Updater (SPSO)", "Updates personal best point and quality as well as neighbor best point and quality.")]
[16565]37  [StorableType("B8244196-9DB9-477C-A0A1-C1EB5BF4E1C1")]
[15102]38  public sealed class SPSOSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater, ISingleObjectiveOperator {
39
40    [Storable]
41    private ResultsCollector ResultsCollector;
42
43    public override bool CanChangeName {
44      get { return false; }
45    }
46
47    #region Parameter properties
48    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
49      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
50    }
51    public IScopeTreeLookupParameter<DoubleValue> PersonalBestQualityParameter {
52      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["PersonalBestQuality"]; }
53    }
54    public IScopeTreeLookupParameter<DoubleValue> NeighborBestQualityParameter {
55      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["NeighborBestQuality"]; }
56    }
57    public IScopeTreeLookupParameter<RealVector> RealVectorParameter {
58      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["RealVector"]; }
59    }
60    public IScopeTreeLookupParameter<RealVector> PersonalBestParameter {
61      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["PersonalBest"]; }
62    }
63    public IScopeTreeLookupParameter<RealVector> NeighborBestParameter {
64      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["NeighborBest"]; }
65    }
66    public ILookupParameter<BoolValue> MaximizationParameter {
67      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
68    }
69    public ILookupParameter<DoubleValue> SwarmBestQualityParameter {
70      get { return (ILookupParameter<DoubleValue>)Parameters["SwarmBestQuality"]; }
71    }
72    public ILookupParameter<RealVector> BestRealVectorParameter {
73      get { return (ILookupParameter<RealVector>)Parameters["BestRealVector"]; }
74    }
75    public IScopeTreeLookupParameter<IntArray> NeighborsParameter {
76      get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; }
77    }
78    public IValueLookupParameter<DoubleValue> MaxVelocityParameter {
79      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaxVelocity"]; }
80    }
81    public ILookupParameter<DoubleValue> CurrentMaxVelocityParameter {
82      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; }
83    }
84    public LookupParameter<ResultCollection> ResultsParameter {
85      get { return (LookupParameter<ResultCollection>)Parameters["Results"]; }
86    }
87
88    #region Max Velocity Updating
89    public IConstrainedValueParameter<IDiscreteDoubleValueModifier> MaxVelocityScalingOperatorParameter {
90      get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["MaxVelocityScalingOperator"]; }
91    }
92    public IValueLookupParameter<DoubleValue> FinalMaxVelocityParameter {
93      get { return (IValueLookupParameter<DoubleValue>)Parameters["FinalMaxVelocity"]; }
94    }
95    public ILookupParameter<IntValue> MaxVelocityIndexParameter {
96      get { return (ILookupParameter<IntValue>)Parameters["MaxVelocityIndex"]; }
97    }
98    public IValueLookupParameter<IntValue> MaxVelocityStartIndexParameter {
99      get { return (IValueLookupParameter<IntValue>)Parameters["MaxVelocityStartIndex"]; }
100    }
101    public IValueLookupParameter<IntValue> MaxVelocityEndIndexParameter {
102      get { return (IValueLookupParameter<IntValue>)Parameters["MaxVelocityEndIndex"]; }
103    }
104    #endregion
105
106    #endregion
107   
108    #region Construction & Cloning
109
110    [StorableConstructor]
[16565]111    private SPSOSwarmUpdater(StorableConstructorFlag _) : base(_) { }
[15102]112    private SPSOSwarmUpdater(SPSOSwarmUpdater original, Cloner cloner)
113      : base(original, cloner) {
114      ResultsCollector = cloner.Clone(original.ResultsCollector);
115    }
116    public SPSOSwarmUpdater()
117      : base() {
118      Parameters.Add(new LookupParameter<DoubleValue>("SwarmBestQuality", "Swarm's best quality."));
119      Parameters.Add(new LookupParameter<RealVector>("BestRealVector", "Global best particle position."));
120      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "Particles' qualities."));
121      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("PersonalBestQuality", "Particles' personal best qualities."));
122      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("NeighborBestQuality", "Best neighbor particles' qualities."));
123      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "Particles' positions."));
124      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("PersonalBest", "Particles' personal best positions."));
125      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("NeighborBest", "Neighborhood (or global in case of totally connected neighborhood) best particle positions."));
126      Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle."));
127      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
[15181]128      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaxVelocity", "The maximum velocity for each particle and initial velocity if scaling is used.", new DoubleValue(double.MaxValue)));
[15102]129      Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Current value of the speed limit."));
130      Parameters.Add(new LookupParameter<ResultCollection>("Results", "Results"));
131
132      #region Max Velocity Updating
133      Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("MaxVelocityScalingOperator", "Modifies the value"));
[15181]134      Parameters.Add(new ValueLookupParameter<DoubleValue>("FinalMaxVelocity", "The value of maximum velocity if scaling is used and PSO has reached maximum iterations.", new DoubleValue(1E-10)));
[15102]135      Parameters.Add(new LookupParameter<IntValue>("MaxVelocityIndex", "The current index.", "Iterations"));
136      Parameters.Add(new ValueLookupParameter<IntValue>("MaxVelocityStartIndex", "The start index at which to start modifying 'Value'.", new IntValue(0)));
137      Parameters.Add(new ValueLookupParameter<IntValue>("MaxVelocityEndIndex", "The end index by which 'Value' should have reached 'EndValue'.", "MaxIterations"));
138      MaxVelocityStartIndexParameter.Hidden = true;
139      MaxVelocityEndIndexParameter.Hidden = true;
140      #endregion
141
142      Initialize();
143    }
144
145    public override IDeepCloneable Clone(Cloner cloner) {
146      return new SPSOSwarmUpdater(this, cloner);
147    }
148
149    #endregion
150
151    private void Initialize() {
152      ResultsCollector = new ResultsCollector();
153      ResultsCollector.CollectedValues.Add(CurrentMaxVelocityParameter);
154      ResultsCollector.CollectedValues.Add(MaxVelocityParameter);
155
156      foreach (IDiscreteDoubleValueModifier op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>()) {
157        MaxVelocityScalingOperatorParameter.ValidValues.Add(op);
158        op.ValueParameter.ActualName = CurrentMaxVelocityParameter.Name;
159        op.StartValueParameter.ActualName = MaxVelocityParameter.Name;
160        op.EndValueParameter.ActualName = FinalMaxVelocityParameter.Name;
161        op.IndexParameter.ActualName = MaxVelocityIndexParameter.Name;
162        op.StartIndexParameter.ActualName = MaxVelocityStartIndexParameter.Name;
163        op.EndIndexParameter.ActualName = MaxVelocityEndIndexParameter.Name;
164      }
165      MaxVelocityScalingOperatorParameter.Value = null;
166    }
167
168    public override IOperation Apply() {
169      var max = MaximizationParameter.ActualValue.Value;
170      // Update of the personal bests
171      var points = RealVectorParameter.ActualValue;
172      var qualities = QualityParameter.ActualValue;
173      var particles = points.Select((p, i) => new { Particle = p, Index = i })
174        .Zip(qualities, (p, q) => Tuple.Create(p.Index, p.Particle, q.Value)).ToList();
175      UpdatePersonalBest(max, particles);
176
177      // SPSO: update of the neighbor bests from the personal bests
178      var personalBestPoints = PersonalBestParameter.ActualValue;
179      var personalBestQualities = PersonalBestQualityParameter.ActualValue;
180      particles = personalBestPoints.Select((p, i) => new { Particle = p, Index = i })
181        .Zip(personalBestQualities, (p, q) => Tuple.Create(p.Index, p.Particle, q.Value)).ToList();
182      UpdateNeighborBest(max, particles);
183
184      var next = new OperationCollection() { base.Apply() };
185      next.Insert(0, ExecutionContext.CreateChildOperation(ResultsCollector));
186      if (MaxVelocityScalingOperatorParameter.Value != null) {
187        next.Insert(0, ExecutionContext.CreateChildOperation(MaxVelocityScalingOperatorParameter.Value));
188      } else CurrentMaxVelocityParameter.ActualValue = new DoubleValue(MaxVelocityParameter.ActualValue.Value);
189      return next;
190    }
191
192    private void UpdateNeighborBest(bool maximization, IList<Tuple<int, RealVector, double>> particles) {
193      var neighbors = NeighborsParameter.ActualValue;
194      if (neighbors.Length > 0) {
195        var neighborBest = new ItemArray<RealVector>(neighbors.Length);
196        var neighborBestQuality = new ItemArray<DoubleValue>(neighbors.Length);
[15181]197        double overallBest = double.NaN;
198        RealVector overallBestVector = null;
[15102]199        for (int n = 0; n < neighbors.Length; n++) {
[15181]200          var neighborhood = particles.Where(x => neighbors[n].Contains(x.Item1));
201          var bestNeighbor = (maximization ? neighborhood.MaxItems(p => p.Item3)
202                                           : neighborhood.MinItems(p => p.Item3)).First();
[15102]203          neighborBest[n] = bestNeighbor.Item2;
204          neighborBestQuality[n] = new DoubleValue(bestNeighbor.Item3);
[15181]205          if (double.IsNaN(overallBest) || maximization && bestNeighbor.Item3 > overallBest
206            || !maximization && bestNeighbor.Item3 < overallBest) {
207            overallBest = bestNeighbor.Item3;
208            overallBestVector = bestNeighbor.Item2;
209          }
[15102]210        }
211        NeighborBestParameter.ActualValue = neighborBest;
212        NeighborBestQualityParameter.ActualValue = neighborBestQuality;
[15181]213        SwarmBestQualityParameter.ActualValue = new DoubleValue(overallBest);
214        BestRealVectorParameter.ActualValue = overallBestVector;
[15102]215      } else {
216        // Neighbor best = Global best
217        var best = maximization ? particles.MaxItems(x => x.Item3).First() : particles.MinItems(x => x.Item3).First();
[15181]218        NeighborBestParameter.ActualValue = new ItemArray<RealVector>(Enumerable.Repeat(best.Item2, particles.Count));
219        NeighborBestQualityParameter.ActualValue = new ItemArray<DoubleValue>(Enumerable.Repeat(new DoubleValue(best.Item3), particles.Count));
220        SwarmBestQualityParameter.ActualValue = new DoubleValue(best.Item3);
221        BestRealVectorParameter.ActualValue = best.Item2;
[15102]222      }
223    }
224
225    private void UpdatePersonalBest(bool maximization, IList<Tuple<int, RealVector, double>> particles) {
226      var personalBest = PersonalBestParameter.ActualValue;
227      var personalBestQuality = PersonalBestQualityParameter.ActualValue;
228
229      if (personalBestQuality.Length == 0) {
230        personalBestQuality = new ItemArray<DoubleValue>(particles.Select(x => new DoubleValue(x.Item3)));
231        PersonalBestQualityParameter.ActualValue = personalBestQuality;
232      }
233      foreach (var p in particles) {
234        if (maximization && p.Item3 > personalBestQuality[p.Item1].Value ||
235          !maximization && p.Item3 < personalBestQuality[p.Item1].Value) {
236          personalBestQuality[p.Item1].Value = p.Item3;
[15181]237          personalBest[p.Item1] = new RealVector(p.Item2);
[15102]238        }
239      }
240      PersonalBestParameter.ActualValue = personalBest;
241    }
242  }
243}
Note: See TracBrowser for help on using the repository browser.