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;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Parameters;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Encodings.RealVectorEncoding {
|
---|
31 |
|
---|
32 | [Item("RealVectorParticleUpdater", "Updates a certain particle taking the current position and velocity into account, as well as the best point and the best point in a local neighborhood.")]
|
---|
33 | [StorableClass]
|
---|
34 | public abstract class RealVectorParticleUpdater : SingleSuccessorOperator, IRealVectorParticleUpdater {
|
---|
35 |
|
---|
36 | public override bool CanChangeName {
|
---|
37 | get { return false; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | #region Parameter properties
|
---|
41 | public ILookupParameter<IRandom> RandomParameter {
|
---|
42 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
43 | }
|
---|
44 | public ILookupParameter<RealVector> VelocityParameter {
|
---|
45 | get { return (ILookupParameter<RealVector>)Parameters["Velocity"]; }
|
---|
46 | }
|
---|
47 | public ILookupParameter<RealVector> PersonalBestParameter {
|
---|
48 | get { return (ILookupParameter<RealVector>)Parameters["PersonalBest"]; }
|
---|
49 | }
|
---|
50 | public ILookupParameter<RealVector> NeighborBestParameter {
|
---|
51 | get { return (ILookupParameter<RealVector>)Parameters["NeighborBest"]; }
|
---|
52 | }
|
---|
53 | public LookupParameter<RealVector> BestRealVectorParameter {
|
---|
54 | get { return (LookupParameter<RealVector>)Parameters["BestRealVector"]; }
|
---|
55 | }
|
---|
56 | public ILookupParameter<RealVector> RealVectorParameter {
|
---|
57 | get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
|
---|
58 | }
|
---|
59 | public ILookupParameter<DoubleMatrix> BoundsParameter {
|
---|
60 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
|
---|
61 | }
|
---|
62 | public ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter {
|
---|
63 | get { return (ILookupParameter<DoubleMatrix>)Parameters["CurrentVelocityBounds"]; }
|
---|
64 | }
|
---|
65 | public ILookupParameter<DoubleValue> InertiaParameter {
|
---|
66 | get { return (ILookupParameter<DoubleValue>)Parameters["CurrentInertia"]; }
|
---|
67 | }
|
---|
68 | public ILookupParameter<DoubleValue> PersonalBestAttractionParameter {
|
---|
69 | get { return (ILookupParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; }
|
---|
70 | }
|
---|
71 | public ILookupParameter<DoubleValue> NeighborBestAttractionParameter {
|
---|
72 | get { return (ILookupParameter<DoubleValue>)Parameters["NeighborBestAttraction"]; }
|
---|
73 | }
|
---|
74 | #endregion
|
---|
75 |
|
---|
76 | #region Parameter Values
|
---|
77 | protected IRandom Random {
|
---|
78 | get { return RandomParameter.ActualValue; }
|
---|
79 | }
|
---|
80 | protected RealVector Velocity {
|
---|
81 | get { return VelocityParameter.ActualValue; }
|
---|
82 | set { VelocityParameter.ActualValue = value; }
|
---|
83 | }
|
---|
84 | protected RealVector PersonalBest {
|
---|
85 | get { return PersonalBestParameter.ActualValue; }
|
---|
86 | }
|
---|
87 | protected RealVector BestPoint {
|
---|
88 | get { return BestRealVectorParameter.ActualValue; }
|
---|
89 | }
|
---|
90 | protected RealVector RealVector {
|
---|
91 | get { return RealVectorParameter.ActualValue; }
|
---|
92 | set { RealVectorParameter.ActualValue = value; }
|
---|
93 | }
|
---|
94 | protected RealVector NeighborBest {
|
---|
95 | get { return NeighborBestParameter.ActualValue; }
|
---|
96 | }
|
---|
97 | protected DoubleMatrix Bounds {
|
---|
98 | get { return BoundsParameter.ActualValue; }
|
---|
99 | }
|
---|
100 | protected DoubleMatrix CurrentVelocityBounds {
|
---|
101 | get { return CurrentVelocityBoundsParameter.ActualValue; }
|
---|
102 | }
|
---|
103 | protected DoubleValue Inertia {
|
---|
104 | get { return InertiaParameter.ActualValue; }
|
---|
105 | }
|
---|
106 | protected DoubleValue PersonalBestAttraction {
|
---|
107 | get { return PersonalBestAttractionParameter.ActualValue; }
|
---|
108 | }
|
---|
109 | protected DoubleValue NeighborBestAttraction {
|
---|
110 | get { return NeighborBestAttractionParameter.ActualValue; }
|
---|
111 | }
|
---|
112 | #endregion
|
---|
113 |
|
---|
114 | #region Construction & Cloning
|
---|
115 | [StorableConstructor]
|
---|
116 | protected RealVectorParticleUpdater(bool deserializing) : base(deserializing) { }
|
---|
117 | protected RealVectorParticleUpdater(RealVectorParticleUpdater original, Cloner cloner) : base(original, cloner) { }
|
---|
118 | public RealVectorParticleUpdater()
|
---|
119 | : base() {
|
---|
120 | Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator."));
|
---|
121 | Parameters.Add(new LookupParameter<RealVector>("RealVector", "Particle's current solution"));
|
---|
122 | Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity."));
|
---|
123 | Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution."));
|
---|
124 | Parameters.Add(new LookupParameter<RealVector>("BestRealVector", "Global best position."));
|
---|
125 | Parameters.Add(new LookupParameter<RealVector>("NeighborBest", "Best neighboring solution."));
|
---|
126 | Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector for the current problem."));
|
---|
127 | Parameters.Add(new LookupParameter<DoubleMatrix>("CurrentVelocityBounds", "Upper and lower bounds for the particle's velocity vector."));
|
---|
128 | Parameters.Add(new LookupParameter<DoubleValue>("CurrentInertia", "The weight for the particle's velocity vector."));
|
---|
129 | Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestAttraction", "The weight for the particle's personal best position."));
|
---|
130 | Parameters.Add(new LookupParameter<DoubleValue>("NeighborBestAttraction", "The weight for the global best position."));
|
---|
131 | }
|
---|
132 | #endregion
|
---|
133 |
|
---|
134 | protected void MoveParticle(RealVector velocity, RealVector position) {
|
---|
135 | BoundsChecker.Apply(velocity, CurrentVelocityBounds);
|
---|
136 | for (int i = 0; i < velocity.Length; i++) {
|
---|
137 | position[i] = RealVector[i] + velocity[i];
|
---|
138 | }
|
---|
139 | for (int i = 0; i < position.Length; i++) {
|
---|
140 | double min = Bounds[i % Bounds.Rows, 0];
|
---|
141 | double max = Bounds[i % Bounds.Rows, 1];
|
---|
142 | if (position[i] < min) {
|
---|
143 | int reflectionCount = (int)Math.Truncate((min - position[i]) / (max - min)) + 1;
|
---|
144 | double reflection = (min - position[i]) % (max - min);
|
---|
145 | if (IsOdd(reflectionCount)) {
|
---|
146 | position[i] = min + reflection;
|
---|
147 | velocity[i] = -velocity[i];
|
---|
148 |
|
---|
149 | } else {
|
---|
150 | position[i] = max - reflection;
|
---|
151 | }
|
---|
152 | }
|
---|
153 | if (position[i] > max) {
|
---|
154 | int reflectionCount = (int)Math.Truncate((position[i] - max) / (max - min)) + 1;
|
---|
155 | double reflection = (position[i] - max) % (max - min);
|
---|
156 | if (IsOdd(reflectionCount)) {
|
---|
157 | position[i] = max - reflection;
|
---|
158 | velocity[i] = -velocity[i];
|
---|
159 | } else {
|
---|
160 | position[i] = min + reflection;
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | RealVector = position;
|
---|
166 | Velocity = velocity;
|
---|
167 | }
|
---|
168 |
|
---|
169 | private static bool IsOdd(int number) {
|
---|
170 | return number % 2 == 1;
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|