Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/SwarmUpdater.cs @ 3719

Last change on this file since 3719 was 3682, checked in by mkofler, 15 years ago

Worked on #852 (PSO)

File size: 3.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Operators;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using HeuristicLab.Encodings.RealVectorEncoding;
9using HeuristicLab.Parameters;
10
11namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
12  public class SwarmUpdater : SingleSuccessorOperator {
13    #region Parameter properties
14
15    public ILookupParameter<DoubleValue> CurrentQualityParameter {
16      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentQuality"]; }
17    }
18
19    public ILookupParameter<DoubleValue> LocalBestQualityParameter {
20      get { return (ILookupParameter<DoubleValue>)Parameters["LocalBestQuality"]; }
21    }
22
23    public ILookupParameter<DoubleValue> GlobalBestQualityParameter {
24      get { return (ILookupParameter<DoubleValue>)Parameters["GlobalBestQuality"]; }
25    }
26
27    public ILookupParameter<RealVector> CurrentPositionParameter {
28      get { return (ILookupParameter<RealVector>)Parameters["CurrentPosition"]; }
29    }
30
31    public ILookupParameter<RealVector> BestLocalParameter {
32      get { return (ILookupParameter<RealVector>)Parameters["BestLocal"]; }
33    }
34
35    public ILookupParameter<RealVector> BestGlobalParameter {
36      get { return (ILookupParameter<RealVector>)Parameters["BestGlobal"]; }
37    }
38
39    public ValueLookupParameter<BoolValue> MaximizationParameter {
40      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
41    }
42    #endregion
43
44    public SwarmUpdater()
45      : base() {
46      Parameters.Add(new LookupParameter<RealVector>("CurrentPosition", "Current position"));
47      Parameters.Add(new LookupParameter<RealVector>("BestLocal", "Best local position"));
48      Parameters.Add(new LookupParameter<RealVector>("BestGlobal", "Best global position"));
49      Parameters.Add(new LookupParameter<DoubleValue>("LocalBestQuality", "Best local quality"));
50      Parameters.Add(new LookupParameter<DoubleValue>("GlobalBestQuality", "Best global quality"));
51      Parameters.Add(new LookupParameter<DoubleValue>("CurrentQuality", "Current quality"));
52      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
53    }
54
55    public override IOperation Apply() {
56      if (MaximizationParameter.ActualValue.Value) {
57        if (CurrentQualityParameter.ActualValue.Value > LocalBestQualityParameter.ActualValue.Value) {
58          LocalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
59          BestLocalParameter.ActualValue = (RealVector) CurrentPositionParameter.ActualValue.Clone();
60        }
61        if (CurrentQualityParameter.ActualValue.Value > GlobalBestQualityParameter.ActualValue.Value) {
62          GlobalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
63          BestGlobalParameter.ActualValue = (RealVector)CurrentPositionParameter.ActualValue.Clone();
64        }
65      } else {
66        if (CurrentQualityParameter.ActualValue.Value < LocalBestQualityParameter.ActualValue.Value) {
67          LocalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
68          BestLocalParameter.ActualValue = (RealVector)CurrentPositionParameter.ActualValue.Clone();
69        }
70        if (CurrentQualityParameter.ActualValue.Value < GlobalBestQualityParameter.ActualValue.Value) {
71          GlobalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
72          BestGlobalParameter.ActualValue = (RealVector)CurrentPositionParameter.ActualValue.Clone();
73        }
74      }
75      return base.Apply();
76    }
77
78    public override bool CanChangeName {
79      get { return false; }
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.