Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VNS/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/NeighborUpdater.cs @ 5608

Last change on this file since 5608 was 5560, checked in by mkofler, 14 years ago

#852: Code refactoring. Created new interfaces and moved operators to respective projects as suggested in A. Beham's review. Work in progress.

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