[5209] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5209] | 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 |
|
---|
| 22 | using System.Linq;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
| 27 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[5435] | 31 |
|
---|
[5209] | 32 | namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
|
---|
[5316] | 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]
|
---|
[5435] | 35 | public sealed class NeighborUpdater : SingleSuccessorOperator {
|
---|
| 36 | public override bool CanChangeName {
|
---|
| 37 | get { return false; }
|
---|
| 38 | }
|
---|
[5209] | 39 |
|
---|
| 40 | #region Parameters
|
---|
[5410] | 41 | public IScopeTreeLookupParameter<RealVector> PointsParameter {
|
---|
| 42 | get { return (IScopeTreeLookupParameter<RealVector>)Parameters["Point"]; }
|
---|
[5209] | 43 | }
|
---|
[5410] | 44 | public IScopeTreeLookupParameter<IntegerVector> NeighborsParameter {
|
---|
| 45 | get { return (IScopeTreeLookupParameter<IntegerVector>)Parameters["Neighbors"]; }
|
---|
[5209] | 46 | }
|
---|
[5410] | 47 | public IScopeTreeLookupParameter<RealVector> BestNeighborPointParameter {
|
---|
| 48 | get { return (IScopeTreeLookupParameter<RealVector>)Parameters["BestNeighborPoint"]; }
|
---|
[5209] | 49 | }
|
---|
[5410] | 50 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 51 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
[5209] | 52 | }
|
---|
[5410] | 53 | public ILookupParameter<BoolValue> MaximizationParameter {
|
---|
| 54 | get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
[5209] | 55 | }
|
---|
| 56 | #endregion
|
---|
| 57 |
|
---|
| 58 | #region Parameter Values
|
---|
[5435] | 59 | private ItemArray<RealVector> Points {
|
---|
[5209] | 60 | get { return PointsParameter.ActualValue; }
|
---|
| 61 | }
|
---|
[5435] | 62 | private ItemArray<IntegerVector> Neighbors {
|
---|
[5209] | 63 | get { return NeighborsParameter.ActualValue; }
|
---|
| 64 | }
|
---|
[5435] | 65 | private ItemArray<RealVector> BestNeighborPoints {
|
---|
[5209] | 66 | get { return BestNeighborPointParameter.ActualValue; }
|
---|
| 67 | set { BestNeighborPointParameter.ActualValue = value; }
|
---|
| 68 | }
|
---|
[5435] | 69 | private ItemArray<DoubleValue> Qualities {
|
---|
[5209] | 70 | get { return QualityParameter.ActualValue; }
|
---|
| 71 | }
|
---|
[5435] | 72 | private bool Maximization {
|
---|
[5209] | 73 | get { return MaximizationParameter.ActualValue.Value; }
|
---|
| 74 | }
|
---|
| 75 | #endregion
|
---|
| 76 |
|
---|
| 77 | #region Construction & Cloning
|
---|
[5435] | 78 | [StorableConstructor]
|
---|
| 79 | private NeighborUpdater(bool deserializing) : base(deserializing) { }
|
---|
| 80 | private NeighborUpdater(NeighborUpdater original, Cloner cloner) : base(original, cloner) { }
|
---|
[5209] | 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 | }
|
---|
[5435] | 88 |
|
---|
[5209] | 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);
|
---|
[5435] | 98 | for (int n = 0; n < Neighbors.Length; n++) {
|
---|
[5209] | 99 | var pairs = Qualities.Zip(Points, (q, p) => new { Quality = q, Point = p })
|
---|
| 100 | .Where((p, i) => i == n || Neighbors[n].Contains(i));
|
---|
[5435] | 101 | BestNeighborPoints[n] = Maximization ?
|
---|
[5209] | 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 | }
|
---|