1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
32 | [Item("Swarm Updater", "Updates personal best point and quality as well as global best point and quality.")]
|
---|
33 | [StorableClass]
|
---|
34 | public sealed class RealVectorSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater {
|
---|
35 | public override bool CanChangeName {
|
---|
36 | get { return false; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | #region Parameter properties
|
---|
40 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
41 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
42 | }
|
---|
43 | public IScopeTreeLookupParameter<DoubleValue> PersonalBestQualityParameter {
|
---|
44 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["PersonalBestQuality"]; }
|
---|
45 | }
|
---|
46 | public IScopeTreeLookupParameter<DoubleValue> NeighborBestQualityParameter {
|
---|
47 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["NeighborBestQuality"]; }
|
---|
48 | }
|
---|
49 | public IScopeTreeLookupParameter<RealVector> RealVectorParameter {
|
---|
50 | get { return (IScopeTreeLookupParameter<RealVector>)Parameters["RealVector"]; }
|
---|
51 | }
|
---|
52 | public IScopeTreeLookupParameter<RealVector> PersonalBestParameter {
|
---|
53 | get { return (IScopeTreeLookupParameter<RealVector>)Parameters["PersonalBest"]; }
|
---|
54 | }
|
---|
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"]; }
|
---|
60 | }
|
---|
61 | public ILookupParameter<DoubleValue> BestQualityParameter {
|
---|
62 | get { return (ILookupParameter<DoubleValue>)Parameters["BestQuality"]; }
|
---|
63 | }
|
---|
64 | public ILookupParameter<RealVector> BestPointParameter {
|
---|
65 | get { return (ILookupParameter<RealVector>)Parameters["BestPoint"]; }
|
---|
66 | }
|
---|
67 | public IScopeTreeLookupParameter<IntArray> NeighborsParameter {
|
---|
68 | 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"]; }
|
---|
75 | }
|
---|
76 | #endregion
|
---|
77 |
|
---|
78 | #region Parameter values
|
---|
79 | private DoubleValue BestQuality {
|
---|
80 | get { return BestQualityParameter.ActualValue; }
|
---|
81 | }
|
---|
82 | private RealVector BestPoint {
|
---|
83 | get { return BestPointParameter.ActualValue; }
|
---|
84 | set { BestPointParameter.ActualValue = value; }
|
---|
85 | }
|
---|
86 | private ItemArray<DoubleValue> Quality {
|
---|
87 | get { return QualityParameter.ActualValue; }
|
---|
88 | }
|
---|
89 | private ItemArray<DoubleValue> PersonalBestQuality {
|
---|
90 | get { return PersonalBestQualityParameter.ActualValue; }
|
---|
91 | set { PersonalBestQualityParameter.ActualValue = value; }
|
---|
92 | }
|
---|
93 | private ItemArray<DoubleValue> NeighborBestQuality {
|
---|
94 | get { return NeighborBestQualityParameter.ActualValue; }
|
---|
95 | set { NeighborBestQualityParameter.ActualValue = value; }
|
---|
96 | }
|
---|
97 | private ItemArray<RealVector> RealVector {
|
---|
98 | get { return RealVectorParameter.ActualValue; }
|
---|
99 | }
|
---|
100 | private ItemArray<RealVector> PersonalBest {
|
---|
101 | get { return PersonalBestParameter.ActualValue; }
|
---|
102 | set { PersonalBestParameter.ActualValue = value; }
|
---|
103 | }
|
---|
104 | private ItemArray<RealVector> NeighborBest {
|
---|
105 | get { return NeighborBestParameter.ActualValue; }
|
---|
106 | set { NeighborBestParameter.ActualValue = value; }
|
---|
107 | }
|
---|
108 | private bool Maximization {
|
---|
109 | get { return MaximizationParameter.ActualValue.Value; }
|
---|
110 | }
|
---|
111 | private ItemArray<IntArray> Neighbors {
|
---|
112 | 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; }
|
---|
120 | }
|
---|
121 | #endregion
|
---|
122 |
|
---|
123 | #region Construction & Cloning
|
---|
124 |
|
---|
125 | [StorableConstructor]
|
---|
126 | private RealVectorSwarmUpdater(bool deserializing) : base(deserializing) { }
|
---|
127 | private RealVectorSwarmUpdater(RealVectorSwarmUpdater original, Cloner cloner) : base(original, cloner) { }
|
---|
128 | public RealVectorSwarmUpdater()
|
---|
129 | : base() {
|
---|
130 | Parameters.Add(new LookupParameter<DoubleValue>("BestQuality", "Overall best quality."));
|
---|
131 | Parameters.Add(new LookupParameter<RealVector>("BestPoint", "Global best particle position"));
|
---|
132 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "Particle's quality"));
|
---|
133 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("PersonalBestQuality", "Particle's personal best quality"));
|
---|
134 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("NeighborBestQuality", "Global best particle quality"));
|
---|
135 | Parameters.Add(new ScopeTreeLookupParameter<RealVector>("RealVector", "Particle's position"));
|
---|
136 | Parameters.Add(new ScopeTreeLookupParameter<RealVector>("PersonalBest", "Particle's personal best position"));
|
---|
137 | Parameters.Add(new ScopeTreeLookupParameter<RealVector>("NeighborBest", "Neighborhood (or global in case of totally connected neighborhood) best particle position"));
|
---|
138 | Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle."));
|
---|
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."));
|
---|
142 | }
|
---|
143 |
|
---|
144 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
145 | return new RealVectorSwarmUpdater(this, cloner);
|
---|
146 | }
|
---|
147 |
|
---|
148 | #endregion
|
---|
149 |
|
---|
150 | public override IOperation Apply() {
|
---|
151 | InitializeBestPoint();
|
---|
152 | UpdateNeighbors();
|
---|
153 | UpdateSwarm();
|
---|
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 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | private void InitializeBestPoint() {
|
---|
165 | if (BestQuality == null)
|
---|
166 | BestQualityParameter.ActualValue = new DoubleValue();
|
---|
167 | BestQuality.Value = Maximization ? Quality.Max(v => v.Value) : Quality.Min(v => v.Value);
|
---|
168 | int bestIndex = Quality.FindIndex(v => v.Value == BestQuality.Value);
|
---|
169 | BestPoint = (RealVector)RealVector[bestIndex].Clone();
|
---|
170 | }
|
---|
171 |
|
---|
172 | private void UpdateNeighbors() {
|
---|
173 | if (Neighbors != null & Neighbors.Length > 0) {
|
---|
174 | if (this.NeighborBest == null || NeighborBest.Length != Neighbors.Length)
|
---|
175 | NeighborBest = new ItemArray<RealVector>(Neighbors.Length);
|
---|
176 | for (int n = 0; n < Neighbors.Length; n++) {
|
---|
177 | var pairs = Quality.Zip(RealVector, (q, p) => new { Quality = q, Point = p })
|
---|
178 | .Where((p, i) => i == n || Neighbors[n].Contains(i));
|
---|
179 | NeighborBest[n] = Maximization ?
|
---|
180 | pairs.OrderByDescending(p => p.Quality.Value).First().Point :
|
---|
181 | pairs.OrderBy(p => p.Quality.Value).First().Point;
|
---|
182 | }
|
---|
183 | NeighborBestParameter.ActualValue = NeighborBest;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | private void UpdateSwarm() {
|
---|
188 | if (PersonalBestQuality.Length == 0) {
|
---|
189 | PersonalBestQuality = (ItemArray<DoubleValue>)Quality.Clone();
|
---|
190 | if (VelocityBounds == null)
|
---|
191 | VelocityBounds = new DoubleMatrix(new double[,] { { -1, 1 } });
|
---|
192 | }
|
---|
193 | for (int i = 0; i < RealVector.Length; i++) {
|
---|
194 | if (Maximization && Quality[i].Value > PersonalBestQuality[i].Value ||
|
---|
195 | !Maximization && Quality[i].Value < PersonalBestQuality[i].Value) {
|
---|
196 | PersonalBestQuality[i].Value = Quality[i].Value;
|
---|
197 | PersonalBest[i] = RealVector[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];
|
---|
212 | }
|
---|
213 | }
|
---|
214 | NeighborBestQuality = neighborBestQuality;
|
---|
215 | NeighborBest = neighborBest;
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 | }
|
---|