Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/SPSORandomTopologyInitializer.cs @ 16752

Last change on this file since 16752 was 16559, checked in by jkarder, 6 years ago

#2520: renamed Fossil to Attic and set version to 1.0.0-pre01

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Attic;
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  [StorableType("3A589247-0629-44E4-8A49-610DE0FEC642")]
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(StorableConstructorFlag _) : base(_) { }
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      //       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      }
73
74      var neighbors = new ItemArray<IntArray>(swarmSize);
75      for (int i = 0; i < swarmSize; i++) {
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);
79      }
80      NeighborsParameter.ActualValue = neighbors;
81      return base.Apply();
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.