Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Evolutionary/SubScopesStorer.cs @ 1400

Last change on this file since 1400 was 77, checked in by swagner, 16 years ago

Fixed ticket #67

  • adapted accessing of variables in operators due to changes of variable lookup and the new name aliasing mechanism (actual/formal name translations should not be done directly anymore; instead the new method Scope.TranslateName should be used)
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<IScope>), 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<IScope> subScopesStore = GetVariableValue<ItemList<IScope>>("SubScopesStore", scope, false);
44
45      if (subScopesStore == null) {
46        subScopesStore = new ItemList<IScope>();
47        IVariableInfo info = GetVariableInfo("SubScopesStore");
48        if (info.Local)
49          AddVariable(new Variable(info.ActualName, subScopesStore));
50        else
51          scope.AddVariable(new Variable(scope.TranslateName(info.FormalName), subScopesStore));
52      }
53
54      IScope left = scope.SubScopes[0];
55      IScope right = scope.SubScopes[1];
56
57      if (right.SubScopes.Count + subScopesStore.Count
58          >= subScopes.Data) {  // enough sub-scopes available
59        // restore sub-scopes
60        for (int i = 0; i < subScopesStore.Count; i++) {
61          right.AddSubScope(subScopesStore[i]);
62          IVariableInfo info = GetVariableInfo("SubScopesStore");
63          if (info.Local)
64            RemoveVariable(info.ActualName);
65          else
66            scope.RemoveVariable(scope.TranslateName(info.FormalName));
67        }
68        return null;
69      } else {  // not enough sub-scopes available
70        // store sub-scopes
71        for (int i = 0; i < right.SubScopes.Count; i++) {
72          IScope s = right.SubScopes[0];
73          right.RemoveSubScope(s);
74          subScopesStore.Add(s);
75        }
76
77        // shift left on level up
78        scope.RemoveSubScope(right);
79        scope.RemoveSubScope(left);
80        for (int i = 0; i < left.SubScopes.Count; i++)
81          scope.AddSubScope(left.SubScopes[i]);
82
83        return new AtomicOperation(SubOperators[0], scope);
84      }
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.