Changeset 15091 for trunk/sources/HeuristicLab.Encodings.RealVectorEncoding
- Timestamp:
- 06/29/17 15:26:16 (7 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3
- Files:
-
- 1 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/HeuristicLab.Encodings.RealVectorEncoding-3.3.csproj
r15067 r15091 132 132 <Compile Include="ParticleOperators\RealVectorSwarmUpdater.cs" /> 133 133 <Compile Include="ParticleOperators\RealVectorTotallyConnectedParticleUpdater.cs" /> 134 <Compile Include="ParticleOperators\RealVectorVelocityInitializer.cs" /> 134 135 <Compile Include="Plugin.cs" /> 135 136 <Compile Include="RealVectorCreator.cs" /> -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Interfaces/IRealVectorParticleCreator.cs
r14185 r15091 26 26 namespace HeuristicLab.Encodings.RealVectorEncoding { 27 27 public interface IRealVectorParticleCreator : IParticleCreator, IRealVectorOperator { 28 ILookupParameter<IntValue> ProblemSizeParameter { get; }29 28 ILookupParameter<RealVector> RealVectorParameter { get; } 30 29 ILookupParameter<RealVector> PersonalBestParameter { get; } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Interfaces/IRealVectorParticleUpdater.cs
r14185 r15091 27 27 public interface IRealVectorParticleUpdater : IParticleUpdater, IRealVectorOperator { 28 28 ILookupParameter<RealVector> VelocityParameter { get; } 29 ILookupParameter<Double Matrix> CurrentVelocityBoundsParameter { get; }29 ILookupParameter<DoubleValue> CurrentMaxVelocityParameter { get; } 30 30 ILookupParameter<RealVector> RealVectorParameter { get; } 31 31 ILookupParameter<DoubleMatrix> BoundsParameter { get; } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorNeighborhoodParticleUpdater.cs
r14185 r15091 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Optimization;25 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 25 … … 28 27 [Item("Neighborhood Particle Updater", "Updates the particle's position using (among other things) the best neighbor's position. Point = Point + Velocity*Inertia + (PersonalBestPoint-Point)*Phi_P*r_p + (BestNeighborPoint-Point)*Phi_G*r_g.")] 29 28 [StorableClass] 30 public sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater , ILocalParticleUpdater{29 public sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater { 31 30 32 31 #region Construction & Cloning … … 41 40 42 41 public override IOperation Apply() { 43 double inertia = Inertia.Value; 44 double personalBestAttraction = PersonalBestAttraction.Value; 45 double neighborBestAttraction = NeighborBestAttraction.Value; 46 47 RealVector velocity = new RealVector(Velocity.Length); 48 RealVector position = new RealVector(RealVector.Length); 49 double r_p = Random.NextDouble(); 50 double r_g = Random.NextDouble(); 51 52 for (int i = 0; i < velocity.Length; i++) { 53 velocity[i] = 54 Velocity[i] * inertia + 55 (PersonalBest[i] - RealVector[i]) * personalBestAttraction * r_p + 56 (BestPoint[i] - RealVector[i]) * neighborBestAttraction * r_g; 57 } 58 59 MoveParticle(velocity, position); 42 UpdateVelocity(); 43 UpdatePosition(); 60 44 61 45 return base.Apply(); -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleCreator.cs
r14185 r15091 24 24 using HeuristicLab.Data; 25 25 using HeuristicLab.Operators; 26 using HeuristicLab.Optimization; 26 27 using HeuristicLab.Parameters; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 30 31 [Item("RealVectorParticleCreator", "Creates a particle with position, zero velocity vector and personal best.")] 31 32 [StorableClass] 32 public class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator {33 public class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator, IStochasticOperator { 33 34 34 35 #region Parameters 35 public ILookupParameter<I ntValue> ProblemSizeParameter {36 get { return (ILookupParameter<I ntValue>)Parameters["ProblemSize"]; }36 public ILookupParameter<IRandom> RandomParameter { 37 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 37 38 } 38 39 public IValueLookupParameter<DoubleMatrix> BoundsParameter { … … 48 49 get { return (ILookupParameter<RealVector>)Parameters["Velocity"]; } 49 50 } 50 #endregion 51 52 #region Parameter Values 53 protected int ProblemSize { 54 get { return ProblemSizeParameter.ActualValue.Value; } 55 } 56 protected RealVector Velocity { 57 set { VelocityParameter.ActualValue = value; } 51 public ILookupParameter<ISolutionCreator> SolutionCreatorParameter { 52 get { return (ILookupParameter<ISolutionCreator>)Parameters["SolutionCreator"]; } 58 53 } 59 54 #endregion 60 55 61 56 #region Construction & Cloning 62 57 [StorableConstructor] … … 65 60 public RealVectorParticleCreator() 66 61 : base() { 67 Parameters.Add(new LookupParameter<I ntValue>("ProblemSize", "The dimension of the problem."));62 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 68 63 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension.")); 69 64 Parameters.Add(new LookupParameter<RealVector>("RealVector", "Particle's current solution")); 70 65 Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution.")); 71 66 Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity.")); 67 Parameters.Add(new LookupParameter<ISolutionCreator>("SolutionCreator", "The operator that creates the initial position.")); 72 68 73 UniformRandomRealVectorCreator realVectorCreater = new UniformRandomRealVectorCreator();69 Placeholder realVectorCreater = new Placeholder(); 74 70 Assigner personalBestPositionAssigner = new Assigner(); 71 RealVectorVelocityInitializer velocityInitializer = new RealVectorVelocityInitializer(); 75 72 76 73 OperatorGraph.InitialOperator = realVectorCreater; 77 74 78 realVectorCreater.RealVectorParameter.ActualName = RealVectorParameter.Name; 79 realVectorCreater.LengthParameter.ActualName = ProblemSizeParameter.Name; 80 realVectorCreater.BoundsParameter.ActualName = BoundsParameter.Name; 75 realVectorCreater.OperatorParameter.ActualName = SolutionCreatorParameter.Name; 81 76 realVectorCreater.Successor = personalBestPositionAssigner; 82 77 83 78 personalBestPositionAssigner.LeftSideParameter.ActualName = PersonalBestParameter.Name; 84 79 personalBestPositionAssigner.RightSideParameter.ActualName = RealVectorParameter.Name; 85 personalBestPositionAssigner.Successor = null; 80 personalBestPositionAssigner.Successor = velocityInitializer; 81 82 velocityInitializer.BoundsParameter.ActualName = BoundsParameter.Name; 83 velocityInitializer.RandomParameter.ActualName = RandomParameter.Name; 84 velocityInitializer.RealVectorParameter.ActualName = RealVectorParameter.Name; 85 velocityInitializer.VelocityParameter.ActualName = VelocityParameter.Name; 86 velocityInitializer.Successor = null; 86 87 } 87 88 public override IDeepCloneable Clone(Cloner cloner) { … … 89 90 } 90 91 #endregion 91 92 public override IOperation Apply() {93 Velocity = new RealVector(ProblemSize);94 return base.Apply();95 }96 97 98 92 } 99 93 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.cs
r14185 r15091 51 51 get { return (ILookupParameter<RealVector>)Parameters["NeighborBest"]; } 52 52 } 53 public LookupParameter<RealVector> BestRealVectorParameter {54 get { return (LookupParameter<RealVector>)Parameters["BestRealVector"]; }55 }56 53 public ILookupParameter<RealVector> RealVectorParameter { 57 54 get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; } … … 60 57 get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; } 61 58 } 62 public ILookupParameter<Double Matrix> CurrentVelocityBoundsParameter {63 get { return (ILookupParameter<Double Matrix>)Parameters["CurrentVelocityBounds"]; }59 public ILookupParameter<DoubleValue> CurrentMaxVelocityParameter { 60 get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; } 64 61 } 65 62 public ILookupParameter<DoubleValue> InertiaParameter { … … 73 70 } 74 71 #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 72 114 73 #region Construction & Cloning 115 74 [StorableConstructor] … … 122 81 Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity.")); 123 82 Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution.")); 124 Parameters.Add(new LookupParameter<RealVector>("BestRealVector", "Global best position."));125 83 Parameters.Add(new LookupParameter<RealVector>("NeighborBest", "Best neighboring solution.")); 126 84 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<Double Matrix>("CurrentVelocityBounds", "Upper and lower boundsfor the particle's velocity vector."));85 Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Maximum for the particle's velocity vector.")); 128 86 Parameters.Add(new LookupParameter<DoubleValue>("CurrentInertia", "The weight for the particle's velocity vector.")); 129 87 Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestAttraction", "The weight for the particle's personal best position.")); … … 132 90 #endregion 133 91 134 protected void MoveParticle(RealVector velocity, RealVector position) { 135 BoundsChecker.Apply(velocity, CurrentVelocityBounds); 92 protected void UpdateVelocity() { 93 var velocity = VelocityParameter.ActualValue; 94 var position = RealVectorParameter.ActualValue; 95 var inertia = InertiaParameter.ActualValue.Value; 96 var personalBest = PersonalBestParameter.ActualValue; 97 var personalBestAttraction = PersonalBestAttractionParameter.ActualValue.Value; 98 var neighborBest = NeighborBestParameter.ActualValue; 99 var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value; 100 101 var random = RandomParameter.ActualValue; 102 136 103 for (int i = 0; i < velocity.Length; i++) { 137 position[i] = RealVector[i] + velocity[i]; 104 double r_p = random.NextDouble(); 105 double r_g = random.NextDouble(); 106 velocity[i] = 107 velocity[i] * inertia + 108 (personalBest[i] - position[i]) * personalBestAttraction * r_p + 109 (neighborBest[i] - position[i]) * neighborBestAttraction * r_g; 138 110 } 111 112 var maxVelocity = CurrentMaxVelocityParameter.ActualValue.Value; 113 var speed = Math.Sqrt(velocity.DotProduct(velocity)); 114 if (speed > maxVelocity) { 115 for (var i = 0; i < velocity.Length; i++) { 116 velocity[i] *= maxVelocity / speed; 117 } 118 } 119 } 120 121 protected void UpdatePosition() { 122 var velocity = VelocityParameter.ActualValue; 123 var position = RealVectorParameter.ActualValue; 124 125 for (int i = 0; i < velocity.Length; i++) { 126 position[i] += velocity[i]; 127 } 128 129 var bounds = BoundsParameter.ActualValue; 139 130 for (int i = 0; i < position.Length; i++) { 140 double min = Bounds[i % Bounds.Rows, 0];141 double max = Bounds[i % Bounds.Rows, 1];131 double min = bounds[i % bounds.Rows, 0]; 132 double max = bounds[i % bounds.Rows, 1]; 142 133 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 } 134 position[i] = min; 135 velocity[i] = -0.5 * velocity[i]; // SPSO 2011 152 136 } 153 137 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 } 138 position[i] = max; 139 velocity[i] = -0.5 * velocity[i]; // SPSO 2011 162 140 } 163 141 } 164 165 RealVector = position;166 Velocity = velocity;167 }168 169 private static bool IsOdd(int number) {170 return number % 2 == 1;171 142 } 172 143 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs
r15071 r15091 76 76 get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; } 77 77 } 78 public IValueLookupParameter<Double Matrix> VelocityBoundsParameter {79 get { return (ValueLookupParameter<Double Matrix>)Parameters["VelocityBounds"]; }80 } 81 public ILookupParameter<Double Matrix> CurrentVelocityBoundsParameter {82 get { return (ILookupParameter<Double Matrix>)Parameters["CurrentVelocityBounds"]; }78 public IValueLookupParameter<DoubleValue> MaxVelocityParameter { 79 get { return (ValueLookupParameter<DoubleValue>)Parameters["MaxVelocity"]; } 80 } 81 public ILookupParameter<DoubleValue> CurrentMaxVelocityParameter { 82 get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; } 83 83 } 84 84 public LookupParameter<ResultCollection> ResultsParameter { … … 86 86 } 87 87 88 #region Velocity Bounds Updating 89 public ILookupParameter<DoubleValue> VelocityBoundsScaleParameter { 90 get { return (ILookupParameter<DoubleValue>)Parameters["VelocityBoundsScale"]; } 91 } 92 public IConstrainedValueParameter<IDiscreteDoubleValueModifier> VelocityBoundsScalingOperatorParameter { 93 get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["VelocityBoundsScalingOperator"]; } 94 } 95 public IValueLookupParameter<DoubleValue> VelocityBoundsStartValueParameter { 96 get { return (IValueLookupParameter<DoubleValue>)Parameters["VelocityBoundsStartValue"]; } 97 } 98 public IValueLookupParameter<DoubleValue> VelocityBoundsEndValueParameter { 99 get { return (IValueLookupParameter<DoubleValue>)Parameters["VelocityBoundsEndValue"]; } 100 } 101 public ILookupParameter<IntValue> VelocityBoundsIndexParameter { 102 get { return (ILookupParameter<IntValue>)Parameters["VelocityBoundsIndex"]; } 103 } 104 public IValueLookupParameter<IntValue> VelocityBoundsStartIndexParameter { 105 get { return (IValueLookupParameter<IntValue>)Parameters["VelocityBoundsStartIndex"]; } 106 } 107 public IValueLookupParameter<IntValue> VelocityBoundsEndIndexParameter { 108 get { return (IValueLookupParameter<IntValue>)Parameters["VelocityBoundsEndIndex"]; } 88 #region Max Velocity Updating 89 public IConstrainedValueParameter<IDiscreteDoubleValueModifier> MaxVelocityScalingOperatorParameter { 90 get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["MaxVelocityScalingOperator"]; } 91 } 92 public IValueLookupParameter<DoubleValue> FinalMaxVelocityParameter { 93 get { return (IValueLookupParameter<DoubleValue>)Parameters["FinalMaxVelocity"]; } 94 } 95 public ILookupParameter<IntValue> MaxVelocityIndexParameter { 96 get { return (ILookupParameter<IntValue>)Parameters["MaxVelocityIndex"]; } 97 } 98 public IValueLookupParameter<IntValue> MaxVelocityStartIndexParameter { 99 get { return (IValueLookupParameter<IntValue>)Parameters["MaxVelocityStartIndex"]; } 100 } 101 public IValueLookupParameter<IntValue> MaxVelocityEndIndexParameter { 102 get { return (IValueLookupParameter<IntValue>)Parameters["MaxVelocityEndIndex"]; } 109 103 } 110 104 #endregion … … 132 126 Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle.")); 133 127 Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false.")); 134 Parameters.Add(new ValueLookupParameter<Double Matrix>("VelocityBounds", "Maximum velocity for each dimension.", new DoubleMatrix(new double[,] { { -1, 1 } })));135 Parameters.Add(new LookupParameter<Double Matrix>("CurrentVelocityBounds", "Current value of velocity bounds."));128 Parameters.Add(new ValueLookupParameter<DoubleValue>("MaxVelocity", "Speed limit for each particle.", new DoubleValue(1))); 129 Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Current value of the speed limit.")); 136 130 Parameters.Add(new LookupParameter<ResultCollection>("Results", "Results")); 137 131 138 132 #region Velocity Bounds Updating 139 Parameters.Add(new LookupParameter<DoubleValue>("VelocityBoundsScale", "Scale parameter.")); 140 Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("VelocityBoundsScalingOperator", "Modifies the value")); 141 Parameters.Add(new ValueLookupParameter<DoubleValue>("VelocityBoundsStartValue", "The start value of 'Value'.", new DoubleValue(1))); 142 Parameters.Add(new ValueLookupParameter<DoubleValue>("VelocityBoundsEndValue", "The end value of 'Value'.", new DoubleValue(1E-10))); 143 Parameters.Add(new LookupParameter<IntValue>("VelocityBoundsIndex", "The current index.", "Iterations")); 144 Parameters.Add(new ValueLookupParameter<IntValue>("VelocityBoundsStartIndex", "The start index at which to start modifying 'Value'.", new IntValue(0))); 145 Parameters.Add(new ValueLookupParameter<IntValue>("VelocityBoundsEndIndex", "The end index by which 'Value' should have reached 'EndValue'.", "MaxIterations")); 146 VelocityBoundsStartIndexParameter.Hidden = true; 147 VelocityBoundsEndIndexParameter.Hidden = true; 133 Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("MaxVelocityScalingOperator", "Modifies the value")); 134 Parameters.Add(new ValueLookupParameter<DoubleValue>("FinalMaxVelocity", "The value of maximum velocity if PSO has reached maximum iterations.", new DoubleValue(1E-10))); 135 Parameters.Add(new LookupParameter<IntValue>("MaxVelocityIndex", "The current index.", "Iterations")); 136 Parameters.Add(new ValueLookupParameter<IntValue>("MaxVelocityStartIndex", "The start index at which to start modifying 'Value'.", new IntValue(0))); 137 Parameters.Add(new ValueLookupParameter<IntValue>("MaxVelocityEndIndex", "The end index by which 'Value' should have reached 'EndValue'.", "MaxIterations")); 138 MaxVelocityStartIndexParameter.Hidden = true; 139 MaxVelocityEndIndexParameter.Hidden = true; 148 140 #endregion 149 141 150 142 Initialize(); 151 RegisterEvents();152 143 } 153 144 … … 167 158 Parameters.Remove("BestQuality"); 168 159 } 169 RegisterEvents(); 170 } 171 172 private void RegisterEvents() { 173 VelocityBoundsStartValueParameter.ValueChanged += new EventHandler(VelocityBoundsStartValueParameter_ValueChanged); 174 VelocityBoundsStartValueParameter.Value.ValueChanged += new EventHandler(VelocityBoundsStartValueParameter_Value_ValueChanged); 175 } 176 177 void VelocityBoundsStartValueParameter_Value_ValueChanged(object sender, EventArgs e) { 178 UpdateVelocityBoundsParamater(); 179 } 180 181 void UpdateVelocityBoundsParamater() { 182 if (VelocityBoundsParameter.Value == null) { 183 VelocityBoundsParameter.Value = new DoubleMatrix(1, 2); 184 } else if (VelocityBoundsParameter.Value.Columns != 2) { 185 VelocityBoundsParameter.Value = new DoubleMatrix(VelocityBoundsParameter.Value.Rows, 2); 186 } 187 if (VelocityBoundsStartValueParameter.Value != null) { 188 DoubleMatrix matrix = VelocityBoundsParameter.Value; 189 for (int i = 0; i < matrix.Rows; i++) { 190 matrix[i, 0] = (-1) * VelocityBoundsStartValueParameter.Value.Value; 191 matrix[i, 1] = VelocityBoundsStartValueParameter.Value.Value; 192 } 193 } 194 } 195 196 void VelocityBoundsStartValueParameter_ValueChanged(object sender, EventArgs e) { 197 if (VelocityBoundsStartValueParameter.Value != null) { 198 VelocityBoundsStartValueParameter.Value.ValueChanged += new EventHandler(VelocityBoundsStartValueParameter_Value_ValueChanged); 199 } 200 UpdateVelocityBoundsParamater(); 201 } 202 160 } 203 161 private void Initialize() { 204 162 ResultsCollector = new ResultsCollector(); 205 ResultsCollector.CollectedValues.Add(Current VelocityBoundsParameter);206 ResultsCollector.CollectedValues.Add( VelocityBoundsParameter);163 ResultsCollector.CollectedValues.Add(CurrentMaxVelocityParameter); 164 ResultsCollector.CollectedValues.Add(MaxVelocityParameter); 207 165 208 166 foreach (IDiscreteDoubleValueModifier op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>()) { 209 VelocityBoundsScalingOperatorParameter.ValidValues.Add(op);210 op.ValueParameter.ActualName = VelocityBoundsScaleParameter.Name;211 op.StartValueParameter.ActualName = VelocityBoundsStartValueParameter.Name;212 op.EndValueParameter.ActualName = VelocityBoundsEndValueParameter.Name;213 op.IndexParameter.ActualName = VelocityBoundsIndexParameter.Name;214 op.StartIndexParameter.ActualName = VelocityBoundsStartIndexParameter.Name;215 op.EndIndexParameter.ActualName = VelocityBoundsEndIndexParameter.Name;216 } 217 VelocityBoundsScalingOperatorParameter.Value = null;167 MaxVelocityScalingOperatorParameter.ValidValues.Add(op); 168 op.ValueParameter.ActualName = CurrentMaxVelocityParameter.Name; 169 op.StartValueParameter.ActualName = MaxVelocityParameter.Name; 170 op.EndValueParameter.ActualName = FinalMaxVelocityParameter.Name; 171 op.IndexParameter.ActualName = MaxVelocityIndexParameter.Name; 172 op.StartIndexParameter.ActualName = MaxVelocityStartIndexParameter.Name; 173 op.EndIndexParameter.ActualName = MaxVelocityEndIndexParameter.Name; 174 } 175 MaxVelocityScalingOperatorParameter.Value = null; 218 176 } 219 177 220 178 public override IOperation Apply() { 221 179 var max = MaximizationParameter.ActualValue.Value; 180 // Update of the personal bests 222 181 var points = RealVectorParameter.ActualValue; 223 182 var qualities = QualityParameter.ActualValue; 224 183 var particles = points.Select((p, i) => new { Particle = p, Index = i }) 225 184 .Zip(qualities, (p, q) => Tuple.Create(p.Index, p.Particle, q.Value)).ToList(); 226 UpdateGlobalBest(max, particles); 185 UpdatePersonalBest(max, particles); 186 187 // SPSO: update of the neighbor bests from the personal bests 188 var personalBestPoints = PersonalBestParameter.ActualValue; 189 var personalBestQualities = PersonalBestQualityParameter.ActualValue; 190 particles = personalBestPoints.Select((p, i) => new { Particle = p, Index = i }) 191 .Zip(personalBestQualities, (p, q) => Tuple.Create(p.Index, p.Particle, q.Value)).ToList(); 227 192 UpdateNeighborBest(max, particles); 228 UpdatePersonalBest(max, particles); 229 return UpdateVelocityBounds(); 230 } 231 232 private void UpdateGlobalBest(bool maximization, IList<Tuple<int, RealVector, double>> particles) { 233 var best = maximization ? particles.MaxItems(x => x.Item3).First() : particles.MinItems(x => x.Item3).First(); 234 var bestQuality = SwarmBestQualityParameter.ActualValue; 235 if (bestQuality == null) { 236 SwarmBestQualityParameter.ActualValue = new DoubleValue(best.Item3); 237 } else bestQuality.Value = best.Item3; 238 BestRealVectorParameter.ActualValue = (RealVector)best.Item2.Clone(); 193 194 var next = new OperationCollection() { base.Apply() }; 195 next.Insert(0, ExecutionContext.CreateChildOperation(ResultsCollector)); 196 if (MaxVelocityScalingOperatorParameter.Value != null) { 197 next.Insert(0, ExecutionContext.CreateChildOperation(MaxVelocityScalingOperatorParameter.Value)); 198 } else CurrentMaxVelocityParameter.ActualValue = new DoubleValue(MaxVelocityParameter.ActualValue.Value); 199 return next; 239 200 } 240 201 … … 253 214 NeighborBestParameter.ActualValue = neighborBest; 254 215 NeighborBestQualityParameter.ActualValue = neighborBestQuality; 216 } else { 217 // Neighbor best = Global best 218 var best = maximization ? particles.MaxItems(x => x.Item3).First() : particles.MinItems(x => x.Item3).First(); 219 NeighborBestParameter.ActualValue = new ItemArray<RealVector>(particles.Select(x => best.Item2)); 220 NeighborBestQualityParameter.ActualValue = new ItemArray<DoubleValue>(particles.Select(x => new DoubleValue(best.Item3))); 255 221 } 256 222 } … … 268 234 !maximization && p.Item3 < personalBestQuality[p.Item1].Value) { 269 235 personalBestQuality[p.Item1].Value = p.Item3; 270 personalBest[p.Item1] = p.Item2;236 personalBest[p.Item1] = (RealVector)p.Item2.Clone(); 271 237 } 272 238 } 273 239 PersonalBestParameter.ActualValue = personalBest; 274 }275 276 private IOperation UpdateVelocityBounds() {277 var currentVelocityBounds = CurrentVelocityBoundsParameter.ActualValue;278 279 if (currentVelocityBounds == null) {280 currentVelocityBounds = (DoubleMatrix)VelocityBoundsParameter.ActualValue.Clone();281 CurrentVelocityBoundsParameter.ActualValue = currentVelocityBounds;282 }283 if (VelocityBoundsScalingOperatorParameter.Value == null)284 return new OperationCollection() {285 ExecutionContext.CreateChildOperation(ResultsCollector),286 base.Apply()287 };288 289 var velocityBoundsScale = VelocityBoundsScaleParameter.ActualValue;290 var velocityBoundsStartValue = VelocityBoundsStartValueParameter.ActualValue;291 292 if (velocityBoundsScale == null && velocityBoundsStartValue != null) {293 velocityBoundsScale = new DoubleValue(velocityBoundsStartValue.Value);294 VelocityBoundsScaleParameter.ActualValue = velocityBoundsScale;295 }296 for (int i = 0; i < currentVelocityBounds.Rows; i++) {297 for (int j = 0; j < currentVelocityBounds.Columns; j++) {298 if (currentVelocityBounds[i, j] >= 0) {299 currentVelocityBounds[i, j] = velocityBoundsScale.Value;300 } else {301 currentVelocityBounds[i, j] = (-1) * velocityBoundsScale.Value;302 }303 }304 }305 306 return new OperationCollection() {307 ExecutionContext.CreateChildOperation(ResultsCollector),308 ExecutionContext.CreateChildOperation(VelocityBoundsScalingOperatorParameter.Value),309 base.Apply()310 };311 240 } 312 241 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorTotallyConnectedParticleUpdater.cs
r14185 r15091 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; 24 25 using HeuristicLab.Optimization; 25 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 using HeuristicLab.PluginInfrastructure; 26 28 27 29 namespace HeuristicLab.Encodings.RealVectorEncoding { 28 30 [Item("Totally Connected Particle Updater", "Updates the particle's position using (among other things) the global best position. Use together with the empty topology initialzer. Point = Point + Velocity*Inertia + (PersonalBestPoint-Point)*Phi_P*r_p + (BestPoint-Point)*Phi_G*r_g")] 29 31 [StorableClass] 30 public sealed class RealVectorTotallyConnectedParticleUpdater : RealVectorParticleUpdater, IGlobalParticleUpdater { 32 [NonDiscoverableType] 33 [Obsolete("Same as the RealVectorNeighborhoodParticleUpdate")] 34 internal sealed class RealVectorTotallyConnectedParticleUpdater : RealVectorParticleUpdater { 31 35 32 36 #region Construction & Cloning … … 41 45 42 46 public override IOperation Apply() { 43 double inertia = Inertia.Value; 44 double personalBestAttraction = PersonalBestAttraction.Value; 45 double neighborBestAttraction = NeighborBestAttraction.Value; 46 47 RealVector velocity = new RealVector(Velocity.Length); 48 RealVector position = new RealVector(RealVector.Length); 49 double r_p = Random.NextDouble(); 50 double r_g = Random.NextDouble(); 51 52 for (int i = 0; i < velocity.Length; i++) { 53 velocity[i] = 54 Velocity[i] * inertia + 55 (PersonalBest[i] - RealVector[i]) * personalBestAttraction * r_p + 56 (BestPoint[i] - RealVector[i]) * neighborBestAttraction * r_g; 57 } 58 59 MoveParticle(velocity, position); 47 UpdateVelocity(); 48 UpdatePosition(); 60 49 61 50 return base.Apply(); -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVector.cs
r14185 r15091 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; … … 77 78 } 78 79 80 public double DotProduct(RealVector other) { 81 if (other.Length != Length) throw new ArgumentException("Vectors are of unequal length."); 82 var dotProd = 0.0; 83 for (var i = 0; i < Length; i++) 84 dotProd += this[i] * other[i]; 85 return dotProd; 86 } 79 87 } 80 88 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorEncoding.cs
r14185 r15091 280 280 particleCreator.RealVectorParameter.ActualName = Name; 281 281 particleCreator.BoundsParameter.ActualName = BoundsParameter.Name; 282 particleCreator.ProblemSizeParameter.ActualName = LengthParameter.Name;283 282 } 284 283 }
Note: See TracChangeset
for help on using the changeset viewer.