Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/21/17 11:18:40 (7 years ago)
Author:
abeham
Message:

#2797: merged revisions 15071, 15076, 15091, 15092, 15093, 15096, 15102, 15114, 15181, 15201, 15214, 15223, 15224, 15228 to stable

Location:
stable
Files:
7 edited
8 copied

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Encodings.RealVectorEncoding

  • stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorNeighborhoodParticleUpdater.cs

    r14186 r15277  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    24 using HeuristicLab.Optimization;
    2525using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     26using HeuristicLab.PluginInfrastructure;
    2627
    2728namespace HeuristicLab.Encodings.RealVectorEncoding {
    2829  [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.")]
    2930  [StorableClass]
    30   public sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater, ILocalParticleUpdater {
     31  [NonDiscoverableType]
     32  [Obsolete("Use SPSO2011ParticleUpdater")]
     33  internal sealed class RealVectorNeighborhoodParticleUpdater : RealVectorParticleUpdater {
    3134
    3235    #region Construction & Cloning
  • stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleCreator.cs

    r14186 r15277  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    2425using HeuristicLab.Data;
    2526using HeuristicLab.Operators;
     27using HeuristicLab.Optimization;
    2628using HeuristicLab.Parameters;
    2729using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     30using HeuristicLab.PluginInfrastructure;
    2831
    2932namespace HeuristicLab.Encodings.RealVectorEncoding {
    3033  [Item("RealVectorParticleCreator", "Creates a particle with position, zero velocity vector and personal best.")]
    3134  [StorableClass]
    32   public class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator {
     35  [NonDiscoverableType]
     36  [Obsolete("Use SPSOParticleCreator")]
     37  internal class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator {
    3338
    3439    #region Parameters
     
    4752    public ILookupParameter<RealVector> VelocityParameter {
    4853      get { return (ILookupParameter<RealVector>)Parameters["Velocity"]; }
     54    }
     55    public ILookupParameter<ISolutionCreator> SolutionCreatorParameter {
     56      get { return (ILookupParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
    4957    }
    5058    #endregion
     
    7078      Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution."));
    7179      Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity."));
     80      Parameters.Add(new LookupParameter<ISolutionCreator>("SolutionCreator", "The operator that creates the initial position."));
    7281
    7382      UniformRandomRealVectorCreator realVectorCreater = new UniformRandomRealVectorCreator();
     
    94103      return base.Apply();
    95104    }
    96 
    97 
     105   
     106    [StorableHook(HookType.AfterDeserialization)]
     107    private void AfterDeserialization() {
     108      if (!Parameters.ContainsKey("SolutionCreator"))
     109        Parameters.Add(new LookupParameter<ISolutionCreator>("SolutionCreator", "The operator that creates the initial position."));
     110    }
    98111  }
    99112}
  • stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.cs

    r14186 r15277  
    2727using HeuristicLab.Parameters;
    2828using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     29using HeuristicLab.PluginInfrastructure;
    2930
    3031namespace HeuristicLab.Encodings.RealVectorEncoding {
     
    3233  [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.")]
    3334  [StorableClass]
    34   public abstract class RealVectorParticleUpdater : SingleSuccessorOperator, IRealVectorParticleUpdater {
     35  [NonDiscoverableType]
     36  [Obsolete("Use SPSO2011ParticleUpdater")]
     37  internal abstract class RealVectorParticleUpdater : SingleSuccessorOperator, IRealVectorParticleUpdater {
    3538
    3639    public override bool CanChangeName {
     
    6265    public ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter {
    6366      get { return (ILookupParameter<DoubleMatrix>)Parameters["CurrentVelocityBounds"]; }
     67    }
     68    public ILookupParameter<DoubleValue> CurrentMaxVelocityParameter {
     69      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; }
    6470    }
    6571    public ILookupParameter<DoubleValue> InertiaParameter {
     
    126132      Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the position vector for the current problem."));
    127133      Parameters.Add(new LookupParameter<DoubleMatrix>("CurrentVelocityBounds", "Upper and lower bounds for the particle's velocity vector."));
     134      Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Maximum for the particle's velocity vector."));
    128135      Parameters.Add(new LookupParameter<DoubleValue>("CurrentInertia", "The weight for the particle's velocity vector."));
    129136      Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestAttraction", "The weight for the particle's personal best position."));
     
    131138    }
    132139    #endregion
     140   
     141    [StorableHook(HookType.AfterDeserialization)]
     142    private void AfterDeserialization() {
     143      if (!Parameters.ContainsKey("CurrentMaxVelocity"))
     144        Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Maximum for the particle's velocity vector."));
     145    }
    133146
    134147    protected void MoveParticle(RealVector velocity, RealVector position) {
  • stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs

    r14186 r15277  
    3535  [Item("RealVectorSwarmUpdater", "Updates personal best point and quality as well as global best point and quality.")]
    3636  [StorableClass]
    37   public sealed class RealVectorSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater, ISingleObjectiveOperator {
     37  [NonDiscoverableType]
     38  [Obsolete("Use SPSOSwarmUpdater")]
     39  internal sealed class RealVectorSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater, ISingleObjectiveOperator {
    3840
    3941    [Storable]
  • stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorTotallyConnectedParticleUpdater.cs

    r14186 r15277  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
    24 using HeuristicLab.Optimization;
    2525using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     26using HeuristicLab.PluginInfrastructure;
    2627
    2728namespace HeuristicLab.Encodings.RealVectorEncoding {
    2829  [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")]
    2930  [StorableClass]
    30   public sealed class RealVectorTotallyConnectedParticleUpdater : RealVectorParticleUpdater, IGlobalParticleUpdater {
     31  [NonDiscoverableType]
     32  [Obsolete("Use SPSO2011ParticleUpdater")]
     33  internal sealed class RealVectorTotallyConnectedParticleUpdater : RealVectorParticleUpdater {
    3134
    3235    #region Construction & Cloning
  • stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSO2007ParticleUpdater.cs

    r15096 r15277  
    2424using HeuristicLab.Core;
    2525using HeuristicLab.Data;
     26using HeuristicLab.Parameters;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
     
    2930  [Item("SPSO 2007 Particle Updater", "Updates the particle's position according to the formulae described in SPSO 2007.")]
    3031  [StorableClass]
    31   public sealed class SPSO2007ParticleUpdater : RealVectorParticleUpdater {
     32  public sealed class SPSO2007ParticleUpdater : SPSOParticleUpdater {
    3233
    3334    #region Construction & Cloning
     
    4142    #endregion
    4243   
    43     public static void UpdateVelocity(IRandom random, RealVector velocity, double maxVelocity, RealVector position, double inertia, RealVector personalBest, double personalBestAttraction, RealVector neighborBest, double neighborBestAttraction) {
     44    public static void UpdateVelocity(IRandom random, RealVector velocity, RealVector position, RealVector personalBest, RealVector neighborBest, double inertia = 0.721, double personalBestAttraction = 1.193, double neighborBestAttraction = 1.193, double maxVelocity = double.MaxValue) {
    4445      for (int i = 0; i < velocity.Length; i++) {
    45         double r_p = random.NextDouble() * 1.193;
    46         double r_g = random.NextDouble() * 1.193;
     46        double r_p = random.NextDouble();
     47        double r_g = random.NextDouble();
    4748        velocity[i] =
    4849          velocity[i] * inertia +
     
    6970        if (position[i] < min) {
    7071          position[i] = min;
    71           velocity[i] = 0;
     72          velocity[i] = 0; // SPSO 2007
    7273        }
    7374        if (position[i] > max) {
    7475          position[i] = max;
    75           velocity[i] = 0;
     76          velocity[i] = 0; // SPSO 2007
    7677        }
    7778      }
     
    9091      var neighborBest = NeighborBestParameter.ActualValue;
    9192      var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value;
    92      
    93       UpdateVelocity(random, velocity, maxVelocity, position, inertia, personalBest, personalBestAttraction, neighborBest, neighborBestAttraction);
     93
     94      UpdateVelocity(random, velocity, position, personalBest, neighborBest, inertia, personalBestAttraction, neighborBestAttraction, maxVelocity);
    9495      UpdatePosition(bounds, velocity, position);
    9596
  • stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSO2011ParticleUpdater.cs

    r15096 r15277  
    2525using HeuristicLab.Data;
    2626using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     27using HeuristicLab.Random;
    2728
    2829namespace HeuristicLab.Encodings.RealVectorEncoding {
    2930  [Item("SPSO 2011 Particle Updater", "Updates the particle's position according to the formulae described in SPSO 2011.")]
    3031  [StorableClass]
    31   public sealed class SPSO2011ParticleUpdater : RealVectorParticleUpdater {
    32 
     32  public sealed class SPSO2011ParticleUpdater : SPSOParticleUpdater {
    3333    #region Construction & Cloning
    3434    [StorableConstructor]
     
    4141    #endregion
    4242   
    43     public static void UpdateVelocity(IRandom random, RealVector velocity, double maxVelocity, RealVector position, double inertia, RealVector personalBest, double personalBestAttraction, RealVector neighborBest, double neighborBestAttraction) {
     43    public static void UpdateVelocity(IRandom random, RealVector velocity, RealVector position, RealVector personalBest, RealVector neighborBest, double inertia = 0.721, double personalBestAttraction = 1.193, double neighborBestAttraction = 1.193, double maxVelocity = double.MaxValue) {
    4444      var gravity = new double[velocity.Length];
    45       var direct = new RealVector(velocity.Length);
     45      var direction = new RealVector(velocity.Length);
    4646      var radius = 0.0;
    4747
     48      var nd = new NormalDistributedRandom(random, 0, 1);
     49
    4850      for (int i = 0; i < velocity.Length; i++) {
    49         var g_id = 1.193 * ((personalBest[i] + neighborBest[i] - 2 * position[i]) / 3.0);
     51        var g_id = (personalBestAttraction * personalBest[i]
     52          + neighborBestAttraction * neighborBest[i]
     53          - position[i] * (neighborBestAttraction + personalBestAttraction)) / 3.0;
     54        // center of the hyper-sphere
    5055        gravity[i] = g_id + position[i];
    51         direct[i] = (random.NextDouble() - 0.5) * 2;
     56        // a random direction vector uniform over the surface of hyper-sphere, see http://mathworld.wolfram.com/HyperspherePointPicking.html
     57        direction[i] = nd.NextDouble();
    5258        radius += g_id * g_id;
    5359      }
    5460
     61      // randomly choose a radius within the hyper-sphere
    5562      radius = random.NextDouble() * Math.Sqrt(radius);
    5663
    57       var unitscale = Math.Sqrt(direct.DotProduct(direct));
     64      // unitscale is used to rescale the random direction vector to unit length, resp. length of the radius
     65      var unitscale = Math.Sqrt(direction.DotProduct(direction));
    5866      if (unitscale > 0) {
    5967        for (var i = 0; i < velocity.Length; i++) {
    60           velocity[i] = velocity[i] * inertia + gravity[i] + direct[i] * radius / unitscale - position[i];
     68          var sampledPos = gravity[i] + direction[i] * radius / unitscale;
     69          velocity[i] = velocity[i] * inertia + sampledPos - position[i];
    6170        }
    6271      }
     
    101110      var neighborBest = NeighborBestParameter.ActualValue;
    102111      var neighborBestAttraction = NeighborBestAttractionParameter.ActualValue.Value;
    103      
    104       UpdateVelocity(random, velocity, maxVelocity, position, inertia, personalBest, personalBestAttraction, neighborBest, neighborBestAttraction);
     112
     113      UpdateVelocity(random, velocity, position, personalBest, neighborBest, inertia, personalBestAttraction, neighborBestAttraction, maxVelocity);
    105114      UpdatePosition(bounds, velocity, position);
    106115
  • stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSOParticleUpdater.cs

    r15102 r15277  
    7171      get { return (ILookupParameter<DoubleValue>)Parameters["NeighborBestAttraction"]; }
    7272    }
    73 
    74     public IValueLookupParameter<DoubleValue> MaxBeyondBestParameter {
    75       get { return (IValueLookupParameter<DoubleValue>)Parameters["MaxBeyondBest"]; }
    76     }
    7773    #endregion
    7874
     
    9389      Parameters.Add(new LookupParameter<DoubleValue>("PersonalBestAttraction", "The weight for the particle's personal best position."));
    9490      Parameters.Add(new LookupParameter<DoubleValue>("NeighborBestAttraction", "The weight for the global best position."));
    95       Parameters.Add(new ValueLookupParameter<DoubleValue>("MaxBeyondBest", "A factor of how much the velocity update may maximally aim beyond the personal and neighbor best.", new DoubleValue(1.193)));
    9691    }
    9792    #endregion
  • stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/SPSOSwarmUpdater.cs

    r15102 r15277  
    3434
    3535namespace HeuristicLab.Encodings.RealVectorEncoding {
    36   [Item("Swarm Updater (SPSO)", "Updates personal best point and quality as well as global best point and quality.")]
     36  [Item("Swarm Updater (SPSO)", "Updates personal best point and quality as well as neighbor best point and quality.")]
    3737  [StorableClass]
    3838  public sealed class SPSOSwarmUpdater : SingleSuccessorOperator, IRealVectorSwarmUpdater, ISingleObjectiveOperator {
     
    126126      Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle."));
    127127      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
    128       Parameters.Add(new ValueLookupParameter<DoubleValue>("MaxVelocity", "Speed limit for each particle.", new DoubleValue(1000000)));
     128      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaxVelocity", "The maximum velocity for each particle and initial velocity if scaling is used.", new DoubleValue(double.MaxValue)));
    129129      Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Current value of the speed limit."));
    130130      Parameters.Add(new LookupParameter<ResultCollection>("Results", "Results"));
     
    132132      #region Max Velocity Updating
    133133      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)));
     134      Parameters.Add(new ValueLookupParameter<DoubleValue>("FinalMaxVelocity", "The value of maximum velocity if scaling is used and PSO has reached maximum iterations.", new DoubleValue(1E-10)));
    135135      Parameters.Add(new LookupParameter<IntValue>("MaxVelocityIndex", "The current index.", "Iterations"));
    136136      Parameters.Add(new ValueLookupParameter<IntValue>("MaxVelocityStartIndex", "The start index at which to start modifying 'Value'.", new IntValue(0)));
     
    195195        var neighborBest = new ItemArray<RealVector>(neighbors.Length);
    196196        var neighborBestQuality = new ItemArray<DoubleValue>(neighbors.Length);
     197        double overallBest = double.NaN;
     198        RealVector overallBestVector = null;
    197199        for (int n = 0; n < neighbors.Length; n++) {
    198           var pairs = particles.Where(x => x.Item1 == n || neighbors[n].Contains(x.Item1));
    199           var bestNeighbor = (maximization ? pairs.MaxItems(p => p.Item3)
    200                                            : pairs.MinItems(p => p.Item3)).First();
     200          var neighborhood = particles.Where(x => neighbors[n].Contains(x.Item1));
     201          var bestNeighbor = (maximization ? neighborhood.MaxItems(p => p.Item3)
     202                                           : neighborhood.MinItems(p => p.Item3)).First();
    201203          neighborBest[n] = bestNeighbor.Item2;
    202204          neighborBestQuality[n] = new DoubleValue(bestNeighbor.Item3);
     205          if (double.IsNaN(overallBest) || maximization && bestNeighbor.Item3 > overallBest
     206            || !maximization && bestNeighbor.Item3 < overallBest) {
     207            overallBest = bestNeighbor.Item3;
     208            overallBestVector = bestNeighbor.Item2;
     209          }
    203210        }
    204211        NeighborBestParameter.ActualValue = neighborBest;
    205212        NeighborBestQualityParameter.ActualValue = neighborBestQuality;
     213        SwarmBestQualityParameter.ActualValue = new DoubleValue(overallBest);
     214        BestRealVectorParameter.ActualValue = overallBestVector;
    206215      } else {
    207216        // Neighbor best = Global best
    208217        var best = maximization ? particles.MaxItems(x => x.Item3).First() : particles.MinItems(x => x.Item3).First();
    209         NeighborBestParameter.ActualValue = new ItemArray<RealVector>(particles.Select(x => best.Item2));
    210         NeighborBestQualityParameter.ActualValue = new ItemArray<DoubleValue>(particles.Select(x => new DoubleValue(best.Item3)));
     218        NeighborBestParameter.ActualValue = new ItemArray<RealVector>(Enumerable.Repeat(best.Item2, particles.Count));
     219        NeighborBestQualityParameter.ActualValue = new ItemArray<DoubleValue>(Enumerable.Repeat(new DoubleValue(best.Item3), particles.Count));
     220        SwarmBestQualityParameter.ActualValue = new DoubleValue(best.Item3);
     221        BestRealVectorParameter.ActualValue = best.Item2;
    211222      }
    212223    }
     
    224235          !maximization && p.Item3 < personalBestQuality[p.Item1].Value) {
    225236          personalBestQuality[p.Item1].Value = p.Item3;
    226           personalBest[p.Item1] = (RealVector)p.Item2.Clone();
     237          personalBest[p.Item1] = new RealVector(p.Item2);
    227238        }
    228239      }
Note: See TracChangeset for help on using the changeset viewer.