Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/24/11 19:22:39 (14 years ago)
Author:
mkofler
Message:

#852: Code refactoring. Worked on swarm updater.

Location:
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Interfaces/IRealVectorSwarmUpdater.cs

    r5560 r5561  
    2525namespace HeuristicLab.Encodings.RealVectorEncoding {
    2626  public interface IRealVectorSwarmUpdater : ISwarmUpdater, IRealVectorOperator {
    27     ILookupParameter<RealVector> NeighborsBestParameter { get; }
    28     ILookupParameter<RealVector> PersonalBestParameter { get; }
    29     ILookupParameter<RealVector> RealVectorParameter { get; }
     27    IScopeTreeLookupParameter<RealVector> NeighborsBestParameter { get; }
     28    IScopeTreeLookupParameter<RealVector> PersonalBestParameter { get; }
     29    IScopeTreeLookupParameter<RealVector> RealVectorParameter { get; }
    3030  }
    3131}
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs

    r5560 r5561  
    2020#endregion
    2121
     22using System.Linq;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    3637
    3738    #region Parameter properties
    38     public ILookupParameter<DoubleValue> QualityParameter {
    39       get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
    40     }
    41     public ILookupParameter<DoubleValue> PersonalBestQualityParameter {
    42       get { return (ILookupParameter<DoubleValue>)Parameters["PersonalBestQuality"]; }
    43     }
    44     public ILookupParameter<DoubleValue> NeighborsBestQualityParameter {
    45       get { return (ILookupParameter<DoubleValue>)Parameters["NeighborsBestQuality"]; }
    46     }
    47     public ILookupParameter<RealVector> RealVectorParameter {
    48       get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
    49     }
    50     public ILookupParameter<RealVector> PersonalBestParameter {
    51       get { return (ILookupParameter<RealVector>)Parameters["PersonalBest"]; }
    52     }
    53     public ILookupParameter<RealVector> NeighborsBestParameter {
    54       get { return (ILookupParameter<RealVector>)Parameters["NeighborsBest"]; }
     39    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
     40      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
     41    }
     42    public IScopeTreeLookupParameter<DoubleValue> PersonalBestQualityParameter {
     43      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["PersonalBestQuality"]; }
     44    }
     45    public IScopeTreeLookupParameter<DoubleValue> NeighborsBestQualityParameter {
     46      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["NeighborsBestQuality"]; }
     47    }
     48    public IScopeTreeLookupParameter<RealVector> RealVectorParameter {
     49      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["RealVector"]; }
     50    }
     51    public IScopeTreeLookupParameter<RealVector> PersonalBestParameter {
     52      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["PersonalBest"]; }
     53    }
     54    public IScopeTreeLookupParameter<RealVector> NeighborsBestParameter {
     55      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["NeighborsBest"]; }
    5556    }
    5657    public IValueLookupParameter<BoolValue> MaximizationParameter {
    5758      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
    5859    }
     60    public ILookupParameter<DoubleValue> BestQualityParameter {
     61      get { return (ILookupParameter<DoubleValue>)Parameters["BestQuality"]; }
     62    }
     63    public ILookupParameter<RealVector> BestPointParameter {
     64      get { return (ILookupParameter<RealVector>)Parameters["BestPoint"]; }
     65    }
     66    public IScopeTreeLookupParameter<IntArray> NeighborsParameter {
     67      get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; }
     68    }
    5969    #endregion
    6070
    6171    #region Parameter values
    62     private double Quality {
    63       get { return QualityParameter.ActualValue.Value; }
    64     }
    65     private double PersonalBestQuality {
    66       get { return PersonalBestQualityParameter.ActualValue.Value; }
    67       set { PersonalBestQualityParameter.ActualValue = new DoubleValue(value); }
    68     }
    69     private double NeighborsBestQuality {
    70       get { return NeighborsBestQualityParameter.ActualValue.Value; }
    71       set { NeighborsBestQualityParameter.ActualValue = new DoubleValue(value); }
    72     }
    73     private RealVector RealVector {
     72    private DoubleValue BestQuality {
     73      get { return BestQualityParameter.ActualValue; }
     74    }
     75    private RealVector BestPoint {
     76      get { return BestPointParameter.ActualValue; }
     77      set { BestPointParameter.ActualValue = value; }
     78    }
     79    private ItemArray<DoubleValue> Quality {
     80      get { return QualityParameter.ActualValue; }
     81    }
     82    private ItemArray<DoubleValue> PersonalBestQuality {
     83      get { return PersonalBestQualityParameter.ActualValue; }
     84    }
     85    private ItemArray<DoubleValue> NeighborsBestQuality {
     86      get { return NeighborsBestQualityParameter.ActualValue; }
     87    }
     88    private ItemArray<RealVector> RealVector {
    7489      get { return RealVectorParameter.ActualValue; }
    7590    }
    76     private RealVector PersonalBest {
     91    private ItemArray<RealVector> PersonalBest {
    7792      get { return PersonalBestParameter.ActualValue; }
    7893      set { PersonalBestParameter.ActualValue = value; }
    7994    }
    80     private RealVector NeighborsBest {
     95    private ItemArray<RealVector> NeighborsBest {
    8196      get { return NeighborsBestParameter.ActualValue; }
    8297      set { NeighborsBestParameter.ActualValue = value; }
     
    8499    private bool Maximization {
    85100      get { return MaximizationParameter.ActualValue.Value; }
     101    }
     102    private ItemArray<IntArray> Neighbors {
     103      get { return NeighborsParameter.ActualValue; }
    86104    }
    87105    #endregion
     
    94112    public RealVectorSwarmUpdater()
    95113      : base() {
    96       Parameters.Add(new LookupParameter<DoubleValue>("Quality", "Particle's quality"));
    97       Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestQuality", "Particle's personal best quality"));
    98       Parameters.Add(new LookupParameter<DoubleValue>("NeighborsBestQuality", "Global best particle quality"));
    99       Parameters.Add(new LookupParameter<RealVector>("RealVector", "Particle's position"));
    100       Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best position"));
    101       Parameters.Add(new LookupParameter<RealVector>("NeighborsBest", "Neighborhood (or global in case of totally connected neighborhood) best particle position"));
     114      Parameters.Add(new LookupParameter<DoubleValue>("BestQuality", "Overall best quality."));
     115      Parameters.Add(new LookupParameter<RealVector>("BestPoint", "Global best particle position"));
     116      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "Particle's quality"));
     117      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("PersonalBestQuality", "Particle's personal best quality"));
     118      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("NeighborsBestQuality", "Global best particle quality"));
     119      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "Particle's position"));
     120      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("PersonalBest", "Particle's personal best position"));
     121      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("NeighborsBest", "Neighborhood (or global in case of totally connected neighborhood) best particle position"));
     122      Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle."));
    102123      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
    103124    }
     
    110131
    111132    public override IOperation Apply() {
    112       if (Maximization && Quality > PersonalBestQuality ||
    113          !Maximization && Quality < PersonalBestQuality) {
    114         PersonalBestQuality = Quality;
    115         PersonalBest = RealVector;
    116         if (Maximization && PersonalBestQuality > NeighborsBestQuality ||
    117            !Maximization && PersonalBestQuality < NeighborsBestQuality) {
    118              NeighborsBestQuality = PersonalBestQuality;
    119              NeighborsBest = PersonalBest;
    120         }
    121       }
     133      InitializeBestPoint();
     134      UpdateNeighbors();
     135      UpdateVelocityBounds();
     136      UpdateSwarm();
    122137      return base.Apply();
     138    }
     139
     140    private void InitializeBestPoint() {
     141      if (BestQuality == null)
     142        BestQualityParameter.ActualValue = new DoubleValue();
     143      BestQuality.Value = Maximization ? Quality.Max(v => v.Value) : Quality.Min(v => v.Value);
     144      int bestIndex = Quality.FindIndex(v => v.Value == BestQuality.Value);
     145      BestPoint = (RealVector)RealVector[bestIndex].Clone();
     146    }
     147
     148    private void UpdateNeighbors() {
     149      if (Neighbors != null & Neighbors.Length > 0) {
     150        if (this.NeighborsBest == null || NeighborsBest.Length != Neighbors.Length)
     151          NeighborsBest = new ItemArray<RealVector>(Neighbors.Length);
     152        for (int n = 0; n < Neighbors.Length; n++) {
     153          var pairs = Quality.Zip(RealVector, (q, p) => new { Quality = q, Point = p })
     154            .Where((p, i) => i == n || Neighbors[n].Contains(i));
     155          NeighborsBest[n] = Maximization ?
     156          pairs.OrderByDescending(p => p.Quality.Value).First().Point :
     157          pairs.OrderBy(p => p.Quality.Value).First().Point;
     158        }
     159        NeighborsBestParameter.ActualValue = NeighborsBest;
     160      }
     161    }
     162
     163    private void UpdateVelocityBounds() {
     164      // ToDo: Add code
     165    }
     166
     167    private void UpdateSwarm() {
     168      if (PersonalBestQuality.Length == 0) {
     169        PersonalBestQualityParameter.ActualValue = new ItemArray<DoubleValue>(RealVector.Length);
     170        for (int i = 0; i < RealVector.Length; i++) {
     171          if (Maximization) {
     172            PersonalBestQuality[i] = new DoubleValue();
     173          } else {
     174            PersonalBestQuality[i] = new DoubleValue(double.MaxValue);
     175          }
     176        }
     177      }
     178      if (NeighborsBestQuality.Length == 0) {
     179        NeighborsBestQualityParameter.ActualValue = new ItemArray<DoubleValue>(RealVector.Length);
     180        for (int i = 0; i < RealVector.Length; i++) {
     181          if (Maximization) {
     182            NeighborsBestQuality[i] = new DoubleValue();
     183          } else {
     184            NeighborsBestQuality[i] = new DoubleValue(double.MaxValue);
     185          }
     186        }
     187      }
     188      if (NeighborsBest.Length == 0) {
     189        NeighborsBestParameter.ActualValue = new ItemArray<RealVector>(RealVector.Length);
     190      }
     191      for (int i = 0; i < RealVector.Length; i++) {
     192        if (Maximization && Quality[i].Value > PersonalBestQuality[i].Value ||
     193          !Maximization && Quality[i].Value < PersonalBestQuality[i].Value) {
     194          PersonalBestQuality[i].Value = Quality[i].Value;
     195          PersonalBest[i] = RealVector[i];
     196          if (Maximization && PersonalBestQuality[i].Value > NeighborsBestQuality[i].Value ||
     197             !Maximization && PersonalBestQuality[i].Value < NeighborsBestQuality[i].Value) {
     198            NeighborsBestQuality[i].Value = PersonalBestQuality[i].Value;
     199            NeighborsBest[i] = PersonalBest[i];
     200          }
     201        }
     202      }
    123203    }
    124204
Note: See TracChangeset for help on using the changeset viewer.