Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5426 was 5410, checked in by mkofler, 14 years ago

#852: Realized code cleanup as described in the reviewer comments by Andreas Beham.

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