Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleUpdater.cs @ 3903

Last change on this file since 3903 was 3742, checked in by gkronber, 15 years ago

Fixed GPL license headers and deleted files which are not referenced by projects. #893

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