Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators.Metaprogramming/PermutationInjector.cs @ 1342

Last change on this file since 1342 was 1167, checked in by vdorfer, 15 years ago

Created API documentation for HeuristicLab.Operators.Metaprogramming and HeuristicLab.Logging namespace and changed a comment in HeuristicLab.Core namespace(#331)

File size: 4.5 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;
27using HeuristicLab.Operators;
28
29namespace HeuristicLab.Operators.Metaprogramming {
30  /// <summary>
31  /// Injects a new permutation variable in the given scope. The number of items contained
32  /// are in a predifined range.
33  /// </summary>
34  public class PermutationInjector : OperatorBase {
35    /// <inheritdoc select="summary"/>
36    public override string Description {
37      get { return "TASK."; }
38    }
39
40    /// <summary>
41    /// Initializes a new instance of <see cref="PermutationInjector"/> with five variable infos
42    /// (<c>VariableInjector</c>, <c>VariableName</c>, <c>Items</c>, <c>Min</c> and <c>Max</c>).
43    /// </summary>
44    public PermutationInjector()
45      : base() {
46      AddVariableInfo(new VariableInfo("VariableInjector", "The combined operator that should hold the generated variable injector", typeof(CombinedOperator), VariableKind.New));
47      AddVariableInfo(new VariableInfo("VariableName", "Name of the variable that should be injected", typeof(StringData), VariableKind.In));
48      AddVariableInfo(new VariableInfo("Items", "Set of items that can be part of the permutation", typeof(ItemList), VariableKind.In));
49      AddVariableInfo(new VariableInfo("Min", "Minimal number of items to inject", typeof(IntData), VariableKind.In));
50      AddVariableInfo(new VariableInfo("Max", "Maximal number of items to inject", typeof(IntData), VariableKind.In));
51    }
52
53    /// <summary>
54    /// Injects a new permutation variable into the given <paramref name="scope"/>.
55    /// </summary>
56    /// <param name="scope">The current scope where to inject the permutation list.</param>
57    /// <returns><c>null</c>.</returns>
58    public override IOperation Apply(IScope scope) {
59      int min = GetVariableValue<IntData>("Min", scope, true).Data;
60      int max = GetVariableValue<IntData>("Max", scope, true).Data;
61      string variableName = GetVariableValue<StringData>("VariableName", scope, true).Data;
62      ItemList allItems = GetVariableValue<ItemList>("Items", scope, true);
63      List<IItem> xs = new List<IItem>(allItems);
64      List<List<IItem>> permutations = CalcPermutations(xs, new List<IItem>(), min, max);
65
66      for(int i = 0; i < permutations.Count; i++) {
67        string scopeName = "";
68        foreach(IItem x in permutations[i]) scopeName += x.ToString() + ";";
69        Scope subScope = new Scope(scopeName.TrimEnd(';'));
70        CombinedOperator combOp = new CombinedOperator();
71        VariableInjector varInjector = new VariableInjector();
72        ItemList permutation = new ItemList();
73        foreach(IItem item in permutations[i]) permutation.Add(item);
74        varInjector.AddVariable(new Variable(variableName, permutation));
75
76        combOp.OperatorGraph.AddOperator(varInjector);
77        combOp.OperatorGraph.InitialOperator = varInjector;
78
79        subScope.AddVariable(new Variable(scope.TranslateName("VariableInjector"), combOp));
80        scope.AddSubScope(subScope);
81      }
82      return null;
83    }
84
85    private List<List<IItem>> CalcPermutations(List<IItem> allItems, List<IItem> prefix, int min, int max) {
86      List<List<IItem>> result = new List<List<IItem>>();
87      if(prefix.Count >= max) return result;
88      if(prefix.Count >= min) result.Add(new List<IItem>(prefix));
89      int count = allItems.Count;
90      for(int i = 0; i < count; i++) {
91        IItem x = allItems[0];
92        allItems.RemoveAt(0);
93        prefix.Add(x);
94        result.AddRange(CalcPermutations(new List<IItem>(allItems), prefix, min, max));
95        prefix.RemoveAt(prefix.Count-1);
96      }
97
98      return result;
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.