Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Evolutionary/3.2/SubScopesStorer.cs @ 3452

Last change on this file since 3452 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 4.3 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.Evolutionary {
30  /// <summary>
31  /// Stores the sub scopes of the right sub scope until enough newly created child scopes are available
32  /// (for example to replace a generation).
33  /// </summary>
34  public class SubScopesStorer : OperatorBase {
35    /// <inheritdoc select="summary"/>
36    public override string Description {
37      get { return @"TODO\r\nOperator description still missing ..."; }
38    }
39
40    /// <summary>
41    /// Initializes a new instance of <see cref="SubScopesStorer"/> with two variable infos
42    /// (<c>SubScopes</c> and <c>SubScopesStore</c>).
43    /// </summary>
44    public SubScopesStorer()
45      : base() {
46      AddVariableInfo(new VariableInfo("SubScopes", "Number of sub-scopes that should be available", typeof(IntData), VariableKind.In));
47      AddVariableInfo(new VariableInfo("SubScopesStore", "Temporarily stored sub-scopes", typeof(ItemList<IScope>), VariableKind.New | VariableKind.In | VariableKind.Out | VariableKind.Deleted));
48    }
49
50    /// <summary>
51    /// Stores all sub scopes of the right branch to get the required number of sub scopes. Shifts the left
52    /// branch one level up, if sub scopes are still missing (so that another selection and mutation circle
53    /// can take place).
54    /// </summary>
55    /// <param name="scope">The current scope whose sub scopes to store.</param>
56    /// <returns><c>null</c> if the required number of sub scopes is available, the next
57    /// <see cref="AtomicOperation"/> if sub scopes are still missing.</returns>
58    public override IOperation Apply(IScope scope) {
59      IntData subScopes = GetVariableValue<IntData>("SubScopes", scope, true);
60      ItemList<IScope> subScopesStore = GetVariableValue<ItemList<IScope>>("SubScopesStore", scope, false);
61
62      if (subScopesStore == null) {
63        subScopesStore = new ItemList<IScope>();
64        IVariableInfo info = GetVariableInfo("SubScopesStore");
65        if (info.Local)
66          AddVariable(new Variable(info.ActualName, subScopesStore));
67        else
68          scope.AddVariable(new Variable(scope.TranslateName(info.FormalName), subScopesStore));
69      }
70
71      IScope left = scope.SubScopes[0];
72      IScope right = scope.SubScopes[1];
73
74      if (right.SubScopes.Count + subScopesStore.Count
75          >= subScopes.Data) {  // enough sub-scopes available
76        // restore sub-scopes
77        for (int i = 0; i < subScopesStore.Count; i++) {
78          right.AddSubScope(subScopesStore[i]);
79          IVariableInfo info = GetVariableInfo("SubScopesStore");
80          if (info.Local)
81            RemoveVariable(info.ActualName);
82          else
83            scope.RemoveVariable(scope.TranslateName(info.FormalName));
84        }
85        return null;
86      } else {  // not enough sub-scopes available
87        // store sub-scopes
88        for (int i = 0; i < right.SubScopes.Count; i++) {
89          IScope s = right.SubScopes[0];
90          right.RemoveSubScope(s);
91          subScopesStore.Add(s);
92        }
93
94        // shift left on level up
95        scope.RemoveSubScope(right);
96        scope.RemoveSubScope(left);
97        for (int i = 0; i < left.SubScopes.Count; i++)
98          scope.AddSubScope(left.SubScopes[i]);
99
100        return new AtomicOperation(SubOperators[0], scope);
101      }
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.