Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/24/10 10:01:26 (15 years ago)
Author:
svonolfe
Message:

Ported ported permutation operators to HL 3.3 and added test cases (#889)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Permutation/3.3/ScrambleManipulator.cs

    r1530 r2854  
    11#region License Information
    22/* HeuristicLab
    3  * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
     3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
    44 *
    55 * This file is part of HeuristicLab.
     
    2424using System.Text;
    2525using HeuristicLab.Core;
     26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2627
    2728namespace HeuristicLab.Permutation {
     
    2930  /// Manipulates a permutation array by randomly scrambling the elements in a randomly chosen interval.
    3031  /// </summary>
    31   public class ScrambleManipulator : PermutationManipulatorBase {
    32     /// <inheritdoc select="summary"/>
    33     public override string Description {
    34       get { return @"TODO\r\nOperator description still missing ..."; }
    35     }
    36 
     32  /// <remarks>
     33  /// Implemented as described in Syswerda, G. (1991). Schedule Optimization Using Genetic Algorithms,
     34  /// in Davis, L. (Ed.) Handbook of Genetic Algorithms, Van Nostrand Reinhold, New York, pp 332-349.
     35  /// </remarks>
     36  [Item("ScrambleManipulator", "An operator which manipulates a permutation array by randomly scrambling the elements in a randomly chosen interval.")]
     37  [EmptyStorableClass]
     38  [Creatable("Test")]
     39  public class ScrambleManipulator : PermutationManipulator {
    3740    /// <summary>
    3841    /// Mixes the elements of the given <paramref name="permutation"/> randomly
     
    4043    /// </summary>
    4144    /// <param name="random">The random number generator.</param>
    42     /// <param name="permutation">The permutation array to manipulate.</param>
    43     /// <returns>The new permuation array with the manipulated data.</returns>
    44     public static int[] Apply(IRandom random, int[] permutation) {
    45       int[] result = (int[])permutation.Clone();
     45    /// <param name="permutation">The permutation to manipulate.</param>
     46    public static void Apply(IRandom random, Permutation permutation) {
     47      Permutation original = (Permutation)permutation.Clone();
     48
    4649      int breakPoint1, breakPoint2;
    4750      int[] scrambledIndices, remainingIndices, temp;
    4851      int selectedIndex, index;
    4952
    50       breakPoint1 = random.Next(permutation.Length - 1);
    51       breakPoint2 = random.Next(breakPoint1 + 1, permutation.Length);
     53      breakPoint1 = random.Next(original.Length - 1);
     54      breakPoint2 = random.Next(breakPoint1 + 1, original.Length);
    5255
    5356      scrambledIndices = new int[breakPoint2 - breakPoint1 + 1];
     
    7376
    7477      for (int i = 0; i <= (breakPoint2 - breakPoint1); i++) {  // scramble permutation between breakpoints
    75         result[breakPoint1 + i] = permutation[breakPoint1 + scrambledIndices[i]];
     78        permutation[breakPoint1 + i] = original[breakPoint1 + scrambledIndices[i]];
    7679      }
    77       return result;
    7880    }
    7981
     
    8385    /// </summary>
    8486    /// <remarks>Calls <see cref="Apply"/>.</remarks>
    85     /// <param name="scope">The current scope.</param>
    86     /// <param name="random">The random number generator.</param>
    87     /// <param name="permutation">The permutation array to manipulate.</param>
    88     /// <returns>The new permuation array with the manipulated data.</returns>
    89     protected override int[] Manipulate(IScope scope, IRandom random, int[] permutation) {
    90       return Apply(random, permutation);
     87    /// <param name="random">A random number generator.</param>
     88    /// <param name="permutation">The permutation to manipulate.</param>
     89    protected override void Manipulate(IRandom random, Permutation permutation) {
     90      Apply(random, permutation);
    9191    }
    9292  }
Note: See TracChangeset for help on using the changeset viewer.