Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/02/11 15:22:54 (14 years ago)
Author:
epitzer
Message:

Additional improvements to PSO (#852)

  • simplify and clean up RealVectorSwarmUpdater
  • make the RealVectorParticleCreator an AlgorithmOperator
  • standardize naming of local variables in ParticleUpdaters
  • remove default parameter values from main loop
  • new implementation of MultiPSOTopologyUpdater (using shuffling)
Location:
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorNeighborhoodParticleUpdater.cs

    r5568 r5592  
    3030  public sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater, ILocalParticleUpdater {
    3131
     32    #region Construction & Cloning
    3233    [StorableConstructor]
    3334    private RealVectorNeighborhoodParticleUpdater(bool deserializing) : base(deserializing) { }
    3435    private RealVectorNeighborhoodParticleUpdater(RealVectorNeighborhoodParticleUpdater original, Cloner cloner) : base(original, cloner) { }
    3536    public RealVectorNeighborhoodParticleUpdater() : base() { }
    36 
    3737    public override IDeepCloneable Clone(Cloner cloner) {
    3838      return new RealVectorNeighborhoodParticleUpdater(this, cloner);
    3939    }
     40    #endregion
    4041
    4142    public override IOperation Apply() {
     43      double inertia = Inertia.Value;
     44      double personalBestAttraction = PersonalBestAttraction.Value;
     45      double neighborBestAttraction = NeighborBestAttraction.Value;
     46
    4247      RealVector velocity = new RealVector(Velocity.Length);
    4348      RealVector position = new RealVector(RealVector.Length);
    4449      double r_p = Random.NextDouble();
    4550      double r_g = Random.NextDouble();
    46       double omega = Inertia.Value;
    47       double phi_p = PersonalBestAttraction.Value;
    48       double phi_g = NeighborBestAttraction.Value;
     51
    4952      for (int i = 0; i < velocity.Length; i++) {
    5053        velocity[i] =
    51           Velocity[i] * omega +
    52           (PersonalBest[i] - RealVector[i]) * phi_p * r_p +
    53           (BestPoint[i] - RealVector[i]) * phi_g * r_g;
     54          Velocity[i] * inertia +
     55          (PersonalBest[i] - RealVector[i]) * personalBestAttraction * r_p +
     56          (BestPoint[i] - RealVector[i]) * neighborBestAttraction * r_g;
    5457      }
     58
    5559      BoundsChecker.Apply(velocity, VelocityBounds);
    5660      for (int i = 0; i < velocity.Length; i++) {
     
    5862      }
    5963      BoundsChecker.Apply(position, Bounds);
     64
    6065      RealVector = position;
    6166      Velocity = velocity;
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleCreator.cs

    r5560 r5592  
    2020#endregion
    2121
    22 using System;
    23 using HeuristicLab.Operators;
     22using HeuristicLab.Common;
    2423using HeuristicLab.Core;
    2524using HeuristicLab.Data;
     25using HeuristicLab.Operators;
    2626using HeuristicLab.Parameters;
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    28 using HeuristicLab.Common;
    29 using HeuristicLab.Optimization;
    3028
    3129namespace HeuristicLab.Encodings.RealVectorEncoding {
    3230  [Item("RealVectorParticleCreator", "Creates a particle with position, zero velocity vector and personal best.")]
    3331  [StorableClass]
    34   public class RealVectorParticleCreator : SingleSuccessorOperator, IRealVectorParticleCreator {
    35     #region IRealVectorParticleCreator Members
     32  public class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator {
     33
     34    #region Parameters
    3635    public ILookupParameter<IntValue> ProblemSizeParameter {
    37      get { return (ILookupParameter<IntValue>)Parameters["ProblemSize"]; }
     36      get { return (ILookupParameter<IntValue>)Parameters["ProblemSize"]; }
    3837    }
    39 
     38    public IValueLookupParameter<DoubleMatrix> BoundsParameter {
     39      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
     40    }
    4041    public ILookupParameter<RealVector> RealVectorParameter {
    4142      get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
    4243    }
    43 
    4444    public ILookupParameter<RealVector> PersonalBestParameter {
    4545      get { return (ILookupParameter<RealVector>)Parameters["PersonalBest"]; }
    4646    }
    47 
    48     public IValueLookupParameter<DoubleMatrix> BoundsParameter {
    49       get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
    50     }
    51 
    5247    public ILookupParameter<RealVector> VelocityParameter {
    5348      get { return (ILookupParameter<RealVector>)Parameters["Velocity"]; }
    5449    }
    55 
    5650    #endregion
    5751
    58     public RealVectorParticleCreator() : base() {
     52    #region Parameter Values
     53    protected int ProblemSize {
     54      get { return ProblemSizeParameter.ActualValue.Value; }
     55    }
     56    protected RealVector Velocity {
     57      set { VelocityParameter.ActualValue = value; }
     58    }
     59    #endregion
     60
     61    #region Construction & Cloning
     62    [StorableConstructor]
     63    protected RealVectorParticleCreator(bool deserializing) : base(deserializing) { }
     64    protected RealVectorParticleCreator(RealVectorParticleCreator original, Cloner cloner) : base(original, cloner) { }
     65    public RealVectorParticleCreator()
     66      : base() {
    5967      Parameters.Add(new LookupParameter<IntValue>("ProblemSize", "The dimension of the problem."));
    6068      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension."));
    6169      Parameters.Add(new LookupParameter<RealVector>("RealVector", "Particle's current solution"));
    6270      Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution."));
    63       Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity."));   
    64     }
     71      Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity."));
    6572
    66     public override IOperation Apply() {
    67       VelocityParameter.ActualValue = new RealVector(ProblemSizeParameter.ActualValue.Value);
    68       UniformRandomRealVectorCreator realVectorCreater = new UniformRandomRealVectorCreator();
     73      UniformRandomRealVectorCreator realVectorCreater = new UniformRandomRealVectorCreator();
    6974      Assigner personalBestPositionAssigner = new Assigner();
    7075
    71       this.Name = "Particle Creator";
     76      OperatorGraph.InitialOperator = realVectorCreater;
    7277
    73       //realVectorCreater.Name = "(SolutionCreator)";
    74       realVectorCreater.RealVectorParameter.ActualName = "RealVector";
     78      realVectorCreater.RealVectorParameter.ActualName = RealVectorParameter.Name;
    7579      realVectorCreater.LengthParameter.ActualName = ProblemSizeParameter.Name;
    76       realVectorCreater.BoundsParameter.ActualName = BoundsParameter.Name; 
     80      realVectorCreater.BoundsParameter.ActualName = BoundsParameter.Name;
    7781      realVectorCreater.Successor = personalBestPositionAssigner;
    7882
    79       personalBestPositionAssigner.LeftSideParameter.ActualName = "PersonalBest";
    80       personalBestPositionAssigner.RightSideParameter.ActualName = "RealVector";
    81       personalBestPositionAssigner.Successor = null;
    82 
    83       OperationCollection next = new OperationCollection();
    84       next.Add(ExecutionContext.CreateChildOperation(realVectorCreater));
    85       next.Add(base.Apply());
    86       return next;
     83      personalBestPositionAssigner.LeftSideParameter.ActualName = PersonalBestParameter.Name;
     84      personalBestPositionAssigner.RightSideParameter.ActualName = RealVectorParameter.Name;
     85      personalBestPositionAssigner.Successor = null;
    8786    }
    88 
    8987    public override IDeepCloneable Clone(Cloner cloner) {
    9088      return new RealVectorParticleCreator(this, cloner);
    9189    }
     90    #endregion
    9291
    93     protected RealVectorParticleCreator(RealVectorParticleCreator original, Cloner cloner)
    94       : base(original, cloner) {
     92    public override IOperation Apply() {
     93      Velocity = new RealVector(ProblemSize);
     94      return base.Apply();
    9595    }
    9696
    97     [StorableConstructor]
    98     protected RealVectorParticleCreator(bool deserializing) : base(deserializing) { }
     97
    9998  }
    10099}
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.cs

    r5568 r5592  
    2828
    2929namespace HeuristicLab.Encodings.RealVectorEncoding {
     30
    3031  [Item("RealVectorParticleUpdater", "Updates a certain particle taking the current position and velocity into account, as well as the best point and the best point in a local neighborhood.")]
    3132  [StorableClass]
    3233  public abstract class RealVectorParticleUpdater : SingleSuccessorOperator, IRealVectorParticleUpdater {
     34
    3335    public override bool CanChangeName {
    3436      get { return false; }
     
    110112
    111113    #region Construction & Cloning
    112 
    113114    [StorableConstructor]
    114115    protected RealVectorParticleUpdater(bool deserializing) : base(deserializing) { }
    115116    protected RealVectorParticleUpdater(RealVectorParticleUpdater original, Cloner cloner) : base(original, cloner) { }
    116 
    117117    public RealVectorParticleUpdater()
    118118      : base() {
     
    129129      Parameters.Add(new LookupParameter<DoubleValue>("NeighborBestAttraction", "The weight for the global best position."));
    130130    }
    131 
    132131    #endregion
    133132  }
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs

    r5581 r5592  
    3333  [StorableClass]
    3434  public sealed class RealVectorSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater {
     35
    3536    public override bool CanChangeName {
    3637      get { return false; }
     
    7980    private DoubleValue BestQuality {
    8081      get { return BestQualityParameter.ActualValue; }
     82      set { BestQualityParameter.ActualValue = value; }
    8183    }
    8284    private RealVector BestPoint {
     
    149151
    150152    public override IOperation Apply() {
    151       InitializeBestPoint();
    152       UpdateNeighbors();
    153       UpdateSwarm();
    154       if (VelocityBoundsUpdater != null) {
    155         var ops = new OperationCollection();
    156         ops.Add(ExecutionContext.CreateChildOperation(VelocityBoundsUpdater));
    157         ops.Add(ExecutionContext.CreateOperation(Successor));
    158         return ops;
    159       } else {
    160         return base.Apply();
    161       }
    162     }
    163 
    164     private void InitializeBestPoint() {
     153      UpdateGlobalBest();
     154      UpdateNeighborBest();
     155      UpdatePersonalBest();
     156      return UpdateVelocityBounds();
     157    }
     158
     159    private void UpdateGlobalBest() {
    165160      if (BestQuality == null)
    166         BestQualityParameter.ActualValue = new DoubleValue();
     161        BestQuality = new DoubleValue();
    167162      BestQuality.Value = Maximization ? Quality.Max(v => v.Value) : Quality.Min(v => v.Value);
    168       int bestIndex = Quality.FindIndex(v => v.Value == BestQuality.Value);
    169       BestPoint = (RealVector)RealVector[bestIndex].Clone();
    170     }
    171 
    172     private void UpdateNeighbors() {
    173       if (Neighbors != null & Neighbors.Length > 0) {
    174         if (this.NeighborBest == null || NeighborBest.Length != Neighbors.Length)
    175           NeighborBest = new ItemArray<RealVector>(Neighbors.Length);
     163      BestPoint = (RealVector)RealVector[Quality.FindIndex(v => v.Value == BestQuality.Value)].Clone();
     164    }
     165
     166    private void UpdateNeighborBest() {
     167      if (Neighbors.Length > 0) {
     168        var neighborBest = new ItemArray<RealVector>(Neighbors.Length);
     169        var neighborBestQuality = new ItemArray<DoubleValue>(Neighbors.Length);
    176170        for (int n = 0; n < Neighbors.Length; n++) {
    177171          var pairs = Quality.Zip(RealVector, (q, p) => new { Quality = q, Point = p })
    178172            .Where((p, i) => i == n || Neighbors[n].Contains(i));
    179           NeighborBest[n] = Maximization ?
    180           pairs.OrderByDescending(p => p.Quality.Value).First().Point :
    181           pairs.OrderBy(p => p.Quality.Value).First().Point;
     173          var bestNeighbor = Maximization ?
     174            pairs.OrderByDescending(p => p.Quality.Value).First() :
     175            pairs.OrderBy(p => p.Quality.Value).First();
     176          neighborBest[n] = bestNeighbor.Point;
     177          neighborBestQuality[n] = bestNeighbor.Quality;
    182178        }
    183         NeighborBestParameter.ActualValue = NeighborBest;
     179        NeighborBest = neighborBest;
     180        NeighborBestQuality = neighborBestQuality;
    184181      }
    185182    }
    186183
    187     private void UpdateSwarm() {
    188       if (PersonalBestQuality.Length == 0) {
     184    private void UpdatePersonalBest() {
     185      if (PersonalBestQuality.Length == 0)
    189186        PersonalBestQuality = (ItemArray<DoubleValue>)Quality.Clone();
    190         if (VelocityBounds == null)
    191           VelocityBounds = new DoubleMatrix(new double[,] { { -1, 1 } });
    192       }
    193187      for (int i = 0; i < RealVector.Length; i++) {
    194188        if (Maximization && Quality[i].Value > PersonalBestQuality[i].Value ||
     
    198192        }
    199193      }
    200       if (Neighbors.Length > 0) {
    201         var neighborBestQuality = NeighborBestQuality;
    202         var neighborBest = NeighborBest;
    203         if (NeighborBestQuality.Length == 0) {
    204           neighborBestQuality = (ItemArray<DoubleValue>)Quality.Clone();
    205           neighborBest = (ItemArray<RealVector>)RealVector.Clone();
    206         }
    207         for (int i = 0; i < RealVector.Length; i++) {
    208           if (Maximization && PersonalBestQuality[i].Value > neighborBestQuality[i].Value ||
    209              !Maximization && PersonalBestQuality[i].Value < neighborBestQuality[i].Value) {
    210             neighborBestQuality[i].Value = PersonalBestQuality[i].Value;
    211             neighborBest[i] = PersonalBest[i];
    212           }
    213         }
    214         NeighborBestQuality = neighborBestQuality;
    215         NeighborBest = neighborBest;
    216       }
     194    }
     195
     196    private IOperation UpdateVelocityBounds() {
     197      if (VelocityBounds == null)
     198        VelocityBounds = new DoubleMatrix(new double[,] { { -1, 1 } });
     199      return VelocityBoundsUpdater == null ?
     200        base.Apply() :
     201        new OperationCollection() {
     202          ExecutionContext.CreateChildOperation(VelocityBoundsUpdater),
     203          ExecutionContext.CreateOperation(Successor)
     204        };
    217205    }
    218206  }
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorTotallyConnectedParticleUpdater.cs

    r5568 r5592  
    3131
    3232    #region Construction & Cloning
    33 
    3433    [StorableConstructor]
    3534    private RealVectorTotallyConnectedParticleUpdater(bool deserializing) : base(deserializing) { }
    3635    private RealVectorTotallyConnectedParticleUpdater(RealVectorTotallyConnectedParticleUpdater original, Cloner cloner) : base(original, cloner) { }
    3736    public RealVectorTotallyConnectedParticleUpdater() : base() { }
    38 
    3937    public override IDeepCloneable Clone(Cloner cloner) {
    4038      return new RealVectorTotallyConnectedParticleUpdater(this, cloner);
    4139    }
    42 
    4340    #endregion
    4441
    4542    public override IOperation Apply() {
    46       base.Apply();
     43      double inertia = Inertia.Value;
     44      double personalBestAttraction = PersonalBestAttraction.Value;
     45      double neighborBestAttraction = NeighborBestAttraction.Value;
     46
    4747      RealVector velocity = new RealVector(Velocity.Length);
    4848      RealVector position = new RealVector(RealVector.Length);
    4949      double r_p = Random.NextDouble();
    5050      double r_g = Random.NextDouble();
    51       double omega = Inertia.Value;
    52       double phi_p = PersonalBestAttraction.Value;
    53       double phi_g = NeighborBestAttraction.Value;
     51
    5452      for (int i = 0; i < velocity.Length; i++) {
    5553        velocity[i] =
    56           Velocity[i] * omega +
    57           (PersonalBest[i] - RealVector[i]) * phi_p * r_p +
    58           (BestPoint[i] - RealVector[i]) * phi_g * r_g;
     54          Velocity[i] * inertia +
     55          (PersonalBest[i] - RealVector[i]) * personalBestAttraction * r_p +
     56          (BestPoint[i] - RealVector[i]) * neighborBestAttraction * r_g;
    5957      }
     58
    6059      BoundsChecker.Apply(velocity, VelocityBounds);
    6160      for (int i = 0; i < velocity.Length; i++) {
     
    6362      }
    6463      BoundsChecker.Apply(position, Bounds);
     64
    6565      RealVector = position;
    6666      Velocity = velocity;
Note: See TracChangeset for help on using the changeset viewer.