#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Encodings.RealVectorEncoding {
[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.")]
[StorableClass]
public abstract class RealVectorParticleUpdater : SingleSuccessorOperator, IRealVectorParticleUpdater {
public override bool CanChangeName {
get { return false; }
}
#region Parameter properties
public ILookupParameter RandomParameter {
get { return (ILookupParameter)Parameters["Random"]; }
}
public ILookupParameter VelocityParameter {
get { return (ILookupParameter)Parameters["Velocity"]; }
}
public ILookupParameter PersonalBestParameter {
get { return (ILookupParameter)Parameters["PersonalBest"]; }
}
public ILookupParameter NeighborBestParameter {
get { return (ILookupParameter)Parameters["NeighborBest"]; }
}
public LookupParameter BestRealVectorParameter {
get { return (LookupParameter)Parameters["BestRealVector"]; }
}
public ILookupParameter RealVectorParameter {
get { return (ILookupParameter)Parameters["RealVector"]; }
}
public ILookupParameter BoundsParameter {
get { return (ILookupParameter)Parameters["Bounds"]; }
}
public ILookupParameter CurrentVelocityBoundsParameter {
get { return (ILookupParameter)Parameters["CurrentVelocityBounds"]; }
}
public ILookupParameter InertiaParameter {
get { return (ILookupParameter)Parameters["CurrentInertia"]; }
}
public ILookupParameter PersonalBestAttractionParameter {
get { return (ILookupParameter)Parameters["PersonalBestAttraction"]; }
}
public ILookupParameter NeighborBestAttractionParameter {
get { return (ILookupParameter)Parameters["NeighborBestAttraction"]; }
}
#endregion
#region Parameter Values
protected IRandom Random {
get { return RandomParameter.ActualValue; }
}
protected RealVector Velocity {
get { return VelocityParameter.ActualValue; }
set { VelocityParameter.ActualValue = value; }
}
protected RealVector PersonalBest {
get { return PersonalBestParameter.ActualValue; }
}
protected RealVector BestPoint {
get { return BestRealVectorParameter.ActualValue; }
}
protected RealVector RealVector {
get { return RealVectorParameter.ActualValue; }
set { RealVectorParameter.ActualValue = value; }
}
protected RealVector NeighborBest {
get { return NeighborBestParameter.ActualValue; }
}
protected DoubleMatrix Bounds {
get { return BoundsParameter.ActualValue; }
}
protected DoubleMatrix CurrentVelocityBounds {
get { return CurrentVelocityBoundsParameter.ActualValue; }
}
protected DoubleValue Inertia {
get { return InertiaParameter.ActualValue; }
}
protected DoubleValue PersonalBestAttraction {
get { return PersonalBestAttractionParameter.ActualValue; }
}
protected DoubleValue NeighborBestAttraction {
get { return NeighborBestAttractionParameter.ActualValue; }
}
#endregion
#region Construction & Cloning
[StorableConstructor]
protected RealVectorParticleUpdater(bool deserializing) : base(deserializing) { }
protected RealVectorParticleUpdater(RealVectorParticleUpdater original, Cloner cloner) : base(original, cloner) { }
public RealVectorParticleUpdater()
: base() {
Parameters.Add(new LookupParameter("Random", "Random number generator."));
Parameters.Add(new LookupParameter("RealVector", "Particle's current solution"));
Parameters.Add(new LookupParameter("Velocity", "Particle's current velocity."));
Parameters.Add(new LookupParameter("PersonalBest", "Particle's personal best solution."));
Parameters.Add(new LookupParameter("BestRealVector", "Global best position."));
Parameters.Add(new LookupParameter("NeighborBest", "Best neighboring solution."));
Parameters.Add(new LookupParameter("Bounds", "The lower and upper bounds for each dimension of the position vector for the current problem."));
Parameters.Add(new LookupParameter("CurrentVelocityBounds", "Upper and lower bounds for the particle's velocity vector."));
Parameters.Add(new LookupParameter("CurrentInertia", "The weight for the particle's velocity vector."));
Parameters.Add(new LookupParameter("PersonalBestAttraction", "The weight for the particle's personal best position."));
Parameters.Add(new LookupParameter("NeighborBestAttraction", "The weight for the global best position."));
}
#endregion
protected void MoveParticle(RealVector velocity, RealVector position) {
BoundsChecker.Apply(velocity, CurrentVelocityBounds);
for (int i = 0; i < velocity.Length; i++) {
position[i] = RealVector[i] + velocity[i];
}
for (int i = 0; i < position.Length; i++) {
double min = Bounds[i % Bounds.Rows, 0];
double max = Bounds[i % Bounds.Rows, 1];
if (position[i] < min) {
int reflectionCount = (int)Math.Truncate((min - position[i]) / (max - min)) + 1;
double reflection = (min - position[i]) % (max - min);
if (IsOdd(reflectionCount)) {
position[i] = min + reflection;
velocity[i] = -velocity[i];
} else {
position[i] = max - reflection;
}
}
if (position[i] > max) {
int reflectionCount = (int)Math.Truncate((position[i] - max) / (max - min)) + 1;
double reflection = (position[i] - max) % (max - min);
if (IsOdd(reflectionCount)) {
position[i] = max - reflection;
velocity[i] = -velocity[i];
} else {
position[i] = min + reflection;
}
}
}
RealVector = position;
Velocity = velocity;
}
private static bool IsOdd(int number) {
return number % 2 == 1;
}
}
}