Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5568 for trunk/sources


Ignore:
Timestamp:
02/28/11 17:25:28 (14 years ago)
Author:
epitzer
Message:

Continue working on SwarmUpdater (#852)

Location:
trunk/sources
Files:
11 edited

Legend:

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

    r5561 r5568  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Data;
    28 using HeuristicLab.Encodings.RealVectorEncoding;
    2928using HeuristicLab.Operators;
    3029using HeuristicLab.Optimization;
     
    7877      get { return (IValueParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; }
    7978    }
    80     public IValueParameter<DoubleValue> NeighborsBestAttractionParameter {
    81       get { return (IValueParameter<DoubleValue>)Parameters["NeighborsBestAttraction"]; }
     79    public IValueParameter<DoubleValue> NeighborBestAttractionParameter {
     80      get { return (IValueParameter<DoubleValue>)Parameters["NeighborBestAttraction"]; }
    8281    }
    8382    public IValueParameter<MultiAnalyzer> AnalyzerParameter {
     
    9998      get { return (OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["InertiaUpdater"]; }
    10099    }
     100    public ConstrainedValueParameter<ISwarmUpdater> SwarmUpdaterParameter {
     101      get { return (ConstrainedValueParameter<ISwarmUpdater>)Parameters["SwarmUpdater"]; }
     102
     103    }
    101104    #endregion
    102105
     
    113116    [Storable]
    114117    private ParticleSwarmOptimizationMainLoop mainLoop;
    115 
    116     [Storable]
    117     private CombinedOperator swarmUpdater;
    118118
    119119    public ITopologyInitializer TopologyInitializer {
     
    145145      solutionsCreator = cloner.Clone(original.solutionsCreator);
    146146      mainLoop = cloner.Clone(original.mainLoop);
    147       swarmUpdater = cloner.Clone(original.swarmUpdater);
    148147      Initialize();
    149148    }
     
    157156      Parameters.Add(new ValueParameter<DoubleValue>("Inertia", "Inertia weight on a particle's movement (omega).", new DoubleValue(-0.2)));
    158157      Parameters.Add(new ValueParameter<DoubleValue>("PersonalBestAttraction", "Weight for particle's pull towards its personal best soution (phi_p).", new DoubleValue(-0.01)));
    159       Parameters.Add(new ValueParameter<DoubleValue>("NeighborsBestAttraction", "Weight for pull towards the neighborhood best solution or global best solution in case of a totally connected topology (phi_g).", new DoubleValue(3.7)));
     158      Parameters.Add(new ValueParameter<DoubleValue>("NeighborBestAttraction", "Weight for pull towards the neighborhood best solution or global best solution in case of a totally connected topology (phi_g).", new DoubleValue(3.7)));
    160159      Parameters.Add(new ConstrainedValueParameter<IParticleCreator>("ParticleCreator", "Operator creates a new particle."));
    161160      Parameters.Add(new ConstrainedValueParameter<IParticleUpdater>("ParticleUpdater", "Operator that updates a particle."));
     
    163162      Parameters.Add(new OptionalConstrainedValueParameter<ITopologyUpdater>("TopologyUpdater", "Updates the neighborhood description vectors."));
    164163      Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("InertiaUpdater", "Updates the omega parameter."));
     164      Parameters.Add(new ConstrainedValueParameter<ISwarmUpdater>("SwarmUpdater", "Encoding-specific parameter which is provided by the problem. May provide additional encoding-specific parameters, such as velocity bounds for real valued problems"));
    165165
    166166      RandomCreator randomCreator = new RandomCreator();
     
    168168      solutionsCreator = new SolutionsCreator();
    169169      Placeholder topologyInitializerPlaceholder = new Placeholder();
    170       ResultsCollector resultsCollector = new ResultsCollector();
    171170      Placeholder analyzerPlaceholder = new Placeholder();
    172       swarmUpdater = new CombinedOperator();
    173171      mainLoop = new ParticleSwarmOptimizationMainLoop();
    174172
     
    183181
    184182      solutionsCreator.NumberOfSolutionsParameter.ActualName = "SwarmSize";
    185       ParameterizeSolutionsCreator(); 
    186       solutionsCreator.Successor = topologyInitializerPlaceholder; 
     183      ParameterizeSolutionsCreator();
     184      solutionsCreator.Successor = topologyInitializerPlaceholder;
    187185
    188186      topologyInitializerPlaceholder.Name = "(TopologyInitializer)";
    189187      topologyInitializerPlaceholder.OperatorParameter.ActualName = "TopologyInitializer";
    190       topologyInitializerPlaceholder.Successor = swarmUpdater;
    191 
    192       swarmUpdater.Successor = resultsCollector;
    193 
    194       resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations", null, "CurrentIteration"));
    195       //resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Current Inertia", null, "Inertia"));
    196       //resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
    197       resultsCollector.ResultsParameter.ActualName = "Results";
    198       resultsCollector.Successor = mainLoop;
     188      topologyInitializerPlaceholder.Successor = mainLoop;
    199189
    200190      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
    201191      mainLoop.InertiaParameter.ActualName = InertiaParameter.Name;
    202192      mainLoop.MaxIterationsParameter.ActualName = MaxIterationsParameter.Name;
    203       mainLoop.NeighborsBestAttractionParameter.ActualName = NeighborsBestAttractionParameter.Name;
     193      mainLoop.NeighborBestAttractionParameter.ActualName = NeighborBestAttractionParameter.Name;
    204194      mainLoop.InertiaUpdaterParameter.ActualName = InertiaUpdaterParameter.Name;
    205195      mainLoop.ParticleUpdaterParameter.ActualName = ParticleUpdaterParameter.Name;
    206196      mainLoop.PersonalBestAttractionParameter.ActualName = PersonalBestAttractionParameter.Name;
    207197      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
    208       mainLoop.SwarmSizeParameter.ActualName = SwarmSizeParameter.Name; 
    209       mainLoop.TopologyUpdaterParameter.ActualName = TopologyUpdaterParameter.Name; 
     198      mainLoop.SwarmSizeParameter.ActualName = SwarmSizeParameter.Name;
     199      mainLoop.TopologyUpdaterParameter.ActualName = TopologyUpdaterParameter.Name;
    210200      mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
    211201      mainLoop.ResultsParameter.ActualName = "Results";
    212      // mainLoop.EvaluatedMovesParameter.ActualName = "EvaluatedMoves";
     202      // mainLoop.EvaluatedMovesParameter.ActualName = "EvaluatedMoves";
    213203
    214204      InitializeAnalyzers();
     
    216206      InitializeParticleCreator();
    217207      InitializeSwarmUpdater();
    218       ParameterizeSolutionsCreator(); 
     208      ParameterizeSolutionsCreator();
    219209      UpdateAnalyzers();
    220210      UpdateInertiaUpdater();
     
    222212      UpdateTopologyInitializer();
    223213      Initialize();
    224       ParameterizeMainLoop(); 
     214      ParameterizeMainLoop();
    225215    }
    226216
     
    257247      InitializeParticleCreator();
    258248      InitializeSwarmUpdater();
    259       ParameterizeSolutionsCreator(); 
     249      ParameterizeSolutionsCreator();
    260250      base.OnProblemChanged();
    261251    }
     
    290280          IParticleCreator creator = ParticleCreatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldParticleCreator.GetType());
    291281          if (creator != null) ParticleCreator = creator;
    292         } 
     282        }
    293283      }
    294284    }
     
    400390
    401391    private void ParameterizeMainLoop() {
    402       if (Problem != null) { 
     392      if (Problem != null) {
    403393        mainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
    404394      }
     
    408398      if (Problem != null) {
    409399        ISwarmUpdater updater = Problem.Operators.OfType<ISwarmUpdater>().FirstOrDefault();
    410         swarmUpdater.OperatorGraph.InitialOperator = updater;
     400        SwarmUpdaterParameter.ValidValues.Clear();
     401        SwarmUpdaterParameter.ValidValues.Add(updater);
     402        SwarmUpdaterParameter.Value = updater;
    411403      }
    412404    }
  • trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimizationMainLoop.cs

    r5566 r5568  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Text;
    26 using HeuristicLab.Operators;
    27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2822using HeuristicLab.Common;
    2923using HeuristicLab.Core;
     24using HeuristicLab.Data;
     25using HeuristicLab.Operators;
     26using HeuristicLab.Optimization;
     27using HeuristicLab.Optimization.Operators;
    3028using HeuristicLab.Parameters;
    31 using HeuristicLab.Data;
    32 using HeuristicLab.Analysis;
    33 using HeuristicLab.Optimization;
    34 using HeuristicLab.Random;
    35 using HeuristicLab.Optimization.Operators;
    36 using HeuristicLab.Encodings.RealVectorEncoding;
     29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3730
    3831namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
     
    5952      get { return (IValueLookupParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; }
    6053    }
    61     public IValueLookupParameter<DoubleValue> NeighborsBestAttractionParameter {
    62       get { return (IValueLookupParameter<DoubleValue>)Parameters["NeighborsBestAttraction"]; }
     54    public IValueLookupParameter<DoubleValue> NeighborBestAttractionParameter {
     55      get { return (IValueLookupParameter<DoubleValue>)Parameters["NeighborBestAttraction"]; }
    6356    }
    6457    public IValueLookupParameter<DoubleMatrix> VelocityBoundsParameter {
    6558      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
    66     }                                     
     59    }
    6760    public IValueLookupParameter<IOperator> ParticleUpdaterParameter {
    6861      get { return (IValueLookupParameter<IOperator>)Parameters["ParticleUpdater"]; }
     
    7467      get { return (IValueLookupParameter<IOperator>)Parameters["InertiaUpdater"]; }
    7568    }
    76     public IValueLookupParameter<VariableCollection> ResultsParameter {
    77       get { return (IValueLookupParameter<VariableCollection>)Parameters["Results"]; }
     69    public IValueLookupParameter<ResultCollection> ResultsParameter {
     70      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
    7871    }
    7972    public IValueLookupParameter<IOperator> EvaluatorParameter {
    8073      get { return (IValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
    8174    }
     75    public ValueLookupParameter<ISwarmUpdater> SwarmUpdaterParameter {
     76      get { return (ValueLookupParameter<ISwarmUpdater>)Parameters["SwarmUpdater"]; }
     77    }
    8278    #endregion
    83                                                                  
     79
    8480    public ParticleSwarmOptimizationMainLoop()
    8581      : base() {
     
    10298      Parameters.Add(new ValueLookupParameter<IntValue>("SwarmSize", "Size of the particle swarm.", new IntValue(10)));
    10399      Parameters.Add(new ValueLookupParameter<IntValue>("MaxIterations", "Maximal number of iterations.", new IntValue(1000)));
    104      
     100
    105101      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze each generation."));
    106102
    107103      Parameters.Add(new ValueLookupParameter<DoubleValue>("Inertia", "Inertia weight on a particle's movement (omega)."));
    108104      Parameters.Add(new ValueLookupParameter<DoubleValue>("PersonalBestAttraction", "Weight for particle's pull towards its personal best soution (phi_p)."));
    109       Parameters.Add(new ValueLookupParameter<DoubleValue>("NeighborsBestAttraction", "Weight for pull towards the neighborhood best solution or global best solution in case of a totally connected topology (phi_g)."));
    110      
     105      Parameters.Add(new ValueLookupParameter<DoubleValue>("NeighborBestAttraction", "Weight for pull towards the neighborhood best solution or global best solution in case of a totally connected topology (phi_g)."));
     106
    111107      Parameters.Add(new ValueLookupParameter<IOperator>("ParticleUpdater", "Operator that calculates new position and velocity of a particle"));
    112108      Parameters.Add(new ValueLookupParameter<IOperator>("TopologyUpdater", "Updates the neighborhood description vectors"));
     
    114110      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "Evaluates a particle solution."));
    115111
    116       Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored."));
     112      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The variable collection where results should be stored."));
     113
     114      Parameters.Add(new ValueLookupParameter<ISwarmUpdater>("SwarmUpdater", "The encoding-specific swarm updater."));
    117115      #endregion
    118116
    119117      #region Create operators
     118      ResultsCollector resultsCollector = new ResultsCollector();
     119      Placeholder swarmUpdaterPlaceholer1 = new Placeholder();
    120120      Placeholder evaluatorPlaceholder = new Placeholder();
    121121      Placeholder analyzerPlaceholder = new Placeholder();
     
    135135
    136136      #region Create operator graph
    137       OperatorGraph.InitialOperator = analyzerPlaceholder;
     137      OperatorGraph.InitialOperator = resultsCollector;
     138      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations", null, "CurrentIteration"));
     139      //resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Current Inertia", null, "Inertia"));
     140      //resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
     141      resultsCollector.ResultsParameter.ActualName = "Results";
     142      resultsCollector.Successor = swarmUpdaterPlaceholer1;
     143
     144      swarmUpdaterPlaceholer1.Name = "(Swarm Updater)";
     145      swarmUpdaterPlaceholer1.OperatorParameter.ActualName = SwarmUpdaterParameter.ActualName;
     146      swarmUpdaterPlaceholer1.Successor = analyzerPlaceholder;
    138147
    139148      analyzerPlaceholder.Name = "(Analyzer)";
    140       analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name; 
     149      analyzerPlaceholder.OperatorParameter.ActualName = AnalyzerParameter.Name;
    141150      analyzerPlaceholder.Successor = uniformSubScopeProcessor;
    142151
     
    145154
    146155      particleUpdaterPlaceholder.Name = "(ParticleUpdater)";
    147       particleUpdaterPlaceholder.OperatorParameter.ActualName = ParticleUpdaterParameter.Name; 
     156      particleUpdaterPlaceholder.OperatorParameter.ActualName = ParticleUpdaterParameter.Name;
    148157
    149158      evaluationProcessor.Parallel = new BoolValue(true);
     
    152161
    153162      evaluatorPlaceholder.Name = "(Evaluator)";
    154       evaluatorPlaceholder.OperatorParameter.ActualName = EvaluatorParameter.Name; 
     163      evaluatorPlaceholder.OperatorParameter.ActualName = EvaluatorParameter.Name;
    155164
    156165      topologyUpdaterPlaceholder.Name = "(TopologyUpdater)";
    157       topologyUpdaterPlaceholder.OperatorParameter.ActualName = TopologyUpdaterParameter.Name; 
     166      topologyUpdaterPlaceholder.OperatorParameter.ActualName = TopologyUpdaterParameter.Name;
    158167      topologyUpdaterPlaceholder.Successor = swarmUpdater;
    159168
    160169      swarmUpdater.Name = "Swarm Updater";
    161       swarmUpdater.OperatorParameter.ActualName = "SwarmUpdater";
    162       swarmUpdater.Successor = currentIterationCounter;
     170      swarmUpdater.OperatorParameter.ActualName = SwarmUpdaterParameter.ActualName;
     171      swarmUpdater.Successor = inertiaUpdaterPlaceholder;
     172
     173      inertiaUpdaterPlaceholder.Name = "(Inertia Updater)";
     174      inertiaUpdaterPlaceholder.OperatorParameter.ActualName = InertiaUpdaterParameter.ActualName;
     175      inertiaUpdaterPlaceholder.Successor = currentIterationCounter;
    163176
    164177      currentIterationCounter.Name = "CurrentIteration++";
    165178      currentIterationCounter.ValueParameter.ActualName = "CurrentIteration";
    166       currentIterationCounter.Successor = inertiaUpdaterPlaceholder;
    167 
    168       inertiaUpdaterPlaceholder.Name = "(Inertia Updater)";
    169       inertiaUpdaterPlaceholder.OperatorParameter.ActualName = InertiaUpdaterParameter.ActualName;
    170       inertiaUpdaterPlaceholder.Successor = currentIterationComparator;
     179      currentIterationCounter.Successor = currentIterationComparator;
    171180
    172181      currentIterationComparator.LeftSideParameter.ActualName = "CurrentIteration";
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Interfaces/IRealVectorParticleUpdater.cs

    r5560 r5568  
    3131    ILookupParameter<DoubleMatrix> BoundsParameter { get; }
    3232    ILookupParameter<RealVector> PersonalBestParameter { get; }
    33     ILookupParameter<RealVector> NeighborsBestParameter { get; }
     33    ILookupParameter<RealVector> NeighborBestParameter { get; }
    3434  }
    3535}
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Interfaces/IRealVectorSwarmUpdater.cs

    r5561 r5568  
    2525namespace HeuristicLab.Encodings.RealVectorEncoding {
    2626  public interface IRealVectorSwarmUpdater : ISwarmUpdater, IRealVectorOperator {
    27     IScopeTreeLookupParameter<RealVector> NeighborsBestParameter { get; }
     27    IScopeTreeLookupParameter<RealVector> NeighborBestParameter { get; }
    2828    IScopeTreeLookupParameter<RealVector> PersonalBestParameter { get; }
    2929    IScopeTreeLookupParameter<RealVector> RealVectorParameter { get; }
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorNeighborhoodParticleUpdater.cs

    r5560 r5568  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Encodings.RealVectorEncoding;
     24using HeuristicLab.Optimization;
    2525using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    26 using HeuristicLab.Optimization;
    2726
    2827namespace HeuristicLab.Encodings.RealVectorEncoding {
     
    4746      double omega = Inertia.Value;
    4847      double phi_p = PersonalBestAttraction.Value;
    49       double phi_g = NeighborsBestAttraction.Value;
     48      double phi_g = NeighborBestAttraction.Value;
    5049      for (int i = 0; i < velocity.Length; i++) {
    5150        velocity[i] =
    5251          Velocity[i] * omega +
    5352          (PersonalBest[i] - RealVector[i]) * phi_p * r_p +
    54           (NeighborsBest[i] - RealVector[i]) * phi_g * r_g;
     53          (BestPoint[i] - RealVector[i]) * phi_g * r_g;
    5554      }
    5655      BoundsChecker.Apply(velocity, VelocityBounds);
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.cs

    r5566 r5568  
    4545      get { return (ILookupParameter<RealVector>)Parameters["PersonalBest"]; }
    4646    }
    47     public ILookupParameter<RealVector> NeighborsBestParameter {
    48       get { return (ILookupParameter<RealVector>)Parameters["NeighborsBest"]; }
     47    public ILookupParameter<RealVector> NeighborBestParameter {
     48      get { return (ILookupParameter<RealVector>)Parameters["NeighborBest"]; }
     49    }
     50    public LookupParameter<RealVector> BestPointParameter {
     51      get { return (LookupParameter<RealVector>)Parameters["BestPoint"]; }
    4952    }
    5053    public ILookupParameter<RealVector> RealVectorParameter {
     
    6366      get { return (ILookupParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; }
    6467    }
    65     public ILookupParameter<DoubleValue> NeighborsBestAttractionParameter {
    66       get { return (ILookupParameter<DoubleValue>)Parameters["NeighborsBestAttraction"]; }
     68    public ILookupParameter<DoubleValue> NeighborBestAttractionParameter {
     69      get { return (ILookupParameter<DoubleValue>)Parameters["NeighborBestAttraction"]; }
    6770    }
    6871    #endregion
     
    7982      get { return PersonalBestParameter.ActualValue; }
    8083    }
     84    protected RealVector BestPoint {
     85      get { return BestPointParameter.ActualValue; }
     86    }
    8187    protected RealVector RealVector {
    8288      get { return RealVectorParameter.ActualValue; }
    8389      set { RealVectorParameter.ActualValue = value; }
    8490    }
    85     protected RealVector NeighborsBest {
    86       get { return NeighborsBestParameter.ActualValue; }
     91    protected RealVector NeighborBest {
     92      get { return NeighborBestParameter.ActualValue; }
    8793    }
    8894    protected DoubleMatrix Bounds {
     
    98104      get { return PersonalBestAttractionParameter.ActualValue; }
    99105    }
    100     protected DoubleValue NeighborsBestAttraction {
    101       get { return NeighborsBestAttractionParameter.ActualValue; }
     106    protected DoubleValue NeighborBestAttraction {
     107      get { return NeighborBestAttractionParameter.ActualValue; }
    102108    }
    103109    #endregion
     
    108114    protected RealVectorParticleUpdater(bool deserializing) : base(deserializing) { }
    109115    protected RealVectorParticleUpdater(RealVectorParticleUpdater original, Cloner cloner) : base(original, cloner) { }
    110    
     116
    111117    public RealVectorParticleUpdater()
    112118      : base() {
     
    116122      Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution."));
    117123      Parameters.Add(new LookupParameter<RealVector>("BestPoint", "Global best position."));
    118       Parameters.Add(new LookupParameter<RealVector>("NeighborsBest", "Best neighboring solution."));
     124      Parameters.Add(new LookupParameter<RealVector>("NeighborBest", "Best neighboring solution."));
    119125      Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector for the current problem."));
    120126      Parameters.Add(new LookupParameter<DoubleMatrix>("VelocityBounds", "Upper and lower bounds for the particle's velocity vector."));
    121127      Parameters.Add(new LookupParameter<DoubleValue>("Inertia", "The weight for the particle's velocity vector."));
    122128      Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestAttraction", "The weight for the particle's personal best position."));
    123       Parameters.Add(new LookupParameter<DoubleValue>("NeighborsBestAttraction", "The weight for the global best position."));
     129      Parameters.Add(new LookupParameter<DoubleValue>("NeighborBestAttraction", "The weight for the global best position."));
    124130    }
    125131
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs

    r5566 r5568  
    2525using HeuristicLab.Data;
    2626using HeuristicLab.Operators;
     27using HeuristicLab.Optimization;
    2728using HeuristicLab.Parameters;
    2829using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    4344      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["PersonalBestQuality"]; }
    4445    }
    45     public IScopeTreeLookupParameter<DoubleValue> NeighborsBestQualityParameter {
    46       get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["NeighborsBestQuality"]; }
     46    public IScopeTreeLookupParameter<DoubleValue> NeighborBestQualityParameter {
     47      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["NeighborBestQuality"]; }
    4748    }
    4849    public IScopeTreeLookupParameter<RealVector> RealVectorParameter {
     
    5253      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["PersonalBest"]; }
    5354    }
    54     public IScopeTreeLookupParameter<RealVector> NeighborsBestParameter {
    55       get { return (IScopeTreeLookupParameter<RealVector>)Parameters["NeighborsBest"]; }
    56     }
    57     public IValueLookupParameter<BoolValue> MaximizationParameter {
    58       get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
     55    public IScopeTreeLookupParameter<RealVector> NeighborBestParameter {
     56      get { return (IScopeTreeLookupParameter<RealVector>)Parameters["NeighborBest"]; }
     57    }
     58    public ILookupParameter<BoolValue> MaximizationParameter {
     59      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
    5960    }
    6061    public ILookupParameter<DoubleValue> BestQualityParameter {
     
    6667    public IScopeTreeLookupParameter<IntArray> NeighborsParameter {
    6768      get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; }
     69    }
     70    public ValueLookupParameter<IDiscreteDoubleMatrixModifier> VelocityBoundsUpdaterParameter {
     71      get { return (ValueLookupParameter<IDiscreteDoubleMatrixModifier>)Parameters["VelocityBoundsUpdater"]; }
     72    }
     73    public LookupParameter<DoubleMatrix> VelocityBoundsParameter {
     74      get { return (LookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
    6875    }
    6976    #endregion
     
    8289    private ItemArray<DoubleValue> PersonalBestQuality {
    8390      get { return PersonalBestQualityParameter.ActualValue; }
    84     }
    85     private ItemArray<DoubleValue> NeighborsBestQuality {
    86       get { return NeighborsBestQualityParameter.ActualValue; }
     91      set { PersonalBestQualityParameter.ActualValue = value; }
     92    }
     93    private ItemArray<DoubleValue> NeighborBestQuality {
     94      get { return NeighborBestQualityParameter.ActualValue; }
     95      set { NeighborBestQualityParameter.ActualValue = value; }
    8796    }
    8897    private ItemArray<RealVector> RealVector {
     
    93102      set { PersonalBestParameter.ActualValue = value; }
    94103    }
    95     private ItemArray<RealVector> NeighborsBest {
    96       get { return NeighborsBestParameter.ActualValue; }
    97       set { NeighborsBestParameter.ActualValue = value; }
     104    private ItemArray<RealVector> NeighborBest {
     105      get { return NeighborBestParameter.ActualValue; }
     106      set { NeighborBestParameter.ActualValue = value; }
    98107    }
    99108    private bool Maximization {
     
    102111    private ItemArray<IntArray> Neighbors {
    103112      get { return NeighborsParameter.ActualValue; }
     113    }
     114    private IDiscreteDoubleMatrixModifier VelocityBoundsUpdater {
     115      get { return VelocityBoundsUpdaterParameter.ActualValue; }
     116    }
     117    private DoubleMatrix VelocityBounds {
     118      get { return VelocityBoundsParameter.ActualValue; }
     119      set { VelocityBoundsParameter.ActualValue = value; }
    104120    }
    105121    #endregion
     
    116132      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "Particle's quality"));
    117133      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("PersonalBestQuality", "Particle's personal best quality"));
    118       Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("NeighborsBestQuality", "Global best particle quality"));
     134      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("NeighborBestQuality", "Global best particle quality"));
    119135      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "Particle's position"));
    120136      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"));
     137      Parameters.Add(new ScopeTreeLookupParameter<RealVector>("NeighborBest", "Neighborhood (or global in case of totally connected neighborhood) best particle position"));
    122138      Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle."));
    123       Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
     139      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
     140      Parameters.Add(new ValueLookupParameter<IDiscreteDoubleMatrixModifier>("VelocityBoundsUpdater", "Modifies the velocity bounds in the course of optimization."));
     141      Parameters.Add(new LookupParameter<DoubleMatrix>("VelocityBounds", "Maximum velocity for each dimension."));
    124142    }
    125143
     
    132150    public override IOperation Apply() {
    133151      InitializeBestPoint();
     152      UpdateNeighbors();
    134153      UpdateSwarm();
    135       UpdateNeighbors();
    136       UpdateVelocityBounds();
    137       return base.Apply();
     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      }
    138162    }
    139163
     
    148172    private void UpdateNeighbors() {
    149173      if (Neighbors != null & Neighbors.Length > 0) {
    150         if (this.NeighborsBest == null || NeighborsBest.Length != Neighbors.Length)
    151           NeighborsBest = new ItemArray<RealVector>(Neighbors.Length);
     174        if (this.NeighborBest == null || NeighborBest.Length != Neighbors.Length)
     175          NeighborBest = new ItemArray<RealVector>(Neighbors.Length);
    152176        for (int n = 0; n < Neighbors.Length; n++) {
    153177          var pairs = Quality.Zip(RealVector, (q, p) => new { Quality = q, Point = p })
    154178            .Where((p, i) => i == n || Neighbors[n].Contains(i));
    155           NeighborsBest[n] = Maximization ?
     179          NeighborBest[n] = Maximization ?
    156180          pairs.OrderByDescending(p => p.Quality.Value).First().Point :
    157181          pairs.OrderBy(p => p.Quality.Value).First().Point;
    158182        }
    159         NeighborsBestParameter.ActualValue = NeighborsBest;
    160       }
    161     }
    162 
    163     private void UpdateVelocityBounds() {
    164       // ToDo: Add code
     183        NeighborBestParameter.ActualValue = NeighborBest;
     184      }
    165185    }
    166186
    167187    private void UpdateSwarm() {
    168188      if (PersonalBestQuality.Length == 0) {
    169         ItemArray<DoubleValue> personalBestQualities = new ItemArray<DoubleValue>(RealVector.Length);
    170         for (int i = 0; i < RealVector.Length; i++) {
    171           if (Maximization) {
    172             personalBestQualities[i] = new DoubleValue(double.MinValue);
    173           } else {
    174             personalBestQualities[i] = new DoubleValue(double.MaxValue);
    175           }
    176         }
    177         PersonalBestQualityParameter.ActualValue = personalBestQualities;
    178       }
    179       if (NeighborsBestQuality.Length == 0) {
    180         if (NeighborsParameter != null && Neighbors.Length > 0) {
    181           ItemArray<DoubleValue> neighborsBestQualities = new ItemArray<DoubleValue>(RealVector.Length);
    182           for (int i = 0; i < RealVector.Length; i++) {
    183             if (Maximization) {
    184               neighborsBestQualities[i] = new DoubleValue(double.MinValue);
    185             } else {
    186               neighborsBestQualities[i] = new DoubleValue(double.MaxValue);
    187             }
    188           }
    189           NeighborsBestQualityParameter.ActualValue = neighborsBestQualities;
    190         }
    191       }
    192       ItemArray<RealVector> neighborsBest = new ItemArray<RealVector>(RealVector.Length);
     189        PersonalBestQuality = (ItemArray<DoubleValue>)Quality.Clone();
     190        if (VelocityBounds == null)
     191          VelocityBounds = new DoubleMatrix(new double[,] { { -1, 1 } });
     192      }
    193193      for (int i = 0; i < RealVector.Length; i++) {
    194194        if (Maximization && Quality[i].Value > PersonalBestQuality[i].Value ||
     
    196196          PersonalBestQuality[i].Value = Quality[i].Value;
    197197          PersonalBest[i] = RealVector[i];
    198           if (Maximization && PersonalBestQuality[i].Value > NeighborsBestQuality[i].Value ||
    199              !Maximization && PersonalBestQuality[i].Value < NeighborsBestQuality[i].Value) {
    200             NeighborsBestQuality[i].Value = PersonalBestQuality[i].Value;
    201             neighborsBest[i] = PersonalBest[i];
     198        }
     199      }
     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];
    202212          }
    203213        }
    204       }
    205       if (NeighborsParameter != null && Neighbors.Length > 0) {
    206         NeighborsBestParameter.ActualValue = neighborsBest;
    207       }
    208     }
    209 
     214        NeighborBestQuality = neighborBestQuality;
     215        NeighborBest = neighborBest;
     216      }
     217    }
    210218  }
    211219}
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorTotallyConnectedParticleUpdater.cs

    r5560 r5568  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
     24using HeuristicLab.Optimization;
    2425using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    25 using HeuristicLab.Optimization;
    2626
    2727namespace HeuristicLab.Encodings.RealVectorEncoding {
     
    5151      double omega = Inertia.Value;
    5252      double phi_p = PersonalBestAttraction.Value;
    53       double phi_g = NeighborsBestAttraction.Value;
     53      double phi_g = NeighborBestAttraction.Value;
    5454      for (int i = 0; i < velocity.Length; i++) {
    5555        velocity[i] =
    5656          Velocity[i] * omega +
    5757          (PersonalBest[i] - RealVector[i]) * phi_p * r_p +
    58           (NeighborsBest[i] - RealVector[i]) * phi_g * r_g;
     58          (BestPoint[i] - RealVector[i]) * phi_g * r_g;
    5959      }
    6060      BoundsChecker.Apply(velocity, VelocityBounds);
  • trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/IDiscreteDoubleMatrixModifier.cs

    r5560 r5568  
    2525
    2626namespace HeuristicLab.Optimization {
    27   public interface IDiscreteDoubleMatrixModifier : IItem {
     27  public interface IDiscreteDoubleMatrixModifier : IOperator {
    2828    ILookupParameter<DoubleMatrix> ValueParameter { get; }
    2929    ILookupParameter<DoubleValue> ScaleParameter { get; }
  • trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/IParticleUpdater.cs

    r5560 r5568  
    2929  public interface IParticleUpdater : IOperator {
    3030    ILookupParameter<DoubleValue> InertiaParameter { get; }
    31     ILookupParameter<DoubleValue> NeighborsBestAttractionParameter { get; }
     31    ILookupParameter<DoubleValue> NeighborBestAttractionParameter { get; }
    3232    ILookupParameter<DoubleValue> PersonalBestAttractionParameter { get; }
    3333    ILookupParameter<IRandom> RandomParameter { get; }
  • trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/ISwarmUpdater.cs

    r5561 r5568  
    2626  public interface ISwarmUpdater : IOperator {
    2727    IScopeTreeLookupParameter<DoubleValue> QualityParameter { get; }
    28     IScopeTreeLookupParameter<DoubleValue> NeighborsBestQualityParameter { get; }
     28    IScopeTreeLookupParameter<DoubleValue> NeighborBestQualityParameter { get; }
    2929    IScopeTreeLookupParameter<DoubleValue> PersonalBestQualityParameter { get; }
    30     IValueLookupParameter<BoolValue> MaximizationParameter { get; }
     30    ILookupParameter<BoolValue> MaximizationParameter { get; }
    3131  }
    3232}
Note: See TracChangeset for help on using the changeset viewer.