Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/29/17 15:26:16 (7 years ago)
Author:
abeham
Message:

#2797:

  • Updated PSO to make it more compatible with SPSO 2011
  • Removed truncation of velocity vector and instead rescaled it given the maximum velocity
  • Added non-zero initial velocity according to SPSO 2011
  • Removed complicated bouncing code due to box constraints and instead implemented as described in SPSO 2011
  • Calculating neighbor best has been changed to use personal best
  • Avoiding local and global particle update and instead relying on neighborbest
  • More randomization during velocity update by using a separate random numbers per dimension
  • Reusing problem specific solution creator in RealVectorParticleCreator instead of always using UniformRandomRealVectorCreator
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  
    132132    <Compile Include="ParticleOperators\RealVectorSwarmUpdater.cs" />
    133133    <Compile Include="ParticleOperators\RealVectorTotallyConnectedParticleUpdater.cs" />
     134    <Compile Include="ParticleOperators\RealVectorVelocityInitializer.cs" />
    134135    <Compile Include="Plugin.cs" />
    135136    <Compile Include="RealVectorCreator.cs" />
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Interfaces/IRealVectorParticleCreator.cs

    r14185 r15091  
    2626namespace HeuristicLab.Encodings.RealVectorEncoding {
    2727  public interface IRealVectorParticleCreator : IParticleCreator, IRealVectorOperator {
    28     ILookupParameter<IntValue> ProblemSizeParameter { get; }
    2928    ILookupParameter<RealVector> RealVectorParameter { get; }
    3029    ILookupParameter<RealVector> PersonalBestParameter { get; }
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Interfaces/IRealVectorParticleUpdater.cs

    r14185 r15091  
    2727  public interface IRealVectorParticleUpdater : IParticleUpdater, IRealVectorOperator {
    2828    ILookupParameter<RealVector> VelocityParameter { get; }
    29     ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter { get; }
     29    ILookupParameter<DoubleValue> CurrentMaxVelocityParameter { get; }
    3030    ILookupParameter<RealVector> RealVectorParameter { get; }
    3131    ILookupParameter<DoubleMatrix> BoundsParameter { get; }
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorNeighborhoodParticleUpdater.cs

    r14185 r15091  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Optimization;
    2524using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2625
     
    2827  [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.")]
    2928  [StorableClass]
    30   public sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater, ILocalParticleUpdater {
     29  public sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater {
    3130
    3231    #region Construction & Cloning
     
    4140
    4241    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();
    6044
    6145      return base.Apply();
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleCreator.cs

    r14185 r15091  
    2424using HeuristicLab.Data;
    2525using HeuristicLab.Operators;
     26using HeuristicLab.Optimization;
    2627using HeuristicLab.Parameters;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3031  [Item("RealVectorParticleCreator", "Creates a particle with position, zero velocity vector and personal best.")]
    3132  [StorableClass]
    32   public class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator {
     33  public class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator, IStochasticOperator {
    3334
    3435    #region Parameters
    35     public ILookupParameter<IntValue> ProblemSizeParameter {
    36       get { return (ILookupParameter<IntValue>)Parameters["ProblemSize"]; }
     36    public ILookupParameter<IRandom> RandomParameter {
     37      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
    3738    }
    3839    public IValueLookupParameter<DoubleMatrix> BoundsParameter {
     
    4849      get { return (ILookupParameter<RealVector>)Parameters["Velocity"]; }
    4950    }
    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"]; }
    5853    }
    5954    #endregion
    60 
     55   
    6156    #region Construction & Cloning
    6257    [StorableConstructor]
     
    6560    public RealVectorParticleCreator()
    6661      : base() {
    67       Parameters.Add(new LookupParameter<IntValue>("ProblemSize", "The dimension of the problem."));
     62      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    6863      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension."));
    6964      Parameters.Add(new LookupParameter<RealVector>("RealVector", "Particle's current solution"));
    7065      Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution."));
    7166      Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity."));
     67      Parameters.Add(new LookupParameter<ISolutionCreator>("SolutionCreator", "The operator that creates the initial position."));
    7268
    73       UniformRandomRealVectorCreator realVectorCreater = new UniformRandomRealVectorCreator();
     69      Placeholder realVectorCreater = new Placeholder();
    7470      Assigner personalBestPositionAssigner = new Assigner();
     71      RealVectorVelocityInitializer velocityInitializer = new RealVectorVelocityInitializer();
    7572
    7673      OperatorGraph.InitialOperator = realVectorCreater;
    7774
    78       realVectorCreater.RealVectorParameter.ActualName = RealVectorParameter.Name;
    79       realVectorCreater.LengthParameter.ActualName = ProblemSizeParameter.Name;
    80       realVectorCreater.BoundsParameter.ActualName = BoundsParameter.Name;
     75      realVectorCreater.OperatorParameter.ActualName = SolutionCreatorParameter.Name;
    8176      realVectorCreater.Successor = personalBestPositionAssigner;
    8277
    8378      personalBestPositionAssigner.LeftSideParameter.ActualName = PersonalBestParameter.Name;
    8479      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;
    8687    }
    8788    public override IDeepCloneable Clone(Cloner cloner) {
     
    8990    }
    9091    #endregion
    91 
    92     public override IOperation Apply() {
    93       Velocity = new RealVector(ProblemSize);
    94       return base.Apply();
    95     }
    96 
    97 
    9892  }
    9993}
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.cs

    r14185 r15091  
    5151      get { return (ILookupParameter<RealVector>)Parameters["NeighborBest"]; }
    5252    }
    53     public LookupParameter<RealVector> BestRealVectorParameter {
    54       get { return (LookupParameter<RealVector>)Parameters["BestRealVector"]; }
    55     }
    5653    public ILookupParameter<RealVector> RealVectorParameter {
    5754      get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
     
    6057      get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
    6158    }
    62     public ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter {
    63       get { return (ILookupParameter<DoubleMatrix>)Parameters["CurrentVelocityBounds"]; }
     59    public ILookupParameter<DoubleValue> CurrentMaxVelocityParameter {
     60      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; }
    6461    }
    6562    public ILookupParameter<DoubleValue> InertiaParameter {
     
    7370    }
    7471    #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   
    11473    #region Construction & Cloning
    11574    [StorableConstructor]
     
    12281      Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity."));
    12382      Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution."));
    124       Parameters.Add(new LookupParameter<RealVector>("BestRealVector", "Global best position."));
    12583      Parameters.Add(new LookupParameter<RealVector>("NeighborBest", "Best neighboring solution."));
    12684      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."));
     85      Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Maximum for the particle's velocity vector."));
    12886      Parameters.Add(new LookupParameter<DoubleValue>("CurrentInertia", "The weight for the particle's velocity vector."));
    12987      Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestAttraction", "The weight for the particle's personal best position."));
     
    13290    #endregion
    13391
    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
    136103      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;
    138110      }
     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;
    139130      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];
    142133        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
    152136        }
    153137        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
    162140        }
    163141      }
    164 
    165       RealVector = position;
    166       Velocity = velocity;
    167     }
    168 
    169     private static bool IsOdd(int number) {
    170       return number % 2 == 1;
    171142    }
    172143  }
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs

    r15071 r15091  
    7676      get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; }
    7777    }
    78     public IValueLookupParameter<DoubleMatrix> VelocityBoundsParameter {
    79       get { return (ValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
    80     }
    81     public ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter {
    82       get { return (ILookupParameter<DoubleMatrix>)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"]; }
    8383    }
    8484    public LookupParameter<ResultCollection> ResultsParameter {
     
    8686    }
    8787
    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"]; }
    109103    }
    110104    #endregion
     
    132126      Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle."));
    133127      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
    134       Parameters.Add(new ValueLookupParameter<DoubleMatrix>("VelocityBounds", "Maximum velocity for each dimension.", new DoubleMatrix(new double[,] { { -1, 1 } })));
    135       Parameters.Add(new LookupParameter<DoubleMatrix>("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."));
    136130      Parameters.Add(new LookupParameter<ResultCollection>("Results", "Results"));
    137131
    138132      #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;
    148140      #endregion
    149141
    150142      Initialize();
    151       RegisterEvents();
    152143    }
    153144
     
    167158        Parameters.Remove("BestQuality");
    168159      }
    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    }
    203161    private void Initialize() {
    204162      ResultsCollector = new ResultsCollector();
    205       ResultsCollector.CollectedValues.Add(CurrentVelocityBoundsParameter);
    206       ResultsCollector.CollectedValues.Add(VelocityBoundsParameter);
     163      ResultsCollector.CollectedValues.Add(CurrentMaxVelocityParameter);
     164      ResultsCollector.CollectedValues.Add(MaxVelocityParameter);
    207165
    208166      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;
    218176    }
    219177
    220178    public override IOperation Apply() {
    221179      var max = MaximizationParameter.ActualValue.Value;
     180      // Update of the personal bests
    222181      var points = RealVectorParameter.ActualValue;
    223182      var qualities = QualityParameter.ActualValue;
    224183      var particles = points.Select((p, i) => new { Particle = p, Index = i })
    225184        .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();
    227192      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;
    239200    }
    240201
     
    253214        NeighborBestParameter.ActualValue = neighborBest;
    254215        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)));
    255221      }
    256222    }
     
    268234          !maximization && p.Item3 < personalBestQuality[p.Item1].Value) {
    269235          personalBestQuality[p.Item1].Value = p.Item3;
    270           personalBest[p.Item1] = p.Item2;
     236          personalBest[p.Item1] = (RealVector)p.Item2.Clone();
    271237        }
    272238      }
    273239      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       };
    311240    }
    312241  }
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorTotallyConnectedParticleUpdater.cs

    r14185 r15091  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    2425using HeuristicLab.Optimization;
    2526using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     27using HeuristicLab.PluginInfrastructure;
    2628
    2729namespace HeuristicLab.Encodings.RealVectorEncoding {
    2830  [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")]
    2931  [StorableClass]
    30   public sealed class RealVectorTotallyConnectedParticleUpdater : RealVectorParticleUpdater, IGlobalParticleUpdater {
     32  [NonDiscoverableType]
     33  [Obsolete("Same as the RealVectorNeighborhoodParticleUpdate")]
     34  internal sealed class RealVectorTotallyConnectedParticleUpdater : RealVectorParticleUpdater {
    3135
    3236    #region Construction & Cloning
     
    4145
    4246    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();
    6049
    6150      return base.Apply();
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVector.cs

    r14185 r15091  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    7778    }
    7879
     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    }
    7987  }
    8088}
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorEncoding.cs

    r14185 r15091  
    280280        particleCreator.RealVectorParameter.ActualName = Name;
    281281        particleCreator.BoundsParameter.ActualName = BoundsParameter.Name;
    282         particleCreator.ProblemSizeParameter.ActualName = LengthParameter.Name;
    283282      }
    284283    }
Note: See TracChangeset for help on using the changeset viewer.