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)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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  }
Note: See TracChangeset for help on using the changeset viewer.