Changeset 5435 for trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/RandomTopologyInitializer.cs
- Timestamp:
- 02/04/11 21:34:45 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/RandomTopologyInitializer.cs
r5410 r5435 28 28 using HeuristicLab.Parameters; 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 30 31 namespace HeuristicLab.Algorithms.ParticleSwarmOptimization { 31 32 32 [Item("Random Topology Initializer", "Randomly connectes every particle with k other particles.")] 33 33 [StorableClass] 34 public class RandomTopologyInitializer : TopologyInitializer { 35 34 public sealed class RandomTopologyInitializer : TopologyInitializer { 36 35 #region Parameters 37 36 public ILookupParameter<IRandom> RandomParameter { … … 44 43 45 44 #region Parameter Values 46 p ublicIRandom Random {45 private IRandom Random { 47 46 get { return RandomParameter.ActualValue; } 48 47 } 49 p ublicint NrOfConnections {48 private int NrOfConnections { 50 49 get { return NrOfConnectionsParameter.ActualValue.Value; } 51 50 } … … 53 52 54 53 #region Construction & Cloning 54 [StorableConstructor] 55 private RandomTopologyInitializer(bool deserializing) : base(deserializing) { } 56 private RandomTopologyInitializer(RandomTopologyInitializer original, Cloner cloner) : base(original, cloner) { } 55 57 public RandomTopologyInitializer() { 56 58 Parameters.Add(new LookupParameter<IRandom>("Random", "A random number generation.")); 57 59 Parameters.Add(new ValueLookupParameter<IntValue>("NrOfConnections", "Nr of connected neighbors.", new IntValue(3))); 58 60 } 59 [StorableConstructor] 60 protected RandomTopologyInitializer(bool deserializing) : base(deserializing) { } 61 protected RandomTopologyInitializer(RandomTopologyInitializer original, Cloner cloner) 62 : base(original, cloner) { 63 } 61 64 62 public override IDeepCloneable Clone(Cloner cloner) { 65 63 return new RandomTopologyInitializer(this, cloner); … … 69 67 public override IOperation Apply() { 70 68 ItemArray<IntegerVector> neighbors = new ItemArray<IntegerVector>(SwarmSize); 71 for (int i = 0; i <SwarmSize; i++) {69 for (int i = 0; i < SwarmSize; i++) { 72 70 var numbers = Enumerable.Range(0, SwarmSize).ToList(); 73 71 numbers.RemoveAt(i); 74 72 var selectedNumbers = new List<int>(NrOfConnections); 75 for (int j = 0; j <NrOfConnections && numbers.Count > 0; j++) {73 for (int j = 0; j < NrOfConnections && numbers.Count > 0; j++) { 76 74 int index = Random.Next(numbers.Count); 77 75 selectedNumbers.Add(numbers[index]); … … 83 81 return base.Apply(); 84 82 } 85 86 83 } 87 84 }
Note: See TracChangeset
for help on using the changeset viewer.