Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5033 for branches/PSO


Ignore:
Timestamp:
12/06/10 14:45:27 (14 years ago)
Author:
epitzer
Message:

Simple but complete PSO implementation (#852)

Location:
branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3

    • Property svn:ignore
      •  

        old new  
        33obj
        44*.user
         5*.suo
  • branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/HeuristicLab.Algorithms.ParticleSwarmOptimization-3.3.csproj

    r4397 r5033  
    105105  </ItemGroup>
    106106  <ItemGroup>
     107    <Compile Include="BestPointInitializer.cs" />
    107108    <Compile Include="HeuristicLabAlgorithmsParticleSwarmOptimizationPlugin.cs" />
    108109    <Compile Include="ParticleSwarmOptimization.cs" />
    109     <Compile Include="ParticleSwarmOptimizationMainLoop.cs" />
    110     <Compile Include="PermutationToRealVectorEncoder.cs" />
    111110    <Compile Include="Properties\AssemblyInfo.cs" />
    112111    <Compile Include="ParticleUpdater.cs" />
     
    138137      <SpecificVersion>False</SpecificVersion>
    139138      <HintPath>C:\Program Files\HeuristicLab 3.3\HeuristicLab.Data-3.3.dll</HintPath>
    140     </Reference>
    141     <Reference Include="HeuristicLab.Encodings.PermutationEncoding-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
    142       <SpecificVersion>False</SpecificVersion>
    143       <HintPath>C:\Program Files\HeuristicLab 3.3\HeuristicLab.Encodings.PermutationEncoding-3.3.dll</HintPath>
    144139    </Reference>
    145140    <Reference Include="HeuristicLab.Encodings.RealVectorEncoding-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=x86">
     
    208203
    209204call PreBuildEvent.cmd
    210 SubWCRev "%25ProjectDir%25\" "%25ProjectDir%25\HeuristicLabAlgorithmsParticleSwarmOptimizationPlugin.cs.frame" "%25ProjectDir%25\HeuristicLabAlgorithmsParticleSwarmOptimizationPlugin.cs"</PreBuildEvent>
     205SubWCRev "%25ProjectDir%25\" "%25ProjectDir%25\HeuristicLabAlgorithmsParticleSwarmOptimizationPlugin.cs.frame" "%25ProjectDir%25\HeuristicLabAlgorithmsParticleSwarmOptimizationPlugin.cs"
     206SubWCRev "%25ProjectDir%25\" "%25ProjectDir%25\Properties\AssemblyInfo.frame" "%25ProjectDir%25\Properties\AssemblyInfo.cs"</PreBuildEvent>
     207  </PropertyGroup>
     208  <PropertyGroup>
     209    <PostBuildEvent>copy $(TargetFileName) "C:\Program Files\HeuristicLab 3.3"</PostBuildEvent>
    211210  </PropertyGroup>
    212211</Project>
  • branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/HeuristicLabAlgorithmsParticleSwarmOptimizationPlugin.cs.frame

    r3384 r5033  
    4040  [PluginDependency("HeuristicLab.Persistence", "3.3")]
    4141  [PluginDependency("HeuristicLab.Random", "3.3")]
    42   [PluginDependency("HeuristicLab.Selection", "3.3")]
    4342  public class HeuristicLabAlgorithmsParticleSwarmOptimizationPlugin : PluginBase {
    4443  }
  • branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimization.cs

    r4068 r5033  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    2524using HeuristicLab.Analysis;
     
    3635
    3736namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
     37
    3838  [Item("Particle Swarm Optimization", "A particle swarm optimization algorithm.")]
    3939  [Creatable("Algorithms")]
    4040  [StorableClass]
    41   public sealed class ParticleSwarmOptimization : EngineAlgorithm {
     41  public class ParticleSwarmOptimization : EngineAlgorithm {
     42
    4243    #region Problem Properties
    4344    public override Type ProblemType {
     
    4849      set { base.Problem = value; }
    4950    }
    50     public IRealVectorEncoder Encoder {
    51       get { return EncoderParameter.Value; }
    52       set { EncoderParameter.Value = value; }
    53     }
    5451    public MultiAnalyzer Analyzer {
    5552      get { return AnalyzerParameter.Value; }
     
    5956
    6057    #region Parameter Properties
    61     private ValueParameter<IntValue> SeedParameter {
     58    public ValueParameter<IntValue> SeedParameter {
    6259      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
    6360    }
    64     private ValueParameter<BoolValue> SetSeedRandomlyParameter {
     61    public ValueParameter<BoolValue> SetSeedRandomlyParameter {
    6562      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
    6663    }
    67     private ValueParameter<IntValue> SwarmSizeParameter {
     64    public ValueParameter<IntValue> SwarmSizeParameter {
    6865      get { return (ValueParameter<IntValue>)Parameters["SwarmSize"]; }
    6966    }
    70     private ValueParameter<IntValue> MaxIterationsParameter {
     67    public ValueParameter<IntValue> MaxIterationsParameter {
    7168      get { return (ValueParameter<IntValue>)Parameters["MaxIterations"]; }
    7269    }
    73     private OptionalConstrainedValueParameter<IRealVectorEncoder> EncoderParameter {
    74       get { return (OptionalConstrainedValueParameter<IRealVectorEncoder>)Parameters["Encoder"]; }
    75     }
    76     private ValueParameter<MultiAnalyzer> AnalyzerParameter {
     70    public ValueParameter<DoubleValue> OmegaParameter {
     71      get { return (ValueParameter<DoubleValue>)Parameters["Omega"]; }
     72    }
     73    public ValueParameter<DoubleValue> Phi_PParameter {
     74      get { return (ValueParameter<DoubleValue>)Parameters["Phi_P"]; }
     75    }
     76    public ValueParameter<DoubleValue> Phi_GParameter {
     77      get { return (ValueParameter<DoubleValue>)Parameters["Phi_G"]; }
     78    }
     79    public ValueParameter<MultiAnalyzer> AnalyzerParameter {
    7780      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
     81    }
     82    public ValueLookupParameter<DoubleMatrix> VelocityBoundsParameter {
     83      get { return (ValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
    7884    }
    7985    #endregion
     
    8187    #region Properties
    8288    [Storable]
    83     private ParticleSwarmOptimizationMainLoop mainLoop; // Check this !
    84     private ParticleSwarmOptimizationMainLoop MainLoop {
    85       get { return mainLoop; }
    86     }
    87     [Storable]
    88     private Assigner bestLocalQualityInitalizer; // Check this !
    89     private Assigner BestLocalQualityInitalizer {
    90       get { return bestLocalQualityInitalizer; }
    91     }
    92     [Storable]
    9389    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
    9490    #endregion
     91
     92    [StorableConstructor]
     93    protected ParticleSwarmOptimization(bool deserializing) : base(deserializing) { }
     94    protected ParticleSwarmOptimization(ParticleSwarmOptimization original, Cloner cloner)
     95      : base(original, cloner) {
     96      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
     97    }
    9598
    9699    public ParticleSwarmOptimization()
     
    100103      Parameters.Add(new ValueParameter<IntValue>("SwarmSize", "Size of the particle swarm.", new IntValue(10)));
    101104      Parameters.Add(new ValueParameter<IntValue>("MaxIterations", "Maximal number of iterations.", new IntValue(1000)));
    102       Parameters.Add(new ConstrainedValueParameter<IRealVectorEncoder>("Encoder", "The operator used to encode solutions as position vector."));
    103105      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
     106      Parameters.Add(new ValueParameter<DoubleValue>("Omega", "Weight for particle's velocity vector.", new DoubleValue(-0.2)));
     107      Parameters.Add(new ValueParameter<DoubleValue>("Phi_P", "Weight for particle's personal best position.", new DoubleValue(-0.01)));
     108      Parameters.Add(new ValueParameter<DoubleValue>("Phi_G", "Weight for global best position.", new DoubleValue(3.7)));
     109      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("VelocityBounds", "Maximum Velocity in every dimension", new DoubleMatrix(new double[,] { { -1, 1 } })));
     110
    104111      RandomCreator randomCreator = new RandomCreator();
     112      VariableCreator variableCreator = new VariableCreator();
    105113      SolutionsCreator solutionsCreator = new SolutionsCreator();
    106       UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
    107       UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
    108       VariableCreator variableCreator = new VariableCreator();
    109       VariableCreator localVariableCreator = new VariableCreator();
    110       Placeholder encoder = new Placeholder();
    111       UniformRandomRealVectorCreator velocityVectorCreator = new UniformRandomRealVectorCreator();
    112       bestLocalQualityInitalizer = new Assigner();
    113       Assigner bestLocalPositionInitalizer = new Assigner();
    114       Assigner bestGlobalPositionInitalizer = new Assigner();
    115       mainLoop = new ParticleSwarmOptimizationMainLoop();
    116       BestAverageWorstQualityCalculator bawCalculator = new BestAverageWorstQualityCalculator();
    117       Comparator comparator = new Comparator();
    118       ConditionalBranch branch = new ConditionalBranch();
    119 
    120       variableCreator.CollectedValues.Add(new ValueParameter<RealVector>("CurrentBestPosition", new RealVector()));
    121       variableCreator.CollectedValues.Add(new ValueParameter<DoubleMatrix>("ZeroBounds", new DoubleMatrix(new double[,] { { 0, 0 } })));
    122       variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Length", new IntValue(2)));
    123 
    124       localVariableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(0)));
    125       localVariableCreator.CollectedValues.Add(new ValueParameter<RealVector>("BestPosition", new RealVector()));
    126 
    127       randomCreator.RandomParameter.ActualName = "Random";
    128       randomCreator.SeedParameter.ActualName = SeedParameter.Name;
     114      CombinedOperator particleCreator = new CombinedOperator();
     115      Placeholder evaluatorPlaceholder = new Placeholder();
     116      Assigner bestPersonalQualityAssigner = new Assigner();
     117      BestPointInitializer bestPositionInitializer = new BestPointInitializer();
     118      Placeholder analyzerPlaceholder = new Placeholder();
     119      UniformSubScopesProcessor uniformSubScopeProcessor = new UniformSubScopesProcessor();
     120      ParticleUpdater particleUpdater = new ParticleUpdater();
     121      Placeholder evaluatorPlaceholder2 = new Placeholder();
     122      SwarmUpdater swarmUpdater = new SwarmUpdater();
     123      Placeholder analyzerPlaceholder2 = new Placeholder();
     124      IntCounter currentIterationCounter = new IntCounter();
     125      Comparator currentIterationComparator = new Comparator();
     126      ConditionalBranch conditionalBranch = new ConditionalBranch();
     127
     128      OperatorGraph.InitialOperator = randomCreator;
     129
     130      randomCreator.SetSeedRandomlyParameter.Value = null;
    129131      randomCreator.SeedParameter.Value = null;
    130       randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
    131       randomCreator.SetSeedRandomlyParameter.Value = null;
    132 
    133       solutionsCreator.NumberOfSolutionsParameter.ActualName = SwarmSizeParameter.Name;
    134 
    135       encoder.OperatorParameter.ActualName = "Encoder";
    136 
    137       velocityVectorCreator.BoundsParameter.ActualName = "ZeroBounds";
    138       velocityVectorCreator.RealVectorParameter.ActualName = "Velocity";
    139 
    140       bestLocalQualityInitalizer.LeftSideParameter.ActualName = "BestQuality"; // cloned value
    141       bestLocalQualityInitalizer.RightSideParameter.ActualName = "Quality"; // FIXME!!! Should be mapped
    142 
    143       bestLocalPositionInitalizer.LeftSideParameter.ActualName = "BestPosition";
    144       bestLocalPositionInitalizer.RightSideParameter.ActualName = "Position"; // FixMe
    145 
    146       bestGlobalPositionInitalizer.LeftSideParameter.ActualName = "CurrentBestPosition";
    147       bestGlobalPositionInitalizer.RightSideParameter.ActualName = "BestPosition";
    148 
    149       bawCalculator.AverageQualityParameter.ActualName = "CurrentAverageBestQuality";
    150       bawCalculator.BestQualityParameter.ActualName = "CurrentBestBestQuality";
    151       bawCalculator.MaximizationParameter.ActualName = "Maximization"; // FIXME
    152       bawCalculator.QualityParameter.ActualName = "Quality";
    153       bawCalculator.WorstQualityParameter.ActualName = "CurrentWorstBestQuality";
    154 
    155       comparator.Comparison = new Comparison(ComparisonType.Equal);
    156       comparator.LeftSideParameter.ActualName = "Quality";
    157       comparator.ResultParameter.ActualName = "NewGlobalBest";
    158       comparator.RightSideParameter.ActualName = "CurrentBestBestQuality";
    159 
    160       branch.ConditionParameter.ActualName = "NewGlobalBest";
    161       branch.TrueBranch = bestGlobalPositionInitalizer; // copy position vector
    162 
    163       mainLoop.MaximumGenerationsParameter.ActualName = MaxIterationsParameter.Name;
    164       mainLoop.ResultsParameter.ActualName = "Results";
    165       mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
    166 
    167       OperatorGraph.InitialOperator = randomCreator;
    168       randomCreator.Successor = solutionsCreator;
    169       solutionsCreator.Successor = variableCreator;
    170       variableCreator.Successor = uniformSubScopesProcessor;
    171       uniformSubScopesProcessor.Operator = encoder;
    172       encoder.Successor = velocityVectorCreator;
    173       velocityVectorCreator.Successor = localVariableCreator;
    174       localVariableCreator.Successor = bestLocalQualityInitalizer;
    175       bestLocalQualityInitalizer.Successor = bestLocalPositionInitalizer;
    176       uniformSubScopesProcessor.Successor = bawCalculator; // mainLoop;
    177       bawCalculator.Successor = uniformSubScopesProcessor2;
    178       uniformSubScopesProcessor2.Operator = comparator;
    179       comparator.Successor = branch;
    180       uniformSubScopesProcessor2.Successor = mainLoop;
     132      randomCreator.Successor = variableCreator;
     133
     134      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("CurrentIteration", new IntValue(0)));
     135      variableCreator.Successor = solutionsCreator;
     136
     137      solutionsCreator.NumberOfSolutionsParameter.ActualName = "SwarmSize";
     138      solutionsCreator.EvaluatorParameter.Value = evaluatorPlaceholder;
     139      solutionsCreator.SolutionCreatorParameter.Value = particleCreator;
     140      solutionsCreator.Successor = bestPositionInitializer;
     141
     142      InitializeParticleCreator(particleCreator);
     143
     144      evaluatorPlaceholder.Name = "(Evaluator)";
     145      evaluatorPlaceholder.OperatorParameter.ActualName = "Evaluator";
     146      evaluatorPlaceholder.Successor = bestPersonalQualityAssigner;
     147
     148      bestPersonalQualityAssigner.LeftSideParameter.ActualName = "PersonalBestQuality";
     149      bestPersonalQualityAssigner.RightSideParameter.ActualName = "Quality";
     150
     151      bestPositionInitializer.Successor = analyzerPlaceholder;
     152
     153      analyzerPlaceholder.Name = "(Analyzer)";
     154      analyzerPlaceholder.OperatorParameter.ActualName = "Analyzer";
     155      analyzerPlaceholder.Successor = uniformSubScopeProcessor;
     156
     157      uniformSubScopeProcessor.Operator = particleUpdater;
     158      uniformSubScopeProcessor.Successor = analyzerPlaceholder2;
     159
     160      particleUpdater.Successor = evaluatorPlaceholder2;
     161
     162      evaluatorPlaceholder2.Name = "(Evaluator)";
     163      evaluatorPlaceholder2.OperatorParameter.ActualName = "Evaluator";
     164      evaluatorPlaceholder2.Successor = swarmUpdater;
     165
     166      analyzerPlaceholder2.Name = "(Analyzer)";
     167      analyzerPlaceholder2.OperatorParameter.ActualName = "Analyzer";
     168      analyzerPlaceholder2.Successor = currentIterationCounter;
     169
     170      currentIterationCounter.Name = "CurrentIteration++";
     171      currentIterationCounter.ValueParameter.ActualName = "CurrentIteration";
     172      currentIterationCounter.Successor = currentIterationComparator;
     173
     174      currentIterationComparator.LeftSideParameter.ActualName = "CurrentIteration";
     175      currentIterationComparator.Comparison = new Comparison(ComparisonType.Less);
     176      currentIterationComparator.RightSideParameter.ActualName = "MaxIterations";
     177      currentIterationComparator.ResultParameter.ActualName = "ContinueIteration";
     178      currentIterationComparator.Successor = conditionalBranch;
     179
     180      conditionalBranch.Name = "ContinueIteration?";
     181      conditionalBranch.ConditionParameter.ActualName = "ContinueIteration";
     182      conditionalBranch.TrueBranch = uniformSubScopeProcessor;
     183
    181184      InitializeAnalyzers();
    182185      UpdateAnalyzers();
    183       Initialize();
    184     }
    185 
    186     [StorableHook(HookType.AfterDeserialization)]
    187     private void Initialize() {
    188       EncoderParameter.ValueChanged += new EventHandler(EncoderParameter_ValueChanged);
    189       if (Problem != null) {
    190         bestLocalQualityInitalizer.RightSideParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
    191       }
    192     }
    193 
    194     [StorableConstructor]
    195     private ParticleSwarmOptimization(bool deserializing) : base(deserializing) { }
     186    }
     187
     188    private static void InitializeParticleCreator(CombinedOperator particleCreator) {
     189      Placeholder positionCreator = new Placeholder();
     190      Assigner personalBestPositionAssigner = new Assigner();
     191      UniformRandomRealVectorCreator velocityCreator = new UniformRandomRealVectorCreator();
     192
     193      particleCreator.Name = "Particle Creator";
     194      particleCreator.OperatorGraph.InitialOperator = positionCreator;
     195
     196      positionCreator.Name = "(SolutionCreator)";
     197      positionCreator.OperatorParameter.ActualName = "SolutionCreator";
     198      positionCreator.Successor = personalBestPositionAssigner;
     199
     200      personalBestPositionAssigner.LeftSideParameter.ActualName = "PersonalBestPoint";
     201      personalBestPositionAssigner.RightSideParameter.ActualName = "Point";
     202      personalBestPositionAssigner.Successor = velocityCreator;
     203
     204      velocityCreator.LengthParameter.ActualName = "ProblemSize";
     205      velocityCreator.BoundsParameter.ActualName = "VelocityBounds";
     206      velocityCreator.RealVectorParameter.ActualName = "Velocity";
     207    }
    196208
    197209    public override IDeepCloneable Clone(Cloner cloner) {
    198       ParticleSwarmOptimization clone = (ParticleSwarmOptimization)base.Clone(cloner);
    199       clone.Initialize();
    200       return clone;
     210      return new ParticleSwarmOptimization(this, cloner);
    201211    }
    202212
     
    207217    #region Events
    208218    protected override void OnProblemChanged() {
    209       UpdateEncoders();
    210219      UpdateAnalyzers();
    211220      ParameterizeAnalyzers();
    212       bestLocalQualityInitalizer.RightSideParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
    213221      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    214       MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
    215222      base.OnProblemChanged();
    216223    }
    217224
    218     private void EncoderParameter_ValueChanged(object sender, EventArgs e) {
    219       //MainLoop.EncoderParameter.ActualValue = (IRealVectorEncoder) EncoderParameter.ActualValue;
    220       //((UniformSubScopesProcessor)((VariableCreator)((SolutionsCreator)((RandomCreator)OperatorGraph.InitialOperator).Successor).Successor).Successor).Operator = EncoderParameter.Value;
    221       //((SingleSuccessorOperator)EncoderParameter.Value).Successor = ((SingleSuccessorOperator)old).Successor;
    222     }
    223225    #endregion
    224226
    225227    #region Helpers
    226228    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
    227       //
    228       //
    229     }
    230 
    231     private void UpdateEncoders() {
    232       IRealVectorEncoder oldEncoder = EncoderParameter.Value;
    233       EncoderParameter.ValidValues.Clear();
    234       List<IRealVectorEncoder> encoders = Problem.Operators.OfType<IRealVectorEncoder>().OrderBy(x => x.Name).ToList<IRealVectorEncoder>();
    235       if (encoders.Count > 0) {  // ToDo: Add wiring; else: use Position Vector directly --> name matching
    236         foreach (IRealVectorEncoder encoder in Problem.Operators.OfType<IRealVectorEncoder>().OrderBy(x => x.Name)) {
    237           EncoderParameter.ValidValues.Add(encoder);
    238           ((ILookupParameter)encoder.RealVectorParameter).ActualName = "Position";
    239         }
    240         if (oldEncoder != null) {
    241           IRealVectorEncoder encoder = EncoderParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldEncoder.GetType());
    242           if (encoder != null) EncoderParameter.Value = encoder;
    243         }
    244       }
    245229    }
    246230
     
    251235
    252236    private void ParameterizeAnalyzers() {
    253       qualityAnalyzer.ResultsParameter.ActualName = "Results";
    254237      if (Problem != null) {
    255238        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
  • branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleUpdater.cs

    r4068 r5033  
    2525using HeuristicLab.Operators;
    2626using HeuristicLab.Parameters;
     27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using HeuristicLab.Common;
    2729
    2830namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
    29   public class ParticleUpdater : SingleSuccessorOperator { // ParticleUpdater
     31  public class ParticleUpdater : SingleSuccessorOperator {
     32
    3033    #region Parameter properties
    31 
    32     public ILookupParameter<RealVector> VelocityParameter {
    33       get { return (ILookupParameter<RealVector>)Parameters["Velocity"]; }
     34    public LookupParameter<IRandom> RandomParameter {
     35      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
    3436    }
    35 
    36     public IParameter RandomParameter {
    37       get { return (IParameter)Parameters["Random"]; }
     37    public LookupParameter<RealVector> PointParameter {
     38      get { return (LookupParameter<RealVector>)Parameters["Point"]; }
    3839    }
    39 
    40     public ILookupParameter<RealVector> CurrentPositionParameter {
    41       get { return (ILookupParameter<RealVector>)Parameters["CurrentPosition"]; }
     40    public LookupParameter<RealVector> VelocityParameter {
     41      get { return (LookupParameter<RealVector>)Parameters["Velocity"]; }
    4242    }
    43 
    44     public ILookupParameter<RealVector> BestLocalParameter {
    45       get { return (ILookupParameter<RealVector>)Parameters["BestLocal"]; }
     43    public LookupParameter<RealVector> PersonalBestPointParameter {
     44      get { return (LookupParameter<RealVector>)Parameters["PersonalBestPoint"]; }
    4645    }
    47 
    48     public ILookupParameter<RealVector> BestGlobalParameter {
    49       get { return (ILookupParameter<RealVector>)Parameters["BestGlobal"]; }
     46    public LookupParameter<RealVector> BestPointParameter {
     47      get { return (LookupParameter<RealVector>)Parameters["BestPoint"]; }
    5048    }
    51 
    52     public ILookupParameter<DoubleMatrix> BoundsParameter {
    53       get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
     49    public LookupParameter<DoubleMatrix> BoundsParameter {
     50      get { return (LookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
     51    }
     52    public LookupParameter<DoubleMatrix> VelocityBoundsParameter {
     53      get { return (LookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
     54    }
     55    public LookupParameter<DoubleValue> OmegaParameter {
     56      get { return (LookupParameter<DoubleValue>)Parameters["Omega"]; }
     57    }
     58    public LookupParameter<DoubleValue> Phi_PParameter {
     59      get { return (LookupParameter<DoubleValue>)Parameters["Phi_P"]; }
     60    }
     61    public LookupParameter<DoubleValue> Phi_GParameter {
     62      get { return (LookupParameter<DoubleValue>)Parameters["Phi_G"]; }
    5463    }
    5564    #endregion
    5665
     66    #region Parameter Values
     67    public IRandom Random {
     68      get { return RandomParameter.ActualValue; }
     69    }
     70    public RealVector Point {
     71      get { return PointParameter.ActualValue;  }
     72      set { PointParameter.ActualValue = value;  }
     73    }
     74    public RealVector Velocity {
     75      get { return VelocityParameter.ActualValue; }
     76      set { VelocityParameter.ActualValue = value; }
     77    }
     78    public RealVector PersonalBestPoint {
     79      get { return PersonalBestPointParameter.ActualValue; }
     80    }
     81    public RealVector BestPoint {
     82      get { return BestPointParameter.ActualValue; }
     83    }
     84    public DoubleMatrix Bounds {
     85      get { return BoundsParameter.ActualValue;  }
     86    }
     87    public DoubleMatrix VelocityBounds {
     88      get { return VelocityBoundsParameter.ActualValue; }
     89    }
     90    public double Omega {
     91      get { return OmegaParameter.ActualValue.Value; }
     92    }
     93    public double Phi_P {
     94      get { return Phi_PParameter.ActualValue.Value;  }
     95    }
     96    public double Phi_G {
     97      get { return Phi_GParameter.ActualValue.Value; }
     98    }
     99    #endregion
     100
     101    #region Construction & Cloning
     102
     103    [StorableConstructor]
     104    protected ParticleUpdater(bool deserializing) : base(deserializing) { }
     105    protected ParticleUpdater(ParticleUpdater original, Cloner cloner)
     106      : base(original, cloner) {
     107    }
     108
    57109    public ParticleUpdater()
    58110      : base() {
    59       Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator (to generate alpha an beta)."));
    60       Parameters.Add(new LookupParameter<RealVector>("Velocity", "The velocity vector to update."));
    61       Parameters.Add(new LookupParameter<RealVector>("CurrentPosition", "Current position"));
    62       Parameters.Add(new LookupParameter<RealVector>("BestLocal", "Best local position"));
    63       Parameters.Add(new LookupParameter<RealVector>("BestGlobal", "Best global position"));
    64       Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector."));
     111      Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator."));
     112      Parameters.Add(new LookupParameter<RealVector>("Point", "Particle's current position"));
     113      Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity."));
     114      Parameters.Add(new LookupParameter<RealVector>("PersonalBestPoint", "Particle's personal best position"));
     115      Parameters.Add(new LookupParameter<RealVector>("BestPoint", "Global best position"));
     116      Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector for the current problem."));
     117      Parameters.Add(new LookupParameter<DoubleMatrix>("VelocityBounds", "Upper and lower bounds for the particle's velocity vector."));
     118      Parameters.Add(new LookupParameter<DoubleValue>("Omega", "The weight for the particle's velocity vector."));
     119      Parameters.Add(new LookupParameter<DoubleValue>("Phi_P", "The weight for the particle's personal best position."));
     120      Parameters.Add(new LookupParameter<DoubleValue>("Phi_G", "The weight for the global best position."));
    65121    }
    66122
     123    public override IDeepCloneable Clone(Cloner cloner) {
     124      return new ParticleUpdater(this, cloner);
     125    }
     126
     127    #endregion
     128
    67129    public override IOperation Apply() {
    68       double alpha = ((IRandom)RandomParameter.ActualValue).NextDouble();
    69       double beta = ((IRandom)RandomParameter.ActualValue).NextDouble();
    70       RealVector velocity = (RealVector)VelocityParameter.ActualValue;
     130      RealVector velocity = new RealVector(Velocity.Length);
     131      RealVector position = new RealVector(Point.Length);
     132      double r_p = Random.NextDouble();
     133      double r_g = Random.NextDouble();
    71134      for (int i = 0; i < velocity.Length; i++) {
    72         velocity[i] = velocity[i] + alpha * (BestLocalParameter.ActualValue[i] - CurrentPositionParameter.ActualValue[i]) + beta * (BestGlobalParameter.ActualValue[i] - CurrentPositionParameter.ActualValue[i]);
     135        velocity[i] =
     136          Velocity[i] * Omega +
     137          (PersonalBestPoint[i] - Point[i]) * Phi_P * r_p +
     138          (BestPoint[i] - Point[i]) * Phi_G * r_g;
    73139      }
    74       VelocityParameter.ActualValue = velocity;
    75       for (int i = 0; i < CurrentPositionParameter.ActualValue.Length; i++) {
    76         CurrentPositionParameter.ActualValue[i] = CurrentPositionParameter.ActualValue[i] + VelocityParameter.ActualValue[i];
    77         if (CurrentPositionParameter.ActualValue[i] < BoundsParameter.ActualValue[0, 0]) {
    78           CurrentPositionParameter.ActualValue[i] = BoundsParameter.ActualValue[0, 0];
    79         } else if (CurrentPositionParameter.ActualValue[i] > BoundsParameter.ActualValue[0, 1]) {
    80           CurrentPositionParameter.ActualValue[i] = BoundsParameter.ActualValue[0, 1];
    81         }
     140      BoundsChecker.Apply(velocity, VelocityBounds);
     141      for (int i = 0; i < velocity.Length; i++) {
     142        position[i] = Point[i] + velocity[i];
    82143      }
     144      BoundsChecker.Apply(position, Bounds);
     145      Point = position;
     146      Velocity = velocity;
     147
    83148      return base.Apply();
    84149    }
  • branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/SwarmUpdater.cs

    r4068 r5033  
    2525using HeuristicLab.Operators;
    2626using HeuristicLab.Parameters;
     27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using HeuristicLab.Common;
    2729
    2830namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
    2931  public class SwarmUpdater : SingleSuccessorOperator {
     32
    3033    #region Parameter properties
    31 
    32     public ILookupParameter<DoubleValue> CurrentQualityParameter {
    33       get { return (ILookupParameter<DoubleValue>)Parameters["CurrentQuality"]; }
     34    public LookupParameter<DoubleValue> QualityParameter {
     35      get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
    3436    }
    35 
    36     public ILookupParameter<DoubleValue> LocalBestQualityParameter {
    37       get { return (ILookupParameter<DoubleValue>)Parameters["LocalBestQuality"]; }
     37    public LookupParameter<DoubleValue> PersonalBestQualityParameter {
     38      get { return (LookupParameter<DoubleValue>)Parameters["PersonalBestQuality"]; }
    3839    }
    39 
    40     public ILookupParameter<DoubleValue> GlobalBestQualityParameter {
    41       get { return (ILookupParameter<DoubleValue>)Parameters["GlobalBestQuality"]; }
     40    public LookupParameter<DoubleValue> BestQualityParameter {
     41      get { return (LookupParameter<DoubleValue>)Parameters["BestQuality"]; }
    4242    }
    43 
    44     public ILookupParameter<RealVector> CurrentPositionParameter {
    45       get { return (ILookupParameter<RealVector>)Parameters["CurrentPosition"]; }
     43    public LookupParameter<RealVector> PointParameter {
     44      get { return (LookupParameter<RealVector>)Parameters["Point"]; }
    4645    }
    47 
    48     public ILookupParameter<RealVector> BestLocalParameter {
    49       get { return (ILookupParameter<RealVector>)Parameters["BestLocal"]; }
     46    public LookupParameter<RealVector> PersonalBestPointParameter {
     47      get { return (LookupParameter<RealVector>)Parameters["PersonalBestPoint"]; }
    5048    }
    51 
    52     public ILookupParameter<RealVector> BestGlobalParameter {
    53       get { return (ILookupParameter<RealVector>)Parameters["BestGlobal"]; }
     49    public LookupParameter<RealVector> BestPointParameter {
     50      get { return (LookupParameter<RealVector>)Parameters["BestPoint"]; }
    5451    }
    55 
    5652    public ValueLookupParameter<BoolValue> MaximizationParameter {
    5753      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
     
    5955    #endregion
    6056
     57    #region Parameter values
     58    public double Quality {
     59      get { return QualityParameter.ActualValue.Value; }
     60    }
     61    public double PersonalBestQuality {
     62      get { return PersonalBestQualityParameter.ActualValue.Value;  }
     63      set { PersonalBestQualityParameter.ActualValue = new DoubleValue(value);  }
     64    }
     65    public double BestQuality {
     66      get { return BestQualityParameter.ActualValue.Value; }
     67      set { BestQualityParameter.ActualValue = new DoubleValue(value); }
     68    }
     69    public RealVector Point {
     70      get { return PointParameter.ActualValue; }
     71    }
     72    public RealVector PersonalBestPoint {
     73      get { return PersonalBestPointParameter.ActualValue; }
     74      set { PersonalBestPointParameter.ActualValue = value; }
     75    }
     76    public RealVector BestPoint {
     77      get { return BestPointParameter.ActualValue; }
     78      set { BestPointParameter.ActualValue = value; }
     79    }
     80    public bool Maximization {
     81      get { return MaximizationParameter.ActualValue.Value; }
     82    }
     83    #endregion
     84
     85    #region Construction & Cloning
     86
     87    [StorableConstructor]
     88    protected SwarmUpdater(bool deserializing) : base(deserializing) { }
     89    protected SwarmUpdater(SwarmUpdater original, Cloner cloner)
     90      : base(original, cloner) {
     91    }
     92
    6193    public SwarmUpdater()
    6294      : base() {
    63       Parameters.Add(new LookupParameter<RealVector>("CurrentPosition", "Current position"));
    64       Parameters.Add(new LookupParameter<RealVector>("BestLocal", "Best local position"));
    65       Parameters.Add(new LookupParameter<RealVector>("BestGlobal", "Best global position"));
    66       Parameters.Add(new LookupParameter<DoubleValue>("LocalBestQuality", "Best local quality"));
    67       Parameters.Add(new LookupParameter<DoubleValue>("GlobalBestQuality", "Best global quality"));
    68       Parameters.Add(new LookupParameter<DoubleValue>("CurrentQuality", "Current quality"));
     95      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "Particle's quality"));
     96      Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestQuality", "Particle's personal best quality"));
     97      Parameters.Add(new LookupParameter<DoubleValue>("BestQuality", "Global best particle quality"));
     98      Parameters.Add(new LookupParameter<RealVector>("Point", "Particle's position"));
     99      Parameters.Add(new LookupParameter<RealVector>("PersonalBestPoint", "Particle's personal best position"));
     100      Parameters.Add(new LookupParameter<RealVector>("BestPoint", "Globa best particle position"));
    69101      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
    70102    }
    71103
     104    public override IDeepCloneable Clone(Cloner cloner) {
     105      return new SwarmUpdater(this, cloner);
     106    }
     107
     108    #endregion
     109
    72110    public override IOperation Apply() {
    73       if (MaximizationParameter.ActualValue.Value) {
    74         if (CurrentQualityParameter.ActualValue.Value > LocalBestQualityParameter.ActualValue.Value) {
    75           LocalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
    76           BestLocalParameter.ActualValue = (RealVector)CurrentPositionParameter.ActualValue.Clone();
    77         }
    78         if (CurrentQualityParameter.ActualValue.Value > GlobalBestQualityParameter.ActualValue.Value) {
    79           GlobalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
    80           BestGlobalParameter.ActualValue = (RealVector)CurrentPositionParameter.ActualValue.Clone();
    81         }
    82       } else {
    83         if (CurrentQualityParameter.ActualValue.Value < LocalBestQualityParameter.ActualValue.Value) {
    84           LocalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
    85           BestLocalParameter.ActualValue = (RealVector)CurrentPositionParameter.ActualValue.Clone();
    86         }
    87         if (CurrentQualityParameter.ActualValue.Value < GlobalBestQualityParameter.ActualValue.Value) {
    88           GlobalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
    89           BestGlobalParameter.ActualValue = (RealVector)CurrentPositionParameter.ActualValue.Clone();
     111
     112      if (Maximization && Quality > PersonalBestQuality ||
     113         !Maximization && Quality < PersonalBestQuality) {
     114        PersonalBestQuality = Quality;
     115        PersonalBestPoint = Point;
     116        if (Maximization && PersonalBestQuality > BestQuality ||
     117           !Maximization && PersonalBestQuality < BestQuality) {
     118          BestQuality = PersonalBestQuality;
     119          BestPoint = PersonalBestPoint;
    90120        }
    91121      }
Note: See TracChangeset for help on using the changeset viewer.