- Timestamp:
- 02/14/10 05:31:49 (14 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Permutation/3.3/RandomPermutationCreator.cs
r2792 r2794 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-20 08Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 20 20 #endregion 21 21 22 using System;23 using System.Collections.Generic;24 using System.Text;25 22 using HeuristicLab.Core; 26 23 using HeuristicLab.Data; 24 using HeuristicLab.Operators; 25 using HeuristicLab.Parameters; 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 27 28 28 namespace HeuristicLab.Permutation { 29 29 /// <summary> 30 /// Generates a randomly shuffled permuation.30 /// An operator which creates a new random permutation of integer values. 31 31 /// </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"]; } 36 44 } 37 45 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() 43 47 : 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.")); 47 51 } 48 52 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(); 86 56 } 87 57 }
Note: See TracChangeset
for help on using the changeset viewer.