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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
|
---|
31 |
|
---|
32 | public class RandomTopologyInitializer : TopologyInitializer {
|
---|
33 |
|
---|
34 | #region Parameters
|
---|
35 | public LookupParameter<IRandom> RandomParameter {
|
---|
36 | get { return (LookupParameter<IRandom>)Parameters["Random"]; }
|
---|
37 | }
|
---|
38 | public ValueLookupParameter<IntValue> NrOfConnectionsParameter {
|
---|
39 | get { return (ValueLookupParameter<IntValue>)Parameters["NrOfConnections"]; }
|
---|
40 | }
|
---|
41 | #endregion
|
---|
42 |
|
---|
43 | #region Parameter Values
|
---|
44 | public IRandom Random {
|
---|
45 | get { return RandomParameter.ActualValue; }
|
---|
46 | }
|
---|
47 | public int NrOfConnections {
|
---|
48 | get { return NrOfConnectionsParameter.ActualValue.Value; }
|
---|
49 | }
|
---|
50 | #endregion
|
---|
51 |
|
---|
52 | #region Construction & Cloning
|
---|
53 | public RandomTopologyInitializer() {
|
---|
54 | Parameters.Add(new LookupParameter<IRandom>("Random", "A random number generation."));
|
---|
55 | Parameters.Add(new ValueLookupParameter<IntValue>("NrOfConnections", "Nr of connected neighbors.", new IntValue(3)));
|
---|
56 | }
|
---|
57 | [StorableConstructor]
|
---|
58 | protected RandomTopologyInitializer(bool deserializing) : base(deserializing) { }
|
---|
59 | protected RandomTopologyInitializer(RandomTopologyInitializer original, Cloner cloner)
|
---|
60 | : base(original, cloner) {
|
---|
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 | }
|
---|
85 | }
|
---|