Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/RandomTopologyInitializer.cs @ 14165

Last change on this file since 14165 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
31  [Item("Random Topology Initializer", "Randomly connectes every particle with k other particles.")]
32  [StorableClass]
33  public sealed class RandomTopologyInitializer : TopologyInitializer {
34    #region Parameters
35    public ILookupParameter<IRandom> RandomParameter {
36      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
37    }
38    public IValueLookupParameter<IntValue> NrOfConnectionsParameter {
39      get { return (IValueLookupParameter<IntValue>)Parameters["NrOfConnections"]; }
40    }
41    #endregion
42
43    #region Parameter Values
44    private IRandom Random {
45      get { return RandomParameter.ActualValue; }
46    }
47    private int NrOfConnections {
48      get { return NrOfConnectionsParameter.ActualValue.Value; }
49    }
50    #endregion
51
52    #region Construction & Cloning
53    [StorableConstructor]
54    private RandomTopologyInitializer(bool deserializing) : base(deserializing) { }
55    private RandomTopologyInitializer(RandomTopologyInitializer original, Cloner cloner) : base(original, cloner) { }
56    public RandomTopologyInitializer() {
57      Parameters.Add(new LookupParameter<IRandom>("Random", "A random number generation."));
58      Parameters.Add(new ValueLookupParameter<IntValue>("NrOfConnections", "Nr of connected neighbors.", new IntValue(3)));
59    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new RandomTopologyInitializer(this, cloner);
63    }
64    #endregion
65
66    public override IOperation Apply() {
67      ItemArray<IntArray> neighbors = new ItemArray<IntArray>(SwarmSize);
68      for (int i = 0; i < SwarmSize; i++) {
69        var numbers = Enumerable.Range(0, SwarmSize).ToList();
70        numbers.RemoveAt(i);
71        var selectedNumbers = new List<int>(NrOfConnections);
72        for (int j = 0; j < NrOfConnections && numbers.Count > 0; j++) {
73          int index = Random.Next(numbers.Count);
74          selectedNumbers.Add(numbers[index]);
75          numbers.RemoveAt(index);
76        }
77        neighbors[i] = new IntArray(selectedNumbers.ToArray());
78      }
79      Neighbors = neighbors;
80      return base.Apply();
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.