Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/14/10 05:31:49 (14 years ago)
Author:
swagner
Message:

Operator architecture refactoring (#95)

  • worked on operators
File:
1 moved

Legend:

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

    r2792 r2794  
    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.
     
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    24 using System.Text;
    2522using HeuristicLab.Core;
    2623using HeuristicLab.Data;
     24using HeuristicLab.Operators;
     25using HeuristicLab.Parameters;
     26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2727
    2828namespace HeuristicLab.Permutation {
    2929  /// <summary>
    30   /// Generates a randomly shuffled permuation.
     30  /// An operator which creates a new random permutation of integer values.
    3131  /// </summary>
    32   public class RandomPermutationGenerator : OperatorBase {
    33     /// <inheritdoc select="summary"/>
    34     public override string Description {
    35       get { return @"TODO\r\nOperator description still missing ..."; }
     32  [Item("RandomPermutationCreator", "An operator which creates a new random permutation of integer values.")]
     33  [EmptyStorableClass]
     34  [Creatable("Test")]
     35  public sealed class RandomPermutationCreator : SingleSuccessorOperator {
     36    public LookupParameter<IRandom> RandomParameter {
     37      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
     38    }
     39    public ValueLookupParameter<IntData> LengthParameter {
     40      get { return (ValueLookupParameter<IntData>)Parameters["Length"]; }
     41    }
     42    public LookupParameter<Permutation> PermutationParameter {
     43      get { return (LookupParameter<Permutation>)Parameters["Permutation"]; }
    3644    }
    3745
    38     /// <summary>
    39     /// Initializes a new instance of <see cref="RandomPermutationGenerator"/> with three variable infos
    40     /// (<c>Length</c>, <c>Random</c> and <c>Permutation</c>).
    41     /// </summary>
    42     public RandomPermutationGenerator()
     46    public RandomPermutationCreator()
    4347      : base() {
    44       AddVariableInfo(new VariableInfo("Length", "Permutation length", typeof(IntData), VariableKind.In));
    45       AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
    46       AddVariableInfo(new VariableInfo("Permutation", "Created random permutation", typeof(Permutation), VariableKind.New));
     48      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used to initialize the new random permutation."));
     49      Parameters.Add(new ValueLookupParameter<IntData>("Length", "The length of the new random permutation."));
     50      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The new random permutation."));
    4751    }
    4852
    49     /// <summary>
    50     /// Generates a randomly shuffled permutation.
    51     /// </summary>
    52     /// <param name="random">The random number generator.</param>
    53     /// <param name="length">The length of the permutation to create.</param>
    54     /// <returns>The generated permuation as int array.</returns>
    55     public static int[] Apply(IRandom random, int length) {
    56       int[] perm = new int[length];
    57 
    58       for (int i = 0; i < length; i++)
    59         perm[i] = i;
    60 
    61       // Knuth shuffle
    62       int index, tmp;
    63       for (int i = 0; i < length - 1; i++) {
    64         index = random.Next(i, length);
    65         tmp = perm[i];
    66         perm[i] = perm[index];
    67         perm[index] = tmp;
    68       }
    69       return perm;
    70     }
    71 
    72     /// <summary>
    73     /// Generates a randomly shuffled permutation.
    74     /// </summary>
    75     /// <remarks>Calls <see cref="Apply(HeuristicLab.Core.IRandom, int)"/>.</remarks>
    76     /// <param name="scope">The current scope with the variables.</param>
    77     /// <returns><c>null</c>.</returns>
    78     public override IOperation Apply(IScope scope) {
    79       IRandom random = GetVariableValue<IRandom>("Random", scope, true);
    80       IntData length = GetVariableValue<IntData>("Length", scope, true);
    81 
    82       int[] perm = Apply(random, length.Data);
    83       scope.AddVariable(new Variable(scope.TranslateName("Permutation"), new Permutation(perm)));
    84 
    85       return null;
     53    public override IExecutionSequence Apply() {
     54      PermutationParameter.ActualValue = new Permutation(LengthParameter.ActualValue.Value, RandomParameter.ActualValue);
     55      return base.Apply();
    8656    }
    8757  }
Note: See TracChangeset for help on using the changeset viewer.