[3682] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Operators;
|
---|
| 6 | using HeuristicLab.Parameters;
|
---|
| 7 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 8 | using HeuristicLab.Data;
|
---|
| 9 | using HeuristicLab.Core;
|
---|
| 10 |
|
---|
| 11 | namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
|
---|
| 12 | public class ParticleUpdater : SingleSuccessorOperator { // ParticleUpdater
|
---|
| 13 | #region Parameter properties
|
---|
| 14 |
|
---|
| 15 | public ILookupParameter<RealVector> VelocityParameter {
|
---|
| 16 | get { return (ILookupParameter<RealVector>)Parameters["Velocity"]; }
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | public IParameter RandomParameter {
|
---|
| 20 | get { return (IParameter)Parameters["Random"]; }
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | public ILookupParameter<RealVector> CurrentPositionParameter {
|
---|
| 24 | get { return (ILookupParameter<RealVector>)Parameters["CurrentPosition"]; }
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | public ILookupParameter<RealVector> BestLocalParameter {
|
---|
| 28 | get { return (ILookupParameter<RealVector>)Parameters["BestLocal"]; }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | public ILookupParameter<RealVector> BestGlobalParameter {
|
---|
| 32 | get { return (ILookupParameter<RealVector>)Parameters["BestGlobal"]; }
|
---|
| 33 | }
|
---|
| 34 | #endregion
|
---|
| 35 |
|
---|
| 36 | public ParticleUpdater()
|
---|
| 37 | : base() {
|
---|
| 38 | Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator (to generate alpha an beta)."));
|
---|
| 39 | Parameters.Add(new LookupParameter<RealVector>("Velocity", "The velocity vector to update."));
|
---|
| 40 | Parameters.Add(new LookupParameter<RealVector>("CurrentPosition", "Current position"));
|
---|
| 41 | Parameters.Add(new LookupParameter<RealVector>("BestLocal", "Best local position"));
|
---|
| 42 | Parameters.Add(new LookupParameter<RealVector>("BestGlobal", "Best global position"));
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public override IOperation Apply() {
|
---|
| 46 | double alpha = ((IRandom)RandomParameter.ActualValue).NextDouble();
|
---|
| 47 | double beta = ((IRandom)RandomParameter.ActualValue).NextDouble();
|
---|
| 48 | RealVector velocity = (RealVector) VelocityParameter.ActualValue;
|
---|
| 49 | for (int i = 0; i < velocity.Length; i++) {
|
---|
| 50 | velocity[i] = velocity[i] + alpha * (BestLocalParameter.ActualValue[i] - CurrentPositionParameter.ActualValue[i]) + beta * (BestGlobalParameter.ActualValue[i] - CurrentPositionParameter.ActualValue[i]);
|
---|
| 51 | }
|
---|
| 52 | VelocityParameter.ActualValue = velocity;
|
---|
| 53 | for (int i = 0; i < CurrentPositionParameter.ActualValue.Length; i++) {
|
---|
| 54 | CurrentPositionParameter.ActualValue[i] = CurrentPositionParameter.ActualValue[i] + VelocityParameter.ActualValue[i];
|
---|
| 55 | }
|
---|
| 56 | return base.Apply();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public override bool CanChangeName {
|
---|
| 60 | get { return false; }
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 | }
|
---|