Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3832 was 3742, checked in by gkronber, 15 years ago

Fixed GPL license headers and deleted files which are not referenced by projects. #893

File size: 4.6 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 System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Operators;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.RealVectorEncoding;
30using HeuristicLab.Parameters;
31
32namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
33  public class SwarmUpdater : SingleSuccessorOperator {
34    #region Parameter properties
35
36    public ILookupParameter<DoubleValue> CurrentQualityParameter {
37      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentQuality"]; }
38    }
39
40    public ILookupParameter<DoubleValue> LocalBestQualityParameter {
41      get { return (ILookupParameter<DoubleValue>)Parameters["LocalBestQuality"]; }
42    }
43
44    public ILookupParameter<DoubleValue> GlobalBestQualityParameter {
45      get { return (ILookupParameter<DoubleValue>)Parameters["GlobalBestQuality"]; }
46    }
47
48    public ILookupParameter<RealVector> CurrentPositionParameter {
49      get { return (ILookupParameter<RealVector>)Parameters["CurrentPosition"]; }
50    }
51
52    public ILookupParameter<RealVector> BestLocalParameter {
53      get { return (ILookupParameter<RealVector>)Parameters["BestLocal"]; }
54    }
55
56    public ILookupParameter<RealVector> BestGlobalParameter {
57      get { return (ILookupParameter<RealVector>)Parameters["BestGlobal"]; }
58    }
59
60    public ValueLookupParameter<BoolValue> MaximizationParameter {
61      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
62    }
63    #endregion
64
65    public SwarmUpdater()
66      : base() {
67      Parameters.Add(new LookupParameter<RealVector>("CurrentPosition", "Current position"));
68      Parameters.Add(new LookupParameter<RealVector>("BestLocal", "Best local position"));
69      Parameters.Add(new LookupParameter<RealVector>("BestGlobal", "Best global position"));
70      Parameters.Add(new LookupParameter<DoubleValue>("LocalBestQuality", "Best local quality"));
71      Parameters.Add(new LookupParameter<DoubleValue>("GlobalBestQuality", "Best global quality"));
72      Parameters.Add(new LookupParameter<DoubleValue>("CurrentQuality", "Current quality"));
73      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
74    }
75
76    public override IOperation Apply() {
77      if (MaximizationParameter.ActualValue.Value) {
78        if (CurrentQualityParameter.ActualValue.Value > LocalBestQualityParameter.ActualValue.Value) {
79          LocalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
80          BestLocalParameter.ActualValue = (RealVector) CurrentPositionParameter.ActualValue.Clone();
81        }
82        if (CurrentQualityParameter.ActualValue.Value > GlobalBestQualityParameter.ActualValue.Value) {
83          GlobalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
84          BestGlobalParameter.ActualValue = (RealVector)CurrentPositionParameter.ActualValue.Clone();
85        }
86      } else {
87        if (CurrentQualityParameter.ActualValue.Value < LocalBestQualityParameter.ActualValue.Value) {
88          LocalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
89          BestLocalParameter.ActualValue = (RealVector)CurrentPositionParameter.ActualValue.Clone();
90        }
91        if (CurrentQualityParameter.ActualValue.Value < GlobalBestQualityParameter.ActualValue.Value) {
92          GlobalBestQualityParameter.ActualValue.Value = CurrentQualityParameter.ActualValue.Value;
93          BestGlobalParameter.ActualValue = (RealVector)CurrentPositionParameter.ActualValue.Clone();
94        }
95      }
96      return base.Apply();
97    }
98
99    public override bool CanChangeName {
100      get { return false; }
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.