Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/19/10 01:51:50 (14 years ago)
Author:
abeham
Message:

Added gender specific selection and ported the necessary operators RightChildReducer and SubScopesMixer #976

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Operators/3.3/SubScopesMixer.cs

    r1530 r3404  
    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;
    2624using HeuristicLab.Data;
     25using HeuristicLab.Parameters;
     26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2727
    2828namespace HeuristicLab.Operators {
     
    3030  /// Mixes the sub scopes of a specified scope according to a specified number of partitions.
    3131  /// </summary>
    32   public class SubScopesMixer : OperatorBase {
    33     /// <inheritdoc select="summary"/>
    34     public override string Description {
    35       get { return @"TODO\r\nOperator description still missing ..."; }
     32  [Item("SubScopesMixer", "Changes the order of the sub-scopes by repartitioning the sub-scopes such that each new partition contains one scope from each old partition.")]
     33  [StorableClass]
     34  public class SubScopesMixer : SingleSuccessorOperator {
     35    public ValueParameter<IntValue> PartitionsParameter {
     36      get { return (ValueParameter<IntValue>)Parameters["Partitions"]; }
    3637    }
     38
     39    public IntValue Partitions {
     40      get { return PartitionsParameter.Value; }
     41      set { PartitionsParameter.Value = value; }
     42    }
     43
    3744    /// <summary>
    3845    /// Initializes a new instance of <see cref="SubScopesMixer"/> with one variable infos
     
    4148    public SubScopesMixer()
    4249      : base() {
    43       AddVariableInfo(new VariableInfo("Partitions", "Number of partitions to mix", typeof(IntData), VariableKind.In));
    44       GetVariableInfo("Partitions").Local = true;
    45       AddVariable(new Variable("Partitions", new IntData(2)));
     50      Parameters.Add(new ValueParameter<IntValue>("Partitions", "The number of equal-sized partitions.", new IntValue(2)));
    4651    }
    4752
    4853    /// <summary>
    49     /// Mixes the subscopes of the given <paramref name="scope"/>.
     54    /// Mixes the sub-scopes of the scope this operator is applied on.
    5055    /// </summary>
    51     /// <remarks>Calls <see cref="IScope.ReorderSubScopes"/>.<br/>
    52     /// Mixing of sub scopes is based on the number of partitions.
    53     /// <example>12 sub scopes and 3 partitions:<br/>
    54     /// Partition 1 contains sub scopes 1-4, partition 2 sub scopes 5-8 and partition 3 sub scopes 9-12. <br/>
    55     /// Mixing is realized by selecting at the beginning the first sub scope from partition one, then the
    56     /// first sub scope from partition 2, afterwards first sub scope from partition 3,
    57     /// then the second sub scope from the first partition and so on. <br/>
     56    /// <remarks>Mixing of sub-scopes is based on the number of partitions.
     57    /// <example>12 sub-scopes and 3 partitions:<br/>
     58    /// Partition 1 contains scopes 1-4, partition 2 scopes 5-8 and partition 3 scopes 9-12. <br/>
     59    /// Mixing is realized by selecting at the beginning the first scope from partition one, then the
     60    /// first scope from partition 2, afterwards first scope from partition 3,
     61    /// then the second scope from the first partition and so on. <br/>
    5862    /// In the end the new sorting of the sub scopes is 1-5-9-2-6-10-3-7-11-4-8-12.
    5963    /// </example>
     
    6367    /// <param name="scope">The scope whose sub scopes should be mixed.</param>
    6468    /// <returns><c>null</c>.</returns>
    65     public override IOperation Apply(IScope scope) {
    66       int partitions = GetVariableValue<IntData>("Partitions", scope, true).Data;
    67       int[] sequence = new int[scope.SubScopes.Count];
    68       if ((sequence.Length % partitions) != 0)
    69         throw new ArgumentException("The number of subScopes is not divisible by the number of partitions without remainder.");
    70       int partitionSize = sequence.Length / partitions;
     69    public override IOperation Apply() {
     70      int partitions = Partitions.Value;
     71      IScope scope = ExecutionContext.Scope;
     72      int count = scope.SubScopes.Count;
     73      if ((count % partitions) != 0)
     74        throw new ArgumentException(Name + ": The number of sub-scopes is not divisible by the number of partitions without remainder.");
     75      int partitionSize = count / partitions;
     76
     77      IScope[] reorderedSubScopes = new IScope[count];
    7178
    7279      // mix sub-scopes -> alternately take one sub-scope from each partition
    7380      for (int i = 0; i < partitionSize; i++) {
    74         for (int j = 0; j < partitions; j++)
    75           sequence[i * partitions + j] = j * partitionSize + i;
     81        for (int j = 0; j < partitions; j++) {
     82          reorderedSubScopes[i * partitions + j] = scope.SubScopes[j * partitionSize + i];
     83        }
    7684      }
    77       scope.ReorderSubScopes(sequence);
     85      scope.SubScopes.Clear();
     86      scope.SubScopes.AddRange(reorderedSubScopes);
    7887
    79       return null;
     88      return base.Apply();
    8089    }
    8190  }
Note: See TracChangeset for help on using the changeset viewer.