Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators/3.2/SubScopesMixer.cs @ 1875

Last change on this file since 1875 was 1530, checked in by gkronber, 16 years ago

Moved source files of plugins Hive ... Visualization.Test into version-specific sub-folders. #576

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27
28namespace HeuristicLab.Operators {
29  /// <summary>
30  /// Mixes the sub scopes of a specified scope according to a specified number of partitions.
31  /// </summary>
32  public class SubScopesMixer : OperatorBase {
33    /// <inheritdoc select="summary"/>
34    public override string Description {
35      get { return @"TODO\r\nOperator description still missing ..."; }
36    }
37    /// <summary>
38    /// Initializes a new instance of <see cref="SubScopesMixer"/> with one variable infos
39    /// (<c>Partitions</c>) and the <c>Local</c> flag set to <c>true</c>.
40    /// </summary>
41    public SubScopesMixer()
42      : 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)));
46    }
47
48    /// <summary>
49    /// Mixes the subscopes of the given <paramref name="scope"/>.
50    /// </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/>
58    /// In the end the new sorting of the sub scopes is 1-5-9-2-6-10-3-7-11-4-8-12.
59    /// </example>
60    /// </remarks>
61    /// <exception cref="ArgumentException">Thrown when the number of sub scopes cannot be divided by
62    /// the number of partitions without remainder.</exception>
63    /// <param name="scope">The scope whose sub scopes should be mixed.</param>
64    /// <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;
71
72      // mix sub-scopes -> alternately take one sub-scope from each partition
73      for (int i = 0; i < partitionSize; i++) {
74        for (int j = 0; j < partitions; j++)
75          sequence[i * partitions + j] = j * partitionSize + i;
76      }
77      scope.ReorderSubScopes(sequence);
78
79      return null;
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.