Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5445 was 5445, checked in by swagner, 13 years ago

Updated year of copyrights (#1406)

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