Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/30/11 16:45:28 (14 years ago)
Author:
epitzer
Message:

Make particles reflect from the bounds to prevent them from becoming stuck. (#852)

Location:
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorNeighborhoodParticleUpdater.cs

    r5866 r5892  
    5757      }
    5858
    59       BoundsChecker.Apply(velocity, CurrentVelocityBounds);
    60       for (int i = 0; i < velocity.Length; i++) {
    61         position[i] = RealVector[i] + velocity[i];
    62       }
    63       BoundsChecker.Apply(position, Bounds);
    64 
    65       RealVector = position;
    66       Velocity = velocity;
     59      MoveParticle(velocity, position);
    6760
    6861      return base.Apply();
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleUpdater.cs

    r5866 r5892  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    130131    }
    131132    #endregion
     133
     134    protected void MoveParticle(RealVector velocity, RealVector position) {
     135      BoundsChecker.Apply(velocity, CurrentVelocityBounds);
     136      for (int i = 0; i < velocity.Length; i++) {
     137        position[i] = RealVector[i] + velocity[i];
     138      }
     139      for (int i = 0; i < position.Length; i++) {
     140        double min = Bounds[i % Bounds.Rows, 0];
     141        double max = Bounds[i % Bounds.Rows, 1];
     142        if (position[i] < min) {
     143          int reflectionCount = (int)Math.Truncate((min - position[i]) / (max - min)) + 1;
     144          double reflection = (min - position[i]) % (max - min);
     145          if (IsOdd(reflectionCount)) {
     146            position[i] = min + reflection;
     147            velocity[i] = -velocity[i];
     148
     149          } else {
     150            position[i] = max - reflection;
     151          }
     152        }
     153        if (position[i] > max) {
     154          int reflectionCount = (int)Math.Truncate((position[i] - max) / (max - min)) + 1;
     155          double reflection = (position[i] - max) % (max - min);
     156          if (IsOdd(reflectionCount)) {
     157            position[i] = max - reflection;
     158            velocity[i] = -velocity[i];
     159          } else {
     160            position[i] = min + reflection;
     161          }
     162        }
     163      }
     164
     165      RealVector = position;
     166      Velocity = velocity;
     167    }
     168
     169    private static bool IsOdd(int number) {
     170      return number % 2 == 1;
     171    }
    132172  }
    133173}
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorTotallyConnectedParticleUpdater.cs

    r5866 r5892  
    5757      }
    5858
    59       BoundsChecker.Apply(velocity, CurrentVelocityBounds);
    60       for (int i = 0; i < velocity.Length; i++) {
    61         position[i] = RealVector[i] + velocity[i];
    62       }
    63       BoundsChecker.Apply(position, Bounds);
    64 
    65       RealVector = position;
    66       Velocity = velocity;
     59      MoveParticle(velocity, position);
    6760
    6861      return base.Apply();
Note: See TracChangeset for help on using the changeset viewer.