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

Last change on this file since 15181 was 15181, checked in by abeham, 3 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
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
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.")]
33  [StorableClass]
34  public sealed class SPSORandomTopologyInitializer : TopologyInitializer, IStochasticOperator {
35    #region Parameters
36    public ILookupParameter<IRandom> RandomParameter {
37      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
38    }
39    public IValueLookupParameter<IntValue> KParameter {
40      get { return (IValueLookupParameter<IntValue>)Parameters["K"]; }
41    }
42    #endregion
43   
44    #region Construction & Cloning
45    [StorableConstructor]
46    private SPSORandomTopologyInitializer(bool deserializing) : base(deserializing) { }
47    private SPSORandomTopologyInitializer(SPSORandomTopologyInitializer original, Cloner cloner) : base(original, cloner) { }
48    public SPSORandomTopologyInitializer() {
49      Parameters.Add(new LookupParameter<IRandom>("Random", "A random number generation."));
50      Parameters.Add(new ValueLookupParameter<IntValue>("K", "The number of informed particles (excluding itself).", new IntValue(3)));
51    }
52
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new SPSORandomTopologyInitializer(this, cloner);
55    }
56    #endregion
57
58    public override IOperation Apply() {
59      var random = RandomParameter.ActualValue;
60      var swarmSize = SwarmSizeParameter.ActualValue.Value;
61      var k = KParameter.ActualValue.Value;
62
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);
68      for (int i = 0; i < swarmSize; i++) {
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);
72      }
73      NeighborsParameter.ActualValue = neighbors;
74      return base.Apply();
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.