Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs @ 5566

Last change on this file since 5566 was 5566, checked in by mkofler, 13 years ago

#852: PSO code refactoring. Worked on swarm updater and particle updater.

File size: 9.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.RealVectorEncoding {
31  [Item("Swarm Updater", "Updates personal best point and quality as well as global best point and quality.")]
32  [StorableClass]
33  public sealed class RealVectorSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater {
34    public override bool CanChangeName {
35      get { return false; }
36    }
37
38    #region Parameter properties
39    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
40      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
41    }
42    public IScopeTreeLookupParameter<DoubleValue> PersonalBestQualityParameter {
43      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["PersonalBestQuality"]; }
44    }
45    public IScopeTreeLookupParameter<DoubleValue> NeighborsBestQualityParameter {
46      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["NeighborsBestQuality"]; }
47    }
48    public IScopeTreeLookupParameter<RealVector> RealVectorParameter {
49      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["RealVector"]; }
50    }
51    public IScopeTreeLookupParameter<RealVector> PersonalBestParameter {
52      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["PersonalBest"]; }
53    }
54    public IScopeTreeLookupParameter<RealVector> NeighborsBestParameter {
55      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["NeighborsBest"]; }
56    }
57    public IValueLookupParameter<BoolValue> MaximizationParameter {
58      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
59    }
60    public ILookupParameter<DoubleValue> BestQualityParameter {
61      get { return (ILookupParameter<DoubleValue>)Parameters["BestQuality"]; }
62    }
63    public ILookupParameter<RealVector> BestPointParameter {
64      get { return (ILookupParameter<RealVector>)Parameters["BestPoint"]; }
65    }
66    public IScopeTreeLookupParameter<IntArray> NeighborsParameter {
67      get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; }
68    }
69    #endregion
70
71    #region Parameter values
72    private DoubleValue BestQuality {
73      get { return BestQualityParameter.ActualValue; }
74    }
75    private RealVector BestPoint {
76      get { return BestPointParameter.ActualValue; }
77      set { BestPointParameter.ActualValue = value; }
78    }
79    private ItemArray<DoubleValue> Quality {
80      get { return QualityParameter.ActualValue; }
81    }
82    private ItemArray<DoubleValue> PersonalBestQuality {
83      get { return PersonalBestQualityParameter.ActualValue; }
84    }
85    private ItemArray<DoubleValue> NeighborsBestQuality {
86      get { return NeighborsBestQualityParameter.ActualValue; }
87    }
88    private ItemArray<RealVector> RealVector {
89      get { return RealVectorParameter.ActualValue; }
90    }
91    private ItemArray<RealVector> PersonalBest {
92      get { return PersonalBestParameter.ActualValue; }
93      set { PersonalBestParameter.ActualValue = value; }
94    }
95    private ItemArray<RealVector> NeighborsBest {
96      get { return NeighborsBestParameter.ActualValue; }
97      set { NeighborsBestParameter.ActualValue = value; }
98    }
99    private bool Maximization {
100      get { return MaximizationParameter.ActualValue.Value; }
101    }
102    private ItemArray<IntArray> Neighbors {
103      get { return NeighborsParameter.ActualValue; }
104    }
105    #endregion
106
107    #region Construction & Cloning
108
109    [StorableConstructor]
110    private RealVectorSwarmUpdater(bool deserializing) : base(deserializing) { }
111    private RealVectorSwarmUpdater(RealVectorSwarmUpdater original, Cloner cloner) : base(original, cloner) { }
112    public RealVectorSwarmUpdater()
113      : base() {
114      Parameters.Add(new LookupParameter<DoubleValue>("BestQuality", "Overall best quality."));
115      Parameters.Add(new LookupParameter<RealVector>("BestPoint", "Global best particle position"));
116      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "Particle's quality"));
117      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("PersonalBestQuality", "Particle's personal best quality"));
118      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("NeighborsBestQuality", "Global best particle quality"));
119      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "Particle's position"));
120      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("PersonalBest", "Particle's personal best position"));
121      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("NeighborsBest", "Neighborhood (or global in case of totally connected neighborhood) best particle position"));
122      Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle."));
123      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
124    }
125
126    public override IDeepCloneable Clone(Cloner cloner) {
127      return new RealVectorSwarmUpdater(this, cloner);
128    }
129
130    #endregion
131
132    public override IOperation Apply() {
133      InitializeBestPoint();
134      UpdateSwarm();
135      UpdateNeighbors();
136      UpdateVelocityBounds();
137      return base.Apply();
138    }
139
140    private void InitializeBestPoint() {
141      if (BestQuality == null)
142        BestQualityParameter.ActualValue = new DoubleValue();
143      BestQuality.Value = Maximization ? Quality.Max(v => v.Value) : Quality.Min(v => v.Value);
144      int bestIndex = Quality.FindIndex(v => v.Value == BestQuality.Value);
145      BestPoint = (RealVector)RealVector[bestIndex].Clone();
146    }
147
148    private void UpdateNeighbors() {
149      if (Neighbors != null & Neighbors.Length > 0) {
150        if (this.NeighborsBest == null || NeighborsBest.Length != Neighbors.Length)
151          NeighborsBest = new ItemArray<RealVector>(Neighbors.Length);
152        for (int n = 0; n < Neighbors.Length; n++) {
153          var pairs = Quality.Zip(RealVector, (q, p) => new { Quality = q, Point = p })
154            .Where((p, i) => i == n || Neighbors[n].Contains(i));
155          NeighborsBest[n] = Maximization ?
156          pairs.OrderByDescending(p => p.Quality.Value).First().Point :
157          pairs.OrderBy(p => p.Quality.Value).First().Point;
158        }
159        NeighborsBestParameter.ActualValue = NeighborsBest;
160      }
161    }
162
163    private void UpdateVelocityBounds() {
164      // ToDo: Add code
165    }
166
167    private void UpdateSwarm() {
168      if (PersonalBestQuality.Length == 0) {
169        ItemArray<DoubleValue> personalBestQualities = new ItemArray<DoubleValue>(RealVector.Length);
170        for (int i = 0; i < RealVector.Length; i++) {
171          if (Maximization) {
172            personalBestQualities[i] = new DoubleValue(double.MinValue);
173          } else {
174            personalBestQualities[i] = new DoubleValue(double.MaxValue);
175          }
176        }
177        PersonalBestQualityParameter.ActualValue = personalBestQualities;
178      }
179      if (NeighborsBestQuality.Length == 0) {
180        if (NeighborsParameter != null && Neighbors.Length > 0) {
181          ItemArray<DoubleValue> neighborsBestQualities = new ItemArray<DoubleValue>(RealVector.Length);
182          for (int i = 0; i < RealVector.Length; i++) {
183            if (Maximization) {
184              neighborsBestQualities[i] = new DoubleValue(double.MinValue);
185            } else {
186              neighborsBestQualities[i] = new DoubleValue(double.MaxValue);
187            }
188          }
189          NeighborsBestQualityParameter.ActualValue = neighborsBestQualities;
190        }
191      }
192      ItemArray<RealVector> neighborsBest = new ItemArray<RealVector>(RealVector.Length);
193      for (int i = 0; i < RealVector.Length; i++) {
194        if (Maximization && Quality[i].Value > PersonalBestQuality[i].Value ||
195          !Maximization && Quality[i].Value < PersonalBestQuality[i].Value) {
196          PersonalBestQuality[i].Value = Quality[i].Value;
197          PersonalBest[i] = RealVector[i];
198          if (Maximization && PersonalBestQuality[i].Value > NeighborsBestQuality[i].Value ||
199             !Maximization && PersonalBestQuality[i].Value < NeighborsBestQuality[i].Value) {
200            NeighborsBestQuality[i].Value = PersonalBestQuality[i].Value;
201            neighborsBest[i] = PersonalBest[i];
202          }
203        }
204      }
205      if (NeighborsParameter != null && Neighbors.Length > 0) {
206        NeighborsBestParameter.ActualValue = neighborsBest;
207      }
208    }
209
210  }
211}
Note: See TracBrowser for help on using the repository browser.