[13368] | 1 | #region License Information
|
---|
[5209] | 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5209] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[5435] | 29 |
|
---|
[5209] | 30 | namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
|
---|
[5316] | 31 | [Item("Random Topology Initializer", "Randomly connectes every particle with k other particles.")]
|
---|
[13368] | 32 | [StorableClass("51896760-09D3-404D-8ADD-F1A38E21C95A")]
|
---|
[5435] | 33 | public sealed class RandomTopologyInitializer : TopologyInitializer {
|
---|
[5209] | 34 | #region Parameters
|
---|
[5410] | 35 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 36 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
[5209] | 37 | }
|
---|
[5410] | 38 | public IValueLookupParameter<IntValue> NrOfConnectionsParameter {
|
---|
| 39 | get { return (IValueLookupParameter<IntValue>)Parameters["NrOfConnections"]; }
|
---|
[5209] | 40 | }
|
---|
| 41 | #endregion
|
---|
| 42 |
|
---|
| 43 | #region Parameter Values
|
---|
[5435] | 44 | private IRandom Random {
|
---|
[5209] | 45 | get { return RandomParameter.ActualValue; }
|
---|
| 46 | }
|
---|
[5435] | 47 | private int NrOfConnections {
|
---|
[5209] | 48 | get { return NrOfConnectionsParameter.ActualValue.Value; }
|
---|
| 49 | }
|
---|
| 50 | #endregion
|
---|
| 51 |
|
---|
| 52 | #region Construction & Cloning
|
---|
[5435] | 53 | [StorableConstructor]
|
---|
| 54 | private RandomTopologyInitializer(bool deserializing) : base(deserializing) { }
|
---|
| 55 | private RandomTopologyInitializer(RandomTopologyInitializer original, Cloner cloner) : base(original, cloner) { }
|
---|
[5209] | 56 | public RandomTopologyInitializer() {
|
---|
| 57 | Parameters.Add(new LookupParameter<IRandom>("Random", "A random number generation."));
|
---|
| 58 | Parameters.Add(new ValueLookupParameter<IntValue>("NrOfConnections", "Nr of connected neighbors.", new IntValue(3)));
|
---|
| 59 | }
|
---|
[5435] | 60 |
|
---|
[5209] | 61 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 62 | return new RandomTopologyInitializer(this, cloner);
|
---|
| 63 | }
|
---|
| 64 | #endregion
|
---|
| 65 |
|
---|
| 66 | public override IOperation Apply() {
|
---|
[5560] | 67 | ItemArray<IntArray> neighbors = new ItemArray<IntArray>(SwarmSize);
|
---|
[5435] | 68 | for (int i = 0; i < SwarmSize; i++) {
|
---|
[5209] | 69 | var numbers = Enumerable.Range(0, SwarmSize).ToList();
|
---|
| 70 | numbers.RemoveAt(i);
|
---|
| 71 | var selectedNumbers = new List<int>(NrOfConnections);
|
---|
[5435] | 72 | for (int j = 0; j < NrOfConnections && numbers.Count > 0; j++) {
|
---|
[5209] | 73 | int index = Random.Next(numbers.Count);
|
---|
| 74 | selectedNumbers.Add(numbers[index]);
|
---|
| 75 | numbers.RemoveAt(index);
|
---|
| 76 | }
|
---|
[5560] | 77 | neighbors[i] = new IntArray(selectedNumbers.ToArray());
|
---|
[5209] | 78 | }
|
---|
| 79 | Neighbors = neighbors;
|
---|
| 80 | return base.Apply();
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|