Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5561 for trunk/sources


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

#852: Code refactoring. Worked on swarm updater.

Location:
trunk/sources
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimization.cs

    r5560 r5561  
    114114    private ParticleSwarmOptimizationMainLoop mainLoop;
    115115
     116    [Storable]
     117    private CombinedOperator swarmUpdater;
     118
    116119    public ITopologyInitializer TopologyInitializer {
    117120      get { return TopologyInitializerParameter.Value; }
     
    141144      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
    142145      solutionsCreator = cloner.Clone(original.solutionsCreator);
    143       mainLoop = cloner.Clone(original.mainLoop);
     146      mainLoop = cloner.Clone(original.mainLoop);
     147      swarmUpdater = cloner.Clone(original.swarmUpdater);
    144148      Initialize();
    145149    }
     
    166170      ResultsCollector resultsCollector = new ResultsCollector();
    167171      Placeholder analyzerPlaceholder = new Placeholder();
    168       RealVectorSwarmUpdater swarmUpdater = new RealVectorSwarmUpdater();
     172      swarmUpdater = new CombinedOperator();
    169173      mainLoop = new ParticleSwarmOptimizationMainLoop();
    170174
     
    206210      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
    207211      mainLoop.ResultsParameter.ActualName = "Results";
    208       mainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
    209212     // mainLoop.EvaluatedMovesParameter.ActualName = "EvaluatedMoves";
    210213
     
    212215      ////InitVelocityBoundsUpdater();
    213216      InitializeParticleCreator();
     217      InitializeSwarmUpdater();
    214218      ParameterizeSolutionsCreator();
    215219      UpdateAnalyzers();
     
    252256      UpdateTopologyParameters();
    253257      InitializeParticleCreator();
     258      InitializeSwarmUpdater();
    254259      ParameterizeSolutionsCreator();
    255260      base.OnProblemChanged();
     
    395400
    396401    private void ParameterizeMainLoop() {
    397       mainLoop.MaxIterationsParameter.ActualName = MaxIterationsParameter.Name;
     402      if (Problem != null) {
     403        mainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
     404      }
     405    }
     406
     407    private void InitializeSwarmUpdater() {
     408      if (Problem != null) {
     409        ISwarmUpdater updater = Problem.Operators.OfType<ISwarmUpdater>().FirstOrDefault();
     410        swarmUpdater.OperatorGraph.InitialOperator = updater;
     411      }
    398412    }
    399413    #endregion
  • 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
  • trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/ISwarmUpdater.cs

    r5560 r5561  
    2525namespace HeuristicLab.Optimization {
    2626  public interface ISwarmUpdater : IOperator {
    27     ILookupParameter<DoubleValue> QualityParameter { get; }
    28     ILookupParameter<DoubleValue> NeighborsBestQualityParameter { get; }
    29     ILookupParameter<DoubleValue> PersonalBestQualityParameter { get; }
     27    IScopeTreeLookupParameter<DoubleValue> QualityParameter { get; }
     28    IScopeTreeLookupParameter<DoubleValue> NeighborsBestQualityParameter { get; }
     29    IScopeTreeLookupParameter<DoubleValue> PersonalBestQualityParameter { get; }
    3030    IValueLookupParameter<BoolValue> MaximizationParameter { get; }
    3131  }
  • trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs

    r5560 r5561  
    412412        op.BoundsParameter.ActualName = BoundsParameter.Name;
    413413      }
     414      foreach (IRealVectorSwarmUpdater op in Operators.OfType<IRealVectorSwarmUpdater>()) {
     415        op.RealVectorParameter.ActualName = SolutionCreator.RealVectorParameter.ActualName;
     416        op.MaximizationParameter.ActualName = MaximizationParameter.Name;
     417      }
    414418    }
    415419    private void UpdateStrategyVectorBounds() {
Note: See TracChangeset for help on using the changeset viewer.