- Timestamp:
- 06/29/17 15:26:16 (7 years ago)
- Location:
- trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimization.cs
r14185 r15091 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using HeuristicLab.Analysis; … … 179 180 Parameters.Add(new ValueParameter<IntValue>("MaxIterations", "Maximal number of iterations.", new IntValue(1000))); 180 181 Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer())); 181 Parameters.Add(new ValueParameter<DoubleValue>("Inertia", "Inertia weight on a particle's movement (omega).", new DoubleValue( 1)));182 Parameters.Add(new ValueParameter<DoubleValue>("PersonalBestAttraction", "Weight for particle's pull towards its personal best soution (phi_p).", new DoubleValue( -0.01)));183 Parameters.Add(new ValueParameter<DoubleValue>("NeighborBestAttraction", "Weight for pull towards the neighborhood best solution or global best solution in case of a totally connected topology (phi_g).", new DoubleValue( 3.7)));182 Parameters.Add(new ValueParameter<DoubleValue>("Inertia", "Inertia weight on a particle's movement (omega).", new DoubleValue(0.9))); 183 Parameters.Add(new ValueParameter<DoubleValue>("PersonalBestAttraction", "Weight for particle's pull towards its personal best soution (phi_p).", new DoubleValue(0.05))); 184 Parameters.Add(new ValueParameter<DoubleValue>("NeighborBestAttraction", "Weight for pull towards the neighborhood best solution or global best solution in case of a totally connected topology (phi_g).", new DoubleValue(0.5))); 184 185 Parameters.Add(new ConstrainedValueParameter<IParticleCreator>("ParticleCreator", "Operator that creates a new particle.")); 185 186 Parameters.Add(new ConstrainedValueParameter<IParticleUpdater>("ParticleUpdater", "Operator that updates a particle.")); … … 262 263 UpdateAnalyzers(); 263 264 ParameterizeAnalyzers(); 265 UpdateParticleUpdaterParameter(); 264 266 UpdateTopologyParameters(); 265 267 InitializeParticleCreator(); … … 280 282 281 283 private void InitializeParticleCreator() { 284 ParticleCreatorParameter.ValidValues.Clear(); 282 285 if (Problem != null) { 283 286 IParticleCreator oldParticleCreator = ParticleCreator; 284 287 IParticleCreator defaultParticleCreator = Problem.Operators.OfType<IParticleCreator>().FirstOrDefault(); 285 ParticleCreatorParameter.ValidValues.Clear(); 286 foreach (IParticleCreator Creator in Problem.Operators.OfType<IParticleCreator>().OrderBy(x => x.Name)) { 287 ParticleCreatorParameter.ValidValues.Add(Creator); 288 foreach (var creator in Problem.Operators.OfType<IParticleCreator>().OrderBy(x => x.Name)) { 289 ParticleCreatorParameter.ValidValues.Add(creator); 288 290 } 289 291 if (oldParticleCreator != null) { 290 IParticleCreator creator = ParticleCreatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldParticleCreator.GetType());292 var creator = ParticleCreatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldParticleCreator.GetType()); 291 293 if (creator != null) ParticleCreator = creator; 292 294 else oldParticleCreator = null; … … 328 330 updater.IndexParameter.ActualName = "Iterations"; 329 331 updater.ValueParameter.ActualName = "CurrentInertia"; 330 updater.StartValueParameter. Value = new DoubleValue(1);331 updater.EndValueParameter.Value = new DoubleValue( 1E-10);332 updater.StartValueParameter.ActualName = InertiaParameter.Name; 333 updater.EndValueParameter.Value = new DoubleValue(0.70); 332 334 } 333 335 } … … 346 348 347 349 private void UpdateTopologyInitializer() { 348 ITopologyInitializer oldTopologyInitializer = TopologyInitializer;350 var oldTopologyInitializer = TopologyInitializer; 349 351 TopologyInitializerParameter.ValidValues.Clear(); 350 352 foreach (ITopologyInitializer topologyInitializer in ApplicationManager.Manager.GetInstances<ITopologyInitializer>().OrderBy(x => x.Name)) { 351 353 TopologyInitializerParameter.ValidValues.Add(topologyInitializer); 352 354 } 355 353 356 if (oldTopologyInitializer != null && TopologyInitializerParameter.ValidValues.Any(x => x.GetType() == oldTopologyInitializer.GetType())) 354 357 TopologyInitializer = TopologyInitializerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTopologyInitializer.GetType()); … … 357 360 358 361 private void ParameterizeTopologyUpdaters() { 359 foreach (var updater in TopologyUpdaterParameter.ValidValues) { 360 var multiPsoUpdater = updater as MultiPSOTopologyUpdater; 361 if (multiPsoUpdater != null) { 362 multiPsoUpdater.CurrentIterationParameter.ActualName = "Iterations"; 363 } 364 } 365 } 366 367 private void UpdateTopologyParameters() { 368 ITopologyUpdater oldTopologyUpdater = TopologyUpdater; 369 IParticleUpdater oldParticleUpdater = ParticleUpdater; 370 ClearTopologyParameters(); 371 if (Problem != null) { 372 IParticleUpdater defaultParticleUpdater = null; 373 if (TopologyInitializer != null) { 374 foreach (ITopologyUpdater topologyUpdater in ApplicationManager.Manager.GetInstances<ITopologyUpdater>()) 375 TopologyUpdaterParameter.ValidValues.Add(topologyUpdater); 376 defaultParticleUpdater = Problem.Operators.OfType<ILocalParticleUpdater>().FirstOrDefault(); 377 foreach (IParticleUpdater particleUpdater in Problem.Operators.OfType<ILocalParticleUpdater>().OrderBy(x => x.Name)) 378 ParticleUpdaterParameter.ValidValues.Add(particleUpdater); 379 } else { 380 defaultParticleUpdater = Problem.Operators.OfType<IGlobalParticleUpdater>().FirstOrDefault(); 381 foreach (IParticleUpdater particleUpdater in Problem.Operators.OfType<IGlobalParticleUpdater>().OrderBy(x => x.Name)) 382 ParticleUpdaterParameter.ValidValues.Add(particleUpdater); 383 } 384 if (oldTopologyUpdater != null) { 385 ITopologyUpdater newTopologyUpdater = TopologyUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldParticleUpdater.GetType()); 386 if (newTopologyUpdater != null) TopologyUpdater = newTopologyUpdater; 387 } 362 foreach (var updater in TopologyUpdaterParameter.ValidValues.OfType<MultiPSOTopologyUpdater>()) { 363 updater.CurrentIterationParameter.ActualName = "Iterations"; 364 } 365 } 366 367 private void UpdateParticleUpdaterParameter() { 368 var oldParticleUpdater = ParticleUpdater; 369 ParticleUpdaterParameter.ValidValues.Clear(); 370 if (Problem != null) { 371 var defaultParticleUpdater = Problem.Operators.OfType<IParticleUpdater>().FirstOrDefault(); 372 373 foreach (var particleUpdater in Problem.Operators.OfType<IParticleUpdater>().OrderBy(x => x.Name)) 374 ParticleUpdaterParameter.ValidValues.Add(particleUpdater); 375 388 376 if (oldParticleUpdater != null) { 389 IParticleUpdater newParticleUpdater = ParticleUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldParticleUpdater.GetType());377 var newParticleUpdater = ParticleUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldParticleUpdater.GetType()); 390 378 if (newParticleUpdater != null) ParticleUpdater = newParticleUpdater; 391 379 else oldParticleUpdater = null; … … 393 381 if (oldParticleUpdater == null && defaultParticleUpdater != null) 394 382 ParticleUpdater = defaultParticleUpdater; 395 396 ParameterizeTopologyUpdaters(); 397 } 398 } 399 400 private void ClearTopologyParameters() { 383 } 384 } 385 386 private void UpdateTopologyParameters() { 387 ITopologyUpdater oldTopologyUpdater = TopologyUpdater; 401 388 TopologyUpdaterParameter.ValidValues.Clear(); 402 ParticleUpdaterParameter.ValidValues.Clear(); 389 if (Problem != null) { 390 if (TopologyInitializerParameter.Value != null) { 391 foreach (ITopologyUpdater topologyUpdater in ApplicationManager.Manager.GetInstances<ITopologyUpdater>()) 392 TopologyUpdaterParameter.ValidValues.Add(topologyUpdater); 393 394 if (oldTopologyUpdater != null) { 395 ITopologyUpdater newTopologyUpdater = TopologyUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldTopologyUpdater.GetType()); 396 if (newTopologyUpdater != null) TopologyUpdater = newTopologyUpdater; 397 } 398 ParameterizeTopologyUpdaters(); 399 } 400 } 403 401 } 404 402 -
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 } -
trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/RingTopologyInitializer.cs
r14185 r15091 43 43 44 44 public override IOperation Apply() { 45 ItemArray<IntArray> neighbors = new ItemArray<IntArray>(SwarmSize); 46 for (int i = 0; i < SwarmSize; i++) { 47 neighbors[i] = new IntArray(new[] { (SwarmSize + i - 1) % SwarmSize, (i + 1) % SwarmSize }); 45 var swarmSize = SwarmSizeParameter.ActualValue.Value; 46 47 ItemArray<IntArray> neighbors = new ItemArray<IntArray>(swarmSize); 48 for (int i = 0; i < swarmSize; i++) { 49 neighbors[i] = new IntArray(new[] { (swarmSize + i - 1) % swarmSize, (i + 1) % swarmSize }); 48 50 } 49 Neighbors = neighbors;51 NeighborsParameter.ActualValue = neighbors; 50 52 return base.Apply(); 51 53 } -
trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/TopologyInitializer.cs
r14185 r15091 40 40 get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; } 41 41 } 42 43 42 public ILookupParameter<IntValue> SwarmSizeParameter { 44 43 get { return (ILookupParameter<IntValue>)Parameters["SwarmSize"]; } 45 }46 47 #endregion48 49 #region Parameter Values50 protected ItemArray<IntArray> Neighbors {51 get { return NeighborsParameter.ActualValue; }52 set { NeighborsParameter.ActualValue = value; }53 }54 protected int SwarmSize {55 get { return SwarmSizeParameter.ActualValue.Value; }56 44 } 57 45 #endregion -
trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/VonNeumannTopologyInitializer.cs
r14185 r15091 44 44 45 45 public override IOperation Apply() { 46 ItemArray<IntArray> neighbors = new ItemArray<IntArray>(SwarmSize); 47 for (int i = 0; i < SwarmSize; i++) { 46 var swarmSize = SwarmSizeParameter.ActualValue.Value; 47 48 ItemArray<IntArray> neighbors = new ItemArray<IntArray>(swarmSize); 49 for (int i = 0; i < swarmSize; i++) { 48 50 neighbors[i] = new IntArray(new[] { 49 ( SwarmSize + i-2) % SwarmSize,50 ( SwarmSize + i-1) % SwarmSize,51 (i+1) % SwarmSize,52 (i+2) % SwarmSize51 (swarmSize + i-2) % swarmSize, 52 (swarmSize + i-1) % swarmSize, 53 (i+1) % swarmSize, 54 (i+2) % swarmSize 53 55 }); 54 56 } 55 Neighbors = neighbors;57 NeighborsParameter.ActualValue = neighbors; 56 58 return base.Apply(); 57 59 }
Note: See TracChangeset
for help on using the changeset viewer.