Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/SPSORandomTopologyInitializer.cs @ 17513

Last change on this file since 17513 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 3.5 KB
RevLine 
[5209]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) 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;
[17097]29using HEAL.Attic;
[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.")]
[17097]33  [StorableType("3A589247-0629-44E4-8A49-610DE0FEC642")]
[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]
[17097]46    private SPSORandomTopologyInitializer(StorableConstructorFlag _) : base(_) { }
[15181]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)
[15277]64      //       it is by design that we draw from the particles with repetition
65      var particlesInform = new List<HashSet<int>>(swarmSize);
66      for (var i = 0; i < swarmSize; i++) {
67        var informs = new HashSet<int>() { i };
68        for (var j = 0; j < k; j++) {
69          informs.Add(random.Next(swarmSize));
70        }
71        particlesInform.Add(informs);
72      }
[15181]73
74      var neighbors = new ItemArray<IntArray>(swarmSize);
[15091]75      for (int i = 0; i < swarmSize; i++) {
[15181]76        // calculate the informants for each particle
77        var informants = particlesInform.Select((val, idx) => val.Contains(i) ? idx : -1).Where(x => x >= 0).ToArray();
78        neighbors[i] = new IntArray(informants);
[5209]79      }
[15091]80      NeighborsParameter.ActualValue = neighbors;
[5209]81      return base.Apply();
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.