Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/SwarmUpdater.cs @ 4682

Last change on this file since 4682 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 4.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Core;
23using HeuristicLab.Data;
24using HeuristicLab.Encodings.RealVectorEncoding;
25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
27
28namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
29  public class SwarmUpdater : SingleSuccessorOperator {
30    #region Parameter properties
31
32    public ILookupParameter<DoubleValue> CurrentQualityParameter {
33      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentQuality"]; }
34    }
35
36    public ILookupParameter<DoubleValue> LocalBestQualityParameter {
37      get { return (ILookupParameter<DoubleValue>)Parameters["LocalBestQuality"]; }
38    }
39
40    public ILookupParameter<DoubleValue> GlobalBestQualityParameter {
41      get { return (ILookupParameter<DoubleValue>)Parameters["GlobalBestQuality"]; }
42    }
43
44    public ILookupParameter<RealVector> CurrentPositionParameter {
45      get { return (ILookupParameter<RealVector>)Parameters["CurrentPosition"]; }
46    }
47
48    public ILookupParameter<RealVector> BestLocalParameter {
49      get { return (ILookupParameter<RealVector>)Parameters["BestLocal"]; }
50    }
51
52    public ILookupParameter<RealVector> BestGlobalParameter {
53      get { return (ILookupParameter<RealVector>)Parameters["BestGlobal"]; }
54    }
55
56    public ValueLookupParameter<BoolValue> MaximizationParameter {
57      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
58    }
59    #endregion
60
61    public SwarmUpdater()
62      : 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"));
69      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
70    }
71
72    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();
90        }
91      }
92      return base.Apply();
93    }
94
95    public override bool CanChangeName {
96      get { return false; }
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.