Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/NeighborUpdater.cs @ 5681

Last change on this file since 5681 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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