Changeset 16692 for branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/RandomTopologyInitializer.cs
- Timestamp:
- 03/18/19 17:24:30 (5 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring
- Property svn:ignore
-
old new 24 24 protoc.exe 25 25 obj 26 .vs
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/RandomTopologyInitializer.cs
r12012 r16692 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 5Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 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; 29 30 30 31 namespace HeuristicLab.Algorithms.ParticleSwarmOptimization { 31 [Item("Random Topology Initializer", "Randomly connectes every particle with k other particles.")]32 [Item("Random Distinct Topology Initializer", "Each particle is informed by exactly k+1 distinct other particles (including itself).")] 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 + 1); 68 selectedNumbers.Add(i); 69 for (int j = 0; j < nrOfConnections && numbers.Count > 0; j++) { 70 int index = random.Next(numbers.Count); 74 71 selectedNumbers.Add(numbers[index]); 75 72 numbers.RemoveAt(index); … … 77 74 neighbors[i] = new IntArray(selectedNumbers.ToArray()); 78 75 } 79 Neighbors = neighbors;76 NeighborsParameter.ActualValue = neighbors; 80 77 return base.Apply(); 81 78 }
Note: See TracChangeset
for help on using the changeset viewer.