Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/SPSORandomTopologyInitializer.cs @ 15181

Last change on this file since 15181 was 15181, checked in by abeham, 7 years ago

#2797:

  • Added IStochasticOperator interface to MultiPSOTopologyUpdater
  • Changed parameter defaults to those described in the paper
  • Added analyzer placeholder for the last iteration (has not been previously analyzed)
  • Changed random topology initializer to include itself (to be able to use it with SPSOSwarmUpdater -> this should not change the old RealVectorSwarmUpdater)
  • Changed ring topology initializer to include itself (same as above)
  • Changed von neumann topology initializer to include itself (same as above)
  • Added SPSO compatible random topology initializer (as described in the paper by Clerc)
  • Changed sampling of the random directional vector to be uniformly random on the surface of a hypersphere to avoid a slight bias in diagonal direction
  • Updating SwarmBestQuality and BestRealVector parameters in SPSOSwarmUpdater (an oversight)
  • Added a faster method to create a copy of a RealVector (based on Array.Copy)
  • Updated the sample
  • Updated the sample's test results (due to changed sampling in SPSO2011ParticleUpdater)
File size: 3.4 KB
RevLine 
[5209]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
[15091]27using HeuristicLab.Optimization;
[5209]28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[5435]30
[5209]31namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
[15181]32  [Item("SPSO Random Topology Initializer", "Each particle informs k+1 other particles (including itself). The same particle (including itself) can be informed multiple times.")]
[5316]33  [StorableClass]
[15181]34  public sealed class SPSORandomTopologyInitializer : TopologyInitializer, IStochasticOperator {
[5209]35    #region Parameters
[5410]36    public ILookupParameter<IRandom> RandomParameter {
37      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
[5209]38    }
[15181]39    public IValueLookupParameter<IntValue> KParameter {
40      get { return (IValueLookupParameter<IntValue>)Parameters["K"]; }
[5209]41    }
42    #endregion
[15091]43   
[5209]44    #region Construction & Cloning
[5435]45    [StorableConstructor]
[15181]46    private SPSORandomTopologyInitializer(bool deserializing) : base(deserializing) { }
47    private SPSORandomTopologyInitializer(SPSORandomTopologyInitializer original, Cloner cloner) : base(original, cloner) { }
48    public SPSORandomTopologyInitializer() {
[5209]49      Parameters.Add(new LookupParameter<IRandom>("Random", "A random number generation."));
[15181]50      Parameters.Add(new ValueLookupParameter<IntValue>("K", "The number of informed particles (excluding itself).", new IntValue(3)));
[5209]51    }
[5435]52
[5209]53    public override IDeepCloneable Clone(Cloner cloner) {
[15181]54      return new SPSORandomTopologyInitializer(this, cloner);
[5209]55    }
56    #endregion
57
58    public override IOperation Apply() {
[15091]59      var random = RandomParameter.ActualValue;
60      var swarmSize = SwarmSizeParameter.ActualValue.Value;
[15181]61      var k = KParameter.ActualValue.Value;
[15091]62
[15181]63      // SPSO: Each particle informs at most K+1 particles (at least itself and K others)
64      var particlesInform = Enumerable.Repeat(k + 1, swarmSize)
65        .Select((v, i) => new HashSet<int>(Enumerable.Range(0, v).Select(x => x == 0 ? i : random.Next(swarmSize)))).ToList();
66
67      var neighbors = new ItemArray<IntArray>(swarmSize);
[15091]68      for (int i = 0; i < swarmSize; i++) {
[15181]69        // calculate the informants for each particle
70        var informants = particlesInform.Select((val, idx) => val.Contains(i) ? idx : -1).Where(x => x >= 0).ToArray();
71        neighbors[i] = new IntArray(informants);
[5209]72      }
[15091]73      NeighborsParameter.ActualValue = neighbors;
[5209]74      return base.Apply();
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.