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 HeuristicLab.Core;
|
---|
23 | using HeuristicLab.Data;
|
---|
24 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
|
---|
29 | public class ParticleUpdater : SingleSuccessorOperator { // ParticleUpdater
|
---|
30 | #region Parameter properties
|
---|
31 |
|
---|
32 | public ILookupParameter<RealVector> VelocityParameter {
|
---|
33 | get { return (ILookupParameter<RealVector>)Parameters["Velocity"]; }
|
---|
34 | }
|
---|
35 |
|
---|
36 | public IParameter RandomParameter {
|
---|
37 | get { return (IParameter)Parameters["Random"]; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public ILookupParameter<RealVector> CurrentPositionParameter {
|
---|
41 | get { return (ILookupParameter<RealVector>)Parameters["CurrentPosition"]; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public ILookupParameter<RealVector> BestLocalParameter {
|
---|
45 | get { return (ILookupParameter<RealVector>)Parameters["BestLocal"]; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public ILookupParameter<RealVector> BestGlobalParameter {
|
---|
49 | get { return (ILookupParameter<RealVector>)Parameters["BestGlobal"]; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public ILookupParameter<DoubleMatrix> BoundsParameter {
|
---|
53 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 |
|
---|
57 | public ParticleUpdater()
|
---|
58 | : base() {
|
---|
59 | Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator (to generate alpha an beta)."));
|
---|
60 | Parameters.Add(new LookupParameter<RealVector>("Velocity", "The velocity vector to update."));
|
---|
61 | Parameters.Add(new LookupParameter<RealVector>("CurrentPosition", "Current position"));
|
---|
62 | Parameters.Add(new LookupParameter<RealVector>("BestLocal", "Best local position"));
|
---|
63 | Parameters.Add(new LookupParameter<RealVector>("BestGlobal", "Best global position"));
|
---|
64 | Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector."));
|
---|
65 | }
|
---|
66 |
|
---|
67 | public override IOperation Apply() {
|
---|
68 | double alpha = ((IRandom)RandomParameter.ActualValue).NextDouble();
|
---|
69 | double beta = ((IRandom)RandomParameter.ActualValue).NextDouble();
|
---|
70 | RealVector velocity = (RealVector)VelocityParameter.ActualValue;
|
---|
71 | for (int i = 0; i < velocity.Length; i++) {
|
---|
72 | velocity[i] = velocity[i] + alpha * (BestLocalParameter.ActualValue[i] - CurrentPositionParameter.ActualValue[i]) + beta * (BestGlobalParameter.ActualValue[i] - CurrentPositionParameter.ActualValue[i]);
|
---|
73 | }
|
---|
74 | VelocityParameter.ActualValue = velocity;
|
---|
75 | for (int i = 0; i < CurrentPositionParameter.ActualValue.Length; i++) {
|
---|
76 | CurrentPositionParameter.ActualValue[i] = CurrentPositionParameter.ActualValue[i] + VelocityParameter.ActualValue[i];
|
---|
77 | if (CurrentPositionParameter.ActualValue[i] < BoundsParameter.ActualValue[0, 0]) {
|
---|
78 | CurrentPositionParameter.ActualValue[i] = BoundsParameter.ActualValue[0, 0];
|
---|
79 | } else if (CurrentPositionParameter.ActualValue[i] > BoundsParameter.ActualValue[0, 1]) {
|
---|
80 | CurrentPositionParameter.ActualValue[i] = BoundsParameter.ActualValue[0, 1];
|
---|
81 | }
|
---|
82 | }
|
---|
83 | return base.Apply();
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override bool CanChangeName {
|
---|
87 | get { return false; }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|