Free cookie consent management tool by TermsFeed Policy Generator

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

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

Configurable ParticleUpdater, and topologies, different topology initializers and support for dynamic topology changes (#852)

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