Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/NeighborUpdater.cs @ 5342

Last change on this file since 5342 was 5316, checked in by epitzer, 14 years ago

added item descriptions, implemented IStorableContent interface, added missing StorableClass attributes, and fixed value updater reset (#852)

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Encodings.IntegerVectorEncoding;
27using HeuristicLab.Encodings.RealVectorEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
32
33  [Item("Neighbor Updater", "Determines the best quality and point among the defined list of neigbors for every particle and its individual neighborhood.")]
34  [StorableClass]
35  public class NeighborUpdater : SingleSuccessorOperator {
36
37    #region Parameters
38    public ScopeTreeLookupParameter<RealVector> PointsParameter {
39      get { return (ScopeTreeLookupParameter<RealVector>)Parameters["Point"]; }
40    }
41    public ScopeTreeLookupParameter<IntegerVector> NeighborsParameter {
42      get { return (ScopeTreeLookupParameter<IntegerVector>)Parameters["Neighbors"]; }
43    }
44    public ScopeTreeLookupParameter<RealVector> BestNeighborPointParameter {
45      get { return (ScopeTreeLookupParameter<RealVector>)Parameters["BestNeighborPoint"]; }
46    }
47    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
48      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
49    }
50    public LookupParameter<BoolValue> MaximizationParameter {
51      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
52    }
53    #endregion
54
55    #region Parameter Values
56    public ItemArray<RealVector> Points {
57      get { return PointsParameter.ActualValue; }
58    }
59    public ItemArray<IntegerVector> Neighbors {
60      get { return NeighborsParameter.ActualValue; }
61    }
62    public ItemArray<RealVector> BestNeighborPoints {
63      get { return BestNeighborPointParameter.ActualValue; }
64      set { BestNeighborPointParameter.ActualValue = value; }
65    }
66    public ItemArray<DoubleValue> Qualities {
67      get { return QualityParameter.ActualValue; }
68    }
69    public bool Maximization {
70      get { return MaximizationParameter.ActualValue.Value; }
71    }
72    #endregion
73
74    #region Construction & Cloning
75    public NeighborUpdater() {
76      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("Point", "The position of the particle."));
77      Parameters.Add(new ScopeTreeLookupParameter<IntegerVector>("Neighbors", "The list of neighbors for each particle."));
78      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("BestNeighborPoint", "The position of the best neighboring particle."));
79      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The list of qualities of all particles."));
80      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether the problem is a maximization problem."));
81    }
82    [StorableConstructor]
83    protected NeighborUpdater(bool deserializing) : base(deserializing) { }
84    protected NeighborUpdater(NeighborUpdater original, Cloner cloner)
85      : base(original, cloner) {
86    }
87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new NeighborUpdater(this, cloner);
89    }
90    #endregion
91
92    public override IOperation Apply() {
93      if (Neighbors != null & Neighbors.Length > 0) {
94        if (BestNeighborPoints == null || BestNeighborPoints.Length != Neighbors.Length)
95          BestNeighborPoints = new ItemArray<RealVector>(Neighbors.Length);
96        for (int n = 0; n<Neighbors.Length; n++) {
97          var pairs = Qualities.Zip(Points, (q, p) => new { Quality = q, Point = p })
98            .Where((p, i) => i == n || Neighbors[n].Contains(i));
99          BestNeighborPoints[n] = Maximization ?
100          pairs.OrderByDescending(p => p.Quality.Value).First().Point :
101          pairs.OrderBy(p => p.Quality.Value).First().Point;
102        }
103        BestNeighborPointParameter.ActualValue = BestNeighborPoints;
104      }
105      return base.Apply();
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.