Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Evolutionary/SubScopesStorer.cs @ 26

Last change on this file since 26 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 3.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  public class SubScopesStorer : OperatorBase {
31    public override string Description {
32      get { return @"TODO\r\nOperator description still missing ..."; }
33    }
34
35    public SubScopesStorer()
36      : base() {
37      AddVariableInfo(new VariableInfo("SubScopes", "Number of sub-scopes that should be available", typeof(IntData), VariableKind.In));
38      AddVariableInfo(new VariableInfo("SubScopesStore", "Temporarily stored sub-scopes", typeof(ItemList), VariableKind.New | VariableKind.In | VariableKind.Out | VariableKind.Deleted));
39    }
40
41    public override IOperation Apply(IScope scope) {
42      IntData subScopes = GetVariableValue<IntData>("SubScopes", scope, true);
43      ItemList subScopesStore = GetVariableValue<ItemList>("SubScopesStore", scope, false);
44
45      if (subScopesStore == null) {
46        subScopesStore = new ItemList();
47        subScopesStore.ItemType = typeof(IScope);
48        IVariableInfo info = GetVariableInfo("SubScopesStore");
49        if (info.Local)
50          AddVariable(new Variable(info.ActualName, subScopesStore));
51        else
52          scope.AddVariable(new Variable(info.ActualName, subScopesStore));
53      }
54
55      IScope left = scope.SubScopes[0];
56      IScope right = scope.SubScopes[1];
57
58      if (right.SubScopes.Count + subScopesStore.Count
59          >= subScopes.Data) {  // enough sub-scopes available
60        // restore sub-scopes
61        for (int i = 0; i < subScopesStore.Count; i++) {
62          right.AddSubScope((IScope)subScopesStore[i]);
63          IVariableInfo info = GetVariableInfo("SubScopesStore");
64          if (info.Local)
65            RemoveVariable(info.ActualName);
66          else
67            scope.RemoveVariable(info.ActualName);
68        }
69        return null;
70      } else {  // not enough sub-scopes available
71        // store sub-scopes
72        for (int i = 0; i < right.SubScopes.Count; i++) {
73          IScope s = right.SubScopes[0];
74          right.RemoveSubScope(s);
75          subScopesStore.Add(s);
76        }
77
78        // shift left on level up
79        scope.RemoveSubScope(right);
80        scope.RemoveSubScope(left);
81        for (int i = 0; i < left.SubScopes.Count; i++)
82          scope.AddSubScope(left.SubScopes[i]);
83
84        return new AtomicOperation(SubOperators[0], scope);
85      }
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.