Free cookie consent management tool by TermsFeed Policy Generator

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

Operator architecture refactoring (#95)

  • worked on operators
Location:
trunk/sources/HeuristicLab.Permutation/3.3
Files:
2 added
2 deleted
5 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Permutation/3.3/HeuristicLab.Permutation-3.3.csproj

    r2790 r2794  
    8282  <ItemGroup>
    8383    <None Include="HeuristicLabPermutationPlugin.cs.frame" />
     84    <Compile Include="InversionManipulator.cs">
     85      <SubType>Code</SubType>
     86    </Compile>
     87    <Compile Include="PermutationManipulator.cs" />
    8488    <Compile Include="HeuristicLabPermutationPlugin.cs" />
     89    <Compile Include="OrderCrossover.cs" />
     90    <Compile Include="PermutationCrossover.cs" />
     91    <Compile Include="Permutation.cs" />
    8592    <Compile Include="Properties\AssemblyInfo.cs" />
     93    <Compile Include="RandomPermutationCreator.cs" />
    8694  </ItemGroup>
    8795  <ItemGroup>
     
    9098  </ItemGroup>
    9199  <ItemGroup>
     100    <ProjectReference Include="..\..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj">
     101      <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project>
     102      <Name>HeuristicLab.Collections-3.3</Name>
     103    </ProjectReference>
     104    <ProjectReference Include="..\..\HeuristicLab.Common\3.2\HeuristicLab.Common-3.2.csproj">
     105      <Project>{1FC004FC-59AF-4249-B1B6-FF25873A20E4}</Project>
     106      <Name>HeuristicLab.Common-3.2</Name>
     107    </ProjectReference>
    92108    <ProjectReference Include="..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj">
    93109      <Project>{C36BD924-A541-4A00-AFA8-41701378DDC5}</Project>
     
    98114      <Name>HeuristicLab.Data-3.3</Name>
    99115    </ProjectReference>
    100     <ProjectReference Include="..\..\HeuristicLab.Evolutionary\3.3\HeuristicLab.Evolutionary-3.3.csproj">
    101       <Project>{25087811-F74C-4128-BC86-8324271DA13E}</Project>
    102       <Name>HeuristicLab.Evolutionary-3.3</Name>
     116    <ProjectReference Include="..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj">
     117      <Project>{23DA7FF4-D5B8-41B6-AA96-F0561D24F3EE}</Project>
     118      <Name>HeuristicLab.Operators-3.3</Name>
     119    </ProjectReference>
     120    <ProjectReference Include="..\..\HeuristicLab.Parameters\3.3\HeuristicLab.Parameters-3.3.csproj">
     121      <Project>{56F9106A-079F-4C61-92F6-86A84C2D84B7}</Project>
     122      <Name>HeuristicLab.Parameters-3.3</Name>
     123    </ProjectReference>
     124    <ProjectReference Include="..\..\HeuristicLab.Persistence\3.3\HeuristicLab.Persistence-3.3.csproj">
     125      <Project>{102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}</Project>
     126      <Name>HeuristicLab.Persistence-3.3</Name>
    103127    </ProjectReference>
    104128    <ProjectReference Include="..\..\HeuristicLab.PluginInfrastructure\HeuristicLab.PluginInfrastructure.csproj">
    105129      <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project>
    106130      <Name>HeuristicLab.PluginInfrastructure</Name>
    107     </ProjectReference>
    108     <ProjectReference Include="..\..\HeuristicLab.Random\3.3\HeuristicLab.Random-3.3.csproj">
    109       <Project>{F4539FB6-4708-40C9-BE64-0A1390AEA197}</Project>
    110       <Name>HeuristicLab.Random-3.3</Name>
    111131    </ProjectReference>
    112132  </ItemGroup>
  • trunk/sources/HeuristicLab.Permutation/3.3/HeuristicLabPermutationPlugin.cs.frame

    r2790 r2794  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    24 using System.Text;
    2522using HeuristicLab.PluginInfrastructure;
    2623
     
    3128  [Plugin("HeuristicLab.Permutation", "3.3.0.$WCREV$")]
    3229  [PluginFile("HeuristicLab.Permutation-3.3.dll", PluginFileType.Assembly)]
     30  [PluginDependency("HeuristicLab.Collections", "3.3")]
     31  [PluginDependency("HeuristicLab.Common", "3.2")]
    3332  [PluginDependency("HeuristicLab.Core", "3.3")]
    3433  [PluginDependency("HeuristicLab.Data", "3.3")]
    35   [PluginDependency("HeuristicLab.Evolutionary", "3.3")]
    36   [PluginDependency("HeuristicLab.Random", "3.3")]
     34  [PluginDependency("HeuristicLab.Operators", "3.3")]
     35  [PluginDependency("HeuristicLab.Parameters", "3.3")]
     36  [PluginDependency("HeuristicLab.Persistence", "3.3")]
    3737  public class HeuristicLabPermutationPlugin : PluginBase {
    3838  }
  • trunk/sources/HeuristicLab.Permutation/3.3/InversionManipulator.cs

    r1530 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;
     23using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2624
    2725namespace HeuristicLab.Permutation {
    2826  /// <summary>
    29   /// Manipulates a permutation array by reversing a randomly chosen interval.
     27  /// An operator which inverts a randomly chosen part of a permutation.
    3028  /// </summary>
    31   public class InversionManipulator : PermutationManipulatorBase {
    32     /// <inheritdoc select="summary"/>
    33     public override string Description {
    34       get { return @"TODO\r\nOperator description still missing ..."; }
    35     }
     29  [Item("InversionManipulator", "An operator which inverts a randomly chosen part of a permutation.")]
     30  [EmptyStorableClass]
     31  [Creatable("Test")]
     32  public class InversionManipulator : PermutationManipulator {
    3633
    3734    /// <summary>
    38     /// Reverses the specified <paramref name="permutation"/> between two randomly generated positions.
     35    /// Inverts a randomly chosen part of a permutation.
    3936    /// </summary>
    40     /// <param name="random">The random number generator.</param>
    41     /// <param name="permutation">The permutation array to manipulate.</param>
    42     /// <returns>The new permuation array with the manipulated data.</returns>
    43     public static int[] Apply(IRandom random, int[] permutation) {
    44       int[] result = (int[])permutation.Clone();
     37    /// <param name="random">A random number generator.</param>
     38    /// <param name="permutation">The permutation to manipulate.</param>
     39    /// <returns>The new manipulated permutation.</returns>
     40    public static Permutation Apply(IRandom random, Permutation permutation) {
     41      Permutation result = (Permutation)permutation.Clone();
    4542      int breakPoint1, breakPoint2;
    4643
     
    5148      if (breakPoint2 < breakPoint1) { int h = breakPoint1; breakPoint1 = breakPoint2; breakPoint2 = h; }
    5249
    53       for (int i = 0; i <= (breakPoint2 - breakPoint1); i++) {  // reverse permutation between breakpoints
     50      for (int i = 0; i <= (breakPoint2 - breakPoint1); i++) {  // invert permutation between breakpoints
    5451        result[breakPoint1 + i] = permutation[breakPoint2 - i];
    5552      }
     
    5855
    5956    /// <summary>
    60     /// Reverses the specified <paramref name="permutation"/> between two randomly generated positions.
     57    /// Inverts a randomly chosen part of a permutation.
    6158    /// </summary>
    62     /// <remarks>Calls <see cref="Apply"/>.</remarks>
    63     /// <param name="scope">The current scope.</param>
    64     /// <param name="random">The random number generator.</param>
    65     /// <param name="permutation">The permutation array to manipulate.</param>
    66     /// <returns>The new permuation array with the manipulated data.</returns>
    67     protected override int[] Manipulate(IScope scope, IRandom random, int[] permutation) {
     59    /// <param name="random">A random number generator.</param>
     60    /// <param name="permutation">The permutation to manipulate.</param>
     61    /// <returns>The new manipulated permuation.</returns>
     62    protected override Permutation Manipulate(IRandom random, Permutation permutation) {
    6863      return Apply(random, permutation);
    6964    }
  • trunk/sources/HeuristicLab.Permutation/3.3/OrderCrossover.cs

    r1530 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.
     
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Text;
    2523using HeuristicLab.Core;
    26 using HeuristicLab.Data;
     24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2725
    2826namespace HeuristicLab.Permutation {
    2927  /// <summary>
    30   /// Performs a cross over permutation between two permuation arrays
    31   /// by taking a randomly chosen interval from the frist, preserving the positions,
    32   /// then the missing values from the second array in the order they occur in the second array.
     28  /// An operator which performs an order crossover of two permutations.
    3329  /// </summary>
    34   public class OrderCrossover : PermutationCrossoverBase {
    35     /// <inheritdoc select="summary"/>
    36     public override string Description {
    37       get { return @"TODO\r\nOperator description still missing ..."; }
    38     }
    39 
     30  /// <remarks>
     31  /// Crosses two permutations by taking a randomly chosen interval from the frist permutation, preserving
     32  /// the positions, and then the missing values from the second permutation in the order they occur in the
     33  /// second permutation.
     34  /// </remarks>
     35  [Item("OrderCrossover", "An operator which performs an order crossover of two permutations.")]
     36  [EmptyStorableClass]
     37  [Creatable("Test")]
     38  public class OrderCrossover : PermutationCrossover {
    4039    /// <summary>
    41     /// Performs a cross over permutation of <paramref name="parent1"/> and
    42     /// <paramref name="parent2"/> by taking a randomly chosen interval from <paramref name="parent1"/>,
    43     /// preserving the positions and then the missing values from <paramref name="parent2"/> in the
    44     /// order they occur in <paramref name="parent2"/>.
     40    /// Performs an order crossover of two permutations.
    4541    /// </summary>
    46     /// <param name="random">The random number generator.</param>
    47     /// <param name="parent1">The parent scope 1 to cross over.</param>
    48     /// <param name="parent2">The parent scope 2 to cross over.</param>
    49     /// <returns>The created cross over permutation as int array.</returns>
    50     public static int[] Apply(IRandom random, int[] parent1, int[] parent2) {
    51       int[] result = new int[parent1.Length];
     42    /// <param name="random">A random number generator.</param>
     43    /// <param name="parent1">The first parent permutation to cross.</param>
     44    /// <param name="parent2">The second parent permutation to cross.</param>
     45    /// <returns>The new permutation resulting from the crossover.</returns>
     46    public static Permutation Apply(IRandom random, Permutation parent1, Permutation parent2) {
     47      Permutation result = new Permutation(parent1.Length);
    5248      bool[] copied = new bool[result.Length];
    5349
     
    7470
    7571    /// <summary>
    76     /// Performs an order crossover operation for two given parent permutations.
     72    /// Performs an order crossover of two permutations.
    7773    /// </summary>
    7874    /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception>
    79     /// <param name="scope">The current scope.</param>
    8075    /// <param name="random">A random number generator.</param>
    8176    /// <param name="parents">An array containing the two permutations that should be crossed.</param>
    82     /// <returns>The newly created permutation, resulting from the crossover operation.</returns>
    83     protected override int[] Cross(IScope scope, IRandom random, int[][] parents) {
    84       if (parents.Length != 2) throw new InvalidOperationException("ERROR in OrderCrossover: The number of parents is not equal to 2");
     77    /// <returns>The new permutation resulting from the crossover.</returns>
     78    protected override Permutation Cross(IRandom random, ItemArray<Permutation> parents) {
     79      if (parents.Length != 2) throw new InvalidOperationException("Number of parents is not equal to 2.");
    8580      return Apply(random, parents[0], parents[1]);
    8681    }
  • trunk/sources/HeuristicLab.Permutation/3.3/Permutation.cs

    r2526 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.
     
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Text;
    25 using System.Xml;
     24using HeuristicLab.Common;
    2625using HeuristicLab.Core;
    2726using HeuristicLab.Data;
     27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2828
    2929namespace HeuristicLab.Permutation {
    30   /// <summary>
    31   /// The base class for a permutation, which is an ordered combination.
    32   /// </summary>
    33   public class Permutation : IntArrayData {
    34     /// <summary>
    35     /// Initializes a new instance of <see cref="Permutation"/>.
    36     /// </summary>
    37     public Permutation()
    38       : base() {
     30  [EmptyStorableClass]
     31  [Item("Permutation", "Represents a permutation of integer values.")]
     32  [Creatable("Test")]
     33  public sealed class Permutation : ValueTypeArrayData<int>, IStringConvertibleMatrixData {
     34    public Permutation() : base() { }
     35    public Permutation(int length)
     36      : base(length) {
     37      for (int i = 0; i < length; i++)
     38        this[i] = i;
    3939    }
    40     /// <summary>
    41     /// Initializes a new instance of <see cref="Permutation"/> with the given
    42     /// <paramref name="permutation"/> array.
    43     /// </summary>
    44     /// <param name="permutation">The permutation array to assign.</param>
    45     public Permutation(int[] permutation)
    46       : base(permutation) {
     40    public Permutation(int length, IRandom random)
     41      : this(length) {
     42      Randomize(random);
    4743    }
     44    public Permutation(int[] elements)
     45      : base(elements) {
     46      if (!Validate()) throw new ArgumentException("Elements do not represent a valid permutation.");
     47    }
     48    private Permutation(Permutation elements) : base(elements) { }
    4849
    49     /// <summary>
    50     /// Clones the current instance.
    51     /// </summary>
    52     /// <param name="clonedObjects">A dictionary of all already cloned objects. (Needed to
    53     /// avoid cycles.)</param>
    54     /// <returns>The cloned object as <see cref="Permutation"/>.</returns>
    55     public override IItem Clone(ICloner cloner) {
    56       Permutation clone = new Permutation((int[])Data.Clone());
     50    public override IDeepCloneable Clone(Cloner cloner) {
     51      Permutation clone = new Permutation(this);
    5752      cloner.RegisterClonedObject(this, clone);
    5853      return clone;
    5954    }
     55
     56    public bool Validate() {
     57      bool[] values = new bool[Length];
     58      int value;
     59
     60      for (int i = 0; i < values.Length; i++)
     61        values[i] = false;
     62      for (int i = 0; i < Length; i++) {
     63        value = this[i];
     64        if ((value < 0) || (value >= values.Length)) return false;
     65        if (values[value]) return false;
     66        values[value] = true;
     67      }
     68      return true;
     69    }
     70
     71    public void Randomize(IRandom random, int startIndex, int length) {  // Knuth shuffle
     72      int index1, index2;
     73      int val;
     74      for (int i = length - 1; i > 0; i--) {
     75        index1 = startIndex + i;
     76        index2 = startIndex + random.Next(i + 1);
     77        if (index1 != index2) {
     78          val = this[index1];
     79          this[index1] = this[index2];
     80          this[index2] = val;
     81        }
     82      }
     83    }
     84    public void Randomize(IRandom random) {
     85      Randomize(random, 0, Length);
     86    }
     87
     88    #region IStringConvertibleMatrixData Members
     89    StringConvertibleArrayDataDimensions IStringConvertibleMatrixData.Dimensions {
     90      get { return StringConvertibleArrayDataDimensions.Rows; }
     91    }
     92    int IStringConvertibleMatrixData.Rows {
     93      get { return Length; }
     94      set { Length = value; }
     95    }
     96    int IStringConvertibleMatrixData.Columns {
     97      get { return 1; }
     98      set { throw new NotSupportedException("Columns cannot be changed."); }
     99    }
     100
     101    bool IStringConvertibleMatrixData.Validate(string value, out string errorMessage) {
     102      int val;
     103      bool valid = int.TryParse(value, out val);
     104      errorMessage = string.Empty;
     105      if (!valid) {
     106        StringBuilder sb = new StringBuilder();
     107        sb.Append("Invalid Value (Valid Value Format: \"");
     108        sb.Append(FormatPatterns.GetIntFormatPattern());
     109        sb.Append("\")");
     110        errorMessage = sb.ToString();
     111      }
     112      return valid;
     113    }
     114    string IStringConvertibleMatrixData.GetValue(int rowIndex, int columIndex) {
     115      return this[rowIndex].ToString();
     116    }
     117    bool IStringConvertibleMatrixData.SetValue(string value, int rowIndex, int columnIndex) {
     118      int val;
     119      if (int.TryParse(value, out val)) {
     120        this[rowIndex] = val;
     121        return true;
     122      } else {
     123        return false;
     124      }
     125    }
     126    event EventHandler<EventArgs<int, int>> IStringConvertibleMatrixData.ItemChanged {
     127      add { base.ItemChanged += value; }
     128      remove { base.ItemChanged -= value; }
     129    }
     130    event EventHandler IStringConvertibleMatrixData.Reset {
     131      add { base.Reset += value; }
     132      remove { base.Reset -= value; }
     133    }
     134    #endregion
    60135  }
    61136}
  • 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.