Changeset 15091 for trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/RandomTopologyInitializer.cs
- Timestamp:
- 06/29/17 15:26:16 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/RandomTopologyInitializer.cs
r14185 r15091 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Optimization; 27 28 using HeuristicLab.Parameters; 28 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 31 32 [Item("Random Topology Initializer", "Randomly connectes every particle with k other particles.")] 32 33 [StorableClass] 33 public sealed class RandomTopologyInitializer : TopologyInitializer {34 public sealed class RandomTopologyInitializer : TopologyInitializer, IStochasticOperator { 34 35 #region Parameters 35 36 public ILookupParameter<IRandom> RandomParameter { … … 40 41 } 41 42 #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 43 52 44 #region Construction & Cloning 53 45 [StorableConstructor] … … 65 57 66 58 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(); 59 var random = RandomParameter.ActualValue; 60 var swarmSize = SwarmSizeParameter.ActualValue.Value; 61 var nrOfConnections = NrOfConnectionsParameter.ActualValue.Value; 62 63 ItemArray<IntArray> neighbors = new ItemArray<IntArray>(swarmSize); 64 for (int i = 0; i < swarmSize; i++) { 65 var numbers = Enumerable.Range(0, swarmSize).ToList(); 70 66 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);67 var selectedNumbers = new List<int>(nrOfConnections); 68 for (int j = 0; j < nrOfConnections && numbers.Count > 0; j++) { 69 int index = random.Next(numbers.Count); 74 70 selectedNumbers.Add(numbers[index]); 75 71 numbers.RemoveAt(index); … … 77 73 neighbors[i] = new IntArray(selectedNumbers.ToArray()); 78 74 } 79 Neighbors = neighbors;75 NeighborsParameter.ActualValue = neighbors; 80 76 return base.Apply(); 81 77 }
Note: See TracChangeset
for help on using the changeset viewer.